Files
NodeBB/src/topics/unread.js

565 lines
15 KiB
JavaScript
Raw Normal View History

2014-03-21 15:40:37 -04:00
'use strict';
2016-01-04 11:22:35 +02:00
var async = require('async');
2017-06-25 20:00:05 -04:00
var _ = require('lodash');
2014-03-21 15:40:37 -04:00
2016-01-04 11:22:35 +02:00
var db = require('../database');
var user = require('../user');
var posts = require('../posts');
2016-01-04 11:22:35 +02:00
var notifications = require('../notifications');
var categories = require('../categories');
var privileges = require('../privileges');
2016-01-04 14:33:47 +02:00
var meta = require('../meta');
var utils = require('../utils');
var plugins = require('../plugins');
2014-03-21 15:40:37 -04:00
module.exports = function (Topics) {
Topics.getTotalUnread = function (uid, filter, callback) {
if (!callback) {
callback = filter;
filter = '';
}
Topics.getUnreadTids({ cid: 0, uid: uid, count: true }, function (err, counts) {
callback(err, counts && counts[filter]);
2014-03-21 15:40:37 -04:00
});
};
2017-01-27 20:37:54 +03:00
Topics.getUnreadTopics = function (params, callback) {
2014-08-29 14:50:24 -04:00
var unreadTopics = {
showSelect: true,
2017-02-18 01:27:46 -07:00
nextStart: 0,
2017-02-17 19:31:21 -07:00
topics: [],
2014-08-29 14:50:24 -04:00
};
2015-01-23 18:19:30 -05:00
async.waterfall([
function (next) {
2017-01-27 20:37:54 +03:00
Topics.getUnreadTids(params, next);
2015-01-23 18:19:30 -05:00
},
function (tids, next) {
2016-05-13 14:08:50 +03:00
unreadTopics.topicCount = tids.length;
2015-01-23 18:19:30 -05:00
if (!tids.length) {
return next(null, []);
2014-08-29 14:50:24 -04:00
}
2016-05-13 14:08:50 +03:00
tids = tids.slice(params.start, params.stop !== -1 ? params.stop + 1 : undefined);
2016-05-13 14:08:50 +03:00
2017-01-27 20:37:54 +03:00
Topics.getTopicsByTids(tids, params.uid, next);
2015-01-23 18:19:30 -05:00
},
function (topicData, next) {
2017-05-27 00:30:07 -04:00
if (!topicData.length) {
2015-01-23 18:19:30 -05:00
return next(null, unreadTopics);
2014-08-29 14:50:24 -04:00
}
2019-01-08 18:56:12 -05:00
Topics.calculateTopicIndices(topicData, params.start);
unreadTopics.topics = topicData;
2017-01-27 20:37:54 +03:00
unreadTopics.nextStart = params.stop + 1;
2015-01-23 18:19:30 -05:00
next(null, unreadTopics);
2017-02-17 19:31:21 -07:00
},
2015-01-23 18:19:30 -05:00
], callback);
2014-08-29 14:50:24 -04:00
};
Topics.unreadCutoff = function () {
return Date.now() - (meta.config.unreadCutoff * 86400000);
2016-01-04 14:33:47 +02:00
};
2017-01-27 20:37:54 +03:00
Topics.getUnreadTids = function (params, callback) {
2018-12-16 00:09:13 -05:00
async.waterfall([
function (next) {
Topics.getUnreadData(params, next);
},
function (results, next) {
next(null, params.count ? results.counts : results.tids);
},
], callback);
};
Topics.getUnreadData = function (params, callback) {
const uid = parseInt(params.uid, 10);
const counts = {
'': 0,
new: 0,
watched: 0,
unreplied: 0,
};
2018-12-16 00:09:13 -05:00
const noUnreadData = {
tids: [],
counts: counts,
tidsByFilter: {
'': [],
new: [],
watched: [],
unreplied: [],
},
};
2018-09-22 01:28:13 -04:00
if (uid <= 0) {
2018-12-16 00:09:13 -05:00
return setImmediate(callback, null, noUnreadData);
2014-08-15 18:11:57 -04:00
}
2017-04-21 21:36:42 -04:00
params.filter = params.filter || '';
2017-01-27 20:37:54 +03:00
var cutoff = params.cutoff || Topics.unreadCutoff();
2014-03-21 15:40:37 -04:00
2017-10-30 15:26:12 -04:00
if (params.cid && !Array.isArray(params.cid)) {
params.cid = [params.cid];
}
2016-04-18 15:44:07 +03:00
async.waterfall([
function (next) {
async.parallel({
ignoredTids: function (next) {
2016-05-18 19:02:43 +03:00
user.getIgnoredTids(uid, 0, -1, next);
},
recentTids: function (next) {
2016-04-18 15:44:07 +03:00
db.getSortedSetRevRangeByScoreWithScores('topics:recent', 0, -1, '+inf', cutoff, next);
},
userScores: function (next) {
2016-04-18 15:44:07 +03:00
db.getSortedSetRevRangeByScoreWithScores('uid:' + uid + ':tids_read', 0, -1, '+inf', cutoff, next);
},
tids_unread: function (next) {
2016-04-18 15:44:07 +03:00
db.getSortedSetRevRangeWithScores('uid:' + uid + ':tids_unread', 0, -1, next);
2017-02-17 19:31:21 -07:00
},
2016-04-18 15:44:07 +03:00
}, next);
},
function (results, next) {
if (results.recentTids && !results.recentTids.length && !results.tids_unread.length) {
2018-12-16 00:09:13 -05:00
return callback(null, noUnreadData);
2016-04-18 15:44:07 +03:00
}
2014-09-27 17:41:49 -04:00
filterTopics(params, results, next);
2016-04-18 15:44:07 +03:00
},
function (data, next) {
plugins.fireHook('filter:topics.getUnreadTids', {
uid: uid,
tids: data.tids,
counts: data.counts,
tidsByFilter: data.tidsByFilter,
cid: params.cid,
filter: params.filter,
}, next);
},
2016-04-18 15:44:07 +03:00
], callback);
};
function filterTopics(params, results, callback) {
const counts = {
'': 0,
new: 0,
watched: 0,
unreplied: 0,
};
const tidsByFilter = {
'': [],
new: [],
watched: [],
unreplied: [],
};
var userRead = {};
results.userScores.forEach(function (userItem) {
userRead[userItem.value] = userItem.score;
});
results.recentTids = results.recentTids.concat(results.tids_unread);
results.recentTids.sort(function (a, b) {
return b.score - a.score;
});
var tids = results.recentTids.filter(function (recentTopic) {
if (results.ignoredTids.includes(String(recentTopic.value))) {
return false;
}
return !userRead[recentTopic.value] || recentTopic.score > userRead[recentTopic.value];
});
tids = _.uniq(tids.map(topic => topic.value));
var cid = params.cid;
var uid = params.uid;
var cids;
var topicData;
var blockedUids;
tids = tids.slice(0, 200);
2017-04-21 21:36:42 -04:00
if (!tids.length) {
2018-12-16 00:09:13 -05:00
return callback(null, { counts: counts, tids: tids, tidsByFilter: tidsByFilter });
2014-08-29 15:57:20 -04:00
}
async.waterfall([
function (next) {
user.blocks.list(uid, next);
},
function (_blockedUids, next) {
blockedUids = _blockedUids;
filterTidsThatHaveBlockedPosts({
uid: uid,
tids: tids,
blockedUids: blockedUids,
recentTids: results.recentTids,
}, next);
},
function (_tids, next) {
tids = _tids;
Topics.getTopicsFields(tids, ['tid', 'cid', 'uid', 'postcount'], next);
},
function (_topicData, next) {
topicData = _topicData;
cids = _.uniq(topicData.map(topic => topic.cid)).filter(Boolean);
async.parallel({
isTopicsFollowed: function (next) {
db.sortedSetScores('uid:' + uid + ':followed_tids', tids, next);
2017-02-17 19:31:21 -07:00
},
categoryWatchState: function (next) {
categories.getWatchState(cids, uid, next);
2017-04-21 21:36:42 -04:00
},
readableCids: function (next) {
privileges.categories.filterCids('read', cids, uid, next);
},
}, next);
},
function (results, next) {
2017-10-30 15:26:12 -04:00
cid = cid && cid.map(String);
2018-09-25 11:14:25 -04:00
results.readableCids = results.readableCids.map(String);
const userCidState = _.zipObject(cids, results.categoryWatchState);
topicData.forEach(function (topic, index) {
function cidMatch(topicCid) {
2018-09-25 11:14:25 -04:00
return (!cid || (cid.length && cid.includes(String(topicCid)))) && results.readableCids.includes(String(topicCid));
}
if (topic && topic.cid && cidMatch(topic.cid) && !blockedUids.includes(parseInt(topic.uid, 10))) {
topic.tid = parseInt(topic.tid, 10);
if ((results.isTopicsFollowed[index] || userCidState[topic.cid] === categories.watchStates.watching)) {
tidsByFilter[''].push(topic.tid);
}
if (results.isTopicsFollowed[index]) {
tidsByFilter.watched.push(topic.tid);
}
2018-10-25 17:02:59 -04:00
if (topic.postcount <= 1) {
tidsByFilter.unreplied.push(topic.tid);
}
if (!userRead[topic.tid]) {
tidsByFilter.new.push(topic.tid);
}
}
});
counts[''] = tidsByFilter[''].length;
counts.watched = tidsByFilter.watched.length;
counts.unreplied = tidsByFilter.unreplied.length;
counts.new = tidsByFilter.new.length;
next(null, {
counts: counts,
tids: tidsByFilter[params.filter],
tidsByFilter: tidsByFilter,
});
},
], callback);
}
function filterTidsThatHaveBlockedPosts(params, callback) {
if (!params.blockedUids.length) {
return setImmediate(callback, null, params.tids);
}
2018-09-27 09:52:56 -04:00
const topicScores = _.mapValues(_.keyBy(params.recentTids, 'value'), 'score');
2018-09-27 09:52:56 -04:00
db.sortedSetScores('uid:' + params.uid + ':tids_read', params.tids, function (err, results) {
if (err) {
return callback(err);
}
2018-09-27 09:52:56 -04:00
const userScores = _.zipObject(params.tids, results);
async.filter(params.tids, function (tid, next) {
2018-09-27 09:52:56 -04:00
doesTidHaveUnblockedUnreadPosts(tid, {
blockedUids: params.blockedUids,
topicTimestamp: topicScores[tid],
userLastReadTimestamp: userScores[tid],
}, next);
}, callback);
});
}
function doesTidHaveUnblockedUnreadPosts(tid, params, callback) {
2018-09-27 09:52:56 -04:00
var userLastReadTimestamp = params.userLastReadTimestamp;
if (!userLastReadTimestamp) {
return setImmediate(callback, null, true);
}
var start = 0;
2018-09-27 09:52:56 -04:00
var count = 3;
var done = false;
2018-09-27 09:52:56 -04:00
var hasUnblockedUnread = params.topicTimestamp > userLastReadTimestamp;
if (!params.blockedUids.length) {
return setImmediate(callback, null, hasUnblockedUnread);
}
async.whilst(function (next) {
next(null, !done);
}, function (_next) {
async.waterfall([
function (next) {
db.getSortedSetRangeByScore('tid:' + tid + ':posts', start, count, userLastReadTimestamp, '+inf', next);
},
function (pidsSinceLastVisit, next) {
if (!pidsSinceLastVisit.length) {
done = true;
return _next();
}
posts.getPostsFields(pidsSinceLastVisit, ['pid', 'uid'], next);
},
function (postData, next) {
postData = postData.filter(function (post) {
return !params.blockedUids.includes(parseInt(post.uid, 10));
});
done = postData.length > 0;
hasUnblockedUnread = postData.length > 0;
start += count;
next();
},
], _next);
}, function (err) {
callback(err, hasUnblockedUnread);
});
2014-08-29 15:57:20 -04:00
}
2014-03-21 15:40:37 -04:00
Topics.pushUnreadCount = function (uid, callback) {
callback = callback || function () {};
2014-03-21 15:40:37 -04:00
2018-11-17 22:31:39 -05:00
if (!uid || parseInt(uid, 10) <= 0) {
2017-04-21 21:36:42 -04:00
return setImmediate(callback);
2014-03-21 15:40:37 -04:00
}
2016-01-04 11:22:35 +02:00
2017-04-21 21:36:42 -04:00
async.waterfall([
function (next) {
Topics.getUnreadTids({ uid: uid, count: true }, next);
2017-04-21 21:36:42 -04:00
},
function (results, next) {
require('../socket.io').in('uid_' + uid).emit('event:unread.updateCount', {
unreadTopicCount: results[''],
unreadNewTopicCount: results.new,
unreadWatchedTopicCount: results.watched,
unreadUnrepliedTopicCount: results.unreplied,
});
2017-04-21 21:36:42 -04:00
setImmediate(next);
},
], callback);
2014-03-21 15:40:37 -04:00
};
Topics.markAsUnreadForAll = function (tid, callback) {
2014-09-27 17:41:49 -04:00
Topics.markCategoryUnreadForAll(tid, callback);
2014-03-21 15:40:37 -04:00
};
Topics.markAsRead = function (tids, uid, callback) {
callback = callback || function () {};
if (!Array.isArray(tids) || !tids.length) {
2017-03-09 22:36:28 +03:00
return setImmediate(callback, null, false);
2014-03-21 15:40:37 -04:00
}
2016-01-04 14:33:47 +02:00
2017-06-25 20:00:05 -04:00
tids = _.uniq(tids).filter(function (tid) {
return tid && utils.isNumber(tid);
2016-01-04 14:33:47 +02:00
});
2015-01-27 10:36:30 -05:00
if (!tids.length) {
2017-03-09 22:36:28 +03:00
return setImmediate(callback, null, false);
2015-01-27 10:36:30 -05:00
}
2014-03-21 15:40:37 -04:00
async.waterfall([
function (next) {
async.parallel({
topicScores: async.apply(db.sortedSetScores, 'topics:recent', tids),
2017-02-17 19:31:21 -07:00
userScores: async.apply(db.sortedSetScores, 'uid:' + uid + ':tids_read', tids),
}, next);
},
function (results, next) {
tids = tids.filter(function (tid, index) {
return results.topicScores[index] && (!results.userScores[index] || results.userScores[index] < results.topicScores[index]);
});
2015-01-27 10:36:30 -05:00
if (!tids.length) {
2016-01-04 14:33:47 +02:00
return callback(null, false);
}
var now = Date.now();
var scores = tids.map(function () {
return now;
});
async.parallel({
markRead: async.apply(db.sortedSetAdd, 'uid:' + uid + ':tids_read', scores, tids),
2016-01-04 11:22:35 +02:00
markUnread: async.apply(db.sortedSetRemove, 'uid:' + uid + ':tids_unread', tids),
2017-02-17 19:31:21 -07:00
topicData: async.apply(Topics.getTopicsFields, tids, ['cid']),
}, next);
},
function (results, next) {
var cids = results.topicData.map(function (topic) {
return topic && topic.cid;
2017-06-25 20:00:05 -04:00
}).filter(Boolean);
cids = _.uniq(cids);
categories.markAsRead(cids, uid, next);
},
function (next) {
2017-05-19 18:50:19 -04:00
plugins.fireHook('action:topics.markAsRead', { uid: uid, tids: tids });
next(null, true);
2017-02-17 19:31:21 -07:00
},
], callback);
};
2014-03-21 15:40:37 -04:00
Topics.markAllRead = function (uid, callback) {
2016-01-04 14:33:47 +02:00
async.waterfall([
function (next) {
db.getSortedSetRevRangeByScore('topics:recent', 0, -1, '+inf', Topics.unreadCutoff(), next);
},
function (tids, next) {
Topics.markTopicNotificationsRead(tids, uid);
2016-01-07 20:29:31 +02:00
Topics.markAsRead(tids, uid, next);
2016-01-04 14:33:47 +02:00
},
function (markedRead, next) {
db.delete('uid:' + uid + ':tids_unread', next);
2017-02-17 19:31:21 -07:00
},
2016-01-04 14:33:47 +02:00
], callback);
};
2016-11-28 14:15:20 +03:00
Topics.markTopicNotificationsRead = function (tids, uid, callback) {
callback = callback || function () {};
if (!Array.isArray(tids) || !tids.length) {
2016-11-28 14:15:20 +03:00
return callback();
2014-09-19 19:45:16 -04:00
}
async.waterfall([
function (next) {
user.notifications.getUnreadByField(uid, 'tid', tids, next);
},
function (nids, next) {
notifications.markReadMultiple(nids, uid, next);
2016-11-28 14:15:20 +03:00
},
function (next) {
user.notifications.pushCount(uid);
next();
2017-02-17 19:31:21 -07:00
},
2016-11-28 14:15:20 +03:00
], callback);
2014-03-21 15:40:37 -04:00
};
Topics.markCategoryUnreadForAll = function (tid, callback) {
2016-11-28 14:15:20 +03:00
async.waterfall([
function (next) {
Topics.getTopicField(tid, 'cid', next);
},
function (cid, next) {
categories.markAsUnreadForAll(cid, next);
2017-02-17 19:31:21 -07:00
},
2016-11-28 14:15:20 +03:00
], callback);
2014-03-21 15:40:37 -04:00
};
Topics.hasReadTopics = function (tids, uid, callback) {
if (!(parseInt(uid, 10) > 0)) {
return setImmediate(callback, null, tids.map(() => false));
2014-03-21 15:40:37 -04:00
}
2017-05-26 00:02:20 -04:00
async.waterfall([
function (next) {
async.parallel({
topicScores: function (next) {
2017-05-26 00:02:20 -04:00
db.sortedSetScores('topics:recent', tids, next);
},
userScores: function (next) {
db.sortedSetScores('uid:' + uid + ':tids_read', tids, next);
},
tids_unread: function (next) {
db.sortedSetScores('uid:' + uid + ':tids_unread', tids, next);
},
blockedUids: function (next) {
user.blocks.list(uid, next);
},
2017-05-26 00:02:20 -04:00
}, next);
2017-02-17 19:31:21 -07:00
},
2017-05-26 00:02:20 -04:00
function (results, next) {
var cutoff = Topics.unreadCutoff();
var result = tids.map(function (tid, index) {
var read = !results.tids_unread[index] &&
(results.topicScores[index] < cutoff ||
!!(results.userScores[index] && results.userScores[index] >= results.topicScores[index]));
2018-09-27 09:52:56 -04:00
return { tid: tid, read: read, index: index };
});
async.map(result, function (data, next) {
if (data.read) {
return next(null, true);
}
doesTidHaveUnblockedUnreadPosts(data.tid, {
2018-09-27 09:52:56 -04:00
topicTimestamp: results.topicScores[data.index],
userLastReadTimestamp: results.userScores[data.index],
blockedUids: results.blockedUids,
}, function (err, hasUnblockedUnread) {
if (err) {
return next(err);
}
if (!hasUnblockedUnread) {
data.read = true;
}
next(null, data.read);
});
}, next);
2017-05-26 00:02:20 -04:00
},
], callback);
2014-03-21 15:40:37 -04:00
};
Topics.hasReadTopic = function (tid, uid, callback) {
Topics.hasReadTopics([tid], uid, function (err, hasRead) {
2014-09-27 17:41:49 -04:00
callback(err, Array.isArray(hasRead) && hasRead.length ? hasRead[0] : false);
});
2014-03-21 15:40:37 -04:00
};
Topics.markUnread = function (tid, uid, callback) {
2016-01-04 11:22:35 +02:00
async.waterfall([
function (next) {
Topics.exists(tid, next);
},
function (exists, next) {
if (!exists) {
return next(new Error('[[error:no-topic]]'));
}
db.sortedSetRemove('uid:' + uid + ':tids_read', tid, next);
},
function (next) {
db.sortedSetAdd('uid:' + uid + ':tids_unread', Date.now(), tid, next);
2017-02-17 19:31:21 -07:00
},
2016-01-04 11:22:35 +02:00
], callback);
};
2014-03-21 15:40:37 -04:00
2016-11-03 13:06:21 +03:00
Topics.filterNewTids = function (tids, uid, callback) {
2018-11-22 22:21:03 -05:00
if (parseInt(uid, 10) <= 0) {
return setImmediate(callback, null, []);
}
2017-04-21 21:36:42 -04:00
async.waterfall([
function (next) {
db.sortedSetScores('uid:' + uid + ':tids_read', tids, next);
},
function (scores, next) {
2018-11-22 22:21:03 -05:00
tids = tids.filter((tid, index) => tid && !scores[index]);
2017-04-21 21:36:42 -04:00
next(null, tids);
},
], callback);
2016-11-03 13:06:21 +03:00
};
2017-10-19 13:53:05 -04:00
Topics.filterUnrepliedTids = function (tids, callback) {
async.waterfall([
function (next) {
db.sortedSetScores('topics:posts', tids, next);
},
function (scores, next) {
2018-11-22 22:21:03 -05:00
tids = tids.filter((tid, index) => tid && scores[index] <= 1);
2017-10-19 13:53:05 -04:00
next(null, tids);
},
], callback);
};
2014-04-10 20:31:57 +01:00
};