ニジカ投稿局 https://tv.nizika.tv
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

auto-follow-index-instances.ts 2.4 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { doJSONRequest } from '@server/helpers/requests.js'
  2. import { JobQueue } from '@server/lib/job-queue/index.js'
  3. import { ActorFollowModel } from '@server/models/actor/actor-follow.js'
  4. import { getServerActor } from '@server/models/application/application.js'
  5. import chunk from 'lodash-es/chunk.js'
  6. import { logger } from '../../helpers/logger.js'
  7. import { CONFIG } from '../../initializers/config.js'
  8. import { SCHEDULER_INTERVALS_MS, SERVER_ACTOR_NAME } from '../../initializers/constants.js'
  9. import { AbstractScheduler } from './abstract-scheduler.js'
  10. export class AutoFollowIndexInstances extends AbstractScheduler {
  11. private static instance: AbstractScheduler
  12. protected schedulerIntervalMs = SCHEDULER_INTERVALS_MS.AUTO_FOLLOW_INDEX_INSTANCES
  13. private lastCheck: Date
  14. private constructor () {
  15. super()
  16. }
  17. protected async internalExecute () {
  18. return this.autoFollow()
  19. }
  20. private async autoFollow () {
  21. if (CONFIG.FOLLOWINGS.INSTANCE.AUTO_FOLLOW_INDEX.ENABLED === false) return
  22. const indexUrl = CONFIG.FOLLOWINGS.INSTANCE.AUTO_FOLLOW_INDEX.INDEX_URL
  23. logger.info('Auto follow instances of index %s.', indexUrl)
  24. try {
  25. const serverActor = await getServerActor()
  26. const searchParams = { count: 1000 }
  27. if (this.lastCheck) Object.assign(searchParams, { since: this.lastCheck.toISOString() })
  28. this.lastCheck = new Date()
  29. const { body } = await doJSONRequest<any>(indexUrl, { searchParams })
  30. if (!body.data || Array.isArray(body.data) === false) {
  31. logger.error('Cannot auto follow instances of index %s. Please check the auto follow URL.', indexUrl, { body })
  32. return
  33. }
  34. const hosts: string[] = body.data.map(o => o.host)
  35. const chunks = chunk(hosts, 20)
  36. for (const chunk of chunks) {
  37. const unfollowedHosts = await ActorFollowModel.keepUnfollowedInstance(chunk)
  38. for (const unfollowedHost of unfollowedHosts) {
  39. const payload = {
  40. host: unfollowedHost,
  41. name: SERVER_ACTOR_NAME,
  42. followerActorId: serverActor.id,
  43. isAutoFollow: true
  44. }
  45. JobQueue.Instance.createJobAsync({ type: 'activitypub-follow', payload })
  46. }
  47. }
  48. } catch (err) {
  49. logger.error('Cannot auto follow hosts of index %s.', indexUrl, { err })
  50. }
  51. }
  52. static get Instance () {
  53. return this.instance || (this.instance = new this())
  54. }
  55. }