2023-07-31 14:34:36 +02:00
|
|
|
import { doJSONRequest } from '@server/helpers/requests.js'
|
|
|
|
import { JobQueue } from '@server/lib/job-queue/index.js'
|
|
|
|
import { ActorFollowModel } from '@server/models/actor/actor-follow.js'
|
|
|
|
import { getServerActor } from '@server/models/application/application.js'
|
|
|
|
import chunk from 'lodash-es/chunk.js'
|
|
|
|
import { logger } from '../../helpers/logger.js'
|
|
|
|
import { CONFIG } from '../../initializers/config.js'
|
|
|
|
import { SCHEDULER_INTERVALS_MS, SERVER_ACTOR_NAME } from '../../initializers/constants.js'
|
|
|
|
import { AbstractScheduler } from './abstract-scheduler.js'
|
2019-09-04 11:18:33 +02:00
|
|
|
|
|
|
|
export class AutoFollowIndexInstances extends AbstractScheduler {
|
|
|
|
|
|
|
|
private static instance: AbstractScheduler
|
|
|
|
|
2021-10-22 10:28:00 +02:00
|
|
|
protected schedulerIntervalMs = SCHEDULER_INTERVALS_MS.AUTO_FOLLOW_INDEX_INSTANCES
|
2019-09-04 11:18:33 +02:00
|
|
|
|
|
|
|
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 14:24:11 +01:00
|
|
|
const searchParams = { count: 1000 }
|
|
|
|
if (this.lastCheck) Object.assign(searchParams, { since: this.lastCheck.toISOString() })
|
2019-11-04 15:20:34 +01:00
|
|
|
|
2019-09-04 11:18:33 +02:00
|
|
|
this.lastCheck = new Date()
|
|
|
|
|
2024-08-14 15:32:25 +02:00
|
|
|
const { body } = await doJSONRequest<any>(indexUrl, { searchParams, preventSSRF: false })
|
2020-05-11 10:48:58 +02:00
|
|
|
if (!body.data || Array.isArray(body.data) === false) {
|
2020-05-12 09:37:39 +02:00
|
|
|
logger.error('Cannot auto follow instances of index %s. Please check the auto follow URL.', indexUrl, { body })
|
2020-05-11 10:48:58 +02:00
|
|
|
return
|
|
|
|
}
|
2019-09-04 11:18:33 +02: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
|
|
|
|
}
|
|
|
|
|
2022-08-08 15:48:17 +02:00
|
|
|
JobQueue.Instance.createJobAsync({ type: 'activitypub-follow', payload })
|
2019-09-04 11:18:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
logger.error('Cannot auto follow hosts of index %s.', indexUrl, { err })
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static get Instance () {
|
|
|
|
return this.instance || (this.instance = new this())
|
|
|
|
}
|
|
|
|
}
|