From 6fc864c947f85999056657b17dae14e4cd167ecb Mon Sep 17 00:00:00 2001 From: Fabrixxm Date: Thu, 25 Jun 2015 10:02:26 +0200 Subject: [PATCH] rework mod/ping and desktop notification logic `mod/ping.php` returns all notifications ordered by date mixing system notifications, private messages, intros, registrations remove 'notify_full' mode remove `&arr;` from unseen notes, 'seen' attrib now is "1" or "0" new note attribute 'timestamp' desktop notification logic use timestamp to check which notification show to the user notification html template use timestamp to show full date to user in tooltip `relative_date()` don't append full date on dates older than 24 hours --- include/datetime.php | 4 +- js/main.js | 13 +- mod/ping.php | 204 ++++++++++------------ view/templates/nav.tpl | 2 +- view/theme/duepuntozero/templates/nav.tpl | 2 +- view/theme/quattro/templates/nav.tpl | 2 +- view/theme/vier/templates/nav.tpl | 38 +--- 7 files changed, 112 insertions(+), 153 deletions(-) diff --git a/include/datetime.php b/include/datetime.php index 8ac8ff0f2..6461298ba 100644 --- a/include/datetime.php +++ b/include/datetime.php @@ -262,10 +262,12 @@ function relative_date($posted_date,$format = null) { return t('less than a second ago'); } + /* $time_append = ''; if ($etime >= 86400) { $time_append = ' ('.$localtime.')'; } + */ $a = array( 12 * 30 * 24 * 60 * 60 => array( t('year'), t('years')), 30 * 24 * 60 * 60 => array( t('month'), t('months')), @@ -283,7 +285,7 @@ function relative_date($posted_date,$format = null) { // translators - e.g. 22 hours ago, 1 minute ago if(! $format) $format = t('%1$d %2$s ago'); - return sprintf( $format,$r, (($r == 1) ? $str[0] : $str[1])).$time_append; + return sprintf( $format,$r, (($r == 1) ? $str[0] : $str[1])); } } }} diff --git a/js/main.js b/js/main.js index b812e2508..239a875cb 100644 --- a/js/main.js +++ b/js/main.js @@ -186,13 +186,20 @@ var notification_id = 0; eNotif.children("note").each(function(){ e = $(this); - text = e.text().format(""+e.attr('name')+""); - html = notifications_tpl.format(e.attr('href'),e.attr('photo'), text, e.attr('date'), e.attr('seen')); + var text = e.text().format(""+e.attr('name')+""); + var seenclass = (e.attr('seen')==1)?"notify-seen":"notify-unseen"; + var html = notifications_tpl.format(e.attr('href'), + e.attr('photo'), // {0} + text, // {1} + e.attr('date'), // {2} + seenclass, // {3} + new Date(e.attr('timestamp')*1000) // {4} + ); nnm.append(html); }); $(eNotif.children("note").get().reverse()).each(function(){ e = $(this); - notification_id = parseInt(e.attr('href').match(/\d+$/)[0]); + notification_id = parseInt(e.attr('timestamp')); if (notification_lastitem!== null && notification_id > notification_lastitem) { if (getNotificationPermission()==="granted") { var notification = new Notification(document.title, { diff --git a/mod/ping.php b/mod/ping.php index a5f23e1eb..e87ed9855 100644 --- a/mod/ping.php +++ b/mod/ping.php @@ -21,9 +21,7 @@ function ping_init(&$a) { killme(); } - $firehose = intval(get_pconfig(local_user(),'system','notify_full')); - - $z = ping_get_notifications(local_user()); + $notifs = ping_get_notifications(local_user()); $sysnotify = 0; // we will update this in a moment $tags = array(); @@ -162,12 +160,23 @@ function ping_init(&$a) { } + /** + * return xml from notification array + * + * @param array $n Notification array: + * 'href' => notification link + * 'name' => subject name + * 'url' => subject url + * 'photo' => subject photo + * 'date' => notification date + * 'seen' => bool true/false + * 'message' => notification message. "{0}" will be replaced by subject name + **/ + function xmlize($n){ + $n['photo'] = proxy_url($n['photo']); - function xmlize($href, $name, $url, $photo, $date, $seen, $message){ - $photo = proxy_url($photo); - - $message = html_entity_decode($message, ENT_COMPAT | ENT_HTML401, "UTF-8"); - $name = html_entity_decode($name, ENT_COMPAT | ENT_HTML401, "UTF-8"); + $n['message'] = html_entity_decode($n['message'], ENT_COMPAT | ENT_HTML401, "UTF-8"); + $n['name'] = html_entity_decode($n['name'], ENT_COMPAT | ENT_HTML401, "UTF-8"); // Are the nofications calles from the regular process or via the friendica app? $regularnotifications = (intval($_GET['uid']) AND intval($_GET['_'])); @@ -175,13 +184,16 @@ function ping_init(&$a) { $a = get_app(); if ($a->is_friendica_app() OR !$regularnotifications) - $message = str_replace("{0}", $name, $message); + $n['message'] = str_replace("{0}", $n['name'], $n['message']); - $data = array('href' => &$href, 'name' => &$name, 'url'=>&$url, 'photo'=>&$photo, 'date'=>&$date, 'seen'=>&$seen, 'messsage'=>&$message); - call_hooks('ping_xmlize', $data); - $notsxml = '%s'."\n"; + $local_time = datetime_convert('UTC',date_default_timezone_get(),$n['date']); + + call_hooks('ping_xmlize', $n); + $notsxml = '%s'."\n"; return sprintf ( $notsxml, - xmlify($href), xmlify($name), xmlify($url), xmlify($photo), xmlify($date), xmlify($seen), xmlify($message) + xmlify($n['href']), xmlify($n['name']), xmlify($n['url']), xmlify($n['photo']), + xmlify(relative_date($n['date'])), xmlify($n['seen']), xmlify(strtotime($local_time)), + xmlify($n['message']) ); } @@ -198,98 +210,78 @@ function ping_init(&$a) { $birthdays $birthdays_today\r\n"; - $tot = $mail+$intro+$register+count($comments)+count($likes)+count($dislikes)+count($friends)+count($posts)+count($tags); - if($firehose) { - echo ' '; - } - else { - if(count($z) && (! $sysnotify)) { - foreach($z as $zz) { - if($zz['seen'] == 0) - $sysnotify ++; - } - } - - echo ' '; - - if ($intro>0){ - foreach ($intros as $i) { - echo xmlize($a->get_baseurl().'/notifications/intros/'.$i['id'], $i['name'], $i['url'], $i['photo'], relative_date($i['datetime']), 'notify-unseen', t("{0} wants to be your friend")); - }; - } - if ($mail>0){ - foreach ($mails as $i) { - echo xmlize($a->get_baseurl().'/message/'.$i['id'], $i['from-name'], $i['from-url'], $i['from-photo'], relative_date($i['created']), 'notify-unseen', t("{0} sent you a message")); - }; - } - if ($register>0){ - foreach ($regs as $i) { - echo xmlize($a->get_baseurl().'/admin/users/', $i['name'], $i['url'], $i['micro'], relative_date($i['created']), 'notify-unseen', t("{0} requested registration")); - }; - } - - if(count($z)) { - foreach($z as $zz) { - echo xmlize($a->get_baseurl() . '/notify/view/' . $zz['id'], $zz['name'],$zz['url'],$zz['photo'],relative_date($zz['date']), ($zz['seen'] ? 'notify-seen' : 'notify-unseen'), strip_tags(bbcode($zz['msg']))); - } + if(count($notifs) && (! $sysnotify)) { + foreach($notifs as $zz) { + if($zz['seen'] == 0) + $sysnotify ++; } } - if($firehose) { - if ($intro>0){ - foreach ($intros as $i) { - echo xmlize( $a->get_baseurl().'/notifications/intros/'.$i['id'], $i['name'], $i['url'], $i['photo'], relative_date($i['datetime']), 'notify-unseen',t("{0} wants to be your friend") ); - }; - } - if ($mail>0){ - foreach ($mails as $i) { - echo xmlize( $a->get_baseurl().'/message/'.$i['id'], $i['from-name'], $i['from-url'], $i['from-photo'], relative_date($i['created']), 'notify-unseen',t("{0} sent you a message") ); - }; - } - if ($register>0){ - foreach ($regs as $i) { - echo xmlize( $a->get_baseurl().'/admin/users/', $i['name'], $i['url'], $i['micro'], relative_date($i['created']), 'notify-unseen',t("{0} requested registration") ); - }; - } + echo ' '; - if (count($comments)){ - foreach ($comments as $i) { - echo xmlize( $a->get_baseurl().'/display/'.$a->user['nickname']."/".$i['parent'], $i['author-name'], $i['author-link'], $i['author-avatar'], relative_date($i['created']), 'notify-unseen',sprintf( t("{0} commented %s's post"), $i['pname'] ) ); - }; - } - if (count($likes)){ - foreach ($likes as $i) { - echo xmlize( $a->get_baseurl().'/display/'.$a->user['nickname']."/".$i['parent'], $i['author-name'], $i['author-link'], $i['author-avatar'], relative_date($i['created']), 'notify-unseen',sprintf( t("{0} liked %s's post"), $i['pname'] ) ); - }; - } - if (count($dislikes)){ - foreach ($dislikes as $i) { - echo xmlize( $a->get_baseurl().'/display/'.$a->user['nickname']."/".$i['parent'], $i['author-name'], $i['author-link'], $i['author-avatar'], relative_date($i['created']), 'notify-unseen',sprintf( t("{0} disliked %s's post"), $i['pname'] ) ); - }; - } - if (count($friends)){ - foreach ($friends as $i) { - echo xmlize($a->get_baseurl().'/display/'.$a->user['nickname']."/".$i['parent'],$i['author-name'],$i['author-link'], $i['author-avatar'], relative_date($i['created']), 'notify-unseen',sprintf( t("{0} is now friends with %s"), $i['fname'] ) ); - }; - } - if (count($posts)){ - foreach ($posts as $i) { - echo xmlize( $a->get_baseurl().'/display/'.$a->user['nickname']."/".$i['parent'], $i['author-name'], $i['author-link'], $i['author-avatar'], relative_date($i['created']), 'notify-unseen',sprintf( t("{0} posted") ) ); - }; - } - if (count($tags)){ - foreach ($tags as $i) { - echo xmlize( $a->get_baseurl().'/display/'.$a->user['nickname']."/".$i['parent'], $i['author-name'], $i['author-link'], $i['author-avatar'], relative_date($i['created']), 'notify-unseen',sprintf( t("{0} tagged %s's post with #%s"), $i['pname'], $i['tname'] ) ); - }; - } - - if (count($cit)){ - foreach ($cit as $i) { - echo xmlize( $a->get_baseurl().'/display/'.$a->user['nickname']."/".$i['parent'], $i['author-name'], $i['author-link'], $i['author-avatar'], relative_date($i['created']), 'notify-unseen',t("{0} mentioned you in a post") ); - }; + // merge all notification types in one array + if ($intro>0){ + foreach ($intros as $i) { + $n = array( + 'href' => $a->get_baseurl().'/notifications/intros/'.$i['id'], + 'name' => $i['name'], + 'url' => $i['url'], + 'photo' => $i['photo'], + 'date' => $i['datetime'], + 'seen' => false, + 'message' => t("{0} wants to be your friend"), + ); + $notifs[] = $n; } } + + if ($mail>0){ + foreach ($mails as $i) { + $n = array( + 'href' => $a->get_baseurl().'/message/'.$i['id'], + 'name' => $i['from-name'], + 'url' => $i['from-url'], + 'photo' => $i['from-photo'], + 'date' => $i['created'], + 'seen' => false, + 'message' => t("{0} sent you a message"), + ); + $notifs[] = $n; + } + } + + if ($register>0){ + foreach ($regs as $i) { + $n = array( + 'href' => $a->get_baseurl().'/admin/users/', + 'name' => $i['name'], + 'url' => $i['url'], + 'photo' => $i['micro'], + 'date' => $i['created'], + 'seen' => false, + 'message' => t("{0} requested registration"), + ); + $notifs[] = $n; + } + } + // sort notifications by $[]['date'] + $sort_function = function($a, $b) { + $adate = date($a['date']); + $bdate = date($b['date']); + if ($adate == $bdate) { + return 0; + } + return ($adate < $bdate) ? 1 : -1; + }; + usort($notifs, $sort_function); + + if(count($notifs)) { + foreach($notifs as $n) { + echo xmlize($n); + } + } + echo " "; } @@ -359,16 +351,18 @@ function ping_get_notifications($uid) { if (is_null($notification["deleted"])) $notification["deleted"] = 0; - $notification["msg"] = strip_tags(bbcode($notification["msg"])); + $notification["message"] = strip_tags(bbcode($notification["msg"])); $notification["name"] = strip_tags(bbcode($notification["name"])); // Replace the name with {0} but ensure to make that only once // The {0} is used later and prints the name in bold. - $pos = strpos($notification["msg"],$notification['name']); + $pos = strpos($notification["message"],$notification['name']); if ($pos !== false) - $notification["msg"] = substr_replace($notification["msg"],"{0}",$pos,strlen($notification["name"])); + $notification["message"] = substr_replace($notification["message"],"{0}",$pos,strlen($notification["name"])); + $notification['href'] = $a->get_baseurl() . '/notify/view/' . $notification['id']; + if ($notification["visible"] AND !$notification["spam"] AND !$notification["deleted"] AND !is_array($result[$notification["parent"]])) { $result[$notification["parent"]] = $notification; @@ -377,14 +371,6 @@ function ping_get_notifications($uid) { } while ((count($result) < 50) AND !$quit); - // sort result by $[]['id'], inversed - $sort_function = function($a, $b) { - if ($a['id'] == $b['id']) { - return 0; - } - return ($a['id'] < $b['id']) ? 1 : -1; - }; - usort($result, $sort_function); return($result); } diff --git a/view/templates/nav.tpl b/view/templates/nav.tpl index 3a54066ad..02fe6da3b 100644 --- a/view/templates/nav.tpl +++ b/view/templates/nav.tpl @@ -65,5 +65,5 @@ diff --git a/view/theme/duepuntozero/templates/nav.tpl b/view/theme/duepuntozero/templates/nav.tpl index 3e821a31c..8149ef374 100644 --- a/view/theme/duepuntozero/templates/nav.tpl +++ b/view/theme/duepuntozero/templates/nav.tpl @@ -67,5 +67,5 @@ diff --git a/view/theme/quattro/templates/nav.tpl b/view/theme/quattro/templates/nav.tpl index c77ed99e4..e0c7ba8e3 100644 --- a/view/theme/quattro/templates/nav.tpl +++ b/view/theme/quattro/templates/nav.tpl @@ -109,7 +109,7 @@
{{$langselector}}
diff --git a/view/theme/vier/templates/nav.tpl b/view/theme/vier/templates/nav.tpl index 4ef640080..26b34073a 100644 --- a/view/theme/vier/templates/nav.tpl +++ b/view/theme/vier/templates/nav.tpl @@ -97,44 +97,8 @@ -{{* - -{{if $nav.logout}}{{$nav.logout.1}} {{/if}} -{{if $nav.login}} {{/if}} - - - -{{if $nav.register}}{{$nav.register.1}}{{/if}} - -{{$nav.help.1}} - -{{if $nav.apps}}{{$nav.apps.1}}{{/if}} - -{{$nav.search.1}} -{{$nav.directory.1}} - -{{if $nav.admin}}{{$nav.admin.1}}{{/if}} - -{{if $nav.notifications}} -{{$nav.notifications.1}} - -{{/if}} -{{if $nav.messages}} -{{$nav.messages.1}} - -{{/if}} - -{{if $nav.manage}}{{$nav.manage.1}}{{/if}} -{{if $nav.settings}}{{$nav.settings.1}}{{/if}} -{{if $nav.profiles}}{{$nav.profiles.1}}{{/if}} - - - - - -*}}