@@ -16,6 +16,7 @@ import checkUnscore from '../etc/checkUnscore';
16
16
import { purgePost } from '../lib/graphcdn' ;
17
17
import {
18
18
CommentNotificationActionInput ,
19
+ CommentReplyNotificationActionInput ,
19
20
notificationService ,
20
21
} from '../services/notificationService' ;
21
22
import db from '../lib/db' ;
@@ -164,7 +165,9 @@ export const resolvers: IResolvers<any, ApolloContext> = {
164
165
}
165
166
166
167
if ( comment_id ) {
167
- const commentTarget = await commentRepo . findOne ( comment_id ) ;
168
+ const commentTarget = await commentRepo . findOne ( comment_id , {
169
+ relations : [ 'user' ] ,
170
+ } ) ;
168
171
if ( ! commentTarget ) {
169
172
throw new ApolloError ( 'Target comment is not found' , 'NOT_FOUND' ) ;
170
173
}
@@ -287,45 +290,60 @@ export const resolvers: IResolvers<any, ApolloContext> = {
287
290
await purgePost ( post . id ) ;
288
291
} catch ( e ) { }
289
292
290
- // create notification
293
+ // create comment notification
291
294
if ( post . user . id !== ctx . user_id ) {
292
- const notification = await notificationService . findByUniqueKey ( {
293
- fkUserId : post . user . id ,
294
- actionId : comment . id ,
295
- actorId : ctx . user_id ,
296
- type : 'comment' ,
297
- } ) ;
298
-
299
- if ( notification ) {
300
- await db . notification . update ( {
301
- where : {
302
- id : notification . id ,
303
- } ,
304
- data : {
305
- is_deleted : false ,
295
+ try {
296
+ await notificationService . createNotification ( {
297
+ type : 'comment' ,
298
+ fk_user_id : post . user . id ,
299
+ action_id : comment . id ,
300
+ actor_id : ctx . user_id ,
301
+ action : {
302
+ comment : {
303
+ actor_display_name : user . profile . display_name ,
304
+ actor_thumbnail : user . profile . thumbnail || '' ,
305
+ actor_username : user . username ,
306
+ comment_id : comment . id ,
307
+ comment_text : comment . text ,
308
+ post_id : post . id ,
309
+ post_title : post . title ,
310
+ post_url_slug : post . url_slug ,
311
+ post_writer_username : post . user . username ,
312
+ type : 'comment' ,
313
+ } ,
306
314
} ,
315
+ cookies : ctx . cookies ,
307
316
} ) ;
317
+ } catch ( error ) {
318
+ console . log ( 'err' , error ) ;
308
319
}
320
+ }
321
+
322
+ // create comment reply notification
323
+ if ( comment_id ) {
324
+ const commentTarget = await commentRepo . findOne ( comment_id , {
325
+ relations : [ 'user' ] ,
326
+ } ) ;
309
327
310
- if ( ! notification ) {
328
+ if ( commentTarget && commentTarget ?. user . id !== ctx . user_id ) {
311
329
try {
312
330
await notificationService . createNotification ( {
313
- type : 'comment ' ,
314
- fk_user_id : post . user . id ,
331
+ type : 'commentReply ' ,
332
+ fk_user_id : commentTarget . user . id ,
315
333
action_id : comment . id ,
316
334
actor_id : ctx . user_id ,
317
335
action : {
318
- comment : {
336
+ commentReply : {
337
+ type : 'commentReply' ,
338
+ parent_comment_text : commentTarget . text ,
339
+ reply_comment_text : comment . text ,
319
340
actor_display_name : user . profile . display_name ,
320
341
actor_thumbnail : user . profile . thumbnail || '' ,
321
342
actor_username : user . username ,
322
343
comment_id : comment . id ,
323
- comment_text : comment . text ,
324
344
post_id : post . id ,
325
- post_title : post . title ,
326
345
post_url_slug : post . url_slug ,
327
346
post_writer_username : post . user . username ,
328
- type : 'comment' ,
329
347
} ,
330
348
} ,
331
349
cookies : ctx . cookies ,
@@ -382,17 +400,37 @@ export const resolvers: IResolvers<any, ApolloContext> = {
382
400
await purgePost ( post . id ) ;
383
401
} catch ( e ) { }
384
402
385
- const notification = await notificationService . findByUniqueKey ( {
403
+ // remove notification
404
+ const comemntNotification = await notificationService . findByUniqueKey ( {
386
405
fkUserId : post . user . id ,
387
406
actionId : comment . id ,
388
407
actorId : ctx . user_id ,
389
408
type : 'comment' ,
390
409
} ) ;
391
410
392
- if ( notification ) {
411
+ if ( comemntNotification ) {
412
+ await db . notification . update ( {
413
+ where : {
414
+ id : comemntNotification . id ,
415
+ } ,
416
+ data : {
417
+ is_deleted : true ,
418
+ } ,
419
+ } ) ;
420
+ }
421
+
422
+ // remove notification
423
+ const commentReplyNotificaiton = await notificationService . findByUniqueKey ( {
424
+ fkUserId : post . user . id ,
425
+ actionId : comment . id ,
426
+ actorId : ctx . user_id ,
427
+ type : 'commentReply' ,
428
+ } ) ;
429
+
430
+ if ( commentReplyNotificaiton ) {
393
431
await db . notification . update ( {
394
432
where : {
395
- id : notification . id ,
433
+ id : commentReplyNotificaiton . id ,
396
434
} ,
397
435
data : {
398
436
is_deleted : true ,
@@ -425,22 +463,45 @@ export const resolvers: IResolvers<any, ApolloContext> = {
425
463
426
464
// update notification
427
465
if ( post ) {
428
- const notification = await notificationService . findByUniqueKey ( {
466
+ const commentNotification = await notificationService . findByUniqueKey ( {
429
467
fkUserId : post . user . id ,
430
468
actionId : comment . id ,
431
469
actorId : ctx . user_id ,
432
470
type : 'comment' ,
433
471
} ) ;
434
472
435
- if ( notification ) {
473
+ if ( commentNotification ) {
436
474
const action : CommentNotificationActionInput = {
437
- ...( notification . action as CommentNotificationActionInput ) ,
475
+ ...( commentNotification . action as CommentNotificationActionInput ) ,
438
476
comment_text : text ,
439
477
} ;
440
478
441
479
await db . notification . update ( {
442
480
where : {
443
- id : notification . id ,
481
+ id : commentNotification . id ,
482
+ } ,
483
+ data : {
484
+ action,
485
+ } ,
486
+ } ) ;
487
+ }
488
+
489
+ const commentReplyNotification = await notificationService . findByUniqueKey ( {
490
+ fkUserId : post . user . id ,
491
+ actionId : comment . id ,
492
+ actorId : ctx . user_id ,
493
+ type : 'commentReply' ,
494
+ } ) ;
495
+
496
+ if ( commentReplyNotification ) {
497
+ const action : CommentReplyNotificationActionInput = {
498
+ ...( commentReplyNotification . action as CommentReplyNotificationActionInput ) ,
499
+ reply_comment_text : text ,
500
+ } ;
501
+
502
+ await db . notification . update ( {
503
+ where : {
504
+ id : commentReplyNotification . id ,
444
505
} ,
445
506
data : {
446
507
action,
0 commit comments