v1.18.x
barisusakli 9 years ago
parent caabbcf9fd
commit 7ad90b4ef6

@ -42,6 +42,7 @@
"login_to_subscribe": "Please register or log in in order to subscribe to this topic.", "login_to_subscribe": "Please register or log in in order to subscribe to this topic.",
"markAsUnreadForAll.success" : "Topic marked as unread for all.", "markAsUnreadForAll.success" : "Topic marked as unread for all.",
"mark_unread": "Mark unread",
"watch": "Watch", "watch": "Watch",
"unwatch": "Unwatch", "unwatch": "Unwatch",

@ -53,6 +53,11 @@ define('forum/topic/threadTools', [
return false; return false;
}); });
topicContainer.on('click', '[component="topic/mark-unread"]', function() {
socket.emit('topics.markUnread', tid);
return false;
});
topicContainer.on('click', '[component="topic/mark-unread-for-all"]', function() { topicContainer.on('click', '[component="topic/mark-unread-for-all"]', function() {
var btn = $(this); var btn = $(this);
socket.emit('topics.markAsUnreadForAll', [tid], function(err) { socket.emit('topics.markAsUnreadForAll', [tid], function(err) {

@ -25,7 +25,7 @@ module.exports = function(middleware) {
options = {}; options = {};
} }
options.loggedIn = req.user ? parseInt(req.user.uid, 10) !== 0 : false; options.loggedIn = !!req.uid;
options.relative_path = nconf.get('relative_path'); options.relative_path = nconf.get('relative_path');
options.template = {name: template}; options.template = {name: template};
options.template[template] = true; options.template[template] = true;

@ -1,11 +1,11 @@
'use strict'; 'use strict';
var async = require('async'), var async = require('async');
db = require('../../database'), var db = require('../../database');
user = require('../../user'), var user = require('../../user');
topics = require('../../topics'), var topics = require('../../topics');
utils = require('../../../public/src/utils'); var utils = require('../../../public/src/utils');
module.exports = function(SocketTopics) { module.exports = function(SocketTopics) {
@ -62,6 +62,19 @@ module.exports = function(SocketTopics) {
}); });
}; };
SocketTopics.markUnread = function(socket, tid, callback) {
if (!tid || !socket.uid) {
return callback();
}
topics.markUnread(tid, socket.uid, function(err) {
if (err) {
return callback(err);
}
topics.pushUnreadCount(socket.uid);
});
};
SocketTopics.markAsUnreadForAll = function(socket, tids, callback) { SocketTopics.markAsUnreadForAll = function(socket, tids, callback) {
if (!Array.isArray(tids)) { if (!Array.isArray(tids)) {
return callback(new Error('[[error:invalid-tid]]')); return callback(new Error('[[error:invalid-tid]]'));

@ -1,14 +1,14 @@
'use strict'; 'use strict';
var async = require('async'), var async = require('async');
winston = require('winston'), var winston = require('winston');
db = require('../database'), var db = require('../database');
user = require('../user'), var user = require('../user');
notifications = require('../notifications'), var notifications = require('../notifications');
categories = require('../categories'), var categories = require('../categories');
privileges = require('../privileges'); var privileges = require('../privileges');
module.exports = function(Topics) { module.exports = function(Topics) {
@ -69,13 +69,16 @@ module.exports = function(Topics) {
}, },
userScores: function(next) { userScores: function(next) {
db.getSortedSetRevRangeByScoreWithScores('uid:' + uid + ':tids_read', 0, -1, '+inf', cutoff, next); db.getSortedSetRevRangeByScoreWithScores('uid:' + uid + ':tids_read', 0, -1, '+inf', cutoff, next);
},
tids_unread: function(next) {
db.getSortedSetRevRangeWithScores('uid:' + uid + ':tids_unread', 0, -1, next);
} }
}, function(err, results) { }, function(err, results) {
if (err) { if (err) {
return callback(err); return callback(err);
} }
if (results.recentTids && !results.recentTids.length) { if (results.recentTids && !results.recentTids.length && !results.tids_unread.length) {
return callback(null, []); return callback(null, []);
} }
@ -84,11 +87,17 @@ module.exports = function(Topics) {
userRead[userItem.value] = userItem.score; 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, index) { var tids = results.recentTids.filter(function(recentTopic) {
return !userRead[recentTopic.value] || recentTopic.score > userRead[recentTopic.value]; return !userRead[recentTopic.value] || recentTopic.score > userRead[recentTopic.value];
}).map(function(topic) { }).map(function(topic) {
return topic.value; return topic.value;
}).filter(function(tid, index, array) {
return array.indexOf(tid) === index;
}); });
tids = tids.slice(0, 100); tids = tids.slice(0, 100);
@ -142,6 +151,7 @@ module.exports = function(Topics) {
if (err) { if (err) {
return callback(err); return callback(err);
} }
require('../socket.io').in('uid_' + uid).emit('event:unread.updateCount', count); require('../socket.io').in('uid_' + uid).emit('event:unread.updateCount', count);
callback(); callback();
}); });
@ -184,6 +194,7 @@ module.exports = function(Topics) {
async.parallel({ async.parallel({
markRead: async.apply(db.sortedSetAdd, 'uid:' + uid + ':tids_read', scores, tids), markRead: async.apply(db.sortedSetAdd, 'uid:' + uid + ':tids_read', scores, tids),
markUnread: async.apply(db.sortedSetRemove, 'uid:' + uid + ':tids_unread', tids),
topicData: async.apply( Topics.getTopicsFields, tids, ['cid']) topicData: async.apply( Topics.getTopicsFields, tids, ['cid'])
}, next); }, next);
}, },
@ -227,7 +238,7 @@ module.exports = function(Topics) {
}; };
Topics.hasReadTopics = function(tids, uid, callback) { Topics.hasReadTopics = function(tids, uid, callback) {
if(!parseInt(uid, 10)) { if (!parseInt(uid, 10)) {
return callback(null, tids.map(function() { return callback(null, tids.map(function() {
return false; return false;
})); }));
@ -239,14 +250,20 @@ module.exports = function(Topics) {
}, },
userScores: function(next) { userScores: function(next) {
db.sortedSetScores('uid:' + uid + ':tids_read', tids, next); db.sortedSetScores('uid:' + uid + ':tids_read', tids, next);
},
tids_unread: function (next) {
db.sortedSetScores('uid:' + uid + ':tids_unread', tids, next);
} }
}, function(err, results) { }, function(err, results) {
if (err) { if (err) {
return callback(err); return callback(err);
} }
var cutoff = Date.now() - unreadCutoff; var cutoff = Date.now() - unreadCutoff;
var result = tids.map(function(tid, index) { var result = tids.map(function(tid, index) {
return results.recentScores[index] < cutoff || !!(results.userScores[index] && results.userScores[index] >= results.recentScores[index]); return !results.tids_unread[index] &&
(results.recentScores[index] < cutoff ||
!!(results.userScores[index] && results.userScores[index] >= results.recentScores[index]));
}); });
callback(null, result); callback(null, result);
@ -259,5 +276,21 @@ module.exports = function(Topics) {
}); });
}; };
Topics.markUnread = function(tid, uid, callback) {
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);
}
], callback);
};
}; };

Loading…
Cancel
Save