@@ -130,7 +130,7 @@ var create = function (req, res, next) {
130130 editError = '标题不能为空' ;
131131 } else if ( title . length < 5 || title . length > 100 ) {
132132 editError = '标题字数太多或太少' ;
133- } else if ( ! tab || allTabs . indexOf ( tab ) === - 1 ) {
133+ } else if ( ! tab || ! _ . includes ( allTabs , tab ) ) {
134134 editError = '必须选择一个版块' ;
135135 } else if ( content === '' ) {
136136 editError = '内容不可为空' ;
@@ -171,3 +171,61 @@ var create = function (req, res, next) {
171171
172172exports . create = create ;
173173
174+ exports . update = function ( req , res , next ) {
175+ var topic_id = _ . trim ( req . body . topic_id ) ;
176+ var title = _ . trim ( req . body . title ) ;
177+ var tab = _ . trim ( req . body . tab ) ;
178+ var content = _ . trim ( req . body . content ) ;
179+
180+ // 得到所有的 tab, e.g. ['ask', 'share', ..]
181+ var allTabs = config . tabs . map ( function ( tPair ) {
182+ return tPair [ 0 ] ;
183+ } ) ;
184+
185+ TopicProxy . getTopicById ( topic_id , function ( err , topic , tags ) {
186+ if ( ! topic ) {
187+ res . status ( 400 ) ;
188+ return res . send ( { success : false , error_msg : '此话题不存在或已被删除。' } ) ;
189+ }
190+
191+ if ( topic . author_id . equals ( req . user . _id ) || req . user . is_admin ) {
192+ // 验证
193+ var editError ;
194+ if ( title === '' ) {
195+ editError = '标题不能是空的。' ;
196+ } else if ( title . length < 5 || title . length > 100 ) {
197+ editError = '标题字数太多或太少。' ;
198+ } else if ( ! tab || ! _ . includes ( allTabs , tab ) ) {
199+ editError = '必须选择一个版块。' ;
200+ }
201+ // END 验证
202+
203+ if ( editError ) {
204+ return res . send ( { success : false , error_msg : editError } ) ;
205+ }
206+
207+ //保存话题
208+ topic . title = title ;
209+ topic . content = content ;
210+ topic . tab = tab ;
211+ topic . update_at = new Date ( ) ;
212+
213+ topic . save ( function ( err ) {
214+ if ( err ) {
215+ return next ( err ) ;
216+ }
217+ //发送at消息
218+ at . sendMessageToMentionUsers ( content , topic . _id , req . user . _id ) ;
219+
220+ res . send ( {
221+ success : true ,
222+ topic_id : topic . id
223+ } ) ;
224+ } ) ;
225+ } else {
226+ res . status ( 403 )
227+ return res . send ( { success : false , error_msg : '对不起,你不能编辑此话题。' } ) ;
228+ }
229+ } ) ;
230+ } ;
231+
0 commit comments