2021-10-13 04:47:32 -05:00
|
|
|
import { ACTOR_FOLLOW_SCORE } from '../initializers/constants'
|
|
|
|
import { logger } from '../helpers/logger'
|
2018-12-20 07:31:11 -06:00
|
|
|
|
|
|
|
// Cache follows scores, instead of writing them too often in database
|
|
|
|
// Keep data in memory, we don't really need Redis here as we don't really care to loose some scores
|
2021-10-13 04:47:32 -05:00
|
|
|
class ActorFollowHealthCache {
|
|
|
|
|
|
|
|
private static instance: ActorFollowHealthCache
|
2018-12-20 07:31:11 -06:00
|
|
|
|
|
|
|
private pendingFollowsScore: { [ url: string ]: number } = {}
|
2021-10-13 04:47:32 -05:00
|
|
|
|
2019-08-06 10:19:53 -05:00
|
|
|
private pendingBadServer = new Set<number>()
|
|
|
|
private pendingGoodServer = new Set<number>()
|
2018-12-20 07:31:11 -06:00
|
|
|
|
2021-10-13 09:18:42 -05:00
|
|
|
private readonly badInboxes = new Set<string>()
|
2021-10-13 04:47:32 -05:00
|
|
|
|
2018-12-20 07:31:11 -06:00
|
|
|
private constructor () {}
|
|
|
|
|
|
|
|
static get Instance () {
|
|
|
|
return this.instance || (this.instance = new this())
|
|
|
|
}
|
|
|
|
|
2021-10-13 04:47:32 -05:00
|
|
|
updateActorFollowsHealth (goodInboxes: string[], badInboxes: string[]) {
|
|
|
|
this.badInboxes.clear()
|
|
|
|
|
2018-12-20 07:31:11 -06:00
|
|
|
if (goodInboxes.length === 0 && badInboxes.length === 0) return
|
|
|
|
|
2021-10-13 01:44:34 -05:00
|
|
|
logger.info(
|
|
|
|
'Updating %d good actor follows and %d bad actor follows scores in cache.',
|
|
|
|
goodInboxes.length, badInboxes.length, { badInboxes }
|
|
|
|
)
|
2018-12-20 07:31:11 -06:00
|
|
|
|
|
|
|
for (const goodInbox of goodInboxes) {
|
|
|
|
if (this.pendingFollowsScore[goodInbox] === undefined) this.pendingFollowsScore[goodInbox] = 0
|
|
|
|
|
|
|
|
this.pendingFollowsScore[goodInbox] += ACTOR_FOLLOW_SCORE.BONUS
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const badInbox of badInboxes) {
|
|
|
|
if (this.pendingFollowsScore[badInbox] === undefined) this.pendingFollowsScore[badInbox] = 0
|
|
|
|
|
|
|
|
this.pendingFollowsScore[badInbox] += ACTOR_FOLLOW_SCORE.PENALTY
|
2021-10-13 04:47:32 -05:00
|
|
|
this.badInboxes.add(badInbox)
|
2018-12-20 07:31:11 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-13 04:47:32 -05:00
|
|
|
isBadInbox (inboxUrl: string) {
|
|
|
|
return this.badInboxes.has(inboxUrl)
|
|
|
|
}
|
|
|
|
|
2019-08-06 10:19:53 -05:00
|
|
|
addBadServerId (serverId: number) {
|
|
|
|
this.pendingBadServer.add(serverId)
|
|
|
|
}
|
|
|
|
|
|
|
|
getBadFollowingServerIds () {
|
|
|
|
return Array.from(this.pendingBadServer)
|
|
|
|
}
|
|
|
|
|
|
|
|
clearBadFollowingServerIds () {
|
|
|
|
this.pendingBadServer = new Set<number>()
|
|
|
|
}
|
|
|
|
|
|
|
|
addGoodServerId (serverId: number) {
|
|
|
|
this.pendingGoodServer.add(serverId)
|
|
|
|
}
|
|
|
|
|
|
|
|
getGoodFollowingServerIds () {
|
|
|
|
return Array.from(this.pendingGoodServer)
|
|
|
|
}
|
|
|
|
|
|
|
|
clearGoodFollowingServerIds () {
|
|
|
|
this.pendingGoodServer = new Set<number>()
|
|
|
|
}
|
|
|
|
|
|
|
|
getPendingFollowsScore () {
|
2018-12-20 07:31:11 -06:00
|
|
|
return this.pendingFollowsScore
|
|
|
|
}
|
|
|
|
|
|
|
|
clearPendingFollowsScore () {
|
|
|
|
this.pendingFollowsScore = {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
2021-10-13 04:47:32 -05:00
|
|
|
ActorFollowHealthCache
|
2018-12-20 07:31:11 -06:00
|
|
|
}
|