Files
NodeBB/src/topics/unread.js

268 lines
6.2 KiB
JavaScript
Raw Normal View History

2014-03-21 15:40:37 -04:00
'use strict';
var async = require('async'),
winston = require('winston'),
2014-08-15 18:11:57 -04:00
db = require('../database'),
user = require('../user'),
meta = require('../meta'),
notifications = require('../notifications'),
categories = require('../categories'),
privileges = require('../privileges');
2014-03-21 15:40:37 -04:00
module.exports = function(Topics) {
Topics.getTotalUnread = function(uid, callback) {
Topics.getUnreadTids(uid, 0, 20, function(err, tids) {
callback(err, tids ? tids.length : 0);
});
};
2014-08-29 14:50:24 -04:00
Topics.getUnreadTopics = function(uid, start, stop, callback) {
var unreadTopics = {
showSelect: true,
nextStart : 0,
topics: []
};
2014-09-27 18:09:25 -04:00
Topics.getUnreadTids(uid, start, stop, function(err, tids) {
if (err) {
return callback(err);
}
2014-08-29 15:57:20 -04:00
if (!tids.length) {
return callback(null, unreadTopics);
}
2014-08-29 14:50:24 -04:00
Topics.getTopicsByTids(tids, uid, function(err, topicData) {
if (err) {
return callback(err);
}
if (!Array.isArray(topicData) || !topicData.length) {
return callback(null, unreadTopics);
}
db.sortedSetRevRank('topics:recent', topicData[topicData.length - 1].tid, function(err, rank) {
if (err) {
return callback(err);
}
unreadTopics.topics = topicData;
unreadTopics.nextStart = parseInt(rank, 10) + 1;
callback(null, unreadTopics);
});
});
});
};
2014-03-21 15:40:37 -04:00
Topics.getUnreadTids = function(uid, start, stop, callback) {
uid = parseInt(uid, 10);
if (uid === 0) {
2014-09-16 12:38:27 -04:00
return callback(null, []);
2014-08-15 18:11:57 -04:00
}
2014-03-21 15:40:37 -04:00
2014-09-27 17:41:49 -04:00
var yesterday = Date.now() - 86400000;
2014-09-16 12:38:27 -04:00
async.parallel({
ignoredCids: function(next) {
user.getIgnoredCategories(uid, next);
},
recentTids: function(next) {
2014-09-27 17:41:49 -04:00
db.getSortedSetRevRangeByScoreWithScores('topics:recent', 0, -1, Infinity, yesterday, next);
},
userScores: function(next) {
db.getSortedSetRevRangeByScoreWithScores('uid:' + uid + ':tids_read', 0, -1, Infinity, yesterday, next);
2014-09-16 12:38:27 -04:00
}
}, function(err, results) {
2014-08-29 15:57:20 -04:00
if (err) {
return callback(err);
}
2014-03-21 15:40:37 -04:00
2014-09-16 12:38:27 -04:00
if (results.recentTids && !results.recentTids.length) {
return callback(null, []);
}
2014-08-29 15:57:20 -04:00
2014-09-27 17:41:49 -04:00
var userRead = {};
results.userScores.forEach(function(userItem) {
userRead[userItem.value] = userItem.score;
});
var tids = results.recentTids.filter(function(recentTopic, index) {
return !userRead[recentTopic.value] || recentTopic.score > userRead[recentTopic.value];
}).map(function(topic) {
return topic.value;
});
filterTopics(uid, tids, results.ignoredCids, function(err, tids) {
2014-09-16 12:38:27 -04:00
if (err) {
return callback(err);
}
2014-08-29 15:57:20 -04:00
2014-09-27 17:41:49 -04:00
if (stop === -1) {
tids = tids.slice(start);
} else {
tids = tids.slice(start, stop + 1);
}
2014-08-29 15:57:20 -04:00
2014-09-27 17:41:49 -04:00
callback(err, tids);
2014-03-21 15:40:37 -04:00
});
});
};
2014-09-16 12:38:27 -04:00
function filterTopics(uid, tids, ignoredCids, callback) {
if (!Array.isArray(ignoredCids) || !tids.length) {
2014-08-29 15:57:20 -04:00
return callback(null, tids);
}
var keys = tids.map(function(tid) {
return 'topic:' + tid;
});
db.getObjectsFields(keys, ['tid', 'cid'], function(err, topics) {
if (err) {
return callback(err);
}
2014-09-16 12:38:27 -04:00
2014-09-22 12:39:25 -04:00
var topicCids = topics.filter(function(topic) {
return topic && topic.cid;
}).map(function(topic) {
return topic.cid.toString();
2014-09-16 12:38:27 -04:00
});
topicCids = topicCids.filter(function(cid) {
return ignoredCids.indexOf(cid) === -1;
2014-08-29 15:57:20 -04:00
});
2014-03-21 15:40:37 -04:00
2014-09-16 12:38:27 -04:00
privileges.categories.filterCids('read', topicCids, uid, function(err, readableCids) {
if (err) {
return callback(err);
}
topics = topics.filter(function(topic) {
2014-09-26 20:53:25 -04:00
return topic.cid && readableCids.indexOf(topic.cid.toString()) !== -1;
2014-09-16 12:38:27 -04:00
}).map(function(topic) {
return topic.tid;
});
callback(null, topics);
});
2014-08-29 15:57:20 -04:00
});
}
2014-03-21 15:40:37 -04:00
2014-09-16 11:06:10 -04:00
Topics.pushUnreadCount = function(uid, callback) {
2014-09-16 11:07:59 -04:00
callback = callback || function() {};
2014-03-21 15:40:37 -04:00
2014-09-16 11:06:10 -04:00
if (!uid || parseInt(uid, 10) === 0) {
return callback();
2014-03-21 15:40:37 -04:00
}
2014-09-16 11:06:10 -04:00
Topics.getTotalUnread(uid, function(err, count) {
2014-03-21 15:40:37 -04:00
if (err) {
2014-09-16 11:06:10 -04:00
return callback(err);
2014-03-21 15:40:37 -04:00
}
2014-09-16 11:06:10 -04:00
require('../socket.io').in('uid_' + uid).emit('event:unread.updateCount', null, count);
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) {
2014-03-21 15:40:37 -04:00
return callback();
}
tids = tids.filter(Boolean);
2014-09-27 17:41:49 -04:00
var now = Date.now();
var scores = tids.map(function(tid) {
return now;
});
2014-03-21 15:40:37 -04:00
async.parallel({
markRead: function(next) {
2014-09-27 17:41:49 -04:00
db.sortedSetAdd('uid:' + uid + ':tids_read', scores, tids, next);
},
topicData: function(next) {
Topics.getTopicsFields(tids, ['cid'], next);
}
}, function(err, results) {
if (err) {
return callback(err);
2014-03-21 15:40:37 -04:00
}
var cids = results.topicData.map(function(topic) {
return topic && topic.cid;
}).filter(function(topic, index, array) {
return topic && array.indexOf(topic) === index;
});
categories.markAsRead(cids, uid, callback);
2014-03-21 15:40:37 -04:00
});
};
2014-03-21 15:40:37 -04:00
Topics.markTopicNotificationsRead = function(tid, uid) {
2014-09-19 19:45:16 -04:00
if (!tid) {
return;
}
2014-07-28 15:52:33 -04:00
user.notifications.getUnreadByField(uid, 'tid', tid, function(err, nids) {
2014-09-08 23:03:37 -04:00
if (err) {
return winston.error(err.stack);
}
2014-07-28 15:52:33 -04:00
notifications.markReadMultiple(nids, uid, function() {
2014-03-21 15:40:37 -04:00
user.notifications.pushCount(uid);
});
});
};
Topics.markCategoryUnreadForAll = function(tid, callback) {
Topics.getTopicField(tid, 'cid', function(err, cid) {
if(err) {
return callback(err);
}
categories.markAsUnreadForAll(cid, callback);
});
};
Topics.hasReadTopics = function(tids, uid, callback) {
if(!parseInt(uid, 10)) {
return callback(null, tids.map(function() {
return false;
}));
}
2014-09-27 17:41:49 -04:00
async.parallel({
recentScores: function(next) {
db.sortedSetScores('topics:recent', tids, next);
},
userScores: function(next) {
db.sortedSetScores('uid:' + uid + ':tids_read', tids, next);
}
}, function(err, results) {
if (err) {
return callback(err);
}
var result = tids.map(function(tid, index) {
return !!(results.userScores[index] && results.userScores[index] >= results.recentScores[index]);
});
2014-03-21 15:40:37 -04:00
2014-09-27 17:41:49 -04:00
callback(null, result);
});
2014-03-21 15:40:37 -04:00
};
Topics.hasReadTopic = function(tid, uid, callback) {
2014-09-27 17:41:49 -04:00
Topics.hasReadTopics([tid], uid, function(err, hasRead) {
callback(err, Array.isArray(hasRead) && hasRead.length ? hasRead[0] : false);
});
2014-03-21 15:40:37 -04:00
};
2014-04-10 20:31:57 +01:00
};