From 6f1b4fa417786c2015f16b435e872aa65378efd7 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 4 Sep 2019 11:18:33 +0200 Subject: Add auto follow instances index support --- server/models/activitypub/actor-follow.ts | 43 +++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) (limited to 'server/models') diff --git a/server/models/activitypub/actor-follow.ts b/server/models/activitypub/actor-follow.ts index c8b3aae9f..0833b9a93 100644 --- a/server/models/activitypub/actor-follow.ts +++ b/server/models/activitypub/actor-follow.ts @@ -1,5 +1,5 @@ import * as Bluebird from 'bluebird' -import { values } from 'lodash' +import { values, difference } from 'lodash' import { AfterCreate, AfterDestroy, @@ -21,7 +21,7 @@ import { FollowState } from '../../../shared/models/actors' import { ActorFollow } from '../../../shared/models/actors/follow.model' import { logger } from '../../helpers/logger' import { getServerActor } from '../../helpers/utils' -import { ACTOR_FOLLOW_SCORE, FOLLOW_STATES } from '../../initializers/constants' +import { ACTOR_FOLLOW_SCORE, FOLLOW_STATES, SERVER_ACTOR_NAME } from '../../initializers/constants' import { ServerModel } from '../server/server' import { createSafeIn, getSort } from '../utils' import { ActorModel, unusedActorAttributesForAPI } from './actor' @@ -435,6 +435,45 @@ export class ActorFollowModel extends Model { }) } + static async keepUnfollowedInstance (hosts: string[]) { + const followerId = (await getServerActor()).id + + const query = { + attributes: [], + where: { + actorId: followerId + }, + include: [ + { + attributes: [ ], + model: ActorModel.unscoped(), + required: true, + as: 'ActorFollowing', + where: { + preferredUsername: SERVER_ACTOR_NAME + }, + include: [ + { + attributes: [ 'host' ], + model: ServerModel.unscoped(), + required: true, + where: { + host: { + [Op.in]: hosts + } + } + } + ] + } + ] + } + + const res = await ActorFollowModel.findAll(query) + const followedHosts = res.map(res => res.ActorFollowing.Server.host) + + return difference(hosts, followedHosts) + } + static listAcceptedFollowerUrlsForAP (actorIds: number[], t: Transaction, start?: number, count?: number) { return ActorFollowModel.createListAcceptedFollowForApiQuery('followers', actorIds, t, start, count) } -- cgit v1.2.3