diff --git a/QvitterPlugin.php b/QvitterPlugin.php index 5b1fd88..bdc2b53 100644 --- a/QvitterPlugin.php +++ b/QvitterPlugin.php @@ -140,8 +140,6 @@ class QvitterPlugin extends Plugin { $m->connect(':nickname/notifications', array('action' => 'qvitter', 'nickname' => Nickname::INPUT_FMT)); - $m->connect('api/qvitter/newnotifications.json', - array('action' => 'ApiNewNotifications')); $m->connect('settings/qvitter', array('action' => 'qvittersettings')); $m->connect('panel/qvitter', @@ -775,8 +773,34 @@ class QvitterPlugin extends Plugin { return true; } - - + + /** + * Add unread notification count to all API responses + * + * @return boolean hook flag + */ + public function onEndSetApiUser($user) { + + $user_id = $user->id; + $notification = new QvitterNotification(); + + $notification->selectAdd(); + $notification->selectAdd('ntype'); + $notification->selectAdd('count(id) as count'); + $notification->whereAdd("(to_profile_id = '".$user_id."')"); + $notification->groupBy('ntype'); + $notification->whereAdd("(is_seen = '0')"); + $notification->whereAdd("(notice_id != 'NULL')"); // sometimes notice_id is NULL, those notifications are corrupt and should be discarded + $notification->find(); + + while ($notification->fetch()) { + $new_notifications[$notification->ntype] = $notification->count; + } + + header('Qvitter-Notifications: '.json_encode($new_notifications)); + + return true; + } function onPluginVersion(&$versions) diff --git a/actions/apinewnotifications.php b/actions/apinewnotifications.php deleted file mode 100644 index d0a57db..0000000 --- a/actions/apinewnotifications.php +++ /dev/null @@ -1,101 +0,0 @@ -. - * - * @category API - * @package GNUsocial - * @author Hannes Mannerheim - * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 - * @link http://www.gnu.org/software/social/ - */ - -if (!defined('GNUSOCIAL')) { exit(1); } - -class ApiNewNotificationsAction extends ApiAction -{ - - /** - * Take arguments for running - * - * @param array $args $_REQUEST args - * - * @return boolean success flag - */ - protected function prepare(array $args=array()) - { - parent::prepare($args); - - return true; - } - - /** - * Handle the request - * - * @param array $args $_REQUEST data (unused) - * - * @return void - */ - protected function handle() - { - parent::handle(); - - - $new_notifications = array(); - if(Profile::current()) { - $user_id = Profile::current()->id; - $notification = new QvitterNotification(); - - $notification->selectAdd(); - $notification->selectAdd('ntype'); - $notification->selectAdd('count(id) as count'); - $notification->whereAdd("(to_profile_id = '".$user_id."')"); - $notification->groupBy('ntype'); - $notification->whereAdd("(is_seen = '0')"); - $notification->whereAdd("(notice_id != 'NULL')"); // sometimes notice_id is NULL, those notifications are corrupt and should be discarded - $notification->find(); - - while ($notification->fetch()) { - $new_notifications[$notification->ntype] = $notification->count; - } - } - else { - $new_notifications = 'You must be logged in.'; - } - - - $this->initDocument('json'); - $this->showJsonObjects($new_notifications); - $this->endDocument('json'); - } - - /** - * Return true if read only. - * - * MAY override - * - * @param array $args other arguments - * - * @return boolean is read only action? - */ - - function isReadOnly($args) - { - return true; - } -} diff --git a/js/ajax-functions.js b/js/ajax-functions.js index b2f802e..d0d5278 100644 --- a/js/ajax-functions.js +++ b/js/ajax-functions.js @@ -138,8 +138,10 @@ function getFromAPI(stream, actionOnSuccess) { } } }, - success: function(data) { + success: function(data, textStatus, request) { + displayOrHideUnreadNotifications(request.getResponseHeader('Qvitter-Notifications')); + data = convertEmptyObjectToEmptyArray(data); data = iterateRecursiveReplaceHtmlSpecialChars(data); @@ -379,53 +381,3 @@ function getFavsOrRequeetsForQueet(apiaction,qid,actionOnSuccess) { } }); } - - -/* · - · - · Check for unseen notifications - · - · @param actionOnSuccess: callback function - · - · · · · · · · · · */ - -function checkForNewNotifications() { - $.ajax({ url: window.apiRoot + "qvitter/newnotifications.json", - type: "GET", - dataType: 'json', - success: function(data) { - - if(data.length == 0) { - $('#unseen-notifications').hide(); - document.title = window.siteTitle; - } - else { - - var totNotif = 0; - $.each(data,function(k,v){ - totNotif = totNotif + parseInt(v,10); - }); - - if(totNotif>0) { - $('#unseen-notifications').html(totNotif); - document.title = window.siteTitle + ' (' + totNotif + ')'; // update html page title - $('#unseen-notifications').show(); - } - else { - $('#unseen-notifications').hide(); - document.title = window.siteTitle; - } - } - - - }, - error: function(data) { - $('#unseen-notifications').hide(); - document.title = window.siteTitle; - remove_spinner(); - console.log(data); - } - }); - } - - \ No newline at end of file diff --git a/js/dom-functions.js b/js/dom-functions.js index de66e4c..336ae13 100644 --- a/js/dom-functions.js +++ b/js/dom-functions.js @@ -293,9 +293,6 @@ function setNewCurrentStream(stream,actionOnSuccess,setLocation) { // halt interval that checks for new queets window.clearInterval(checkForNewQueetsInterval); - if(typeof checkForNewNotificationsInterval != 'undefined') { - window.clearInterval(checkForNewNotificationsInterval); - } display_spinner(); @@ -458,14 +455,8 @@ function setNewCurrentStream(stream,actionOnSuccess,setLocation) { // start checking for new queets again window.clearInterval(checkForNewQueetsInterval); - if(typeof checkForNewNotificationsInterval != 'undefined') { - window.clearInterval(checkForNewNotificationsInterval); - } checkForNewQueetsInterval=window.setInterval(function(){checkForNewQueets()},window.timeBetweenPolling); - if(typeof checkForNewNotificationsInterval != 'undefined') { - checkForNewNotificationsInterval=window.setInterval(function(){checkForNewNotifications()},window.timeBetweenPolling); - checkForNewNotifications(); - } + remove_spinner(); $('#feed-body').html(''); // empty feed only now so the scrollers don't flicker on and off $('#new-queets-bar').parent().addClass('hidden'); document.title = window.siteTitle; // hide new queets bar if it's visible there @@ -506,18 +497,7 @@ function setNewCurrentStream(stream,actionOnSuccess,setLocation) { // start checking for new queets again window.clearInterval(checkForNewQueetsInterval); - if(typeof checkForNewNotificationsInterval != 'undefined') { - window.clearInterval(checkForNewNotificationsInterval); - } checkForNewQueetsInterval=window.setInterval(function(){checkForNewQueets()},window.timeBetweenPolling); - if(window.currentStream != 'qvitter/statuses/notifications.json' && typeof checkForNewNotificationsInterval != 'undefined') { // don't check for notifications if this is the notifications page - checkForNewNotifications(); - checkForNewNotificationsInterval=window.setInterval(function(){checkForNewNotifications()},window.timeBetweenPolling); - } - else { - $('#unseen-notifications').hide(); - document.title = window.siteTitle; - } remove_spinner(); $('#feed-body').html(''); // empty feed only now so the scrollers don't flicker on and off diff --git a/js/misc-functions.js b/js/misc-functions.js index 034616a..d9be7e9 100644 --- a/js/misc-functions.js +++ b/js/misc-functions.js @@ -33,6 +33,56 @@ · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · */ +/* · + · + · Display unread notifications + · + · · · · · · · · · */ + +function displayOrHideUnreadNotifications(notifications) { + + var data = $.parseJSON(notifications); + + // if this is notifications page, we use the info from the hidden items in the feed + if(window.currentStream == 'qvitter/statuses/notifications.json') { + var new_queets_num = $('#feed-body').find('.stream-item.hidden').length; + + if(new_queets_num == 0) { + document.title = window.siteTitle; + $('#unseen-notifications').hide(); + } + else { + document.title = window.siteTitle + ' (' + new_queets_num + ')'; + $('#unseen-notifications').html(new_queets_num); + $('#unseen-notifications').show(); + } + } + // all other pages use the header info + else if(data === null || typeof data == 'undefined' || data.length == 0) { + $('#unseen-notifications').hide(); + document.title = window.siteTitle; + } + else { + + var totNotif = 0; + $.each(data,function(k,v){ + totNotif = totNotif + parseInt(v,10); + }); + + if(totNotif>0) { + $('#unseen-notifications').html(totNotif); + document.title = window.siteTitle + ' (' + totNotif + ')'; // update html page title + $('#unseen-notifications').show(); + } + else { + $('#unseen-notifications').hide(); + document.title = window.siteTitle; + } + } + + } + + /* · · · Removes HTML special chars recursively from strings in objects diff --git a/js/qvitter.js b/js/qvitter.js index 5d42334..47578af 100644 --- a/js/qvitter.js +++ b/js/qvitter.js @@ -358,11 +358,7 @@ function doLogin(streamToSet) { // load history loadHistoryFromLocalStorage(); - - // start checking for notifications - var checkForNewNotificationsInterval=window.setInterval(function(){checkForNewNotifications()},window.timeBetweenPolling); - checkForNewNotifications(); - + // set stream window.currentStream = ''; // always reload stream on login setNewCurrentStream(streamToSet,function(){