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 } = {}
10 private pendingBadServer = new Set<number>()
11 private pendingGoodServer = new Set<number>()
13 private constructor () {}
15 static get Instance () {
16 return this.instance || (this.instance = new this())
19 updateActorFollowsScore (goodInboxes: string[], badInboxes: string[]) {
20 if (goodInboxes.length === 0 && badInboxes.length === 0) return
22 logger.info('Updating %d good actor follows and %d bad actor follows scores in cache.', goodInboxes.length, badInboxes.length)
24 for (const goodInbox of goodInboxes) {
25 if (this.pendingFollowsScore[goodInbox] === undefined) this.pendingFollowsScore[goodInbox] = 0
27 this.pendingFollowsScore[goodInbox] += ACTOR_FOLLOW_SCORE.BONUS
30 for (const badInbox of badInboxes) {
31 if (this.pendingFollowsScore[badInbox] === undefined) this.pendingFollowsScore[badInbox] = 0
33 this.pendingFollowsScore[badInbox] += ACTOR_FOLLOW_SCORE.PENALTY
37 addBadServerId (serverId: number) {
38 this.pendingBadServer.add(serverId)
41 getBadFollowingServerIds () {
42 return Array.from(this.pendingBadServer)
45 clearBadFollowingServerIds () {
46 this.pendingBadServer = new Set<number>()
49 addGoodServerId (serverId: number) {
50 this.pendingGoodServer.add(serverId)
53 getGoodFollowingServerIds () {
54 return Array.from(this.pendingGoodServer)
57 clearGoodFollowingServerIds () {
58 this.pendingGoodServer = new Set<number>()
61 getPendingFollowsScore () {
62 return this.pendingFollowsScore
65 clearPendingFollowsScore () {
66 this.pendingFollowsScore = {}