unoffical wafrn mirror
wafrn.net
atproto
social-network
activitypub
1import { Queue } from 'bullmq'
2import { User } from '../../models/index.js'
3import { redisCache } from '../redis.js'
4import { getUserIdFromRemoteId } from './getUserIdFromRemoteId.js'
5import { completeEnvironment } from '../backendOptions.js'
6
7const queue = new Queue('getRemoteActorId', {
8 connection: completeEnvironment.bullmqConnection,
9 defaultJobOptions: {
10 removeOnComplete: true,
11 removeOnFail: true,
12 attempts: 2,
13 backoff: {
14 type: 'exponential',
15 delay: 1000
16 }
17 }
18})
19
20async function getKey(remoteUserUrl: string, adminUser: any): Promise<{ user?: User; key?: string }> {
21 const cachedKey = await redisCache.get('key:' + remoteUserUrl)
22 let user
23 let remoteKey = cachedKey || undefined //if petition from neew user we need to get the key first
24 if (!remoteKey) {
25 const userId = await getUserIdFromRemoteId(remoteUserUrl)
26 if (userId && userId !== '') {
27 user = (await User.findByPk(userId)) || undefined
28 remoteKey = user?.publicKey ?? ''
29 } else {
30 await queue.add(
31 'getRemoteActorId',
32 { actorUrl: remoteUserUrl, userId: adminUser.id, forceUpdate: true },
33 {
34 jobId: remoteUserUrl.replaceAll(':', '_').replaceAll('/', '_')
35 }
36 )
37 return {}
38 }
39 }
40 if (!cachedKey && remoteKey) {
41 // we set the key valid for 5 minutes
42 redisCache.set('key:' + remoteUserUrl, remoteKey, 'EX', 3600)
43 }
44 return { user: user, key: remoteKey }
45}
46
47export { getKey }