]>
Commit | Line | Data |
---|---|---|
74dc3bca | 1 | import { ACTOR_FOLLOW_SCORE } from '../../initializers/constants' |
2f5c6b2f C |
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 } = {} | |
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 | ||
22 | logger.info('Updating %d good actor follows and %d bad actor follows scores in cache.', goodInboxes.length, badInboxes.length) | |
23 | ||
24 | for (const goodInbox of goodInboxes) { | |
25 | if (this.pendingFollowsScore[goodInbox] === undefined) this.pendingFollowsScore[goodInbox] = 0 | |
26 | ||
27 | this.pendingFollowsScore[goodInbox] += ACTOR_FOLLOW_SCORE.BONUS | |
28 | } | |
29 | ||
30 | for (const badInbox of badInboxes) { | |
31 | if (this.pendingFollowsScore[badInbox] === undefined) this.pendingFollowsScore[badInbox] = 0 | |
32 | ||
33 | this.pendingFollowsScore[badInbox] += ACTOR_FOLLOW_SCORE.PENALTY | |
34 | } | |
35 | } | |
36 | ||
6b9c966f C |
37 | addBadServerId (serverId: number) { |
38 | this.pendingBadServer.add(serverId) | |
39 | } | |
40 | ||
41 | getBadFollowingServerIds () { | |
42 | return Array.from(this.pendingBadServer) | |
43 | } | |
44 | ||
45 | clearBadFollowingServerIds () { | |
46 | this.pendingBadServer = new Set<number>() | |
47 | } | |
48 | ||
49 | addGoodServerId (serverId: number) { | |
50 | this.pendingGoodServer.add(serverId) | |
51 | } | |
52 | ||
53 | getGoodFollowingServerIds () { | |
54 | return Array.from(this.pendingGoodServer) | |
55 | } | |
56 | ||
57 | clearGoodFollowingServerIds () { | |
58 | this.pendingGoodServer = new Set<number>() | |
59 | } | |
60 | ||
61 | getPendingFollowsScore () { | |
2f5c6b2f C |
62 | return this.pendingFollowsScore |
63 | } | |
64 | ||
65 | clearPendingFollowsScore () { | |
66 | this.pendingFollowsScore = {} | |
67 | } | |
68 | } | |
69 | ||
70 | export { | |
71 | ActorFollowScoreCache | |
72 | } |