PeerTube/server/lib/schedulers/auto-follow-index-instances.ts

76 lines
2.3 KiB
TypeScript
Raw Normal View History

import { chunk } from 'lodash'
import { doRequest } from '@server/helpers/requests'
import { JobQueue } from '@server/lib/job-queue'
2020-05-12 09:37:39 +02:00
import { ActorFollowModel } from '@server/models/activitypub/actor-follow'
2020-04-23 09:32:53 +02:00
import { getServerActor } from '@server/models/application/application'
2020-05-12 09:37:39 +02: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'
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()
2019-11-04 15:20:34 +01:00
const qs = { count: 1000 }
if (this.lastCheck) Object.assign(qs, { since: this.lastCheck.toISOString() })
this.lastCheck = new Date()
2020-05-12 09:37:39 +02:00
const { body } = await doRequest<any>({ uri: indexUrl, qs, json: true })
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
}
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 16:56:52 +01:00
JobQueue.Instance.createJob({ 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())
}
}