+ @AfterCreate
+ @AfterUpdate
+ static incrementFollowerAndFollowingCount (instance: ActorFollowModel) {
+ if (instance.state !== 'accepted') return undefined
+
+ return Promise.all([
+ ActorModel.incrementFollows(instance.actorId, 'followingCount', 1),
+ ActorModel.incrementFollows(instance.targetActorId, 'followersCount', 1)
+ ])
+ }
+
+ @AfterDestroy
+ static decrementFollowerAndFollowingCount (instance: ActorFollowModel) {
+ return Promise.all([
+ ActorModel.incrementFollows(instance.actorId, 'followingCount',-1),
+ ActorModel.incrementFollows(instance.targetActorId, 'followersCount', -1)
+ ])
+ }
+
+ // Remove actor follows with a score of 0 (too many requests where they were unreachable)
+ static async removeBadActorFollows () {
+ const actorFollows = await ActorFollowModel.listBadActorFollows()
+
+ const actorFollowsRemovePromises = actorFollows.map(actorFollow => actorFollow.destroy())
+ await Promise.all(actorFollowsRemovePromises)
+
+ const numberOfActorFollowsRemoved = actorFollows.length
+
+ if (numberOfActorFollowsRemoved) logger.info('Removed bad %d actor follows.', numberOfActorFollowsRemoved)
+ }
+
+ static updateActorFollowsScoreAndRemoveBadOnes (goodInboxes: string[], badInboxes: string[], t: Sequelize.Transaction) {
+ if (goodInboxes.length === 0 && badInboxes.length === 0) return
+
+ logger.info('Updating %d good actor follows and %d bad actor follows scores.', goodInboxes.length, badInboxes.length)
+
+ if (goodInboxes.length !== 0) {
+ ActorFollowModel.incrementScores(goodInboxes, ACTOR_FOLLOW_SCORE.BONUS, t)
+ .catch(err => logger.error('Cannot increment scores of good actor follows.', err))
+ }
+
+ if (badInboxes.length !== 0) {
+ ActorFollowModel.incrementScores(badInboxes, ACTOR_FOLLOW_SCORE.PENALTY, t)
+ .catch(err => logger.error('Cannot decrement scores of bad actor follows.', err))
+ }
+ }
+