From 2b76d5ab541441053aea974f50b56672fbfc63bf Mon Sep 17 00:00:00 2001 From: OldHawk Date: Sun, 9 Jul 2017 17:54:31 +0800 Subject: [PATCH] feat(forums): reply owner or oper/admin can edit reply --- config/env/torrents.js | 3 +- .../forums-topic.client.controller.js | 59 +++++++++++++++++++ .../controllers/forums.server.controller.js | 34 +++++++++-- 3 files changed, 91 insertions(+), 5 deletions(-) diff --git a/config/env/torrents.js b/config/env/torrents.js index 6a64960e..2a5ed64a 100644 --- a/config/env/torrents.js +++ b/config/env/torrents.js @@ -79,7 +79,8 @@ module.exports = { userAnnounceData: {name: 'userAnnounceData', enable: true}, userScoreChange: {name: 'userScoreChange', enable: true}, - forumDeleteTopic: {name: 'forumDeleteTopic', enable: true} + forumDeleteTopic: {name: 'forumDeleteTopic', enable: true}, + forumDeleteReply: {name: 'forumDeleteReply', enable: true} } }, torrentType: { diff --git a/modules/forums/client/controllers/forums-topic.client.controller.js b/modules/forums/client/controllers/forums-topic.client.controller.js index 733e2640..8d06e015 100644 --- a/modules/forums/client/controllers/forums-topic.client.controller.js +++ b/modules/forums/client/controllers/forums-topic.client.controller.js @@ -152,6 +152,65 @@ }); }; + /** + * beginEditReply + * @param r + */ + vm.beginEditReply = function (r) { + var el = $('#' + r._id); + + el.markdown({ + autofocus: true, + savable: true, + hideable: true, + iconlibrary: 'fa', + resize: 'vertical', + language: localStorageService.get('storage_user_lang'), + fullscreen: {enable: false}, + onSave: function (e) { + if (e.isDirty()) { + //save content + var rep = new RepliesService({ + forum: vm.forum._id, + topic: vm.topic._id, + _id: r._id, + content: e.getContent() + }); + + rep.$update(function (res) { + vm.topic = res; + NotifycationService.showSuccessNotify('FORUMS.REPLY_EDIT_SUCCESSFULLY'); + }, function (res) { + NotifycationService.showErrorNotify(res.data.message, 'FORUMS.FORUMS'); + }); + + e.$options.hideable = true; + e.blur(); + } else { + e.$options.hideable = true; + e.blur(); + } + }, + onChange: function (e) { + e.$options.hideable = false; + }, + onShow: function (e) { + e.setContent(r.content); + + $('.md-footer').addClass('text-right'); + $('.md-footer')[0].childNodes[0].innerText = $translate.instant('FORUMS.BTN_SAVE'); + + var cbtn = angular.element(''); + cbtn.bind('click', function (evt) { + e.$options.hideable = true; + e.blur(); + }); + $('.md-footer').append(cbtn); + $compile($('.md-footer').contents())($scope); + } + }); + }; + /** * beginDeleteTopic * @param t diff --git a/modules/forums/server/controllers/forums.server.controller.js b/modules/forums/server/controllers/forums.server.controller.js index 197fd39f..28bd7a8a 100644 --- a/modules/forums/server/controllers/forums.server.controller.js +++ b/modules/forums/server/controllers/forums.server.controller.js @@ -215,7 +215,25 @@ exports.postNewReply = function (req, res) { * @param res */ exports.updateReply = function (req, res) { - console.log('-----updateReply-----'); + var topic = req.topic; + + topic._replies.forEach(function (t) { + if (t._id.equals(req.params.replyId)) { + t.content = req.body.content; + t.updatedAt = Date.now(); + t.updatedBy = req.user; + + topic.save(function (err) { + if (err) { + return res.status(422).send({ + message: errorHandler.getErrorMessage(err) + }); + } else { + res.json(topic); + } + }); + } + }); }; @@ -228,9 +246,9 @@ exports.deleteReply = function (req, res) { var forum = req.forum; var topic = req.topic; - topic._replies.forEach(function (r) { - if (r._id.equals(req.params.replyId)) { - topic._replies.pull(r); + topic._replies.forEach(function (t) { + if (t._id.equals(req.params.replyId)) { + topic._replies.pull(t); topic.replyCount--; topic.save(function (err) { if (err) { @@ -247,6 +265,13 @@ exports.deleteReply = function (req, res) { forum.update({ $inc: {replyCount: -1} }).exec(); + + //create trace log + traceLogCreate(req, traceConfig.action.forumDeleteReply, { + forum: forum._id, + topic: topic._id, + reply: req.params.replyId + }); }; /** @@ -266,6 +291,7 @@ exports.topicById = function (req, res, next, id) { .populate('updatedBy', 'username displayName profileImageURL uploaded downloaded') .populate('_scoreList.user', 'username displayName profileImageURL uploaded downloaded') .populate('_replies.user', 'username displayName profileImageURL uploaded downloaded') + .populate('_replies.updatedBy', 'username displayName profileImageURL uploaded downloaded') .exec(function (err, topic) { if (err) { return next(err);