@@ -7,7 +7,7 @@ import { AgentService } from '@/services/agent.service';
77import { BoardCardService } from '@/services/board-card.service' ;
88import { BoardMemberService } from '@/services/board-member.service' ;
99import { orm } from '@/utils/orm' ;
10- import { isBordlyComment } from '@/utils/shared' ;
10+ import { isCommentForBordly } from '@/utils/shared' ;
1111
1212export class CommentService {
1313 static async create (
@@ -34,14 +34,17 @@ export class CommentService {
3434 boardCard . setSnippet ( `${ user . firstName } : ${ contentText } ` ) ;
3535 boardCard . setLastEventAt ( comment . createdAt ) ;
3636
37- const userBoardCardReadPosition = boardCard . boardCardReadPositions . find ( ( pos ) => pos . user . id === user . id ) ! ;
38- userBoardCardReadPosition . setLastReadAt ( boardCard . lastEventAt ) ;
37+ if ( ! user . isBordly ) {
38+ const userBoardCardReadPosition = boardCard . boardCardReadPositions . find ( ( pos ) => pos . user . id === user . id ) ! ;
39+ userBoardCardReadPosition . setLastReadAt ( boardCard . lastEventAt ) ;
40+ orm . em . persist ( userBoardCardReadPosition ) ;
41+ }
3942
40- orm . em . persist ( [ comment , boardCard , userBoardCardReadPosition ] ) ;
43+ orm . em . persist ( [ comment , boardCard ] ) ;
4144
4245 await orm . em . flush ( ) ;
4346
44- if ( isBordlyComment ( contentText ) ) {
47+ if ( isCommentForBordly ( contentText ) && ! user . isBordly ) {
4548 const userBoardMember = await BoardMemberService . findById ( board , {
4649 boardMemberId : boardMember . id ,
4750 populate : [ 'user' , 'memory' ] ,
@@ -80,7 +83,12 @@ export class CommentService {
8083 populate ?: Populate < Comment , Hint > ;
8184 } ,
8285 ) {
83- const comment = await CommentService . findById ( boardCard , { commentId, populate } ) ;
86+ const comment = await CommentService . findById ( boardCard , {
87+ commentId,
88+ populate : [ ...( populate || [ ] ) , 'user' ] as Populate < Comment , Hint > ,
89+ } ) ;
90+ if ( comment . user . id !== user . id ) throw new Error ( 'You can only edit your own comments' ) ;
91+
8492 const boardMember = user . boardMembers . find ( ( bm ) => bm . board . id === board . id ) ! ;
8593 const wasLastBoardCardEvent = comment . createdAt . getTime ( ) === boardCard . lastEventAt . getTime ( ) ;
8694
@@ -93,7 +101,7 @@ export class CommentService {
93101
94102 await orm . em . flush ( ) ;
95103
96- if ( isBordlyComment ( contentText ) ) {
104+ if ( isCommentForBordly ( contentText ) ) {
97105 const userBoardMember = await BoardMemberService . findById ( board , {
98106 boardMemberId : boardMember . id ,
99107 populate : [ 'user' , 'memory' ] ,
@@ -113,8 +121,12 @@ export class CommentService {
113121 return { boardCard, comment } ;
114122 }
115123
116- static async delete ( boardCard : BoardCard , { commentId } : { commentId : string } ) {
117- const comment = await CommentService . findById ( boardCard , { commentId } ) ;
124+ static async delete ( boardCard : BoardCard , { user, commentId } : { user : Loaded < User > ; commentId : string } ) {
125+ const comment = await CommentService . findById ( boardCard , { commentId, populate : [ 'user' ] } ) ;
126+ if ( comment . user . id !== user . id && ! comment . loadedUser . isBordly ) {
127+ throw new Error ( 'You can only delete your own comments or comments from Bordly' ) ;
128+ }
129+
118130 orm . em . remove ( comment ) ;
119131
120132 await BoardCardService . rebuildLastEventAtAndSnippet ( boardCard , { ignoreLastEventAt : comment . createdAt } ) ;
0 commit comments