diff options
author | Chocobozzz <me@florianbigard.com> | 2018-08-16 15:25:20 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-08-27 09:41:54 +0200 |
commit | 06a05d5f4784a7cbb27aa1188385b5679845dad8 (patch) | |
tree | ac197f3ed0768529456225ad76c912f22bc55e29 /server/models/activitypub | |
parent | 4bda2e47bbc937c401ddcf14c1be53c70481a294 (diff) | |
download | PeerTube-06a05d5f4784a7cbb27aa1188385b5679845dad8.tar.gz PeerTube-06a05d5f4784a7cbb27aa1188385b5679845dad8.tar.zst PeerTube-06a05d5f4784a7cbb27aa1188385b5679845dad8.zip |
Add subscriptions endpoints to REST API
Diffstat (limited to 'server/models/activitypub')
-rw-r--r-- | server/models/activitypub/actor-follow.ts | 93 |
1 files changed, 76 insertions, 17 deletions
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' | |||
2 | import { values } from 'lodash' | 2 | import { values } from 'lodash' |
3 | import * as Sequelize from 'sequelize' | 3 | import * as Sequelize from 'sequelize' |
4 | import { | 4 | import { |
5 | AfterCreate, AfterDestroy, AfterUpdate, AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, ForeignKey, IsInt, Max, Model, | 5 | AfterCreate, |
6 | Table, UpdatedAt | 6 | AfterDestroy, |
7 | AfterUpdate, | ||
8 | AllowNull, | ||
9 | BelongsTo, | ||
10 | Column, | ||
11 | CreatedAt, | ||
12 | DataType, | ||
13 | Default, | ||
14 | ForeignKey, | ||
15 | IsInt, | ||
16 | Max, | ||
17 | Model, | ||
18 | Table, | ||
19 | UpdatedAt | ||
7 | } from 'sequelize-typescript' | 20 | } from 'sequelize-typescript' |
8 | import { FollowState } from '../../../shared/models/actors' | 21 | import { FollowState } from '../../../shared/models/actors' |
9 | import { AccountFollow } from '../../../shared/models/actors/follow.model' | 22 | import { AccountFollow } from '../../../shared/models/actors/follow.model' |
@@ -14,6 +27,7 @@ import { FOLLOW_STATES } from '../../initializers/constants' | |||
14 | import { ServerModel } from '../server/server' | 27 | import { ServerModel } from '../server/server' |
15 | import { getSort } from '../utils' | 28 | import { getSort } from '../utils' |
16 | import { ActorModel } from './actor' | 29 | import { ActorModel } from './actor' |
30 | import { VideoChannelModel } from '../video/video-channel' | ||
17 | 31 | ||
18 | @Table({ | 32 | @Table({ |
19 | tableName: 'actorFollow', | 33 | tableName: 'actorFollow', |
@@ -151,7 +165,32 @@ export class ActorFollowModel extends Model<ActorFollowModel> { | |||
151 | return ActorFollowModel.findOne(query) | 165 | return ActorFollowModel.findOne(query) |
152 | } | 166 | } |
153 | 167 | ||
154 | static loadByActorAndTargetHost (actorId: number, targetHost: string, t?: Sequelize.Transaction) { | 168 | static loadByActorAndTargetNameAndHost (actorId: number, targetName: string, targetHost: string, t?: Sequelize.Transaction) { |
169 | const actorFollowingPartInclude = { | ||
170 | model: ActorModel, | ||
171 | required: true, | ||
172 | as: 'ActorFollowing', | ||
173 | where: { | ||
174 | preferredUsername: targetName | ||
175 | } | ||
176 | } | ||
177 | |||
178 | if (targetHost === null) { | ||
179 | actorFollowingPartInclude.where['serverId'] = null | ||
180 | } else { | ||
181 | Object.assign(actorFollowingPartInclude, { | ||
182 | include: [ | ||
183 | { | ||
184 | model: ServerModel, | ||
185 | required: true, | ||
186 | where: { | ||
187 | host: targetHost | ||
188 | } | ||
189 | } | ||
190 | ] | ||
191 | }) | ||
192 | } | ||
193 | |||
155 | const query = { | 194 | const query = { |
156 | where: { | 195 | where: { |
157 | actorId | 196 | actorId |
@@ -162,20 +201,7 @@ export class ActorFollowModel extends Model<ActorFollowModel> { | |||
162 | required: true, | 201 | required: true, |
163 | as: 'ActorFollower' | 202 | as: 'ActorFollower' |
164 | }, | 203 | }, |
165 | { | 204 | actorFollowingPartInclude |
166 | model: ActorModel, | ||
167 | required: true, | ||
168 | as: 'ActorFollowing', | ||
169 | include: [ | ||
170 | { | ||
171 | model: ServerModel, | ||
172 | required: true, | ||
173 | where: { | ||
174 | host: targetHost | ||
175 | } | ||
176 | } | ||
177 | ] | ||
178 | } | ||
179 | ], | 205 | ], |
180 | transaction: t | 206 | transaction: t |
181 | } | 207 | } |
@@ -216,6 +242,39 @@ export class ActorFollowModel extends Model<ActorFollowModel> { | |||
216 | }) | 242 | }) |
217 | } | 243 | } |
218 | 244 | ||
245 | static listSubscriptionsForApi (id: number, start: number, count: number, sort: string) { | ||
246 | const query = { | ||
247 | distinct: true, | ||
248 | offset: start, | ||
249 | limit: count, | ||
250 | order: getSort(sort), | ||
251 | where: { | ||
252 | actorId: id | ||
253 | }, | ||
254 | include: [ | ||
255 | { | ||
256 | model: ActorModel, | ||
257 | as: 'ActorFollowing', | ||
258 | required: true, | ||
259 | include: [ | ||
260 | { | ||
261 | model: VideoChannelModel, | ||
262 | required: true | ||
263 | } | ||
264 | ] | ||
265 | } | ||
266 | ] | ||
267 | } | ||
268 | |||
269 | return ActorFollowModel.findAndCountAll(query) | ||
270 | .then(({ rows, count }) => { | ||
271 | return { | ||
272 | data: rows.map(r => r.ActorFollowing.VideoChannel), | ||
273 | total: count | ||
274 | } | ||
275 | }) | ||
276 | } | ||
277 | |||
219 | static listFollowersForApi (id: number, start: number, count: number, sort: string) { | 278 | static listFollowersForApi (id: number, start: number, count: number, sort: string) { |
220 | const query = { | 279 | const query = { |
221 | distinct: true, | 280 | distinct: true, |