Skip to content

Commit

Permalink
feat: add filter for blocked following/followed users (#2595)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilasw authored Jan 16, 2025
1 parent b1c9e16 commit f30950f
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 1 deletion.
78 changes: 78 additions & 0 deletions __tests__/contentPreference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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', () => {
Expand Down
4 changes: 3 additions & 1 deletion src/common/contentPreference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -524,9 +524,11 @@ export const whereNotUserBlocked = (
{
userId,
feedId,
columnName = 'userId',
}: {
userId: string;
feedId?: string;
columnName?: 'userId' | 'referenceId';
},
) => {
const feedIds = feedId ? [feedId, userId] : [userId];
Expand All @@ -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})`;
};
18 changes: 18 additions & 0 deletions src/schema/contentPreference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
followEntity,
unblockEntity,
unfollowEntity,
whereNotUserBlocked,
} from '../common/contentPreference';
import { GQLEmptyResponse, offsetPageGenerator } from './common';
import graphorm from '../graphorm';
Expand Down Expand Up @@ -303,6 +304,14 @@ export const resolvers: IResolvers<unknown, BaseContext> = traceResolvers<
.offset(page.offset)
.addOrderBy(`${builder.alias}."createdAt"`, 'DESC');

if (ctx.userId) {
builder.queryBuilder.andWhere(
whereNotUserBlocked(builder.queryBuilder, {
userId: ctx.userId,
}),
);
}

return builder;
},
undefined,
Expand Down Expand Up @@ -357,6 +366,15 @@ export const resolvers: IResolvers<unknown, BaseContext> = 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,
Expand Down

0 comments on commit f30950f

Please sign in to comment.