aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/schedulers/auto-follow-index-instances.ts
blob: 956ece749b8d54d87af690b0dbce53bd15046035 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { chunk } from 'lodash'
import { doJSONRequest } from '@server/helpers/requests'
import { JobQueue } from '@server/lib/job-queue'
import { ActorFollowModel } from '@server/models/actor/actor-follow'
import { getServerActor } from '@server/models/application/application'
import { logger } from '../../helpers/logger'
import { CONFIG } from '../../initializers/config'
import { SCHEDULER_INTERVALS_MS, SERVER_ACTOR_NAME } from '../../initializers/constants'
import { AbstractScheduler } from './abstract-scheduler'

export class AutoFollowIndexInstances extends AbstractScheduler {

  private static instance: AbstractScheduler

  protected schedulerIntervalMs = SCHEDULER_INTERVALS_MS.AUTO_FOLLOW_INDEX_INSTANCES

  private lastCheck: Date

  private constructor () {
    super()
  }

  protected async internalExecute () {
    return this.autoFollow()
  }

  private async autoFollow () {
    if (CONFIG.FOLLOWINGS.INSTANCE.AUTO_FOLLOW_INDEX.ENABLED === false) return

    const indexUrl = CONFIG.FOLLOWINGS.INSTANCE.AUTO_FOLLOW_INDEX.INDEX_URL

    logger.info('Auto follow instances of index %s.', indexUrl)

    try {
      const serverActor = await getServerActor()

      const searchParams = { count: 1000 }
      if (this.lastCheck) Object.assign(searchParams, { since: this.lastCheck.toISOString() })

      this.lastCheck = new Date()

      const { body } = await doJSONRequest<any>(indexUrl, { searchParams })
      if (!body.data || Array.isArray(body.data) === false) {
        logger.error('Cannot auto follow instances of index %s. Please check the auto follow URL.', indexUrl, { body })
        return
      }

      const hosts: string[] = body.data.map(o => o.host)
      const chunks = chunk(hosts, 20)

      for (const chunk of chunks) {
        const unfollowedHosts = await ActorFollowModel.keepUnfollowedInstance(chunk)

        for (const unfollowedHost of unfollowedHosts) {
          const payload = {
            host: unfollowedHost,
            name: SERVER_ACTOR_NAME,
            followerActorId: serverActor.id,
            isAutoFollow: true
          }

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

    } catch (err) {
      logger.error('Cannot auto follow hosts of index %s.', indexUrl, { err })
    }

  }

  static get Instance () {
    return this.instance || (this.instance = new this())
  }
}