From 06a05d5f4784a7cbb27aa1188385b5679845dad8 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 16 Aug 2018 15:25:20 +0200 Subject: Add subscriptions endpoints to REST API --- server/models/activitypub/actor-follow.ts | 93 +++++++++++++++++++++++++------ 1 file changed, 76 insertions(+), 17 deletions(-) (limited to 'server/models/activitypub/actor-follow.ts') diff --git a/server/models/activitypub/actor-follow.ts b/server/models/activitypub/actor-follow.ts index adec5e92b..90a8ac43c 100644 --- a/server/models/activitypub/actor-follow.ts +++ b/server/models/activitypub/actor-follow.ts @@ -2,8 +2,21 @@ import * as Bluebird from 'bluebird' import { values } from 'lodash' import * as Sequelize from 'sequelize' import { - AfterCreate, AfterDestroy, AfterUpdate, AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, ForeignKey, IsInt, Max, Model, - Table, UpdatedAt + AfterCreate, + AfterDestroy, + AfterUpdate, + AllowNull, + BelongsTo, + Column, + CreatedAt, + DataType, + Default, + ForeignKey, + IsInt, + Max, + Model, + Table, + UpdatedAt } from 'sequelize-typescript' import { FollowState } from '../../../shared/models/actors' import { AccountFollow } from '../../../shared/models/actors/follow.model' @@ -14,6 +27,7 @@ import { FOLLOW_STATES } from '../../initializers/constants' import { ServerModel } from '../server/server' import { getSort } from '../utils' import { ActorModel } from './actor' +import { VideoChannelModel } from '../video/video-channel' @Table({ tableName: 'actorFollow', @@ -151,7 +165,32 @@ export class ActorFollowModel extends Model { return ActorFollowModel.findOne(query) } - static loadByActorAndTargetHost (actorId: number, targetHost: string, t?: Sequelize.Transaction) { + static loadByActorAndTargetNameAndHost (actorId: number, targetName: string, targetHost: string, t?: Sequelize.Transaction) { + const actorFollowingPartInclude = { + model: ActorModel, + required: true, + as: 'ActorFollowing', + where: { + preferredUsername: targetName + } + } + + if (targetHost === null) { + actorFollowingPartInclude.where['serverId'] = null + } else { + Object.assign(actorFollowingPartInclude, { + include: [ + { + model: ServerModel, + required: true, + where: { + host: targetHost + } + } + ] + }) + } + const query = { where: { actorId @@ -162,20 +201,7 @@ export class ActorFollowModel extends Model { required: true, as: 'ActorFollower' }, - { - model: ActorModel, - required: true, - as: 'ActorFollowing', - include: [ - { - model: ServerModel, - required: true, - where: { - host: targetHost - } - } - ] - } + actorFollowingPartInclude ], transaction: t } @@ -216,6 +242,39 @@ export class ActorFollowModel extends Model { }) } + static listSubscriptionsForApi (id: number, start: number, count: number, sort: string) { + const query = { + distinct: true, + offset: start, + limit: count, + order: getSort(sort), + where: { + actorId: id + }, + include: [ + { + model: ActorModel, + as: 'ActorFollowing', + required: true, + include: [ + { + model: VideoChannelModel, + required: true + } + ] + } + ] + } + + return ActorFollowModel.findAndCountAll(query) + .then(({ rows, count }) => { + return { + data: rows.map(r => r.ActorFollowing.VideoChannel), + total: count + } + }) + } + static listFollowersForApi (id: number, start: number, count: number, sort: string) { const query = { distinct: true, -- cgit v1.2.3