Split types and typings
[oweals/peertube.git] / server / lib / activitypub / follow.ts
1 import { MActorFollowActors } from '../../types/models'
2 import { CONFIG } from '../../initializers/config'
3 import { SERVER_ACTOR_NAME } from '../../initializers/constants'
4 import { JobQueue } from '../job-queue'
5 import { logger } from '../../helpers/logger'
6 import { ServerModel } from '../../models/server/server'
7 import { getServerActor } from '@server/models/application/application'
8
9 async function autoFollowBackIfNeeded (actorFollow: MActorFollowActors) {
10   if (!CONFIG.FOLLOWINGS.INSTANCE.AUTO_FOLLOW_BACK.ENABLED) return
11
12   const follower = actorFollow.ActorFollower
13
14   if (follower.type === 'Application' && follower.preferredUsername === SERVER_ACTOR_NAME) {
15     logger.info('Auto follow back %s.', follower.url)
16
17     const me = await getServerActor()
18
19     const server = await ServerModel.load(follower.serverId)
20     const host = server.host
21
22     const payload = {
23       host,
24       name: SERVER_ACTOR_NAME,
25       followerActorId: me.id,
26       isAutoFollow: true
27     }
28
29     JobQueue.Instance.createJob({ type: 'activitypub-follow', payload })
30   }
31 }
32
33 export {
34   autoFollowBackIfNeeded
35 }