unoffical wafrn mirror
wafrn.net
atproto
social-network
activitypub
1import { Op } from 'sequelize'
2import { Blocks, Mutes, User } from '../../models/index.js'
3import { redisCache } from '../redis.js'
4import { logger } from '../logger.js'
5
6export default async function getBlockedIds(
7 userId: string,
8 includeMutes = true,
9 onlyUserBlocks = false
10): Promise<string[]> {
11 const cacheKey = 'blocks:' + includeMutes ? 'mutes:' : '' + onlyUserBlocks ? 'onlyUser:' : ''
12 try {
13 const cacheResult = await redisCache.get(cacheKey + userId)
14 if (cacheResult) {
15 return JSON.parse(cacheResult)
16 }
17 const blocks = Blocks.findAll({
18 where: {
19 [Op.or]: [
20 {
21 blockerId: userId
22 },
23 // if only user blocks we ask twice for the users that only the user has blocked
24 onlyUserBlocks
25 ? {
26 blockerId: userId
27 }
28 : {
29 blockedId: userId
30 }
31 ]
32 }
33 })
34 const mutes = includeMutes
35 ? Mutes.findAll({
36 where: {
37 muterId: userId
38 }
39 })
40 : []
41 await Promise.all([blocks, mutes])
42 const res = (await blocks)
43 .map((block: any) => (block.blockerId !== userId ? block.blockerId : block.blockedId))
44 .concat((await mutes).map((mute: any) => mute.mutedId))
45 redisCache.set(cacheKey + userId, JSON.stringify(res), 'EX', 600)
46 // to avoid sequelize stuff. should add to other cachers too tbh
47 return res.length > 0 ? res : ['00000000-0000-0000-0000-000000000000']
48 } catch (error) {
49 logger.error(error)
50 return ['00000000-0000-0000-0000-000000000000']
51 }
52}