X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Factor%2Factor-follow.ts;h=566bb5f319b0b45a17d081d34aa7251360d73312;hb=927fa4b11f692174d6296aa096d7a74bacdeea8b;hp=0f4d3c0a6085cc314392c7d4c6ec6373984a56f5;hpb=ab4b8974997777373a6032073f9c1aaf33ba9931;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/actor/actor-follow.ts b/server/models/actor/actor-follow.ts index 0f4d3c0a6..566bb5f31 100644 --- a/server/models/actor/actor-follow.ts +++ b/server/models/actor/actor-follow.ts @@ -1,5 +1,5 @@ import { difference, values } from 'lodash' -import { Includeable, IncludeOptions, literal, Op, QueryTypes, Transaction, WhereOptions } from 'sequelize' +import { Attributes, FindOptions, Includeable, IncludeOptions, Op, QueryTypes, Transaction, WhereAttributeHash } from 'sequelize' import { AfterCreate, AfterDestroy, @@ -30,7 +30,6 @@ import { MActorFollowFormattable, MActorFollowSubscriptions } from '@server/types/models' -import { ActivityPubActorType } from '@shared/models' import { AttributesOnly } from '@shared/typescript-utils' import { FollowState } from '../../../shared/models/actors' import { ActorFollow } from '../../../shared/models/actors/follow.model' @@ -39,9 +38,11 @@ import { ACTOR_FOLLOW_SCORE, CONSTRAINTS_FIELDS, FOLLOW_STATES, SERVER_ACTOR_NAM import { AccountModel } from '../account/account' import { ServerModel } from '../server/server' import { doesExist } from '../shared/query' -import { createSafeIn, getFollowsSort, getSort, searchAttribute, throwIfNotValid } from '../utils' +import { createSafeIn, getSort, searchAttribute, throwIfNotValid } from '../utils' import { VideoChannelModel } from '../video/video-channel' import { ActorModel, unusedActorAttributesForAPI } from './actor' +import { InstanceListFollowersQueryBuilder, ListFollowersOptions } from './sql/instance-list-followers-query-builder' +import { InstanceListFollowingQueryBuilder, ListFollowingOptions } from './sql/instance-list-following-query-builder' @Table({ tableName: 'actorFollow', @@ -208,7 +209,9 @@ export class ActorFollowModel extends Model { + static loadByActorAndTargetNameAndHostForAPI (options: { + actorId: number + targetName: string + targetHost: string + state?: FollowState + transaction?: Transaction + }): Promise { + const { actorId, targetHost, targetName, state, transaction } = options + const actorFollowingPartInclude: IncludeOptions = { model: ActorModel, required: true, @@ -270,10 +276,11 @@ export class ActorFollowModel extends Model> = { actorId} + if (state) where.state = state + + const query: FindOptions> = { + where, include: [ actorFollowingPartInclude, { @@ -282,7 +289,7 @@ export class ActorFollowModel extends Model { - const actorModel = forCount - ? ActorModel.unscoped() - : ActorModel - - return { - distinct: true, - offset: start, - limit: count, - order: getFollowsSort(sort), - where: followWhere, - include: [ - { - model: actorModel, - required: true, - as: 'ActorFollower', - where: { - id - } - }, - { - model: actorModel, - as: 'ActorFollowing', - required: true, - where: followingWhere, - include: [ - { - model: ServerModel, - required: true - } - ] - } - ] - } - } - + static listInstanceFollowingForApi (options: ListFollowingOptions) { return Promise.all([ - ActorFollowModel.count(getQuery(true)), - ActorFollowModel.findAll(getQuery(false)) + new InstanceListFollowingQueryBuilder(this.sequelize, options).countFollowing(), + new InstanceListFollowingQueryBuilder(this.sequelize, options).listFollowing() ]).then(([ total, data ]) => ({ total, data })) } - static listFollowersForApi (options: { - actorIds: number[] - start: number - count: number - sort: string - state?: FollowState - actorType?: ActivityPubActorType - search?: string - }) { - const { actorIds, start, count, sort, search, state, actorType } = options - - const followWhere = state ? { state } : {} - const followerWhere: WhereOptions = {} - - if (search) { - const escapedSearch = ActorFollowModel.sequelize.escape('%' + search + '%') - - Object.assign(followerWhere, { - id: { - [Op.in]: literal( - `(` + - `SELECT "actor".id FROM actor LEFT JOIN server on server.id = actor."serverId" ` + - `WHERE "preferredUsername" ILIKE ${escapedSearch} OR "host" ILIKE ${escapedSearch}` + - `)` - ) - } - }) - } - - if (actorType) { - Object.assign(followerWhere, { type: actorType }) - } - - const getQuery = (forCount: boolean) => { - const actorModel = forCount - ? ActorModel.unscoped() - : ActorModel - - return { - distinct: true, - - offset: start, - limit: count, - order: getFollowsSort(sort), - where: followWhere, - include: [ - { - model: actorModel, - required: true, - as: 'ActorFollower', - where: followerWhere - }, - { - model: actorModel, - as: 'ActorFollowing', - required: true, - where: { - id: { - [Op.in]: actorIds - } - } - } - ] - } - } - + static listFollowersForApi (options: ListFollowersOptions) { return Promise.all([ - ActorFollowModel.count(getQuery(true)), - ActorFollowModel.findAll(getQuery(false)) + new InstanceListFollowersQueryBuilder(this.sequelize, options).countFollowers(), + new InstanceListFollowersQueryBuilder(this.sequelize, options).listFollowers() ]).then(([ total, data ]) => ({ total, data })) } @@ -498,7 +379,8 @@ export class ActorFollowModel extends Model