Skip to content

Commit

Permalink
Expose CommentThread.comments.canCreate
Browse files Browse the repository at this point in the history
  • Loading branch information
CarsonF committed Oct 8, 2024
1 parent c18605e commit 0cce117
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
8 changes: 6 additions & 2 deletions src/components/comments/comment-thread.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,15 @@ export class CommentThreadResolver {
})
async comments(
@AnonSession() session: Session,
@Parent() { id }: CommentThread,
@Parent() thread: CommentThread,
@ListArg(CommentListInput) input: CommentListInput,
@Loader(CommentLoader) comments: LoaderOf<CommentLoader>,
): Promise<CommentList> {
const list = await this.service.listCommentsByThreadId(id, input, session);
const list = await this.service.listCommentsByThreadId(
thread,
input,
session,
);
comments.primeAll(list.items);
return list;
}
Expand Down
18 changes: 15 additions & 3 deletions src/components/comments/comment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { CommentRepository } from './comment.repository';
import {
Comment,
Commentable,
CommentList,
CommentListInput,
CommentThread,
CommentThreadList,
Expand Down Expand Up @@ -177,14 +178,25 @@ export class CommentService {
}

async listCommentsByThreadId(
thread: ID,
thread: CommentThread,
input: CommentListInput,
session: Session,
) {
const results = await this.repo.list(thread, input, session);
): Promise<CommentList> {
const perms = await this.getPermissionsFromResource(thread.parent, session);

// Do check here since we don't filter in the db query.
// Will need to be updated with DB switch.
if (!perms.can('read')) {
return SecuredList.Redacted;
}

const results = await this.repo.list(thread.id, input, session);

return {
...results,
items: results.items.map((dto) => this.secureComment(dto, session)),
canRead: true,
canCreate: perms.can('create'),
};
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/components/comments/dto/list-comment.dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { InputType, ObjectType } from '@nestjs/graphql';
import { Order, PaginatedList, SortablePaginationInput } from '~/common';
import { Order, SecuredList, SortablePaginationInput } from '~/common';
import { Comment } from './comment.dto';

@InputType()
Expand All @@ -9,4 +9,4 @@ export class CommentListInput extends SortablePaginationInput<keyof Comment>({
}) {}

@ObjectType()
export abstract class CommentList extends PaginatedList(Comment) {}
export abstract class CommentList extends SecuredList(Comment) {}

0 comments on commit 0cce117

Please sign in to comment.