2014-03-12 18:00:27 -04:00
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
2016-05-05 20:24:03 +03:00
|
|
|
var async = require('async');
|
|
|
|
|
var winston = require('winston');
|
|
|
|
|
var S = require('string');
|
|
|
|
|
|
|
|
|
|
var db = require('../database');
|
|
|
|
|
var meta = require('../meta');
|
|
|
|
|
var notifications = require('../notifications');
|
|
|
|
|
var privileges = require('../privileges');
|
2014-03-12 18:00:27 -04:00
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
(function (UserNotifications) {
|
|
|
|
|
UserNotifications.get = function (uid, callback) {
|
2014-11-05 20:41:31 -05:00
|
|
|
if (!parseInt(uid, 10)) {
|
2017-02-18 12:30:49 -07:00
|
|
|
return callback(null, { read: [], unread: [] });
|
2014-11-05 20:41:31 -05:00
|
|
|
}
|
2016-10-13 11:43:39 +02:00
|
|
|
getNotifications(uid, 0, 9, function (err, notifications) {
|
2014-07-28 16:08:16 -04:00
|
|
|
if (err) {
|
2014-03-12 18:00:27 -04:00
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-16 23:25:30 -04:00
|
|
|
notifications.read = notifications.read.filter(Boolean);
|
|
|
|
|
notifications.unread = notifications.unread.filter(Boolean);
|
2014-03-12 18:00:27 -04:00
|
|
|
|
2014-10-17 18:31:20 -04:00
|
|
|
var maxNotifs = 15;
|
2014-03-12 18:00:27 -04:00
|
|
|
if (notifications.read.length + notifications.unread.length > maxNotifs) {
|
|
|
|
|
notifications.read.length = maxNotifs - notifications.unread.length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
callback(null, notifications);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
UserNotifications.getAll = function (uid, start, stop, callback) {
|
|
|
|
|
getNotifications(uid, start, stop, function (err, notifs) {
|
2015-02-25 15:56:52 -05:00
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
notifs = notifs.unread.concat(notifs.read);
|
2016-10-13 11:43:39 +02:00
|
|
|
notifs = notifs.filter(Boolean).sort(function (a, b) {
|
2015-02-25 15:56:52 -05:00
|
|
|
return b.datetime - a.datetime;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
callback(null, notifs);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2015-10-19 12:48:26 -04:00
|
|
|
function getNotifications(uid, start, stop, callback) {
|
2014-10-17 18:31:20 -04:00
|
|
|
async.parallel({
|
2016-10-13 11:43:39 +02:00
|
|
|
unread: function (next) {
|
2015-10-19 12:48:26 -04:00
|
|
|
getNotificationsFromSet('uid:' + uid + ':notifications:unread', false, uid, start, stop, next);
|
2014-10-17 18:31:20 -04:00
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
read: function (next) {
|
2015-10-19 12:48:26 -04:00
|
|
|
getNotificationsFromSet('uid:' + uid + ':notifications:read', true, uid, start, stop, next);
|
2017-02-17 19:31:21 -07:00
|
|
|
},
|
2014-10-17 18:31:20 -04:00
|
|
|
}, callback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getNotificationsFromSet(set, read, uid, start, stop, callback) {
|
2015-12-19 09:31:03 -05:00
|
|
|
var setNids;
|
|
|
|
|
|
2015-12-16 11:20:21 -05:00
|
|
|
async.waterfall([
|
|
|
|
|
async.apply(db.getSortedSetRevRange, set, start, stop),
|
2016-10-13 11:43:39 +02:00
|
|
|
function (nids, next) {
|
2017-02-18 01:52:56 -07:00
|
|
|
if (!Array.isArray(nids) || !nids.length) {
|
2015-12-16 11:20:21 -05:00
|
|
|
return callback(null, []);
|
2014-08-16 23:25:30 -04:00
|
|
|
}
|
|
|
|
|
|
2015-12-19 09:31:03 -05:00
|
|
|
setNids = nids;
|
2015-12-16 11:20:21 -05:00
|
|
|
UserNotifications.getNotifications(nids, uid, next);
|
|
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
function (notifs, next) {
|
2014-09-08 23:03:37 -04:00
|
|
|
var deletedNids = [];
|
2014-08-16 23:25:30 -04:00
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
notifs.forEach(function (notification, index) {
|
2014-09-08 23:03:37 -04:00
|
|
|
if (!notification) {
|
2015-12-19 09:31:03 -05:00
|
|
|
winston.verbose('[notifications.get] nid ' + setNids[index] + ' not found. Removing.');
|
|
|
|
|
deletedNids.push(setNids[index]);
|
2014-10-17 18:31:20 -04:00
|
|
|
} else {
|
|
|
|
|
notification.read = read;
|
2015-02-19 12:05:19 -05:00
|
|
|
notification.readClass = !notification.read ? 'unread' : '';
|
2014-09-08 23:03:37 -04:00
|
|
|
}
|
2014-08-16 23:25:30 -04:00
|
|
|
});
|
2014-09-08 23:03:37 -04:00
|
|
|
|
|
|
|
|
if (deletedNids.length) {
|
|
|
|
|
db.sortedSetRemove(set, deletedNids);
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-16 11:20:21 -05:00
|
|
|
notifications.merge(notifs, next);
|
2017-02-17 19:31:21 -07:00
|
|
|
},
|
2015-12-16 11:20:21 -05:00
|
|
|
], callback);
|
2014-08-16 23:25:30 -04:00
|
|
|
}
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
UserNotifications.getNotifications = function (nids, uid, callback) {
|
|
|
|
|
notifications.getMultiple(nids, function (err, notifications) {
|
2014-08-16 23:25:30 -04:00
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
2016-10-13 11:43:39 +02:00
|
|
|
notifications = notifications.filter(function (notification) {
|
2016-05-05 20:24:03 +03:00
|
|
|
return notification && notification.path;
|
2014-08-16 23:25:30 -04:00
|
|
|
});
|
2015-03-19 15:24:38 -04:00
|
|
|
callback(null, notifications);
|
2014-08-16 23:25:30 -04:00
|
|
|
});
|
2014-07-28 15:52:33 -04:00
|
|
|
};
|
|
|
|
|
|
2014-08-17 00:14:45 -04:00
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
UserNotifications.getDailyUnread = function (uid, callback) {
|
2016-03-11 14:20:23 +02:00
|
|
|
var yesterday = Date.now() - (1000 * 60 * 60 * 24); // Approximate, can be more or less depending on time changes, makes no difference really.
|
2014-07-28 15:52:33 -04:00
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
db.getSortedSetRevRangeByScore('uid:' + uid + ':notifications:unread', 0, 20, '+inf', yesterday, function (err, nids) {
|
2014-07-28 15:52:33 -04:00
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-08 23:03:37 -04:00
|
|
|
if (!Array.isArray(nids) || !nids.length) {
|
2014-07-28 15:52:33 -04:00
|
|
|
return callback(null, []);
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-19 16:39:36 -04:00
|
|
|
UserNotifications.getNotifications(nids, uid, callback);
|
2014-03-18 15:35:48 -04:00
|
|
|
});
|
2014-03-12 18:00:27 -04:00
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
UserNotifications.getUnreadCount = function (uid, callback) {
|
2014-10-01 15:32:54 -04:00
|
|
|
if (!parseInt(uid, 10)) {
|
|
|
|
|
return callback(null, 0);
|
|
|
|
|
}
|
2016-03-01 16:13:01 -05:00
|
|
|
|
|
|
|
|
// Collapse any notifications with identical mergeIds
|
|
|
|
|
async.waterfall([
|
|
|
|
|
async.apply(db.getSortedSetRevRange, 'uid:' + uid + ':notifications:unread', 0, 99),
|
2016-04-29 12:54:53 -04:00
|
|
|
async.apply(notifications.filterExists),
|
2016-10-13 11:43:39 +02:00
|
|
|
function (nids, next) {
|
|
|
|
|
var keys = nids.map(function (nid) {
|
2016-03-01 16:13:01 -05:00
|
|
|
return 'notifications:' + nid;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
db.getObjectsFields(keys, ['mergeId'], next);
|
2017-02-17 19:31:21 -07:00
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
], function (err, mergeIds) {
|
2016-03-01 16:13:01 -05:00
|
|
|
// A missing (null) mergeId means that notification is counted separately.
|
2016-10-13 11:43:39 +02:00
|
|
|
mergeIds = mergeIds.map(function (set) {
|
2016-03-01 16:13:01 -05:00
|
|
|
return set.mergeId;
|
|
|
|
|
});
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
callback(err, mergeIds.reduce(function (count, cur, idx, arr) {
|
2016-03-01 16:13:01 -05:00
|
|
|
if (cur === null || idx === arr.indexOf(cur)) {
|
2017-02-18 01:12:18 -07:00
|
|
|
count += 1;
|
2016-03-01 16:13:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return count;
|
|
|
|
|
}, 0));
|
2014-10-14 23:12:47 -04:00
|
|
|
});
|
2014-03-12 18:00:27 -04:00
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
UserNotifications.getUnreadByField = function (uid, field, values, callback) {
|
|
|
|
|
db.getSortedSetRevRange('uid:' + uid + ':notifications:unread', 0, 99, function (err, nids) {
|
2014-07-28 15:52:33 -04:00
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
2014-03-12 18:00:27 -04:00
|
|
|
|
2014-09-08 23:03:37 -04:00
|
|
|
if (!Array.isArray(nids) || !nids.length) {
|
2014-07-28 15:52:33 -04:00
|
|
|
return callback(null, []);
|
|
|
|
|
}
|
2014-03-12 18:00:27 -04:00
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
var keys = nids.map(function (nid) {
|
2014-10-17 17:20:45 -04:00
|
|
|
return 'notifications:' + nid;
|
|
|
|
|
});
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
db.getObjectsFields(keys, ['nid', field], function (err, notifications) {
|
2014-07-28 15:52:33 -04:00
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
2014-09-30 23:19:53 -04:00
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
values = values.map(function () { return values.toString(); });
|
|
|
|
|
nids = notifications.filter(function (notification) {
|
2016-09-19 23:43:50 +03:00
|
|
|
return notification && notification[field] && values.indexOf(notification[field].toString()) !== -1;
|
2016-10-13 11:43:39 +02:00
|
|
|
}).map(function (notification) {
|
2014-09-08 23:03:37 -04:00
|
|
|
return notification.nid;
|
2014-07-28 15:52:33 -04:00
|
|
|
});
|
|
|
|
|
|
2014-09-08 23:03:37 -04:00
|
|
|
callback(null, nids);
|
2014-03-12 18:00:27 -04:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
2014-11-05 20:41:31 -05:00
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
UserNotifications.deleteAll = function (uid, callback) {
|
2014-11-05 20:41:31 -05:00
|
|
|
if (!parseInt(uid, 10)) {
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
|
|
|
|
async.parallel([
|
2016-10-13 11:43:39 +02:00
|
|
|
function (next) {
|
2014-11-05 20:41:31 -05:00
|
|
|
db.delete('uid:' + uid + ':notifications:unread', next);
|
|
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
function (next) {
|
2014-11-05 20:41:31 -05:00
|
|
|
db.delete('uid:' + uid + ':notifications:read', next);
|
2017-02-17 19:31:21 -07:00
|
|
|
},
|
2014-11-05 20:41:31 -05:00
|
|
|
], callback);
|
|
|
|
|
};
|
2014-03-12 18:00:27 -04:00
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
UserNotifications.sendTopicNotificationToFollowers = function (uid, topicData, postData) {
|
2016-05-06 13:47:10 +03:00
|
|
|
var followers;
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
db.getSortedSetRange('followers:' + uid, 0, -1, next);
|
|
|
|
|
},
|
|
|
|
|
function (followers, next) {
|
|
|
|
|
if (!Array.isArray(followers) || !followers.length) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
privileges.categories.filterUids('read', topicData.cid, followers, next);
|
|
|
|
|
},
|
|
|
|
|
function (_followers, next) {
|
|
|
|
|
followers = _followers;
|
|
|
|
|
if (!followers.length) {
|
2014-09-08 23:03:37 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-18 16:45:35 -04:00
|
|
|
var title = topicData.title;
|
|
|
|
|
if (title) {
|
|
|
|
|
title = S(title).decodeHTMLEntities().s;
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-15 21:55:31 -04:00
|
|
|
notifications.create({
|
2014-10-18 16:45:35 -04:00
|
|
|
bodyShort: '[[notifications:user_posted_topic, ' + postData.user.username + ', ' + title + ']]',
|
2014-10-15 21:55:31 -04:00
|
|
|
bodyLong: postData.content,
|
|
|
|
|
pid: postData.pid,
|
2016-05-05 20:24:03 +03:00
|
|
|
path: '/post/' + postData.pid,
|
2014-10-19 18:41:54 -04:00
|
|
|
nid: 'tid:' + postData.tid + ':uid:' + uid,
|
2014-10-15 21:55:31 -04:00
|
|
|
tid: postData.tid,
|
2017-02-17 19:31:21 -07:00
|
|
|
from: uid,
|
2016-05-06 13:47:10 +03:00
|
|
|
}, next);
|
2017-02-17 19:31:21 -07:00
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
], function (err, notification) {
|
2016-05-06 13:47:10 +03:00
|
|
|
if (err) {
|
|
|
|
|
return winston.error(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (notification) {
|
|
|
|
|
notifications.push(notification, followers);
|
|
|
|
|
}
|
2014-04-16 15:51:05 -04:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
UserNotifications.sendWelcomeNotification = function (uid, callback) {
|
|
|
|
|
callback = callback || function () {};
|
2014-09-29 12:30:03 -04:00
|
|
|
if (!meta.config.welcomeNotification) {
|
2015-06-27 21:26:19 -04:00
|
|
|
return callback();
|
2014-09-29 12:30:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var path = meta.config.welcomeLink ? meta.config.welcomeLink : '#';
|
|
|
|
|
|
|
|
|
|
notifications.create({
|
|
|
|
|
bodyShort: meta.config.welcomeNotification,
|
|
|
|
|
path: path,
|
2017-02-17 19:31:21 -07:00
|
|
|
nid: 'welcome_' + uid,
|
2016-10-13 11:43:39 +02:00
|
|
|
}, function (err, notification) {
|
2016-01-27 20:03:28 +02:00
|
|
|
if (err || !notification) {
|
2015-06-27 21:26:19 -04:00
|
|
|
return callback(err);
|
|
|
|
|
}
|
2016-01-27 20:03:28 +02:00
|
|
|
|
|
|
|
|
notifications.push(notification, [uid], callback);
|
2014-09-29 12:30:03 -04:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
UserNotifications.sendNameChangeNotification = function (uid, username) {
|
2014-10-28 18:28:26 -04:00
|
|
|
notifications.create({
|
|
|
|
|
bodyShort: '[[user:username_taken_workaround, ' + username + ']]',
|
|
|
|
|
image: 'brand:logo',
|
|
|
|
|
nid: 'username_taken:' + uid,
|
2017-02-17 19:31:21 -07:00
|
|
|
datetime: Date.now(),
|
2016-10-13 11:43:39 +02:00
|
|
|
}, function (err, notification) {
|
2014-10-28 18:28:26 -04:00
|
|
|
if (!err && notification) {
|
|
|
|
|
notifications.push(notification, uid);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
UserNotifications.pushCount = function (uid) {
|
2014-03-12 20:53:42 -04:00
|
|
|
var websockets = require('./../socket.io');
|
2016-10-13 11:43:39 +02:00
|
|
|
UserNotifications.getUnreadCount(uid, function (err, count) {
|
2014-03-12 18:00:27 -04:00
|
|
|
if (err) {
|
2014-09-05 01:36:30 -04:00
|
|
|
return winston.error(err.stack);
|
2014-03-12 18:00:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
websockets.in('uid_' + uid).emit('event:notifications.updateCount', count);
|
|
|
|
|
});
|
|
|
|
|
};
|
2014-04-10 20:31:57 +01:00
|
|
|
}(exports));
|