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