feat: edit comment

Signed-off-by: Innei <i@innei.in>
This commit is contained in:
Innei
2024-09-17 13:55:35 +08:00
parent a0e37aa417
commit ed7b33e3a9
5 changed files with 52 additions and 1 deletions

View File

@@ -10,6 +10,7 @@ export enum BusinessEvents {
COMMENT_CREATE = 'COMMENT_CREATE',
COMMENT_DELETE = 'COMMENT_DELETE',
COMMENT_UPDATE = 'COMMENT_UPDATE',
POST_CREATE = 'POST_CREATE',
POST_UPDATE = 'POST_UPDATE',

View File

@@ -20,7 +20,10 @@ import {
import { ApiController } from '~/common/decorators/api-controller.decorator'
import { Auth } from '~/common/decorators/auth.decorator'
import { CurrentUser } from '~/common/decorators/current-user.decorator'
import {
CurrentReaderId,
CurrentUser,
} from '~/common/decorators/current-user.decorator'
import { HTTPDecorators } from '~/common/decorators/http.decorator'
import { IpLocation, IpRecord } from '~/common/decorators/ip.decorator'
import { IsAuthenticated } from '~/common/decorators/role.decorator'
@@ -43,6 +46,7 @@ import {
CommentDto,
CommentRefTypesDto,
CommentStatePatchDto,
EditCommentDto,
TextOnlyDto,
} from './comment.dto'
import { CommentReplyMailType } from './comment.enum'
@@ -451,4 +455,23 @@ export class CommentController {
})
return
}
@Patch('/edit/:id')
async editComment(
@Param() params: MongoIdDto,
@Body() body: EditCommentDto,
@IsAuthenticated() isAuthenticated: boolean,
@CurrentReaderId() readerId: string,
) {
const { id } = params
const { text } = body
const comment = await this.commentService.model.findById(id).lean()
if (!comment) {
throw new CannotFindException()
}
if (comment.readerId !== readerId && !isAuthenticated) {
throw new ForbiddenException()
}
await this.commentService.editComment(id, text)
}
}

View File

@@ -55,6 +55,12 @@ export class CommentDto {
avatar?: string
}
export class EditCommentDto {
@IsString()
@IsNotEmpty()
text: string
}
export class RequiredGuestReaderCommentDto extends CommentDto {
@IsString()
@IsNotEmpty()

View File

@@ -643,4 +643,19 @@ export class CommentService implements OnModuleInit {
url: `${adminUrl}#/comments`,
})
}
async editComment(id: string, text: string) {
const comment = await this.commentModel.findById(id).lean()
if (!comment) {
throw new CannotFindException()
}
await this.commentModel.updateOne({ _id: id }, { text })
await this.eventManager.broadcast(
BusinessEvents.COMMENT_UPDATE,
{ id, text },
{
scope: comment.isWhispers ? EventScope.TO_SYSTEM_ADMIN : EventScope.ALL,
},
)
}
}

View File

@@ -48,6 +48,12 @@ export interface EventPayloadMapping {
[BusinessEvents.COMMENT_CREATE]: Omit<CommentModel, 'ref'> & {
ref: Id | PostModel | PageModel | NoteModel | RecentlyModel
}
[BusinessEvents.COMMENT_UPDATE]: {
id: string
text: string
}
[BusinessEvents.ARTICLE_READ_COUNT_UPDATE]: {
count: number
type: 'post' | 'note'