diff --git a/__tests__/contentPreference.ts b/__tests__/contentPreference.ts index f9b045cd2..4b968bfad 100644 --- a/__tests__/contentPreference.ts +++ b/__tests__/contentPreference.ts @@ -38,6 +38,7 @@ import { import { ghostUser } from '../src/common'; import { ContentPreferenceKeyword } from '../src/entity/contentPreference/ContentPreferenceKeyword'; import { ContentPreferenceWord } from '../src/entity/contentPreference/ContentPreferenceWord'; +import { ContentPreference } from '../src/entity/contentPreference/ContentPreference'; let con: DataSource; let state: GraphQLTestingState; @@ -306,6 +307,44 @@ describe('query userFollowers', () => { }, }); }); + + it('should hide blocked users for the requesting user', async () => { + // logged as user 2, user 3 is blocked + loggedUser = `2-ufq`; + const blockedUserId = `3-ufq`; + await con.getRepository(ContentPreference).save({ + userId: loggedUser, + referenceId: blockedUserId, + status: ContentPreferenceStatus.Blocked, + type: ContentPreferenceType.User, + feedId: loggedUser, + }); + + const res = await client.query(QUERY, { + variables: { + id: '1-ufq', + entity: ContentPreferenceType.User, + feedId: '1-ufq', + }, + }); + + expect(res.errors).toBeFalsy(); + expect(res.data).toEqual({ + userFollowers: { + edges: [ + { + node: { + referenceId: '1-ufq', + status: 'follow', + user: { + id: '2-ufq', + }, + }, + }, + ], + }, + }); + }); }); describe('query userFollowing', () => { @@ -519,6 +558,45 @@ describe('query userFollowing', () => { }, }); }); + + it('should hide blocked users for the requesting user', async () => { + // logged as user 2, user 3 is blocked + loggedUser = `2-ufwq`; + const blockedUserId = `3-ufwq`; + await con.getRepository(ContentPreference).save({ + userId: loggedUser, + referenceId: blockedUserId, + status: ContentPreferenceStatus.Blocked, + type: ContentPreferenceType.User, + feedId: loggedUser, + }); + + const res = await client.query(QUERY, { + variables: { + id: '1-ufwq', + entity: ContentPreferenceType.User, + feedId: '1-ufwq', + }, + }); + + expect(res.errors).toBeFalsy(); + + expect(res.data).toEqual({ + userFollowing: { + edges: [ + { + node: { + referenceId: '2-ufwq', + status: 'follow', + user: { + id: '1-ufwq', + }, + }, + }, + ], + }, + }); + }); }); describe('query ContentPreferenceStatus', () => { diff --git a/src/common/contentPreference.ts b/src/common/contentPreference.ts index 328fc9dd9..340ee2da8 100644 --- a/src/common/contentPreference.ts +++ b/src/common/contentPreference.ts @@ -524,9 +524,11 @@ export const whereNotUserBlocked = ( { userId, feedId, + columnName = 'userId', }: { userId: string; feedId?: string; + columnName?: 'userId' | 'referenceId'; }, ) => { const feedIds = feedId ? [feedId, userId] : [userId]; @@ -539,7 +541,7 @@ export const whereNotUserBlocked = ( userId, feedId: In(feedIds), }) - .andWhere(`"pref"."referenceId" = ${qb.alias}."userId"`) + .andWhere(`"pref"."referenceId" = ${qb.alias}."${columnName}"`) .getQuery(); return `NOT EXISTS(${query})`; }; diff --git a/src/schema/contentPreference.ts b/src/schema/contentPreference.ts index 469f84a6d..06d243775 100644 --- a/src/schema/contentPreference.ts +++ b/src/schema/contentPreference.ts @@ -16,6 +16,7 @@ import { followEntity, unblockEntity, unfollowEntity, + whereNotUserBlocked, } from '../common/contentPreference'; import { GQLEmptyResponse, offsetPageGenerator } from './common'; import graphorm from '../graphorm'; @@ -303,6 +304,14 @@ export const resolvers: IResolvers = traceResolvers< .offset(page.offset) .addOrderBy(`${builder.alias}."createdAt"`, 'DESC'); + if (ctx.userId) { + builder.queryBuilder.andWhere( + whereNotUserBlocked(builder.queryBuilder, { + userId: ctx.userId, + }), + ); + } + return builder; }, undefined, @@ -357,6 +366,15 @@ export const resolvers: IResolvers = traceResolvers< .offset(page.offset) .addOrderBy(`${builder.alias}."createdAt"`, 'DESC'); + if (ctx.userId) { + builder.queryBuilder.andWhere( + whereNotUserBlocked(builder.queryBuilder, { + userId: ctx.userId, + columnName: `referenceId`, + }), + ); + } + return builder; }, undefined,