Files
NodeBB/src/user/notifications.js

228 lines
6.6 KiB
JavaScript
Raw Normal View History

'use strict';
var winston = require('winston');
2019-06-12 19:59:57 -04:00
var _ = require('lodash');
var db = require('../database');
var meta = require('../meta');
var notifications = require('../notifications');
var privileges = require('../privileges');
var plugins = require('../plugins');
2017-10-13 21:02:41 -06:00
var utils = require('../utils');
2017-05-20 22:30:12 -04:00
var UserNotifications = module.exports;
2019-07-16 12:47:55 -04:00
UserNotifications.get = async function (uid) {
2018-11-12 00:20:44 -05:00
if (parseInt(uid, 10) <= 0) {
2019-07-16 12:47:55 -04:00
return { read: [], unread: [] };
2017-05-20 22:30:12 -04:00
}
2019-01-31 13:13:59 -05:00
2019-07-16 12:47:55 -04:00
let unread = await getNotificationsFromSet('uid:' + uid + ':notifications:unread', uid, 0, 29);
unread = unread.filter(Boolean);
let read = [];
if (unread.length < 30) {
read = await getNotificationsFromSet('uid:' + uid + ':notifications:read', uid, 0, 29 - unread.length);
}
return {
read: read.filter(Boolean),
unread: unread,
};
2017-05-20 22:30:12 -04:00
};
2017-03-14 23:03:03 +03:00
2019-07-16 12:47:55 -04:00
async function filterNotifications(nids, filter) {
2017-05-20 22:30:12 -04:00
if (!filter) {
2019-07-16 12:47:55 -04:00
return nids;
2014-10-17 18:31:20 -04:00
}
2019-07-16 12:47:55 -04:00
const keys = nids.map(nid => 'notifications:' + nid);
const notifications = await db.getObjectsFields(keys, ['nid', 'type']);
return notifications.filter(n => n && n.nid && n.type === filter).map(n => n.nid);
2017-05-20 22:30:12 -04:00
}
2019-07-16 12:47:55 -04:00
UserNotifications.getAll = async function (uid, filter) {
let nids = await db.getSortedSetRevRange([
'uid:' + uid + ':notifications:unread',
'uid:' + uid + ':notifications:read',
], 0, -1);
nids = _.uniq(nids);
const exists = await db.isSortedSetMembers('notifications', nids);
var deleteNids = [];
nids = nids.filter(function (nid, index) {
if (!nid || !exists[index]) {
deleteNids.push(nid);
}
return nid && exists[index];
});
await deleteUserNids(deleteNids, uid);
return await filterNotifications(nids, filter);
2017-05-20 22:30:12 -04:00
};
2019-07-16 12:47:55 -04:00
async function deleteUserNids(nids, uid) {
await db.sortedSetRemove([
'uid:' + uid + ':notifications:read',
'uid:' + uid + ':notifications:unread',
2019-07-16 12:47:55 -04:00
], nids);
2017-05-20 22:30:12 -04:00
}
2019-07-16 12:47:55 -04:00
async function getNotificationsFromSet(set, uid, start, stop) {
const nids = await db.getSortedSetRevRange(set, start, stop);
return await UserNotifications.getNotifications(nids, uid);
2017-05-20 22:30:12 -04:00
}
2019-07-16 12:47:55 -04:00
UserNotifications.getNotifications = async function (nids, uid) {
2017-05-27 00:30:07 -04:00
if (!Array.isArray(nids) || !nids.length) {
2019-07-16 12:47:55 -04:00
return [];
2017-05-27 00:30:07 -04:00
}
2019-07-16 12:47:55 -04:00
const [notifObjs, hasRead] = await Promise.all([
notifications.getMultiple(nids),
db.isSortedSetMembers('uid:' + uid + ':notifications:read', nids),
]);
const deletedNids = [];
let notificationData = notifObjs.filter(function (notification, index) {
if (!notification || !notification.nid) {
deletedNids.push(nids[index]);
}
if (notification) {
notification.read = hasRead[index];
notification.readClass = !notification.read ? 'unread' : '';
}
return notification && notification.path;
});
await deleteUserNids(deletedNids, uid);
notificationData = await notifications.merge(notificationData);
const result = await plugins.fireHook('filter:user.notifications.getNotifications', {
uid: uid,
notifications: notificationData,
});
return result && result.notifications;
2017-05-20 22:30:12 -04:00
};
2019-07-22 18:36:29 -04:00
UserNotifications.getUnreadInterval = async function (uid, interval) {
const dayInMs = 1000 * 60 * 60 * 24;
const times = {
day: dayInMs,
week: 7 * dayInMs,
month: 30 * dayInMs,
};
if (!times[interval]) {
return [];
}
const min = Date.now() - times[interval];
const nids = await db.getSortedSetRevRangeByScore('uid:' + uid + ':notifications:unread', 0, 20, '+inf', min);
2019-07-16 12:47:55 -04:00
return await UserNotifications.getNotifications(nids, uid);
2017-05-20 22:30:12 -04:00
};
2019-07-22 18:36:29 -04:00
UserNotifications.getDailyUnread = async function (uid) {
return await UserNotifications.getUnreadInterval(uid, 'day');
};
2019-07-16 12:47:55 -04:00
UserNotifications.getUnreadCount = async function (uid) {
2018-11-12 00:20:44 -05:00
if (parseInt(uid, 10) <= 0) {
2019-07-16 12:47:55 -04:00
return 0;
2017-05-20 22:30:12 -04:00
}
2019-07-16 12:47:55 -04:00
let nids = await db.getSortedSetRevRange('uid:' + uid + ':notifications:unread', 0, 99);
nids = await notifications.filterExists(nids);
const keys = nids.map(nid => 'notifications:' + nid);
const notifData = await db.getObjectsFields(keys, ['mergeId']);
const mergeIds = notifData.map(n => n.mergeId);
// Collapse any notifications with identical mergeIds
return mergeIds.reduce(function (count, mergeId, idx, arr) {
// A missing (null) mergeId means that notification is counted separately.
if (mergeId === null || idx === arr.indexOf(mergeId)) {
count += 1;
}
2017-05-20 22:30:12 -04:00
2019-07-16 12:47:55 -04:00
return count;
}, 0);
2017-05-20 22:30:12 -04:00
};
2019-07-16 12:47:55 -04:00
UserNotifications.getUnreadByField = async function (uid, field, values) {
const nids = await db.getSortedSetRevRange('uid:' + uid + ':notifications:unread', 0, 99);
if (!nids.length) {
return [];
}
const keys = nids.map(nid => 'notifications:' + nid);
const notifData = await db.getObjectsFields(keys, ['nid', field]);
const valuesSet = new Set(values.map(value => String(value)));
return notifData.filter(n => n && n[field] && valuesSet.has(String(n[field]))).map(n => n.nid);
2017-05-20 22:30:12 -04:00
};
2014-10-18 16:45:35 -04:00
2019-07-16 12:47:55 -04:00
UserNotifications.deleteAll = async function (uid) {
2018-11-12 00:20:44 -05:00
if (parseInt(uid, 10) <= 0) {
2019-07-16 12:47:55 -04:00
return;
2017-05-20 22:30:12 -04:00
}
2019-07-16 12:47:55 -04:00
await db.deleteAll([
2019-06-12 19:59:57 -04:00
'uid:' + uid + ':notifications:unread',
'uid:' + uid + ':notifications:read',
2019-07-16 12:47:55 -04:00
]);
2017-05-20 22:30:12 -04:00
};
2019-07-16 12:47:55 -04:00
UserNotifications.sendTopicNotificationToFollowers = async function (uid, topicData, postData) {
try {
let followers = await db.getSortedSetRange('followers:' + uid, 0, -1);
followers = await privileges.categories.filterUids('read', topicData.cid, followers);
if (!followers.length) {
return;
}
let title = topicData.title;
if (title) {
title = utils.decodeHTMLEntities(title);
2014-09-29 12:30:03 -04:00
}
2019-07-16 12:47:55 -04:00
const notifObj = await notifications.create({
type: 'new-topic',
bodyShort: '[[notifications:user_posted_topic, ' + postData.user.username + ', ' + title + ']]',
bodyLong: postData.content,
pid: postData.pid,
path: '/post/' + postData.pid,
nid: 'tid:' + postData.tid + ':uid:' + uid,
tid: postData.tid,
from: uid,
});
await notifications.push(notifObj, followers);
} catch (err) {
2019-07-23 21:17:00 -04:00
winston.error(err);
2019-07-16 12:47:55 -04:00
}
2017-05-20 22:30:12 -04:00
};
2014-09-29 12:30:03 -04:00
2019-07-16 12:47:55 -04:00
UserNotifications.sendWelcomeNotification = async function (uid) {
2017-05-20 22:30:12 -04:00
if (!meta.config.welcomeNotification) {
2019-07-16 12:47:55 -04:00
return;
2017-05-20 22:30:12 -04:00
}
2016-01-27 20:03:28 +02:00
2017-05-20 22:30:12 -04:00
var path = meta.config.welcomeLink ? meta.config.welcomeLink : '#';
2019-07-16 12:47:55 -04:00
const notifObj = await notifications.create({
bodyShort: meta.config.welcomeNotification,
path: path,
nid: 'welcome_' + uid,
from: meta.config.welcomeUid ? meta.config.welcomeUid : null,
});
2017-05-20 22:30:12 -04:00
2019-07-16 12:47:55 -04:00
await notifications.push(notifObj, [uid]);
2017-05-20 22:30:12 -04:00
};
2019-07-16 12:47:55 -04:00
UserNotifications.sendNameChangeNotification = async function (uid, username) {
const notifObj = await notifications.create({
2017-05-20 22:30:12 -04:00
bodyShort: '[[user:username_taken_workaround, ' + username + ']]',
image: 'brand:logo',
nid: 'username_taken:' + uid,
datetime: Date.now(),
});
2019-07-16 12:47:55 -04:00
await notifications.push(notifObj, uid);
2017-05-20 22:30:12 -04:00
};
2019-07-16 12:47:55 -04:00
UserNotifications.pushCount = async function (uid) {
2017-05-20 22:30:12 -04:00
var websockets = require('./../socket.io');
2019-07-16 12:47:55 -04:00
const count = await UserNotifications.getUnreadCount(uid);
websockets.in('uid_' + uid).emit('event:notifications.updateCount', count);
2017-05-20 22:30:12 -04:00
};