unoffical wafrn mirror
wafrn.net
atproto
social-network
activitypub
1import { Op } from 'sequelize'
2import { Blocks, Follows, User } from '../../models/index.js'
3import getBlockedIds from './getBlockedIds.js'
4import { redisCache } from '../redis.js'
5
6export default async function getFollowedsIds(
7 userId: string,
8 local = false,
9 options = { getFollowersInstead: false }
10): Promise<string[]> {
11 const baseCacheNameString = !options.getFollowersInstead ? 'follows' : 'followers'
12 const cacheResult = await redisCache.get(
13 local ? baseCacheNameString + ':local:' + userId : baseCacheNameString + ':full:' + userId
14 )
15 if (cacheResult) {
16 return JSON.parse(cacheResult)
17 }
18 try {
19 const usersWithBlocks = await getBlockedIds(userId)
20 const whereObject: any = options.getFollowersInstead
21 ? {
22 followedId: userId,
23 accepted: true
24 }
25 : {
26 followerId: userId,
27 accepted: true,
28 followedId: {
29 [Op.notIn]: usersWithBlocks
30 }
31 }
32 const followed = await Follows.findAll({
33 include: [
34 {
35 model: User,
36 as: 'follower',
37 attributes: ['url'],
38 where: {
39 banned: {
40 [Op.ne]: true
41 }
42 }
43 }
44 ],
45 where: whereObject
46 })
47 let result = followed.map((followed: any) =>
48 options.getFollowersInstead ? followed.followerId : followed.followedId
49 )
50 if (!options.getFollowersInstead) {
51 result.push(userId)
52 }
53 // TODO this is sub optimal. I mean we do two queries instead of one EVERY 10 MINUTES OH MY GOD
54 // obviously is not the end of the world. but its still suboptimal
55 if (local) {
56 const localUsers = await User.findAll({
57 where: {
58 id: {
59 [Op.in]: result
60 },
61 email: {
62 [Op.ne]: undefined
63 }
64 }
65 })
66 result = localUsers.map((usr) => usr.id)
67 }
68 redisCache.set(
69 local ? baseCacheNameString + ':local:' + userId : baseCacheNameString + ':full:' + userId,
70 JSON.stringify(result),
71 'EX',
72 60
73 )
74 return result as string[]
75 } catch (error) {
76 return []
77 }
78}