new notifications check moved to http header to reduce polling
This commit is contained in:
parent
d3ba01ebae
commit
8d2d4d7cca
|
@ -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',
|
||||
|
@ -776,7 +774,33 @@ class QvitterPlugin extends Plugin {
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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)
|
||||
|
|
|
@ -1,101 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
*
|
||||
* Qvitter: Unread notifications
|
||||
*
|
||||
* PHP version 5
|
||||
*
|
||||
* LICENCE: This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* @category API
|
||||
* @package GNUsocial
|
||||
* @author Hannes Mannerheim <h@nnesmannerhe.im>
|
||||
* @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;
|
||||
}
|
||||
}
|
|
@ -138,7 +138,9 @@ function getFromAPI(stream, actionOnSuccess) {
|
|||
}
|
||||
}
|
||||
},
|
||||
success: function(data) {
|
||||
success: function(data, textStatus, request) {
|
||||
|
||||
displayOrHideUnreadNotifications(request.getResponseHeader('Qvitter-Notifications'));
|
||||
|
||||
data = convertEmptyObjectToEmptyArray(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);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -359,10 +359,6 @@ 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(){
|
||||
|
|
Loading…
Reference in New Issue
Block a user