From e9b82d46e04e832c5f3c81cb784956367f1958fa Mon Sep 17 00:00:00 2001 From: OldHawk Date: Tue, 11 Jul 2017 18:42:36 +0800 Subject: [PATCH] feat(forums): show forum new topics and replies of today --- modules/core/client/app/init.js | 2 +- modules/core/client/app/trans-string-en.js | 3 + modules/core/client/app/trans-string-zh.js | 3 + .../controllers/forums.client.controller.js | 46 ++++++++- modules/forums/client/less/forum.less | 6 ++ .../client/views/index.client.view.html | 1 + .../client/views/topic.client.view.html | 2 +- .../controllers/forums.server.controller.js | 98 ++++++++++++++++--- 8 files changed, 143 insertions(+), 18 deletions(-) diff --git a/modules/core/client/app/init.js b/modules/core/client/app/init.js index 75d14659..6b6c98d1 100644 --- a/modules/core/client/app/init.js +++ b/modules/core/client/app/init.js @@ -56,7 +56,7 @@ function writeLeaveTime(localStorageService, $window) { $window.onbeforeunload = function () { localStorageService.set('last_leave_time', Date.now()); - } + }; } markedConfig.$inject = ['markedProvider']; diff --git a/modules/core/client/app/trans-string-en.js b/modules/core/client/app/trans-string-en.js index 561addb0..52f2fdf7 100644 --- a/modules/core/client/app/trans-string-en.js +++ b/modules/core/client/app/trans-string-en.js @@ -677,6 +677,9 @@ DELETE_REPLY_CONFIRM_BODY_TEXT: 'Are you sure want to delete this reply?', READ_ONLY_POST: '*** Readonly forum, cannot to post topic!', READ_ONLY_REPLY: '*** Readonly topic, cannot to post reply!', + TODAY_NEW_COUNT_ALL: '(Today: {{topic}} topics, {{reply}} replies)', + TODAY_NEW_COUNT_TOPIC: '(Today: {{topic}} topics)', + TODAY_NEW_COUNT_REPLY: '(Today: {{reply}} replies)', CATEGORY: { AFFAIRS: 'Affairs', diff --git a/modules/core/client/app/trans-string-zh.js b/modules/core/client/app/trans-string-zh.js index 3eba3190..56666a6b 100644 --- a/modules/core/client/app/trans-string-zh.js +++ b/modules/core/client/app/trans-string-zh.js @@ -677,6 +677,9 @@ DELETE_REPLY_CONFIRM_BODY_TEXT: '您确定要删除这条回复?', READ_ONLY_POST: '*** 只读版块,不能发表话题!', READ_ONLY_REPLY: '*** 只读话题,不能发表回复!', + TODAY_NEW_COUNT_ALL: '(今日: {{topic}} 话题, {{reply}} 回复)', + TODAY_NEW_COUNT_TOPIC: '(今日: {{topic}} 话题)', + TODAY_NEW_COUNT_REPLY: '(今日: {{reply}} 回复)', CATEGORY: { AFFAIRS: '站务区', diff --git a/modules/forums/client/controllers/forums.client.controller.js b/modules/forums/client/controllers/forums.client.controller.js index 77bc54cf..a2be57d7 100644 --- a/modules/forums/client/controllers/forums.client.controller.js +++ b/modules/forums/client/controllers/forums.client.controller.js @@ -28,9 +28,11 @@ vm.last_leave_time = localStorageService.get('last_leave_time') || Date.now(); // get forums list - ForumsService.query({}, function (items) { + ForumsService.get({}, function (items) { console.log(items); - vm.forums = items; + vm.forums = items.forumsList; + vm.forumsTopicsCount = items.forumsTopicsCount; + vm.forumsRepliesCount = items.forumsRepliesCount; }); }; @@ -61,5 +63,45 @@ } }; + vm.getTodayNewCount = function (f) { + if (f) { + var n_topic = getNewTopic(f); + var n_reply = getNewReply(f); + if (n_topic === 0 && n_reply === 0) { + return undefined; + } else if (n_topic === 0 && n_reply !== 0) { + return $translate.instant('FORUMS.TODAY_NEW_COUNT_REPLY', {reply: n_reply}); + } else if (n_topic !== 0 && n_reply === 0) { + return $translate.instant('FORUMS.TODAY_NEW_COUNT_TOPIC', {topic: n_topic}); + } else { + return $translate.instant('FORUMS.TODAY_NEW_COUNT_ALL', {topic: n_topic, reply: n_reply}); + } + } else { + return undefined; + } + + function getNewTopic(f) { + var c = 0; + angular.forEach(vm.forumsTopicsCount, function (tc) { + if (tc._id === f._id) { + c = c + tc.count; + } + }); + + return c; + } + + function getNewReply(f) { + var c = 0; + angular.forEach(vm.forumsRepliesCount, function (rc) { + if (rc._id === f._id) { + c = c + rc.count; + } + }); + + return c; + } + }; + } }()); diff --git a/modules/forums/client/less/forum.less b/modules/forums/client/less/forum.less index 5e6c91fd..2be8ad86 100644 --- a/modules/forums/client/less/forum.less +++ b/modules/forums/client/less/forum.less @@ -80,6 +80,12 @@ font-size: 16px; margin-top: 2px; } + .today-new-count { + color: #999; + font-size: 12px; + font-weight: normal; + margin-left: 10px; + } } .moderators-title { color: #666; diff --git a/modules/forums/client/views/index.client.view.html b/modules/forums/client/views/index.client.view.html index b0d5aa7d..acacaf4a 100644 --- a/modules/forums/client/views/index.client.view.html +++ b/modules/forums/client/views/index.client.view.html @@ -51,6 +51,7 @@

{{f.name}} R +

diff --git a/modules/forums/client/views/topic.client.view.html b/modules/forums/client/views/topic.client.view.html index 87183830..78f12b54 100644 --- a/modules/forums/client/views/topic.client.view.html +++ b/modules/forums/client/views/topic.client.view.html @@ -179,7 +179,7 @@
-
+
diff --git a/modules/forums/server/controllers/forums.server.controller.js b/modules/forums/server/controllers/forums.server.controller.js index e9978884..ffa56ce0 100644 --- a/modules/forums/server/controllers/forums.server.controller.js +++ b/modules/forums/server/controllers/forums.server.controller.js @@ -22,25 +22,95 @@ var traceConfig = config.meanTorrentConfig.trace; * @param res */ exports.list = function (req, res) { - Forum.find() - .sort('category order -createdat') - .populate({ - path: 'lastTopic', - populate: { - path: 'user lastUser', - select: 'username displayName profileImageURL uploaded downloaded' + var findForumsList = function (callback) { + Forum.find() + .sort('category order -createdat') + .populate({ + path: 'lastTopic', + populate: { + path: 'user lastUser', + select: 'username displayName profileImageURL uploaded downloaded' + } + }) + .populate('moderators', 'username displayName profileImageURL uploaded downloaded') + .exec(function (err, forums) { + if (err) { + callback(err, null); + } else { + callback(null, forums); + } + }); + }; + + var forumsTopicsCount = function (callback) { + var nd = (new Date()).getDate(); + Topic.aggregate({ + $project: { + 'forum': '$forum', + 'day': { + '$dayOfMonth': '$createdAt' + } } - }) - .populate('moderators', 'username displayName profileImageURL uploaded downloaded') - .exec(function (err, forums) { + }, { + $match: { + day: nd + } + }, { + $group: { + _id: '$forum', + count: {$sum: 1} + } + }).exec(function (err, counts) { if (err) { - return res.status(422).send({ - message: errorHandler.getErrorMessage(err) - }); + callback(err, null); } else { - res.status(200).send(forums); + callback(null, counts); } }); + }; + + var forumsRepliesCount = function (callback) { + var nd = (new Date()).getDate(); + Topic.aggregate({ + $unwind: '$_replies' + }, { + $project: { + 'forum': '$forum', + //'title': '$title', + //'createdAt': '$_replies.createdAt', + 'day': { + '$dayOfMonth': '$_replies.createdAt' + } + } + }, { + $match: { + day: nd + } + }, { + $group: { + _id: '$forum', + count: {$sum: 1} + } + }).exec(function (err, counts) { + if (err) { + callback(err, null); + } else { + callback(null, counts); + } + }); + }; + + async.parallel([findForumsList, forumsTopicsCount, forumsRepliesCount], function (err, results) { + if (err) { + return res.status(422).send(err); + } else { + res.json({ + forumsList: results[0], + forumsTopicsCount: results[1], + forumsRepliesCount: results[2] + }); + } + }); }; /**