2019-09-04 05:18:33 -04:00
|
|
|
import { chunk } from 'lodash'
|
2021-03-08 08:24:11 -05:00
|
|
|
import { doJSONRequest } from '@server/helpers/requests'
|
2019-09-04 05:18:33 -04:00
|
|
|
import { JobQueue } from '@server/lib/job-queue'
|
2021-05-11 05:15:29 -04:00
|
|
|
import { ActorFollowModel } from '@server/models/actor/actor-follow'
|
2020-04-23 03:32:53 -04:00
|
|
|
import { getServerActor } from '@server/models/application/application'
|
2020-05-12 03:37:39 -04:00
|
|
|
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'
|
2019-09-04 05:18:33 -04:00
|
|
|
|
|
|
|
export class AutoFollowIndexInstances extends AbstractScheduler {
|
|
|
|
|
|
|
|
private static instance: AbstractScheduler
|
|
|
|
|
|
|
|
protected schedulerIntervalMs = SCHEDULER_INTERVALS_MS.autoFollowIndexInstances
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
2021-03-08 08:24:11 -05:00
|
|
|
const searchParams = { count: 1000 }
|
|
|
|
if (this.lastCheck) Object.assign(searchParams, { since: this.lastCheck.toISOString() })
|
2019-11-04 09:20:34 -05:00
|
|
|
|
2019-09-04 05:18:33 -04:00
|
|
|
this.lastCheck = new Date()
|
|
|
|
|
2021-03-08 08:24:11 -05:00
|
|
|
const { body } = await doJSONRequest<any>(indexUrl, { searchParams })
|
2020-05-11 04:48:58 -04:00
|
|
|
if (!body.data || Array.isArray(body.data) === false) {
|
2020-05-12 03:37:39 -04:00
|
|
|
logger.error('Cannot auto follow instances of index %s. Please check the auto follow URL.', indexUrl, { body })
|
2020-05-11 04:48:58 -04:00
|
|
|
return
|
|
|
|
}
|
2019-09-04 05:18:33 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-01-31 10:56:52 -05:00
|
|
|
JobQueue.Instance.createJob({ type: 'activitypub-follow', payload })
|
2019-09-04 05:18:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
logger.error('Cannot auto follow hosts of index %s.', indexUrl, { err })
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static get Instance () {
|
|
|
|
return this.instance || (this.instance = new this())
|
|
|
|
}
|
|
|
|
}
|