mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-01 11:35:55 +01:00
foundation for user notifications
This commit is contained in:
60
src/notifications.js
Normal file
60
src/notifications.js
Normal file
@@ -0,0 +1,60 @@
|
||||
var config = require('../config.js'),
|
||||
RDB = require('./redis.js');
|
||||
|
||||
(function(Notifications) {
|
||||
Notifications.get = function(nid, callback) {
|
||||
RDB.hmget('notifications:' + nid, 'text', 'score', 'path', function(err, notification) {
|
||||
callback({
|
||||
text: notification[0],
|
||||
score: notification[1],
|
||||
path: notification[2]
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Notifications.create = function(text, score, path, callback) {
|
||||
/*
|
||||
* Score guide:
|
||||
* 0 Low priority messages (probably unused)
|
||||
* 5 Normal messages
|
||||
* 10 High priority messages
|
||||
*/
|
||||
RDB.incr('notifications:next_nid', function(err, nid) {
|
||||
RDB.hmset(
|
||||
'notifications:' + nid,
|
||||
'text', text || '',
|
||||
'score', score || 5,
|
||||
'path', path || null,
|
||||
'datetime', new Date().getTime(),
|
||||
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);
|
||||
});
|
||||
}
|
||||
}
|
||||
}(exports));
|
||||
49
src/user.js
49
src/user.js
@@ -1,5 +1,3 @@
|
||||
|
||||
|
||||
var config = require('../config.js'),
|
||||
utils = require('./utils.js'),
|
||||
RDB = require('./redis.js'),
|
||||
@@ -7,12 +5,11 @@ var config = require('../config.js'),
|
||||
emailjs = require('emailjs'),
|
||||
emailjsServer = emailjs.server.connect(config.mailer),
|
||||
bcrypt = require('bcrypt'),
|
||||
marked = require('marked');
|
||||
|
||||
marked = require('marked'),
|
||||
notifications = require('./notifications.js'),
|
||||
async = require('async');
|
||||
|
||||
(function(User) {
|
||||
|
||||
|
||||
User.getUserField = function(uid, field, callback) {
|
||||
RDB.hget('user:'+uid, field, function(err, data){
|
||||
if(err === null)
|
||||
@@ -750,4 +747,44 @@ var config = require('../config.js'),
|
||||
this.get();
|
||||
}
|
||||
}
|
||||
|
||||
User.notifications = {
|
||||
get: function(uid, callback) {
|
||||
async.parallel({
|
||||
unread: function(next) {
|
||||
RDB.zrangebyscore('uid:' + uid + ':notifications:unread', 0, 10, function(err, nids) {
|
||||
var unread = [];
|
||||
if (nids && nids.length > 0) {
|
||||
async.eachSeries(nids, function(nid, next) {
|
||||
notifications.get(nid, function(notif_data) {
|
||||
unread.push(notif_data);
|
||||
next();
|
||||
});
|
||||
}, function(err) {
|
||||
next(null, unread);
|
||||
});
|
||||
} else next(null, unread);
|
||||
});
|
||||
},
|
||||
read: function(next) {
|
||||
RDB.zrangebyscore('uid:' + uid + ':notifications:read', 0, 10, function(err, nids) {
|
||||
var read = [];
|
||||
if (nids && nids.length > 0) {
|
||||
async.eachSeries(nids, function(nid, next) {
|
||||
notifications.get(nid, function(notif_data) {
|
||||
read.push(notif_data);
|
||||
next();
|
||||
});
|
||||
}, function(err) {
|
||||
console.log(read);
|
||||
next(null, read);
|
||||
});
|
||||
} else next(null, read);
|
||||
});
|
||||
}
|
||||
}, function(err, notifications) {
|
||||
callback(notifications);
|
||||
});
|
||||
}
|
||||
}
|
||||
}(exports));
|
||||
@@ -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'),
|
||||
fs = require('fs'),
|
||||
|
||||
user = require('./user.js'),
|
||||
categories = require('./categories.js'),
|
||||
posts = require('./posts.js'),
|
||||
topics = require('./topics.js'),
|
||||
notifications = require('./notifications.js'),
|
||||
utils = require('./utils.js'),
|
||||
fs = require('fs'),
|
||||
admin = require('./routes/admin.js'),
|
||||
userRoute = require('./routes/user.js'),
|
||||
auth = require('./routes/authentication.js');
|
||||
@@ -239,9 +240,9 @@ var express = require('express'),
|
||||
app.get('/api/:method/:id*', api_method);
|
||||
|
||||
app.get('/test', function(req, res) {
|
||||
categories.get(function(category) {
|
||||
res.send(JSON.stringify(category, null, 4));
|
||||
}, null, 2, null, null);
|
||||
user.notifications.get(2, function(notifs) {
|
||||
res.send(JSON.stringify(notifs, null, 4));
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
@@ -6,7 +6,8 @@ var SocketIO = require('socket.io').listen(global.server,{log:false}),
|
||||
user = require('./user.js'),
|
||||
posts = require('./posts.js'),
|
||||
topics = require('./topics.js'),
|
||||
categories = require('./categories.js');
|
||||
categories = require('./categories.js'),
|
||||
notifications = require('./notifications.js');
|
||||
|
||||
(function(io) {
|
||||
var users = {},
|
||||
@@ -233,6 +234,12 @@ var SocketIO = require('socket.io').listen(global.server,{log:false}),
|
||||
socket.on('api:posts.restore', function(data) {
|
||||
posts.restore(uid, data.pid);
|
||||
});
|
||||
|
||||
socket.on('api:notifications.get', function(data) {
|
||||
notifications.get(uid, function(notifs) {
|
||||
socket.emit('api:notifications.get', notifs);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
}(SocketIO));
|
||||
|
||||
Reference in New Issue
Block a user