]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/follow.ts
Fix user subscription follows count
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / follow.ts
1 import { Transaction } from 'sequelize'
2 import { getServerActor } from '@server/models/application/application'
3 import { logger } from '../../helpers/logger'
4 import { CONFIG } from '../../initializers/config'
5 import { SERVER_ACTOR_NAME } from '../../initializers/constants'
6 import { ServerModel } from '../../models/server/server'
7 import { MActorFollowActors } from '../../types/models'
8 import { JobQueue } from '../job-queue'
9
10 async function autoFollowBackIfNeeded (actorFollow: MActorFollowActors, transaction?: Transaction) {
11 if (!CONFIG.FOLLOWINGS.INSTANCE.AUTO_FOLLOW_BACK.ENABLED) return
12
13 const follower = actorFollow.ActorFollower
14
15 if (follower.type === 'Application' && follower.preferredUsername === SERVER_ACTOR_NAME) {
16 logger.info('Auto follow back %s.', follower.url)
17
18 const me = await getServerActor()
19
20 const server = await ServerModel.load(follower.serverId, transaction)
21 const host = server.host
22
23 const payload = {
24 host,
25 name: SERVER_ACTOR_NAME,
26 followerActorId: me.id,
27 isAutoFollow: true
28 }
29
30 JobQueue.Instance.createJob({ type: 'activitypub-follow', payload })
31 }
32 }
33
34 // If we only have an host, use a default account handle
35 function getRemoteNameAndHost (handleOrHost: string) {
36 let name = SERVER_ACTOR_NAME
37 let host = handleOrHost
38
39 const splitted = handleOrHost.split('@')
40 if (splitted.length === 2) {
41 name = splitted[0]
42 host = splitted[1]
43 }
44
45 return { name, host }
46 }
47
48 export {
49 autoFollowBackIfNeeded,
50 getRemoteNameAndHost
51 }