22
33const Controller = require ( 'egg' ) . Controller ;
44const _ = require ( 'lodash' ) ;
5- const validator = require ( 'validator' ) ;
5+ // const validator = require('validator');
66const path = require ( 'path' ) ;
77const fs = require ( 'fs' ) ;
88const awaitWriteStream = require ( 'await-stream-ready' ) . write ;
@@ -27,7 +27,7 @@ class TopicController extends Controller {
2727
2828 if ( topic_id . length !== 24 ) {
2929 ctx . status = 404 ;
30- // '此话题不存在或已被删除。'
30+ ctx . message = '此话题不存在或已被删除。' ;
3131 return ;
3232 }
3333
@@ -40,10 +40,9 @@ class TopicController extends Controller {
4040
4141 topic . author = author ;
4242 topic . replies = replies ;
43-
4443 // 点赞数排名第三的回答,它的点赞数就是阈值
45- topic . reply_up_threshold = ( function ( ) {
46- let allUpCount = replies . map ( function ( reply ) {
44+ topic . reply_up_threshold = ( ( ) => {
45+ let allUpCount = replies . map ( reply => {
4746 return ( reply . ups && reply . ups . length ) || 0 ;
4847 } ) ;
4948 allUpCount = _ . sortBy ( allUpCount , Number ) . reverse ( ) ;
@@ -60,7 +59,7 @@ class TopicController extends Controller {
6059 const other_topics = await service . topic . getTopicsByQuery ( query , options ) ;
6160
6261 // get no_reply_topics
63- let no_reply_topics = service . cache . get ( 'no_reply_topics' ) ;
62+ let no_reply_topics = await service . cache . get ( 'no_reply_topics' ) ;
6463 if ( ! no_reply_topics ) {
6564 const query = { reply_count : 0 , tab : { $nin : [ 'job' , 'dev' ] } } ;
6665 const options = { limit : 5 , sort : '-create_at' } ;
@@ -72,7 +71,7 @@ class TopicController extends Controller {
7271 if ( ! currentUser ) {
7372 is_collect = null ;
7473 } else {
75- is_collect = service . topicCollect . getTopicCollect (
74+ is_collect = await service . topicCollect . getTopicCollect (
7675 currentUser . _id ,
7776 topic_id
7877 ) ;
@@ -103,9 +102,9 @@ class TopicController extends Controller {
103102 async put ( ) {
104103 const { ctx, service } = this ;
105104 const { tabs } = this . config ;
106- const title = validator . trim ( ctx . body . title ) ;
107- const tab = validator . trim ( ctx . body . tab ) ;
108- const content = validator . trim ( ctx . body . t_content ) ;
105+ const title = ctx . request . body . title . trim ( ) ;
106+ const tab = ctx . request . body . tab . trim ( ) ;
107+ const content = ctx . request . body . t_content . trim ( ) ;
109108
110109 // 得到所有的 tab, e.g. ['ask', 'share', ..]
111110 const allTabs = tabs . map ( function ( tPair ) {
@@ -121,7 +120,7 @@ class TopicController extends Controller {
121120 } else if ( ! tab || allTabs . indexOf ( tab ) === - 1 ) {
122121 editError = '必须选择一个版块。' ;
123122 } else if ( content === '' ) {
124- editError = '内容不可为空' ;
123+ editError = '内容不可为空。 ' ;
125124 }
126125 // END 验证
127126
@@ -155,6 +154,8 @@ class TopicController extends Controller {
155154 topic . _id ,
156155 ctx . user . _id
157156 ) ;
157+
158+ await ctx . redirect ( '/topic/' + topic . _id ) ;
158159 }
159160
160161 /**
@@ -165,6 +166,7 @@ class TopicController extends Controller {
165166 const topic_id = ctx . params . tid ;
166167
167168 const { topic } = await service . topic . getTopicById ( topic_id ) ;
169+
168170 if ( ! topic ) {
169171 ctx . status = 404 ;
170172 ctx . message = '此话题不存在或已被删除。' ;
@@ -185,7 +187,7 @@ class TopicController extends Controller {
185187 } ) ;
186188 } else {
187189 ctx . status = 403 ;
188- // ctx.message = '对不起,你不能编辑此话题。 ';
190+ ctx . message = '对不起,你不能编辑此话题' ;
189191 }
190192 }
191193
@@ -208,11 +210,11 @@ class TopicController extends Controller {
208210 }
209211
210212 if (
211- topic . author_id === ctx . user . _id || ctx . user . is_admin
213+ topic . author_id . toString ( ) === ctx . user . _id . toString ( ) || ctx . user . is_admin
212214 ) {
213- title = validator . trim ( title ) ;
214- tab = validator . trim ( tab ) ;
215- content = validator . trim ( content ) ;
215+ title = title . trim ( ) ;
216+ tab = tab . trim ( ) ;
217+ content = content . trim ( ) ;
216218
217219 // 验证
218220 let editError ;
@@ -222,6 +224,8 @@ class TopicController extends Controller {
222224 editError = '标题字数太多或太少。' ;
223225 } else if ( ! tab ) {
224226 editError = '必须选择一个版块。' ;
227+ } else if ( content === '' ) {
228+ editError = '内容不可为空。' ;
225229 }
226230 // END 验证
227231
@@ -247,7 +251,8 @@ class TopicController extends Controller {
247251 await service . at . sendMessageToMentionUsers (
248252 content ,
249253 topic . _id ,
250- ctx . user . _id
254+ ctx . user . _id ,
255+ 'at'
251256 ) ;
252257
253258 ctx . redirect ( '/topic/' + topic . _id ) ;
@@ -269,18 +274,18 @@ class TopicController extends Controller {
269274
270275 const [ topic , author ] = await service . topic . getFullTopic ( topic_id ) ;
271276
277+ if ( ! topic ) {
278+ ctx . status = 422 ;
279+ ctx . body = { message : '此话题不存在或已被删除。' , success : false } ;
280+ return ;
281+ }
282+
272283 if (
273284 ! ctx . user . is_admin &&
274285 ! topic . author_id . equals ( ctx . user . _id )
275286 ) {
276287 ctx . status = 403 ;
277- ctx . body = { success : false , message : '无权限' } ;
278- return ;
279- }
280-
281- if ( ! topic ) {
282- ctx . status = 422 ;
283- ctx . body = { success : false , message : '此话题不存在或已被删除。' } ;
288+ ctx . body = { message : '无权限' , success : false } ;
284289 return ;
285290 }
286291
@@ -291,7 +296,8 @@ class TopicController extends Controller {
291296 topic . deleted = true ;
292297
293298 await topic . save ( ) ;
294- ctx . body = { success : true , message : '话题已被删除。' } ;
299+
300+ ctx . body = { message : '话题已被删除。' , success : true } ;
295301 }
296302
297303 /**
@@ -302,12 +308,6 @@ class TopicController extends Controller {
302308 const topic_id = ctx . params . tid ;
303309 const referer = ctx . get ( 'referer' ) ;
304310
305- if ( topic_id . length !== 24 ) {
306- ctx . status = 404 ;
307- ctx . message = '此话题不存在或已被删除。' ;
308- return ;
309- }
310-
311311 const topic = await service . topic . getTopic ( topic_id ) ;
312312
313313 if ( ! topic ) {
@@ -372,6 +372,7 @@ class TopicController extends Controller {
372372
373373 if ( ! topic ) {
374374 ctx . body = { status : 'failed' } ;
375+ return ;
375376 }
376377
377378 const doc = await service . topicCollect . getTopicCollect (
@@ -403,20 +404,23 @@ class TopicController extends Controller {
403404
404405 if ( ! topic ) {
405406 ctx . body = { status : 'failed' } ;
407+ return ;
406408 }
407409
408- const removeResult = service . topic_collect . remove (
410+ const removeResult = await service . topicCollect . remove (
409411 ctx . user . _id ,
410412 topic . _id
411413 ) ;
414+
412415 if ( removeResult . result . n === 0 ) {
413416 ctx . body = { status : 'failed' } ;
417+ return ;
414418 }
415419
416420 const user = await service . user . getUserById ( ctx . user . _id ) ;
417421
418422 user . collect_topic_count -= 1 ;
419- ctx . user = user ;
423+ // ctx.user = user;
420424 await user . save ( ) ;
421425
422426 topic . collect_count -= 1 ;
0 commit comments