actor-follow-scheduler.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { isTestInstance } from '../../helpers/core-utils'
  2. import { logger } from '../../helpers/logger'
  3. import { ActorFollowModel } from '../../models/activitypub/actor-follow'
  4. import { AbstractScheduler } from './abstract-scheduler'
  5. import { ACTOR_FOLLOW_SCORE, SCHEDULER_INTERVALS_MS } from '../../initializers/constants'
  6. import { ActorFollowScoreCache } from '../files-cache'
  7. export class ActorFollowScheduler extends AbstractScheduler {
  8. private static instance: AbstractScheduler
  9. protected schedulerIntervalMs = SCHEDULER_INTERVALS_MS.actorFollowScores
  10. private constructor () {
  11. super()
  12. }
  13. protected async internalExecute () {
  14. await this.processPendingScores()
  15. await this.removeBadActorFollows()
  16. }
  17. private async processPendingScores () {
  18. const pendingScores = ActorFollowScoreCache.Instance.getPendingFollowsScore()
  19. const badServerIds = ActorFollowScoreCache.Instance.getBadFollowingServerIds()
  20. const goodServerIds = ActorFollowScoreCache.Instance.getGoodFollowingServerIds()
  21. ActorFollowScoreCache.Instance.clearPendingFollowsScore()
  22. ActorFollowScoreCache.Instance.clearBadFollowingServerIds()
  23. ActorFollowScoreCache.Instance.clearGoodFollowingServerIds()
  24. for (const inbox of Object.keys(pendingScores)) {
  25. await ActorFollowModel.updateScore(inbox, pendingScores[inbox])
  26. }
  27. await ActorFollowModel.updateScoreByFollowingServers(badServerIds, ACTOR_FOLLOW_SCORE.PENALTY)
  28. await ActorFollowModel.updateScoreByFollowingServers(goodServerIds, ACTOR_FOLLOW_SCORE.BONUS)
  29. }
  30. private async removeBadActorFollows () {
  31. if (!isTestInstance()) logger.info('Removing bad actor follows (scheduler).')
  32. try {
  33. await ActorFollowModel.removeBadActorFollows()
  34. } catch (err) {
  35. logger.error('Error in bad actor follows scheduler.', { err })
  36. }
  37. }
  38. static get Instance () {
  39. return this.instance || (this.instance = new this())
  40. }
  41. }