Skip to content

Commit fefeca5

Browse files
committed
fix: check block list base on username
1 parent 86e26db commit fefeca5

File tree

3 files changed

+11
-12
lines changed

3 files changed

+11
-12
lines changed

src/cache.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ interface CacheService {
44
connect: () => void;
55
remove: (...keys: string[]) => Promise<number>;
66
disconnect: () => void;
7-
readBlackList: () => Promise<string[]>;
7+
readBlockList: () => Promise<string[]>;
88
}
99

1010
class Cache implements CacheService {
@@ -34,13 +34,13 @@ class Cache implements CacheService {
3434
return Promise.resolve();
3535
}
3636

37-
public async readBlackList(): Promise<string[]> {
37+
public async readBlockList(): Promise<string[]> {
3838
if (!this.client) {
3939
this.connect();
4040
}
4141

4242
try {
43-
const keyname = this.setName.blackList;
43+
const keyname = this.setName.blockList;
4444
const list = await this.client?.smembers(keyname);
4545
return list ?? [];
4646
} catch (error) {
@@ -67,7 +67,7 @@ class Cache implements CacheService {
6767

6868
private get setName(): Record<SetName, string> {
6969
return {
70-
blackList: 'set:blackList',
70+
blockList: 'set:blockList',
7171
};
7272
}
7373
}
@@ -87,4 +87,4 @@ type GenerateCacheKey = {
8787

8888
type QueueName = 'feed';
8989

90-
type SetName = 'blackList';
90+
type SetName = 'blockList';

src/graphql/post.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,7 @@ export const resolvers: IResolvers<any, ApolloContext> = {
822822
console.log(e);
823823
}
824824
}
825-
const isBlockList = await checkBlockList(ctx.user_id);
825+
const isBlockList = await checkBlockList(ctx.user_id, user?.username);
826826
if (isBlockList) {
827827
post.is_private = true;
828828
}
@@ -1116,7 +1116,7 @@ export const resolvers: IResolvers<any, ApolloContext> = {
11161116
}
11171117
post.url_slug = processedUrlSlug;
11181118

1119-
const isBlockList = await checkBlockList(ctx.user_id);
1119+
const isBlockList = await checkBlockList(ctx.user_id, user?.username);
11201120
if (isBlockList) {
11211121
post.is_private = true;
11221122
}

src/lib/checkBlockList.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import cache from '../cache';
22

3-
export async function checkBlockList(userId: string) {
3+
export async function checkBlockList(userId: string, username: string = '') {
44
const blockListFromEnv = (process.env.BLOCK_LIST ?? '').split(',');
5-
const blockListFromCache = await cache.readBlackList();
6-
const blockList = [...blockListFromEnv, ...blockListFromCache];
7-
8-
return blockList.includes(userId);
5+
const blockListFromCache = await cache.readBlockList();
6+
const isBlocked = blockListFromEnv.includes(userId) || blockListFromCache.includes(username);
7+
return isBlocked;
98
}

0 commit comments

Comments
 (0)