|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const Controller = require('egg').Controller; |
| 4 | + |
| 5 | +class ReplyController extends Controller { |
| 6 | + /** |
| 7 | + * 添加回复 |
| 8 | + */ |
| 9 | + async add() { |
| 10 | + const { ctx, service } = this; |
| 11 | + const content = ctx.request.body.r_content; |
| 12 | + const reply_id = ctx.params.reply_id; |
| 13 | + |
| 14 | + if (content.trim() === '') { |
| 15 | + ctx.status = 422; |
| 16 | + ctx.body = { |
| 17 | + error: '回复内容不能为空!', |
| 18 | + }; |
| 19 | + return; |
| 20 | + } |
| 21 | + |
| 22 | + const topic_id = ctx.params.topic_id; |
| 23 | + let topic = await service.topic.getTopicById(topic_id); |
| 24 | + topic = topic.topic; |
| 25 | + |
| 26 | + if (!topic) { |
| 27 | + ctx.status = 404; |
| 28 | + ctx.message = '这个主题不存在。'; |
| 29 | + return; |
| 30 | + } |
| 31 | + if (topic.lock) { |
| 32 | + ctx.status = 403; |
| 33 | + ctx.body = { |
| 34 | + error: '该主题已锁定', |
| 35 | + }; |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + const user_id = ctx.user._id; |
| 40 | + const topicAuthor = await service.user.getUserById(topic.author_id); |
| 41 | + const newContent = content.replace('@' + topicAuthor.loginname + ' ', ''); |
| 42 | + const [ |
| 43 | + reply, user, |
| 44 | + ] = await Promise.all([ |
| 45 | + service.reply.newAndSave(content, topic_id, user_id, reply_id), |
| 46 | + service.user.getUserById(user_id), |
| 47 | + ]); |
| 48 | + |
| 49 | + user.score += 5; |
| 50 | + user.reply_count += 1; |
| 51 | + await Promise.all([ |
| 52 | + service.topic.updateLastReply(topic_id, reply._id), |
| 53 | + user.save(), |
| 54 | + ]); |
| 55 | + service.at.sendMessageToMentionUsers(newContent, topic_id, user_id, reply._id); |
| 56 | + if (topic.author_id.toString() !== user_id.toString()) { |
| 57 | + await service.message.sendReplyMessage(topic.author_id, user_id, topic._id, reply._id); |
| 58 | + } |
| 59 | + |
| 60 | + ctx.redirect('/topic/' + topic_id + '#' + reply._id); |
| 61 | + } |
| 62 | + /** |
| 63 | + * 打开回复编辑器 |
| 64 | + */ |
| 65 | + async showEdit() { |
| 66 | + const { ctx, service } = this; |
| 67 | + const reply_id = ctx.params.reply_id; |
| 68 | + const reply = await service.reply.getReplyById(reply_id); |
| 69 | + |
| 70 | + if (!reply) { |
| 71 | + ctx.status = 404; |
| 72 | + ctx.message = '此回复不存在或已被删除。'; |
| 73 | + return; |
| 74 | + } |
| 75 | + if (ctx.user._id.toString() === reply.author_id.toString() || ctx.user.is_admin) { |
| 76 | + await ctx.render('reply/edit', { |
| 77 | + reply_id: reply._id, |
| 78 | + content: reply.content, |
| 79 | + }); |
| 80 | + return; |
| 81 | + } |
| 82 | + ctx.status = 403; |
| 83 | + ctx.body = { |
| 84 | + error: '对不起,你不能编辑此回复', |
| 85 | + }; |
| 86 | + return; |
| 87 | + } |
| 88 | + /** |
| 89 | + * 提交编辑回复 |
| 90 | + */ |
| 91 | + async update() { |
| 92 | + const { ctx, service } = this; |
| 93 | + const reply_id = ctx.params.reply_id; |
| 94 | + const content = ctx.request.body.t_content; |
| 95 | + const reply = await service.reply.getReplyById(reply_id); |
| 96 | + |
| 97 | + if (!reply) { |
| 98 | + ctx.status = 404; |
| 99 | + ctx.message = '此回复不存在或已被删除。'; |
| 100 | + return; |
| 101 | + } |
| 102 | + if (ctx.user._id.toString() === reply.author_id.toString() || ctx.user.is_admin) { |
| 103 | + if (content.trim() !== '') { |
| 104 | + reply.content = content; |
| 105 | + reply.update_at = new Date(); |
| 106 | + await reply.save(); |
| 107 | + ctx.redirect('/topic/' + reply.topic_id + '#' + reply._id); |
| 108 | + return; |
| 109 | + } |
| 110 | + ctx.status = 400; |
| 111 | + ctx.body = { |
| 112 | + error: '回复的字数太少。', |
| 113 | + }; |
| 114 | + return; |
| 115 | + } |
| 116 | + ctx.status = 403; |
| 117 | + ctx.body = { |
| 118 | + error: '对不起,你不能编辑此回复', |
| 119 | + }; |
| 120 | + return; |
| 121 | + } |
| 122 | + /** |
| 123 | + * 删除回复 |
| 124 | + */ |
| 125 | + async delete() { |
| 126 | + const { ctx, service } = this; |
| 127 | + const reply_id = ctx.params.reply_id; |
| 128 | + const reply = await service.reply.getReplyById(reply_id); |
| 129 | + |
| 130 | + if (!reply) { |
| 131 | + ctx.status = 422; |
| 132 | + ctx.body = { status: 'no reply ' + reply_id + ' exists' }; |
| 133 | + return; |
| 134 | + } |
| 135 | + if (reply.author_id.toString() === ctx.user._id.toString() || ctx.user.is_admin) { |
| 136 | + reply.deleted = true; |
| 137 | + reply.save(); |
| 138 | + ctx.status = 200; |
| 139 | + ctx.body = { status: 'success' }; |
| 140 | + reply.author.score -= 5; |
| 141 | + reply.author.reply_count -= 1; |
| 142 | + reply.author.save(); |
| 143 | + } else { |
| 144 | + ctx.status = 200; |
| 145 | + ctx.body = { status: 'failed' }; |
| 146 | + } |
| 147 | + await service.topic.reduceCount(reply.topic_id); |
| 148 | + return; |
| 149 | + } |
| 150 | + /** |
| 151 | + * 回复点赞 |
| 152 | + */ |
| 153 | + async up() { |
| 154 | + const { ctx, service } = this; |
| 155 | + const reply_id = ctx.params.reply_id; |
| 156 | + const user_id = ctx.user._id; |
| 157 | + const reply = await service.reply.getReplyById(reply_id); |
| 158 | + |
| 159 | + if (!reply) { |
| 160 | + ctx.status = 404; |
| 161 | + ctx.message = '此回复不存在或已被删除。'; |
| 162 | + return; |
| 163 | + } |
| 164 | + if (reply.author_id.toString() === user_id.toString()) { |
| 165 | + ctx.body = { |
| 166 | + success: false, |
| 167 | + message: '呵呵,不能帮自己点赞。', |
| 168 | + }; |
| 169 | + return; |
| 170 | + } |
| 171 | + let action; |
| 172 | + reply.ups = reply.ups || []; |
| 173 | + const upIndex = reply.ups.indexOf(user_id); |
| 174 | + if (upIndex === -1) { |
| 175 | + reply.ups.push(user_id); |
| 176 | + action = 'up'; |
| 177 | + } else { |
| 178 | + reply.ups.splice(upIndex, 1); |
| 179 | + action = 'down'; |
| 180 | + } |
| 181 | + await reply.save(); |
| 182 | + ctx.body = { |
| 183 | + success: true, |
| 184 | + action, |
| 185 | + }; |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +module.exports = ReplyController; |
0 commit comments