]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/files-cache/actor-follow-score-cache.ts
Don't expose constants directly in initializers/
[github/Chocobozzz/PeerTube.git] / server / lib / files-cache / actor-follow-score-cache.ts
1 import { ACTOR_FOLLOW_SCORE } from '../../initializers/constants'
2 import { logger } from '../../helpers/logger'
3
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 {
7
8 private static instance: ActorFollowScoreCache
9 private pendingFollowsScore: { [ url: string ]: number } = {}
10
11 private constructor () {}
12
13 static get Instance () {
14 return this.instance || (this.instance = new this())
15 }
16
17 updateActorFollowsScore (goodInboxes: string[], badInboxes: string[]) {
18 if (goodInboxes.length === 0 && badInboxes.length === 0) return
19
20 logger.info('Updating %d good actor follows and %d bad actor follows scores in cache.', goodInboxes.length, badInboxes.length)
21
22 for (const goodInbox of goodInboxes) {
23 if (this.pendingFollowsScore[goodInbox] === undefined) this.pendingFollowsScore[goodInbox] = 0
24
25 this.pendingFollowsScore[goodInbox] += ACTOR_FOLLOW_SCORE.BONUS
26 }
27
28 for (const badInbox of badInboxes) {
29 if (this.pendingFollowsScore[badInbox] === undefined) this.pendingFollowsScore[badInbox] = 0
30
31 this.pendingFollowsScore[badInbox] += ACTOR_FOLLOW_SCORE.PENALTY
32 }
33 }
34
35 getPendingFollowsScoreCopy () {
36 return this.pendingFollowsScore
37 }
38
39 clearPendingFollowsScore () {
40 this.pendingFollowsScore = {}
41 }
42 }
43
44 export {
45 ActorFollowScoreCache
46 }