Skip to content

Commit

Permalink
Merge pull request #162 from plezanje-net/fix-comments-guest
Browse files Browse the repository at this point in the history
Fix bug where comments are not shown if not logged in.
  • Loading branch information
demshy authored Aug 27, 2023
2 parents 831c01d + e33b29f commit c59e747
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 40 deletions.
22 changes: 5 additions & 17 deletions src/crags/loaders/route-comments.loader.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,18 @@
import DataLoader from 'dataloader';
import { Inject, Injectable } from '@nestjs/common';
import { Injectable } from '@nestjs/common';
import { Comment } from '../entities/comment.entity';
import { CommentsService } from '../services/comments.service';
import { NestDataLoader } from '../../core/interceptors/data-loader.interceptor';
import { CONTEXT } from '@nestjs/graphql';
import { User } from '../../users/entities/user.entity';

@Injectable()
export class RouteCommentsLoader implements NestDataLoader<string, Comment[]> {
currentUser: User = null;

constructor(
private readonly commentsService: CommentsService,
@Inject(CONTEXT) private context: any,
) {
this.currentUser = this.context.req.user;
}
constructor(private readonly commentsService: CommentsService) {}

generateDataLoader(): DataLoader<string, Comment[]> {
return new DataLoader<string, Comment[]>(async (keys) => {
const comments = await this.commentsService.find(
{
routeIds: keys.map((k) => k),
},
this.currentUser,
);
const comments = await this.commentsService.find({
routeIds: keys.map((k) => k),
});

const routeComments: { [key: string]: Comment[] } = {};

Expand Down
14 changes: 4 additions & 10 deletions src/crags/resolvers/crags.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,16 +189,10 @@ export class CragsResolver {

@ResolveField('comments', () => [Comment])
@UseGuards(UserAuthGuard)
async getComments(
@Parent() crag: Crag,
@CurrentUser() currentUser: User,
): Promise<Comment[]> {
const comments = await this.commentsService.find(
{
cragId: crag.id,
},
currentUser,
);
async getComments(@Parent() crag: Crag): Promise<Comment[]> {
const comments = await this.commentsService.find({
cragId: crag.id,
});

return comments;
}
Expand Down
14 changes: 1 addition & 13 deletions src/crags/services/comments.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,7 @@ export class CommentsService {
return this.commentsRepository.remove(comment).then(() => true);
}

async find(
params: FindCommentsInput = {},
currentUser: User,
): Promise<Comment[]> {
async find(params: FindCommentsInput = {}): Promise<Comment[]> {
const options: FindManyOptions = {
order: {},
where: {},
Expand All @@ -111,15 +108,6 @@ export class CommentsService {
options.where['type'] = params.type;
}

if (!currentUser) {
options.relations = {
crag: true,
};
options.where['crag'] = {
isHidden: false,
};
}

options.order = { created: 'DESC' };

let comments = await this.commentsRepository.find(options);
Expand Down

0 comments on commit c59e747

Please sign in to comment.