From 6b9c966f6428c9e47bead3410a0401e8ebd744bf Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 6 Aug 2019 17:19:53 +0200 Subject: Automatically remove bad followings --- server/models/activitypub/actor-follow.ts | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) (limited to 'server/models/activitypub') diff --git a/server/models/activitypub/actor-follow.ts b/server/models/activitypub/actor-follow.ts index 3039b90c7..99a5fd117 100644 --- a/server/models/activitypub/actor-follow.ts +++ b/server/models/activitypub/actor-follow.ts @@ -23,7 +23,7 @@ import { logger } from '../../helpers/logger' import { getServerActor } from '../../helpers/utils' import { ACTOR_FOLLOW_SCORE, FOLLOW_STATES } from '../../initializers/constants' import { ServerModel } from '../server/server' -import { getSort } from '../utils' +import { createSafeIn, getSort } from '../utils' import { ActorModel, unusedActorAttributesForAPI } from './actor' import { VideoChannelModel } from '../video/video-channel' import { AccountModel } from '../account/account' @@ -464,7 +464,7 @@ export class ActorFollowModel extends Model { } } - static updateFollowScore (inboxUrl: string, value: number, t?: Transaction) { + static updateScore (inboxUrl: string, value: number, t?: Transaction) { const query = `UPDATE "actorFollow" SET "score" = LEAST("score" + ${value}, ${ACTOR_FOLLOW_SCORE.MAX}) ` + 'WHERE id IN (' + 'SELECT "actorFollow"."id" FROM "actorFollow" ' + @@ -480,6 +480,28 @@ export class ActorFollowModel extends Model { return ActorFollowModel.sequelize.query(query, options) } + static async updateScoreByFollowingServers (serverIds: number[], value: number, t?: Transaction) { + if (serverIds.length === 0) return + + const me = await getServerActor() + const serverIdsString = createSafeIn(ActorFollowModel, serverIds) + + const query = `UPDATE "actorFollow" SET "score" = "score" + ${value} ` + + 'WHERE id IN (' + + 'SELECT "actorFollow"."id" FROM "actorFollow" ' + + 'INNER JOIN "actor" ON "actor"."id" = "actorFollow"."targetActorId" ' + + `WHERE "actorFollow"."actorId" = ${me.Account.actorId} ` + // I'm the follower + `AND "actor"."serverId" IN (${serverIdsString})` + // Criteria on followings + ')' + + const options = { + type: QueryTypes.BULKUPDATE, + transaction: t + } + + return ActorFollowModel.sequelize.query(query, options) + } + private static async createListAcceptedFollowForApiQuery ( type: 'followers' | 'following', actorIds: number[], -- cgit v1.2.3