From 6f1b4fa417786c2015f16b435e872aa65378efd7 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 4 Sep 2019 11:18:33 +0200 Subject: Add auto follow instances index support --- .../lib/schedulers/auto-follow-index-instances.ts | 72 ++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 server/lib/schedulers/auto-follow-index-instances.ts (limited to 'server/lib/schedulers') diff --git a/server/lib/schedulers/auto-follow-index-instances.ts b/server/lib/schedulers/auto-follow-index-instances.ts new file mode 100644 index 000000000..ef11fc87f --- /dev/null +++ b/server/lib/schedulers/auto-follow-index-instances.ts @@ -0,0 +1,72 @@ +import { logger } from '../../helpers/logger' +import { AbstractScheduler } from './abstract-scheduler' +import { INSTANCES_INDEX, SCHEDULER_INTERVALS_MS, SERVER_ACTOR_NAME } from '../../initializers/constants' +import { CONFIG } from '../../initializers/config' +import { chunk } from 'lodash' +import { doRequest } from '@server/helpers/requests' +import { ActorFollowModel } from '@server/models/activitypub/actor-follow' +import { JobQueue } from '@server/lib/job-queue' +import { getServerActor } from '@server/helpers/utils' + +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() + + const uri = indexUrl + INSTANCES_INDEX.HOSTS_PATH + + const qs = this.lastCheck ? { since: this.lastCheck.toISOString() } : {} + this.lastCheck = new Date() + + const { body } = await doRequest({ uri, qs, json: true }) + + 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 + } + + await JobQueue.Instance.createJob({ type: 'activitypub-follow', payload }) + .catch(err => logger.error('Cannot create follow job for %s.', unfollowedHost, err)) + } + } + + } catch (err) { + logger.error('Cannot auto follow hosts of index %s.', indexUrl, { err }) + } + + } + + static get Instance () { + return this.instance || (this.instance = new this()) + } +} -- cgit v1.2.3