X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;ds=sidebyside;f=windowsphonepush%2Fwindowsphonepush.php;h=53bf83b87d1863b93780733b67be68d529cbc53f;hb=48dcbc6f3fc3edae74e3ea2c823011bfb7c26081;hp=9938edc75073058200b1d9d322ccce92d3b6c136;hpb=1436f647e3b058e542d9d2f5df39e659a10074cb;p=friendica-addons.git diff --git a/windowsphonepush/windowsphonepush.php b/windowsphonepush/windowsphonepush.php index 9938edc7..53bf83b8 100644 --- a/windowsphonepush/windowsphonepush.php +++ b/windowsphonepush/windowsphonepush.php @@ -2,7 +2,7 @@ /** * Name: WindowsPhonePush * Description: Enable push notification to send information to Friendica Mobile app on Windows phone (count of unread timeline entries, text of last posting - if wished by user) - * Version: 1.1 + * Version: 2.0 * Author: Gerhard Seeber * * @@ -18,8 +18,14 @@ * Version history: * 1.1 : addon crashed on php versions >= 5.4 as of removed deprecated call-time * pass-by-reference used in function calls within function windowsphonepush_content + * 2.0 : adaption for supporting emphasizing new entries in app (count on tile cannot be read out, + * so we need to retrieve counter through show_settings secondly). Provide new function for + * calling from app to set the counter back after start (if user starts again before cronjob + * sets the counter back + * count only unseen elements which are not type=activity (likes and dislikes not seen as new elements) */ +use Friendica\Core\PConfig; function windowsphonepush_install() { @@ -81,9 +87,14 @@ function windowsphonepush_module() {} function windowsphonepush_settings_post($a,$post) { if(! local_user() || (! x($_POST,'windowsphonepush-submit'))) return; + $enable = intval($_POST['windowsphonepush']); + PConfig::set(local_user(),'windowsphonepush','enable',$enable); - set_pconfig(local_user(),'windowsphonepush','enable',intval($_POST['windowsphonepush'])); - set_pconfig(local_user(),'windowsphonepush','senditemtext',intval($_POST['windowsphonepush-senditemtext'])); + if($enable) { + PConfig::set(local_user(),'windowsphonepush','counterunseen', 0); + } + + PConfig::set(local_user(),'windowsphonepush','senditemtext',intval($_POST['windowsphonepush-senditemtext'])); info( t('WindowsPhonePush settings updated.') . EOL); } @@ -104,13 +115,13 @@ function windowsphonepush_settings(&$a,&$s) { $a->page['htmlhead'] .= '' . "\r\n"; /* Get the current state of our config variables */ - $enabled = get_pconfig(local_user(),'windowsphonepush','enable'); + $enabled = PConfig::get(local_user(),'windowsphonepush','enable'); $checked_enabled = (($enabled) ? ' checked="checked" ' : ''); - $senditemtext = get_pconfig(local_user(), 'windowsphonepush', 'senditemtext'); + $senditemtext = PConfig::get(local_user(), 'windowsphonepush', 'senditemtext'); $checked_senditemtext = (($senditemtext) ? ' checked="checked" ' : ''); - $device_url = get_pconfig(local_user(), 'windowsphonepush', 'device_url'); + $device_url = PConfig::get(local_user(), 'windowsphonepush', 'device_url'); /* Add some HTML to the existing form */ $s .= '
'; @@ -153,8 +164,8 @@ function windowsphonepush_cron() { if(count($r)) { foreach($r as $rr) { // load stored information for the user-id of the current loop - $device_url = get_pconfig($rr['uid'], 'windowsphonepush', 'device_url'); - $lastpushid = get_pconfig($rr['uid'], 'windowsphonepush', 'lastpushid'); + $device_url = PConfig::get($rr['uid'], 'windowsphonepush', 'device_url'); + $lastpushid = PConfig::get($rr['uid'], 'windowsphonepush', 'lastpushid'); // pushing only possible if device_url (the URI on Microsoft server) is available or not "NA" (which will be sent // by app if user has switched the server setting in app - sending blank not possible as this would return an update error) @@ -164,7 +175,7 @@ function windowsphonepush_cron() { } else { // retrieve the number of unseen items and the id of the latest one (if there are more than // one new entries since last poller run, only the latest one will be pushed) - $count = q("SELECT count(`id`) as count, max(`id`) as max FROM `item` WHERE `unseen` = 1 AND `uid` = %d", + $count = q("SELECT count(`id`) as count, max(`id`) as max FROM `item` WHERE `unseen` = 1 AND `type` <> 'activity' AND `uid` = %d", intval($rr['uid']) ); @@ -174,7 +185,8 @@ function windowsphonepush_cron() { $res_tile = send_tile_update($device_url, "", $count[0]['count'], ""); switch (trim($res_tile)) { case "Received": - // ok, count has been pushed + // ok, count has been pushed, let's save it in personal settings + PConfig::set($rr['uid'], 'windowsphonepush', 'counterunseen', $count[0]['count']); break; case "QueueFull": // maximum of 30 messages reached, server rejects any further push notification until device reconnects @@ -197,7 +209,7 @@ function windowsphonepush_cron() { if (intval($count[0]['max']) > intval($lastpushid)) { // user can define if he wants to see the text of the item in the push notification // this has been implemented as the device_url is not a https uri (not so secure) - $senditemtext = get_pconfig($rr['uid'], 'windowsphonepush', 'senditemtext'); + $senditemtext = PConfig::get($rr['uid'], 'windowsphonepush', 'senditemtext'); if ($senditemtext == 1) { // load item with the max id $item = q("SELECT `author-name` as author, `body` as body FROM `item` where `id` = %d", @@ -236,7 +248,7 @@ function windowsphonepush_cron() { // further log information done on count pushing with send_tile (see above) $res_toast = send_toast($device_url, $author, $body); if (trim($res_toast) === 'Received') { - set_pconfig($rr['uid'], 'windowsphonepush', 'lastpushid', $count[0]['max']); + PConfig::set($rr['uid'], 'windowsphonepush', 'lastpushid', $count[0]['max']); } } } @@ -319,7 +331,7 @@ function send_push($device_url, $headers, $msg) { // and log this fact $subscriptionStatus = get_header_value($output, 'X-SubscriptionStatus'); if ($subscriptionStatus == "Expired") { - set_pconfig(local_user(),'windowsphonepush','device_url', ""); + PConfig::set(local_user(),'windowsphonepush','device_url', ""); logger("ERROR: the stored Device-URL " . $device_url . "returned an 'Expired' error, it has been deleted now."); } @@ -342,6 +354,7 @@ function get_header_value($content, $header) { * reading information from url and deciding which function to start * show_settings = delivering settings to check * update_settings = set the device_url + * update_counterunseen = set counter for unseen elements to zero * */ function windowsphonepush_content(&$a) { @@ -362,6 +375,12 @@ function windowsphonepush_content(&$a) { echo json_encode(array('status' => $ret)); killme(); break; + case "update_counterunseen": + $ret = windowsphonepush_updatecounterunseen(); + header("Content-Type: application/json; charset=utf-8"); + echo json_encode(array('status' => $ret)); + killme(); + break; default: echo "Fehler"; } @@ -375,10 +394,12 @@ function windowsphonepush_showsettings(&$a) { if(! local_user()) return; - $enable = get_pconfig(local_user(), 'windowsphonepush', 'enable'); - $device_url = get_pconfig(local_user(), 'windowsphonepush', 'device_url'); - $senditemtext = get_pconfig(local_user(), 'windowsphonepush', 'senditemtext'); - $lastpushid = get_pconfig(local_user(), 'windowsphonepush', 'lastpushid'); + $enable = PConfig::get(local_user(), 'windowsphonepush', 'enable'); + $device_url = PConfig::get(local_user(), 'windowsphonepush', 'device_url'); + $senditemtext = PConfig::get(local_user(), 'windowsphonepush', 'senditemtext'); + $lastpushid = PConfig::get(local_user(), 'windowsphonepush', 'lastpushid'); + $counterunseen = PConfig::get(local_user(), 'windowsphonepush', 'counterunseen'); + $addonversion = "2.0"; if (!$device_url) $device_url = ""; @@ -391,7 +412,9 @@ function windowsphonepush_showsettings(&$a) { 'enable' => $enable, 'device_url' => $device_url, 'senditemtext' => $senditemtext, - 'lastpushid' => $lastpushid)); + 'lastpushid' => $lastpushid, + 'counterunseen' => $counterunseen, + 'addonversion' => $addonversion)); } /* @@ -404,7 +427,7 @@ function windowsphonepush_updatesettings(&$a) { } // no updating if user hasn't enabled the plugin - $enable = get_pconfig(local_user(), 'windowsphonepush', 'enable'); + $enable = PConfig::get(local_user(), 'windowsphonepush', 'enable'); if(! $enable) { return "Plug-in not enabled"; } @@ -426,17 +449,35 @@ function windowsphonepush_updatesettings(&$a) { `v` = '" . $device_url . "'"); if(count($r)) { foreach($r as $rr) { - set_pconfig($rr['uid'], 'windowsphonepush', 'device_url', ''); + PConfig::set($rr['uid'], 'windowsphonepush', 'device_url', ''); logger("WARN: the sent URL was already registered with user '" . $rr['uid'] . "'. Deleted for this user as we expect to be correct now for user '" . local_user() . "'."); } } - set_pconfig(local_user(),'windowsphonepush','device_url', $device_url); + PConfig::set(local_user(),'windowsphonepush','device_url', $device_url); // output the successfull update of the device URL to the logger for error analysis if necessary logger("INFO: Device-URL for user '" . local_user() . "' has been updated with '" . $device_url . "'"); return "Device-URL updated successfully!"; } +/* + * update_counterunseen is used to reset the counter to zero from Windows Phone app + */ +function windowsphonepush_updatecounterunseen() { + if(! local_user()) { + return "Not Authenticated"; + } + + // no updating if user hasn't enabled the plugin + $enable = PConfig::get(local_user(), 'windowsphonepush', 'enable'); + if(! $enable) { + return "Plug-in not enabled"; + } + + PConfig::set(local_user(),'windowsphonepush','counterunseen', 0); + return "Counter set to zero"; +} + /* * helper function to login to the server with the specified Network credentials * (mainly copied from api.php)