Skip to content

Node.js based forum software built for the modern web

Merge remote-tracking branch 'origin' into notifications

Julian Lam1f7ab82f7888
diff --cc public/templates/footer.tpl
index 0e917d98a3,c21eb5599f..272be6477e
--- a/public/templates/footer.tpl
+++ b/public/templates/footer.tpl
@@@ -86,52 -88,6 +86,52 @@@
$(postWindowEl).slideToggle(250);
$(document.body).removeClass('composing');
}, false);
+
+ // Notifications dropdown
- var notifTrigger = document.querySelector('.notifications a'),
++ var notifContainer = document.getElementsByClassName('notifications')[0],
++ notifTrigger = notifContainer.querySelector('a'),
+ notifList = document.getElementById('notif-list');
+ notifTrigger.addEventListener('click', function() {
- socket.emit('api:notifications.get');
++ if (notifContainer.className.indexOf('open') === -1) socket.emit('api:notifications.get');
+ });
+ notifList.addEventListener('click', function(e) {
+ var target;
+ switch(e.target.nodeName) {
+ case 'SPAN': target = e.target.parentNode.parentNode; break;
+ case 'A': target = e.target.parentNode; break;
+ case 'li': target = e.target; break;
+ }
+ if (target) {
+ var nid = target.getAttribute('data-nid');
+ socket.emit('api:notifications.mark_read', nid);
+ }
+ })
+ socket.on('api:notifications.get', function(data) {
- console.log(data);
+ var notifFrag = document.createDocumentFragment(),
+ notifEl = document.createElement('li'),
+ numRead = data.read.length,
+ numUnread = data.unread.length,
+ x;
+ notifList.innerHTML = '';
+ for(x=0;x<numUnread;x++) {
+ notifEl.setAttribute('data-nid', data.unread[x].nid);
+ notifEl.className = 'unread';
+ notifEl.innerHTML = '<a href="' + data.unread[x].path + '"><span class="pull-right">11m</span>' + data.unread[x].text + '</a>';
+ notifFrag.appendChild(notifEl.cloneNode(true));
+ }
+ for(x=0;x<numRead;x++) {
+ notifEl.setAttribute('data-nid', data.read[x].nid);
+ notifEl.className = '';
+ notifEl.innerHTML = '<a href="' + data.read[x].path + '"><span class="pull-right">11m</span>' + data.read[x].text + '</a>';
+ notifFrag.appendChild(notifEl.cloneNode(true));
+ }
+ notifList.appendChild(notifFrag);
+ });
+ socket.on('api:notifications.counts', function(counts) {
+ var notifIcon = document.querySelector('.notifications a i');
+ if ((counts.unread + counts.read) > 0) notifIcon.className = 'icon-circle active';
+ else notifIcon.className = 'icon-circle-blank';
+ });
}());
</script>
<!-- END Forum Info -->
diff --cc src/notifications.js
index 3880d3df06,0000000000..98d61d5294
mode 100644,000000..100644
--- a/src/notifications.js
+++ b/src/notifications.js
@@@ -1,73 -1,0 +1,80 @@@
+var config = require('../config.js'),
+ RDB = require('./redis.js'),
- async = require('async');
++ async = require('async'),
++ utils = require('./utils.js');
+
+(function(Notifications) {
+ Notifications.get = function(nid, callback) {
+ RDB.hmget('notifications:' + nid, 'text', 'score', 'path', 'datetime', function(err, notification) {
+ callback({
+ nid: nid,
+ text: notification[0],
+ score: notification[1],
+ path: notification[2],
+ datetime: notification[3]
+ });
+ });
+ }
+
- Notifications.create = function(text, score, path, callback) {
++ Notifications.create = function(text, score, path, uniqueId, callback) {
+ /*
+ * Score guide:
+ * 0 Low priority messages (probably unused)
+ * 5 Normal messages
+ * 10 High priority messages
++ *
++ * uniqueId is used solely to override stale nids.
++ * If a new nid is pushed to a user and an existing nid in the user's
++ * (un)read list contains the same uniqueId, it will be removed, and
++ * the new one put in its place.
+ */
+ RDB.incr('notifications:next_nid', function(err, nid) {
+ RDB.hmset(
+ 'notifications:' + nid,
+ 'text', text || '',
+ 'score', score || 5,
+ 'path', path || null,
+ 'datetime', new Date().getTime(),
++ 'uniqueId', uniqueId || utils.generateUUID(),
+ function(err, status) {
+ if (status === 'OK') callback(nid);
+ });
+ });
+ }
+
+ Notifications.push = function(nid, uids, callback) {
+ if (!Array.isArray(uids)) uids = [uids];
+
+ var numUids = uids.length,
+ x;
+
+ Notifications.get(nid, function(notif_data) {
+ for(x=0;x<numUids;x++) {
+ if (parseInt(uids[x]) > 0) {
+ RDB.zadd('uid:' + uids[x] + ':notifications:unread', notif_data.score, nid);
+ if (callback) callback(true);
+ }
+ }
+ });
+ }
+
+ Notifications.mark_read = function(nid, uid, callback) {
+ if (parseInt(uid) > 0) {
+ Notifications.get(nid, function(notif_data) {
+ RDB.zrem('uid:' + uid + ':notifications:unread', nid);
+ RDB.zadd('uid:' + uid + ':notifications:read', notif_data.score, nid);
+ if (callback) callback(true);
+ });
+ }
+ }
+
+ Notifications.mark_read_multiple = function(nids, uid, callback) {
+ async.each(nids, function(nid, next) {
+ Notifications.mark_read(nid, uid, function(success) {
+ if (success) next(null);
+ });
+ }, function(err) {
+ if (callback && !err) callback(true);
+ });
+ }
+}(exports));
diff --cc src/user.js
index cefd1eab44,26f679ea4e..a600fbf9e9
--- a/src/user.js
+++ b/src/user.js
@@@ -1,5 -1,7 +1,5 @@@
-
-
var config = require('../config.js'),
- utils = require('./utils.js'),
+ utils = require('./../public/src/utils.js'),
RDB = require('./redis.js'),
crypto = require('crypto'),
emailjs = require('emailjs'),
diff --cc src/webserver.js
index 4af407527a,113d5229fb..c5794fc316
--- a/src/webserver.js
+++ b/src/webserver.js
@@@ -7,14 -7,13 +7,14 @@@ var express = require('express')
redis = require('redis'),
redisServer = redis.createClient(config.redis.port, config.redis.host, config.redis.options),
marked = require('marked'),
++ utils = require('./../public/src/utils.js'),
+ fs = require('fs'),
user = require('./user.js'),
categories = require('./categories.js'),
posts = require('./posts.js'),
topics = require('./topics.js'),
- utils = require('./../public/src/utils.js'),
- fs = require('fs'),
+ notifications = require('./notifications.js'),
- utils = require('./utils.js'),
admin = require('./routes/admin.js'),
userRoute = require('./routes/user.js'),
auth = require('./routes/authentication.js');