X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Factivitypub%2Factor-follow.ts;h=27bb43daedf6d729418a70d75d11339a00bc5afa;hb=d1a63fc7ac58a1db00d8ca4f43aadba02eb9b084;hp=20d3aa5fcbaa7364ac344c0503fc4f6dac01ed62;hpb=99492dbc0d87ef54d0dab7d8d44f8d0de5722bdd;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/activitypub/actor-follow.ts b/server/models/activitypub/actor-follow.ts index 20d3aa5fc..27bb43dae 100644 --- a/server/models/activitypub/actor-follow.ts +++ b/server/models/activitypub/actor-follow.ts @@ -19,16 +19,17 @@ import { UpdatedAt } from 'sequelize-typescript' import { FollowState } from '../../../shared/models/actors' -import { AccountFollow } from '../../../shared/models/actors/follow.model' +import { ActorFollow } from '../../../shared/models/actors/follow.model' import { logger } from '../../helpers/logger' import { getServerActor } from '../../helpers/utils' import { ACTOR_FOLLOW_SCORE } from '../../initializers' import { FOLLOW_STATES } from '../../initializers/constants' import { ServerModel } from '../server/server' import { getSort } from '../utils' -import { ActorModel } from './actor' +import { ActorModel, unusedActorAttributesForAPI } from './actor' import { VideoChannelModel } from '../video/video-channel' import { IIncludeOptions } from '../../../node_modules/sequelize-typescript/lib/interfaces/IIncludeOptions' +import { AccountModel } from '../account/account' @Table({ tableName: 'actorFollow', @@ -166,7 +167,7 @@ export class ActorFollowModel extends Model { return ActorFollowModel.findOne(query) } - static loadByActorAndTargetNameAndHost (actorId: number, targetName: string, targetHost: string, t?: Sequelize.Transaction) { + static loadByActorAndTargetNameAndHostForAPI (actorId: number, targetName: string, targetHost: string, t?: Sequelize.Transaction) { const actorFollowingPartInclude: IIncludeOptions = { model: ActorModel, required: true, @@ -176,7 +177,7 @@ export class ActorFollowModel extends Model { }, include: [ { - model: VideoChannelModel, + model: VideoChannelModel.unscoped(), required: false } ] @@ -199,17 +200,84 @@ export class ActorFollowModel extends Model { actorId }, include: [ + actorFollowingPartInclude, { model: ActorModel, required: true, as: 'ActorFollower' - }, - actorFollowingPartInclude + } ], transaction: t } return ActorFollowModel.findOne(query) + .then(result => { + if (result && result.ActorFollowing.VideoChannel) { + result.ActorFollowing.VideoChannel.Actor = result.ActorFollowing + } + + return result + }) + } + + static listSubscribedIn (actorId: number, targets: { name: string, host?: string }[]) { + const whereTab = targets + .map(t => { + if (t.host) { + return { + [ Sequelize.Op.and ]: [ + { + '$preferredUsername$': t.name + }, + { + '$host$': t.host + } + ] + } + } + + return { + [ Sequelize.Op.and ]: [ + { + '$preferredUsername$': t.name + }, + { + '$serverId$': null + } + ] + } + }) + + const query = { + attributes: [], + where: { + [ Sequelize.Op.and ]: [ + { + [ Sequelize.Op.or ]: whereTab + }, + { + actorId + } + ] + }, + include: [ + { + attributes: [ 'preferredUsername' ], + model: ActorModel.unscoped(), + required: true, + as: 'ActorFollowing', + include: [ + { + attributes: [ 'host' ], + model: ServerModel.unscoped(), + required: false + } + ] + } + ] + } + + return ActorFollowModel.findAll(query) } static listFollowingForApi (id: number, start: number, count: number, sort: string) { @@ -247,6 +315,7 @@ export class ActorFollowModel extends Model { static listSubscriptionsForApi (id: number, start: number, count: number, sort: string) { const query = { + attributes: [], distinct: true, offset: start, limit: count, @@ -256,13 +325,36 @@ export class ActorFollowModel extends Model { }, include: [ { - model: ActorModel, + attributes: [ 'id' ], + model: ActorModel.unscoped(), as: 'ActorFollowing', required: true, include: [ { - model: VideoChannelModel, - required: true + model: VideoChannelModel.unscoped(), + required: true, + include: [ + { + attributes: { + exclude: unusedActorAttributesForAPI + }, + model: ActorModel, + required: true + }, + { + model: AccountModel.unscoped(), + required: true, + include: [ + { + attributes: { + exclude: unusedActorAttributesForAPI + }, + model: ActorModel, + required: true + } + ] + } + ] } ] } @@ -437,7 +529,7 @@ export class ActorFollowModel extends Model { return ActorFollowModel.findAll(query) } - toFormattedJSON (): AccountFollow { + toFormattedJSON (): ActorFollow { const follower = this.ActorFollower.toFormattedJSON() const following = this.ActorFollowing.toFormattedJSON()