actor-follow-score-cache.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { ACTOR_FOLLOW_SCORE } from '../../initializers/constants'
  2. import { logger } from '../../helpers/logger'
  3. // Cache follows scores, instead of writing them too often in database
  4. // Keep data in memory, we don't really need Redis here as we don't really care to loose some scores
  5. class ActorFollowScoreCache {
  6. private static instance: ActorFollowScoreCache
  7. private pendingFollowsScore: { [ url: string ]: number } = {}
  8. private constructor () {}
  9. static get Instance () {
  10. return this.instance || (this.instance = new this())
  11. }
  12. updateActorFollowsScore (goodInboxes: string[], badInboxes: string[]) {
  13. if (goodInboxes.length === 0 && badInboxes.length === 0) return
  14. logger.info('Updating %d good actor follows and %d bad actor follows scores in cache.', goodInboxes.length, badInboxes.length)
  15. for (const goodInbox of goodInboxes) {
  16. if (this.pendingFollowsScore[goodInbox] === undefined) this.pendingFollowsScore[goodInbox] = 0
  17. this.pendingFollowsScore[goodInbox] += ACTOR_FOLLOW_SCORE.BONUS
  18. }
  19. for (const badInbox of badInboxes) {
  20. if (this.pendingFollowsScore[badInbox] === undefined) this.pendingFollowsScore[badInbox] = 0
  21. this.pendingFollowsScore[badInbox] += ACTOR_FOLLOW_SCORE.PENALTY
  22. }
  23. }
  24. getPendingFollowsScoreCopy () {
  25. return this.pendingFollowsScore
  26. }
  27. clearPendingFollowsScore () {
  28. this.pendingFollowsScore = {}
  29. }
  30. }
  31. export {
  32. ActorFollowScoreCache
  33. }