1
1
import { Injectable } from '@nestjs/common' ;
2
+ import { difference } from 'lodash' ;
2
3
import {
3
4
ID ,
4
5
InvalidIdForTypeException ,
@@ -26,6 +27,7 @@ import {
26
27
CreateCommentInput ,
27
28
UpdateCommentInput ,
28
29
} from './dto' ;
30
+ import { CommentViaMentionNotificationService } from './mention-notification/comment-via-mention-notification.service' ;
29
31
30
32
type CommentableRef = ID | BaseNode | Commentable ;
31
33
@@ -36,6 +38,7 @@ export class CommentService {
36
38
private readonly privileges : Privileges ,
37
39
private readonly resources : ResourceLoader ,
38
40
private readonly resourcesHost : ResourcesHost ,
41
+ private readonly mentionNotificationService : CommentViaMentionNotificationService ,
39
42
) { }
40
43
41
44
async create ( input : CreateCommentInput , session : Session ) {
@@ -45,12 +48,13 @@ export class CommentService {
45
48
) ;
46
49
perms . verifyCan ( 'create' ) ;
47
50
51
+ let dto ;
48
52
try {
49
53
const result = await this . repo . create ( input , session ) ;
50
54
if ( ! result ) {
51
55
throw new ServerException ( 'Failed to create comment' ) ;
52
56
}
53
- return await this . readOne ( result . id , session ) ;
57
+ dto = await this . repo . readOne ( result . id ) ;
54
58
} catch ( exception ) {
55
59
if (
56
60
input . threadId &&
@@ -64,6 +68,11 @@ export class CommentService {
64
68
65
69
throw new ServerException ( 'Failed to create comment' , exception ) ;
66
70
}
71
+
72
+ const mentionees = this . mentionNotificationService . extract ( dto ) ;
73
+ await this . mentionNotificationService . notify ( mentionees , dto ) ;
74
+
75
+ return this . secureComment ( dto , session ) ;
67
76
}
68
77
69
78
async getPermissionsFromResource ( resource : CommentableRef , session : Session ) {
@@ -134,7 +143,14 @@ export class CommentService {
134
143
this . privileges . for ( session , Comment , object ) . verifyChanges ( changes ) ;
135
144
await this . repo . update ( object , changes ) ;
136
145
137
- return await this . readOne ( input . id , session ) ;
146
+ const updated = await this . repo . readOne ( object . id ) ;
147
+
148
+ const prevMentionees = this . mentionNotificationService . extract ( object ) ;
149
+ const nowMentionees = this . mentionNotificationService . extract ( updated ) ;
150
+ const newMentionees = difference ( prevMentionees , nowMentionees ) ;
151
+ await this . mentionNotificationService . notify ( newMentionees , updated ) ;
152
+
153
+ return this . secureComment ( updated , session ) ;
138
154
}
139
155
140
156
async delete ( id : ID , session : Session ) : Promise < void > {
0 commit comments