1 import { logger } from '../../helpers/logger'
2 import { AbstractScheduler } from './abstract-scheduler'
3 import { INSTANCES_INDEX, SCHEDULER_INTERVALS_MS, SERVER_ACTOR_NAME } from '../../initializers/constants'
4 import { CONFIG } from '../../initializers/config'
5 import { chunk } from 'lodash'
6 import { doRequest } from '@server/helpers/requests'
7 import { ActorFollowModel } from '@server/models/activitypub/actor-follow'
8 import { JobQueue } from '@server/lib/job-queue'
9 import { getServerActor } from '@server/helpers/utils'
11 export class AutoFollowIndexInstances extends AbstractScheduler {
13 private static instance: AbstractScheduler
15 protected schedulerIntervalMs = SCHEDULER_INTERVALS_MS.autoFollowIndexInstances
17 private lastCheck: Date
19 private constructor () {
23 protected async internalExecute () {
24 return this.autoFollow()
27 private async autoFollow () {
28 if (CONFIG.FOLLOWINGS.INSTANCE.AUTO_FOLLOW_INDEX.ENABLED === false) return
30 const indexUrl = CONFIG.FOLLOWINGS.INSTANCE.AUTO_FOLLOW_INDEX.INDEX_URL
32 logger.info('Auto follow instances of index %s.', indexUrl)
35 const serverActor = await getServerActor()
37 const uri = indexUrl + INSTANCES_INDEX.HOSTS_PATH
39 const qs = { count: 1000 }
40 if (this.lastCheck) Object.assign(qs, { since: this.lastCheck.toISOString() })
42 this.lastCheck = new Date()
44 const { body } = await doRequest<any>({ uri, qs, json: true })
46 const hosts: string[] = body.data.map(o => o.host)
47 const chunks = chunk(hosts, 20)
49 for (const chunk of chunks) {
50 const unfollowedHosts = await ActorFollowModel.keepUnfollowedInstance(chunk)
52 for (const unfollowedHost of unfollowedHosts) {
55 name: SERVER_ACTOR_NAME,
56 followerActorId: serverActor.id,
60 JobQueue.Instance.createJob({ type: 'activitypub-follow', payload })
65 logger.error('Cannot auto follow hosts of index %s.', indexUrl, { err })
70 static get Instance () {
71 return this.instance || (this.instance = new this())