mirror of
https://github.com/docmost/docmost.git
synced 2026-05-21 01:04:39 +08:00
WIP
This commit is contained in:
@@ -2,9 +2,11 @@ import {
|
||||
BadRequestException,
|
||||
ForbiddenException,
|
||||
Injectable,
|
||||
Logger,
|
||||
NotFoundException,
|
||||
} from '@nestjs/common';
|
||||
import { CreateCommentDto } from './dto/create-comment.dto';
|
||||
import { CreateReadOnlyCommentDto } from './dto/create-readonly-comment.dto';
|
||||
import { UpdateCommentDto } from './dto/update-comment.dto';
|
||||
import { CommentRepo } from '@docmost/db/repos/comment/comment.repo';
|
||||
import { Comment, Page, User } from '@docmost/db/types/entity.types';
|
||||
@@ -12,13 +14,19 @@ import { PaginationOptions } from '@docmost/db/pagination/pagination-options';
|
||||
import { PaginationResult } from '@docmost/db/pagination/pagination';
|
||||
import { PageRepo } from '@docmost/db/repos/page/page.repo';
|
||||
import { SpaceMemberRepo } from '@docmost/db/repos/space/space-member.repo';
|
||||
import { CollaborationGateway } from '../../collaboration/collaboration.gateway';
|
||||
import { setYjsMark } from '../../collaboration/collaboration.util';
|
||||
import * as Y from 'yjs';
|
||||
|
||||
@Injectable()
|
||||
export class CommentService {
|
||||
private readonly logger = new Logger(CommentService.name);
|
||||
|
||||
constructor(
|
||||
private commentRepo: CommentRepo,
|
||||
private pageRepo: PageRepo,
|
||||
private spaceMemberRepo: SpaceMemberRepo,
|
||||
private collaborationGateway: CollaborationGateway,
|
||||
) {}
|
||||
|
||||
async findById(commentId: string) {
|
||||
@@ -105,4 +113,49 @@ export class CommentService {
|
||||
|
||||
return comment;
|
||||
}
|
||||
|
||||
async createReadOnlyComment(
|
||||
opts: { userId: string; page: Page; workspaceId: string },
|
||||
createCommentDto: CreateReadOnlyCommentDto,
|
||||
): Promise<Comment> {
|
||||
const { userId, page, workspaceId } = opts;
|
||||
const commentContent = JSON.parse(createCommentDto.content);
|
||||
|
||||
const comment = await this.commentRepo.insertComment({
|
||||
pageId: page.id,
|
||||
content: commentContent,
|
||||
selection: createCommentDto?.selection?.substring(0, 250),
|
||||
type: 'inline',
|
||||
creatorId: userId,
|
||||
workspaceId: workspaceId,
|
||||
spaceId: page.spaceId,
|
||||
});
|
||||
|
||||
const documentName = `page.${page.id}`;
|
||||
const directConnection =
|
||||
await this.collaborationGateway.openDirectConnection(documentName);
|
||||
|
||||
try {
|
||||
await directConnection.transact((doc) => {
|
||||
const fragment = doc.getXmlFragment('default');
|
||||
setYjsMark(doc, fragment, createCommentDto.yjsSelection, 'comment', {
|
||||
commentId: comment.id,
|
||||
resolved: false,
|
||||
});
|
||||
});
|
||||
} catch (error) {
|
||||
this.logger.error(
|
||||
`Failed to apply comment mark for comment ${comment.id}`,
|
||||
error,
|
||||
);
|
||||
await this.commentRepo.deleteComment(comment.id);
|
||||
throw new BadRequestException(
|
||||
'Failed to apply comment mark. Selection may have changed.',
|
||||
);
|
||||
} finally {
|
||||
await directConnection.disconnect();
|
||||
}
|
||||
|
||||
return comment;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user