Skip to content

Commit

Permalink
refactor: changes to comments api and add traces
Browse files Browse the repository at this point in the history
  • Loading branch information
shadrach-tayo committed Feb 5, 2025
1 parent d5c44ae commit c4d0a75
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 41 deletions.
46 changes: 7 additions & 39 deletions desci-server/src/controllers/nodes/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,50 +65,18 @@ export const editComment = async (req: RequestWithUser, res: Response) => {
module: 'Comments::Edit',
user,
body: req.body,
params: req.params,
});

// if (uuid) {
// const node = await prisma.node.findFirst({ where: { uuid: ensureUuidEndsWithDot(uuid) } });
// if (!node) throw new NotFoundError('Node with uuid ${uuid} not found');
// }
logger.trace(`EditComment`);
const comment = await attestationService.editComment({
authorId: parseInt(user.id.toString()),
id,
update: { body, links },
});
logger.trace({ comment }, `EditCommentedComment`);

// let comment = await attestationService.getComment({ id });
// if (!comment) throw new NotFoundError();

// if (comment.authorId !== user.id) throw new ForbiddenError();
const comment = await attestationService.editComment({ authorId: req.user.id, id, update: { body, links } });
// if (highlights?.length > 0) {
// const processedHighlights = await asyncMap(highlights, async (highlight) => {
// if (!('image' in highlight)) return highlight;
// const blob = base64ToBlob(highlight.image);
// const storedCover = await client.add(blob, { cidVersion: 1 });

// return { ...highlight, image: `${PUBLIC_IPFS_PATH}/${storedCover.cid}` };
// });
// logger.info({ processedHighlights }, 'processedHighlights');
// annotation = await attestationService.createHighlight({
// claimId: claimId && parseInt(claimId.toString()),
// authorId: user.id,
// comment: body,
// links,
// highlights: processedHighlights as unknown as HighlightBlock[],
// visible,
// ...(uuid && { uuid: ensureUuidEndsWithDot(uuid) }),
// });
// await saveInteraction(req, ActionType.ADD_COMMENT, { annotationId: annotation.id, claimId, authorId });
// } else {
// annotation = await attestationService.createComment({
// claimId: claimId && parseInt(claimId.toString()),
// authorId: user.id,
// comment: body,
// links,
// visible,
// ...(uuid && { uuid: ensureUuidEndsWithDot(uuid) }),
// });
// }
await saveInteraction(req, ActionType.EDIT_COMMENT, { commentId: comment.id });
// await emitNotificationForAnnotation(annotation.id);
new SuccessResponse(comment).send(res);
};

Expand Down
1 change: 1 addition & 0 deletions desci-server/src/routes/v1/attestations/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export const getCommentsSchema = z.object({
export const editCommentsSchema = z.object({
params: z.object({
// quickly disqualify false uuid strings
uuid: z.string().min(10),
id: z.coerce.number(),
}),
body: z.object({
Expand Down
6 changes: 5 additions & 1 deletion desci-server/src/routes/v1/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,11 @@ router.post(
[ensureUser, validate(postCommentVoteSchema)],
asyncHandler(upvoteComment),
);
router.put('/comments/:id', [ensureUser, validate(editCommentsSchema)], asyncHandler(editComment));
router.put(
'/:uuid/comments/:id',
[ensureUser, ensureNodeAccess, validate(editCommentsSchema)],
asyncHandler(editComment),
);
router.post(
'/:uuid/comments/:commentId/downvote',
[ensureUser, validate(postCommentVoteSchema)],
Expand Down
19 changes: 18 additions & 1 deletion desci-server/src/services/Attestation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,7 @@ export class AttestationService {
const comment = await prisma.annotation.findFirst({ where: { id } });
if (!comment) throw new CommentNotFoundError();

logger.trace({ commentAuthor: comment.authorId, authorId }, 'Inspect authors');
if (comment.authorId !== authorId) throw new ForbiddenError();

if (comment.nodeAttestationId) {
Expand All @@ -722,7 +723,23 @@ export class AttestationService {
body,
links: links || comment.links,
};
return prisma.annotation.update({ where: { id: comment.id }, data });
return prisma.annotation.upsert({
update: { ...data },
create: { ...comment },
where: { id: comment.id },
select: {
id: true,
body: true,
links: true,
uuid: true,
authorId: true,
nodeAttestationId: true,
updatedAt: true,
type: true,
visible: true,
highlights: true,
},
});
}

async createHighlight({
Expand Down

0 comments on commit c4d0a75

Please sign in to comment.