]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/files-cache/actor-follow-score-cache.ts
Fix check after init script
[github/Chocobozzz/PeerTube.git] / server / lib / files-cache / actor-follow-score-cache.ts
CommitLineData
74dc3bca 1import { ACTOR_FOLLOW_SCORE } from '../../initializers/constants'
2f5c6b2f
C
2import { 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
6class ActorFollowScoreCache {
7
8 private static instance: ActorFollowScoreCache
9 private pendingFollowsScore: { [ url: string ]: number } = {}
6b9c966f
C
10 private pendingBadServer = new Set<number>()
11 private pendingGoodServer = new Set<number>()
2f5c6b2f
C
12
13 private constructor () {}
14
15 static get Instance () {
16 return this.instance || (this.instance = new this())
17 }
18
19 updateActorFollowsScore (goodInboxes: string[], badInboxes: string[]) {
20 if (goodInboxes.length === 0 && badInboxes.length === 0) return
21
d41a3805
C
22 logger.info(
23 'Updating %d good actor follows and %d bad actor follows scores in cache.',
24 goodInboxes.length, badInboxes.length, { badInboxes }
25 )
2f5c6b2f
C
26
27 for (const goodInbox of goodInboxes) {
28 if (this.pendingFollowsScore[goodInbox] === undefined) this.pendingFollowsScore[goodInbox] = 0
29
30 this.pendingFollowsScore[goodInbox] += ACTOR_FOLLOW_SCORE.BONUS
31 }
32
33 for (const badInbox of badInboxes) {
34 if (this.pendingFollowsScore[badInbox] === undefined) this.pendingFollowsScore[badInbox] = 0
35
36 this.pendingFollowsScore[badInbox] += ACTOR_FOLLOW_SCORE.PENALTY
37 }
38 }
39
6b9c966f
C
40 addBadServerId (serverId: number) {
41 this.pendingBadServer.add(serverId)
42 }
43
44 getBadFollowingServerIds () {
45 return Array.from(this.pendingBadServer)
46 }
47
48 clearBadFollowingServerIds () {
49 this.pendingBadServer = new Set<number>()
50 }
51
52 addGoodServerId (serverId: number) {
53 this.pendingGoodServer.add(serverId)
54 }
55
56 getGoodFollowingServerIds () {
57 return Array.from(this.pendingGoodServer)
58 }
59
60 clearGoodFollowingServerIds () {
61 this.pendingGoodServer = new Set<number>()
62 }
63
64 getPendingFollowsScore () {
2f5c6b2f
C
65 return this.pendingFollowsScore
66 }
67
68 clearPendingFollowsScore () {
69 this.pendingFollowsScore = {}
70 }
71}
72
73export {
74 ActorFollowScoreCache
75}