mirror of
https://github.com/outline/outline.git
synced 2026-11-28 00:09:58 +03:00
* fix: Allow inline comments on documents without collaborative state Documents created through the API have no collaborative state until they are opened in an editor, which made anchored comments fail. Derive the state from the document content when it is missing. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * test: Read comment marks from content instead of decoding state Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * test: Read node-anchored comment mark from content Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
543 lines
14 KiB
TypeScript
543 lines
14 KiB
TypeScript
import Router from "koa-router";
|
|
import { difference } from "es-toolkit/compat";
|
|
import type { FindOptions, WhereOptions } from "sequelize";
|
|
import { Op } from "sequelize";
|
|
import { v4 as uuidv4 } from "uuid";
|
|
import { CommentStatusFilter, MentionType, IconType } from "@shared/types";
|
|
import { determineIconType } from "@shared/utils/icon";
|
|
import { commentParser } from "@server/editor";
|
|
import auth from "@server/middlewares/authentication";
|
|
import { commentingEnabled } from "@server/middlewares/feature";
|
|
import { rateLimiter } from "@server/middlewares/rateLimiter";
|
|
import { transaction } from "@server/middlewares/transaction";
|
|
import validate from "@server/middlewares/validate";
|
|
import { ValidationError } from "@server/errors";
|
|
import { Document, Comment, Collection, Reaction, Emoji } from "@server/models";
|
|
import { DocumentHelper } from "@server/models/helpers/DocumentHelper";
|
|
import { ProsemirrorHelper } from "@server/models/helpers/ProsemirrorHelper";
|
|
import { TextHelper } from "@server/models/helpers/TextHelper";
|
|
import { authorize } from "@server/policies";
|
|
import { presentComment, presentPolicies } from "@server/presenters";
|
|
import type { APIContext } from "@server/types";
|
|
import { RateLimiterStrategy } from "@server/utils/RateLimiter";
|
|
import pagination from "../middlewares/pagination";
|
|
import * as T from "./schema";
|
|
|
|
const router = new Router();
|
|
|
|
router.post(
|
|
"comments.create",
|
|
rateLimiter(RateLimiterStrategy.TwentyFivePerMinute),
|
|
auth(),
|
|
commentingEnabled(),
|
|
validate(T.CommentsCreateSchema),
|
|
transaction(),
|
|
async (ctx: APIContext<T.CommentsCreateReq>) => {
|
|
const {
|
|
id,
|
|
documentId,
|
|
parentCommentId,
|
|
anchorText,
|
|
anchorPrefix,
|
|
anchorSuffix,
|
|
anchorNodeId,
|
|
} = ctx.input.body;
|
|
const { user } = ctx.state.auth;
|
|
const { transaction } = ctx.state;
|
|
const anchored = !!(anchorText || anchorNodeId);
|
|
|
|
if (anchored) {
|
|
// Acquire the row lock on the document directly when anchoring so a
|
|
// concurrent inline comment can't overwrite our state update.
|
|
await Document.unscoped().findOne({
|
|
where: { id: documentId },
|
|
attributes: ["id"],
|
|
transaction,
|
|
lock: transaction.LOCK.UPDATE,
|
|
});
|
|
}
|
|
|
|
const document = await Document.findByPk(documentId, {
|
|
userId: user.id,
|
|
transaction,
|
|
// We only need to load the state binary if applying a comment mark
|
|
includeState: anchored,
|
|
});
|
|
authorize(user, "comment", document);
|
|
|
|
const text = ctx.input.body.text
|
|
? await TextHelper.replaceImagesWithAttachments(
|
|
ctx,
|
|
ctx.input.body.text,
|
|
user
|
|
)
|
|
: undefined;
|
|
const data = text
|
|
? commentParser.parse(text).toJSON()
|
|
: ctx.input.body.data;
|
|
|
|
const commentId = id || uuidv4();
|
|
|
|
if (anchored) {
|
|
const docState = DocumentHelper.toState(document);
|
|
|
|
const updated = anchorText
|
|
? ProsemirrorHelper.applyCommentMarkByText({
|
|
docState,
|
|
anchorText,
|
|
commentId,
|
|
userId: user.id,
|
|
prefix: anchorPrefix,
|
|
suffix: anchorSuffix,
|
|
})
|
|
: anchorNodeId
|
|
? ProsemirrorHelper.applyCommentMarkByNode({
|
|
docState,
|
|
anchorNodeId,
|
|
commentId,
|
|
userId: user.id,
|
|
})
|
|
: null;
|
|
|
|
if (!updated) {
|
|
throw ValidationError(
|
|
"Could not anchor comment to the provided location in the document"
|
|
);
|
|
}
|
|
|
|
// Save with hooks enabled so the AfterUpdate hook notifies the
|
|
// collaboration server, but silently so the document is not marked
|
|
// as updated by adding a comment.
|
|
await document.update(
|
|
{ state: updated.state, content: updated.content },
|
|
{ ...ctx.context, transaction, silent: true }
|
|
);
|
|
}
|
|
|
|
const comment = await Comment.createWithCtx(ctx, {
|
|
id: commentId,
|
|
data,
|
|
createdById: user.id,
|
|
documentId,
|
|
parentCommentId,
|
|
});
|
|
|
|
comment.createdBy = user;
|
|
|
|
ctx.body = {
|
|
data: presentComment(comment),
|
|
policies: presentPolicies(user, [comment]),
|
|
};
|
|
}
|
|
);
|
|
|
|
router.post(
|
|
"comments.info",
|
|
auth(),
|
|
commentingEnabled(),
|
|
validate(T.CommentsInfoSchema),
|
|
async (ctx: APIContext<T.CommentsInfoReq>) => {
|
|
const { id, includeAnchorText } = ctx.input.body;
|
|
const { user } = ctx.state.auth;
|
|
|
|
const comment = await Comment.findByPk(id, {
|
|
rejectOnEmpty: true,
|
|
});
|
|
const document = await Document.findByPk(comment.documentId, {
|
|
userId: user.id,
|
|
});
|
|
authorize(user, "read", comment);
|
|
authorize(user, "read", document);
|
|
|
|
comment.document = document;
|
|
|
|
ctx.body = {
|
|
data: presentComment(comment, { includeAnchorText }),
|
|
policies: presentPolicies(user, [comment]),
|
|
};
|
|
}
|
|
);
|
|
|
|
router.post(
|
|
"comments.list",
|
|
auth(),
|
|
pagination(),
|
|
commentingEnabled(),
|
|
validate(T.CommentsListSchema),
|
|
async (ctx: APIContext<T.CommentsListReq>) => {
|
|
const {
|
|
sort,
|
|
direction,
|
|
documentId,
|
|
parentCommentId,
|
|
statusFilter,
|
|
collectionId,
|
|
includeAnchorText,
|
|
} = ctx.input.body;
|
|
const { user } = ctx.state.auth;
|
|
const statusQuery = [];
|
|
|
|
if (statusFilter?.includes(CommentStatusFilter.Resolved)) {
|
|
statusQuery.push({ resolvedById: { [Op.not]: null } });
|
|
}
|
|
if (statusFilter?.includes(CommentStatusFilter.Unresolved)) {
|
|
statusQuery.push({ resolvedById: null });
|
|
}
|
|
|
|
const where: WhereOptions<Comment> = {
|
|
[Op.and]: [],
|
|
};
|
|
if (documentId) {
|
|
// @ts-expect-error ignore
|
|
where[Op.and].push({ documentId });
|
|
}
|
|
if (parentCommentId) {
|
|
// @ts-expect-error ignore
|
|
where[Op.and].push({ parentCommentId });
|
|
}
|
|
if (statusQuery.length) {
|
|
// @ts-expect-error ignore
|
|
where[Op.and].push({ [Op.or]: statusQuery });
|
|
}
|
|
|
|
const params: FindOptions<Comment> = {
|
|
where,
|
|
order: [[sort, direction]],
|
|
offset: ctx.state.pagination.offset,
|
|
limit: ctx.state.pagination.limit,
|
|
};
|
|
|
|
let comments, total;
|
|
if (documentId) {
|
|
const document = await Document.findByPk(documentId, { userId: user.id });
|
|
authorize(user, "read", document);
|
|
[comments, total] = await Promise.all([
|
|
Comment.findAll(params),
|
|
Comment.count({ where }),
|
|
]);
|
|
comments.forEach((comment) => (comment.document = document));
|
|
} else if (collectionId) {
|
|
const collection = await Collection.findByPk(collectionId, {
|
|
userId: user.id,
|
|
});
|
|
authorize(user, "read", collection);
|
|
const include = [
|
|
{
|
|
model: Document,
|
|
required: true,
|
|
where: {
|
|
teamId: user.teamId,
|
|
collectionId,
|
|
},
|
|
},
|
|
];
|
|
[comments, total] = await Promise.all([
|
|
Comment.findAll({
|
|
include,
|
|
...params,
|
|
}),
|
|
Comment.count({
|
|
include,
|
|
where,
|
|
}),
|
|
]);
|
|
} else {
|
|
const accessibleCollectionIds = await user.collectionIds();
|
|
const include = [
|
|
{
|
|
model: Document,
|
|
required: true,
|
|
where: {
|
|
teamId: user.teamId,
|
|
collectionId: { [Op.in]: accessibleCollectionIds },
|
|
},
|
|
},
|
|
];
|
|
[comments, total] = await Promise.all([
|
|
Comment.findAll({
|
|
include,
|
|
...params,
|
|
}),
|
|
Comment.count({
|
|
include,
|
|
where,
|
|
}),
|
|
]);
|
|
}
|
|
|
|
ctx.body = {
|
|
pagination: { ...ctx.state.pagination, total },
|
|
data: comments.map((comment) =>
|
|
presentComment(comment, { includeAnchorText })
|
|
),
|
|
policies: presentPolicies(user, comments),
|
|
};
|
|
}
|
|
);
|
|
|
|
router.post(
|
|
"comments.update",
|
|
auth(),
|
|
commentingEnabled(),
|
|
validate(T.CommentsUpdateSchema),
|
|
transaction(),
|
|
async (ctx: APIContext<T.CommentsUpdateReq>) => {
|
|
const { id, data } = ctx.input.body;
|
|
const { user } = ctx.state.auth;
|
|
const { transaction } = ctx.state;
|
|
|
|
const comment = await Comment.findByPk(id, {
|
|
transaction,
|
|
rejectOnEmpty: true,
|
|
lock: {
|
|
level: transaction.LOCK.UPDATE,
|
|
of: Comment,
|
|
},
|
|
});
|
|
const document = await Document.findByPk(comment.documentId, {
|
|
userId: user.id,
|
|
transaction,
|
|
});
|
|
authorize(user, "update", comment);
|
|
authorize(user, "comment", document);
|
|
|
|
let newMentionIds: string[] = [];
|
|
|
|
if (data !== undefined) {
|
|
const existingMentionIds = ProsemirrorHelper.parseMentions(
|
|
ProsemirrorHelper.toProsemirror(comment.data),
|
|
{ type: MentionType.User }
|
|
).map((mention) => mention.id);
|
|
const updatedMentionIds = ProsemirrorHelper.parseMentions(
|
|
ProsemirrorHelper.toProsemirror(data),
|
|
{ type: MentionType.User }
|
|
).map((mention) => mention.id);
|
|
|
|
const existingGroupMentionIds = ProsemirrorHelper.parseMentions(
|
|
ProsemirrorHelper.toProsemirror(comment.data),
|
|
{ type: MentionType.Group }
|
|
).map((mention) => mention.id);
|
|
const updatedGroupMentionIds = ProsemirrorHelper.parseMentions(
|
|
ProsemirrorHelper.toProsemirror(data),
|
|
{ type: MentionType.Group }
|
|
).map((mention) => mention.id);
|
|
|
|
newMentionIds = [
|
|
...difference(updatedMentionIds, existingMentionIds),
|
|
...difference(updatedGroupMentionIds, existingGroupMentionIds),
|
|
];
|
|
|
|
comment.data = data;
|
|
}
|
|
|
|
await comment.saveWithCtx(ctx, undefined, { data: { newMentionIds } });
|
|
|
|
ctx.body = {
|
|
data: presentComment(comment),
|
|
policies: presentPolicies(user, [comment]),
|
|
};
|
|
}
|
|
);
|
|
|
|
router.post(
|
|
"comments.delete",
|
|
auth(),
|
|
commentingEnabled(),
|
|
validate(T.CommentsDeleteSchema),
|
|
transaction(),
|
|
async (ctx: APIContext<T.CommentsDeleteReq>) => {
|
|
const { id } = ctx.input.body;
|
|
const { user } = ctx.state.auth;
|
|
const { transaction } = ctx.state;
|
|
|
|
const comment = await Comment.findByPk(id, {
|
|
transaction,
|
|
rejectOnEmpty: true,
|
|
lock: {
|
|
level: transaction.LOCK.UPDATE,
|
|
of: Comment,
|
|
},
|
|
});
|
|
const document = await Document.findByPk(comment.documentId, {
|
|
userId: user.id,
|
|
});
|
|
authorize(user, "delete", comment);
|
|
authorize(user, "comment", document);
|
|
|
|
await comment.destroyWithCtx(ctx);
|
|
|
|
ctx.body = {
|
|
success: true,
|
|
};
|
|
}
|
|
);
|
|
|
|
router.post(
|
|
"comments.resolve",
|
|
auth(),
|
|
commentingEnabled(),
|
|
validate(T.CommentsResolveSchema),
|
|
transaction(),
|
|
async (ctx: APIContext<T.CommentsResolveReq>) => {
|
|
const { id } = ctx.input.body;
|
|
const { user } = ctx.state.auth;
|
|
const { transaction } = ctx.state;
|
|
|
|
const comment = await Comment.findByPk(id, {
|
|
transaction,
|
|
rejectOnEmpty: true,
|
|
lock: {
|
|
level: transaction.LOCK.UPDATE,
|
|
of: Comment,
|
|
},
|
|
});
|
|
const document = await Document.findByPk(comment.documentId, {
|
|
userId: user.id,
|
|
});
|
|
authorize(user, "resolve", comment);
|
|
authorize(user, "update", document);
|
|
|
|
comment.resolve(user);
|
|
await comment.saveWithCtx(ctx, { silent: true });
|
|
|
|
ctx.body = {
|
|
data: presentComment(comment),
|
|
policies: presentPolicies(user, [comment]),
|
|
};
|
|
}
|
|
);
|
|
|
|
router.post(
|
|
"comments.unresolve",
|
|
auth(),
|
|
commentingEnabled(),
|
|
validate(T.CommentsUnresolveSchema),
|
|
transaction(),
|
|
async (ctx: APIContext<T.CommentsUnresolveReq>) => {
|
|
const { id } = ctx.input.body;
|
|
const { user } = ctx.state.auth;
|
|
const { transaction } = ctx.state;
|
|
|
|
const comment = await Comment.findByPk(id, {
|
|
transaction,
|
|
rejectOnEmpty: true,
|
|
lock: {
|
|
level: transaction.LOCK.UPDATE,
|
|
of: Comment,
|
|
},
|
|
});
|
|
const document = await Document.findByPk(comment.documentId, {
|
|
userId: user.id,
|
|
});
|
|
authorize(user, "unresolve", comment);
|
|
authorize(user, "update", document);
|
|
|
|
comment.unresolve();
|
|
await comment.saveWithCtx(ctx, { silent: true });
|
|
|
|
ctx.body = {
|
|
data: presentComment(comment),
|
|
policies: presentPolicies(user, [comment]),
|
|
};
|
|
}
|
|
);
|
|
|
|
router.post(
|
|
"comments.add_reaction",
|
|
rateLimiter(RateLimiterStrategy.TwentyFivePerMinute),
|
|
auth(),
|
|
commentingEnabled(),
|
|
validate(T.CommentsReactionSchema),
|
|
transaction(),
|
|
async (ctx: APIContext<T.CommentsReactionReq>) => {
|
|
const { id, emoji } = ctx.input.body;
|
|
const { user } = ctx.state.auth;
|
|
const { transaction } = ctx.state;
|
|
|
|
const comment = await Comment.findByPk(id, {
|
|
transaction,
|
|
rejectOnEmpty: true,
|
|
lock: {
|
|
level: transaction.LOCK.UPDATE,
|
|
of: Comment,
|
|
},
|
|
});
|
|
const document = await Document.findByPk(comment.documentId, {
|
|
userId: user.id,
|
|
transaction,
|
|
});
|
|
|
|
authorize(user, "comment", document);
|
|
authorize(user, "addReaction", comment);
|
|
|
|
if (determineIconType(emoji) === IconType.Custom) {
|
|
const customEmoji = await Emoji.findByPk(emoji, {
|
|
transaction,
|
|
});
|
|
authorize(user, "read", customEmoji);
|
|
}
|
|
|
|
await Reaction.findOrCreateWithCtx(
|
|
ctx,
|
|
{
|
|
where: {
|
|
emoji,
|
|
userId: user.id,
|
|
commentId: id,
|
|
},
|
|
},
|
|
{
|
|
persist: false,
|
|
}
|
|
);
|
|
|
|
ctx.body = {
|
|
success: true,
|
|
};
|
|
}
|
|
);
|
|
|
|
router.post(
|
|
"comments.remove_reaction",
|
|
rateLimiter(RateLimiterStrategy.TwentyFivePerMinute),
|
|
auth(),
|
|
commentingEnabled(),
|
|
validate(T.CommentsReactionSchema),
|
|
transaction(),
|
|
async (ctx: APIContext<T.CommentsReactionReq>) => {
|
|
const { id, emoji } = ctx.input.body;
|
|
const { user } = ctx.state.auth;
|
|
const { transaction } = ctx.state;
|
|
|
|
const comment = await Comment.findByPk(id, {
|
|
transaction,
|
|
rejectOnEmpty: true,
|
|
lock: {
|
|
level: transaction.LOCK.UPDATE,
|
|
of: Comment,
|
|
},
|
|
});
|
|
const document = await Document.findByPk(comment.documentId, {
|
|
userId: user.id,
|
|
transaction,
|
|
});
|
|
|
|
authorize(user, "comment", document);
|
|
authorize(user, "removeReaction", comment);
|
|
|
|
const reaction = await Reaction.findOne({
|
|
where: { emoji, userId: user.id, commentId: id },
|
|
transaction,
|
|
});
|
|
authorize(user, "delete", reaction);
|
|
|
|
await reaction.destroy(ctx.context);
|
|
|
|
ctx.body = {
|
|
success: true,
|
|
};
|
|
}
|
|
);
|
|
|
|
export default router;
|