aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/activitypub/follow.ts
blob: f6e2a48fdec3e889bb04a9c72d832fbb110eef35 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { Transaction } from 'sequelize'
import { getServerActor } from '@server/models/application/application'
import { logger } from '../../helpers/logger'
import { CONFIG } from '../../initializers/config'
import { SERVER_ACTOR_NAME } from '../../initializers/constants'
import { ServerModel } from '../../models/server/server'
import { MActorFollowActors } from '../../types/models'
import { JobQueue } from '../job-queue'

async function autoFollowBackIfNeeded (actorFollow: MActorFollowActors, transaction?: Transaction) {
  if (!CONFIG.FOLLOWINGS.INSTANCE.AUTO_FOLLOW_BACK.ENABLED) return

  const follower = actorFollow.ActorFollower

  if (follower.type === 'Application' && follower.preferredUsername === SERVER_ACTOR_NAME) {
    logger.info('Auto follow back %s.', follower.url)

    const me = await getServerActor()

    const server = await ServerModel.load(follower.serverId, transaction)
    const host = server.host

    const payload = {
      host,
      name: SERVER_ACTOR_NAME,
      followerActorId: me.id,
      isAutoFollow: true
    }

    JobQueue.Instance.createJobAsync({ type: 'activitypub-follow', payload })
  }
}

// If we only have an host, use a default account handle
function getRemoteNameAndHost (handleOrHost: string) {
  let name = SERVER_ACTOR_NAME
  let host = handleOrHost

  const splitted = handleOrHost.split('@')
  if (splitted.length === 2) {
    name = splitted[0]
    host = splitted[1]
  }

  return { name, host }
}

export {
  autoFollowBackIfNeeded,
  getRemoteNameAndHost
}