aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/video/video-channel.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2019-08-15 11:53:26 +0200
committerChocobozzz <me@florianbigard.com>2019-08-19 17:26:35 +0200
commit453e83ea5d81d203ba34bc43cd5c2c750ba40568 (patch)
tree604e02f4343d13a4ba42e1fb7527ba6ab9111712 /server/models/video/video-channel.ts
parent13176a07a95984a53cc59aec5217f2ce9806d1bc (diff)
downloadPeerTube-453e83ea5d81d203ba34bc43cd5c2c750ba40568.tar.gz
PeerTube-453e83ea5d81d203ba34bc43cd5c2c750ba40568.tar.zst
PeerTube-453e83ea5d81d203ba34bc43cd5c2c750ba40568.zip
Stronger model typings
Diffstat (limited to 'server/models/video/video-channel.ts')
-rw-r--r--server/models/video/video-channel.ts35
1 files changed, 21 insertions, 14 deletions
diff --git a/server/models/video/video-channel.ts b/server/models/video/video-channel.ts
index 6241a75a3..79b9e7d2b 100644
--- a/server/models/video/video-channel.ts
+++ b/server/models/video/video-channel.ts
@@ -33,6 +33,13 @@ import { ServerModel } from '../server/server'
33import { FindOptions, ModelIndexesOptions, Op } from 'sequelize' 33import { FindOptions, ModelIndexesOptions, Op } from 'sequelize'
34import { AvatarModel } from '../avatar/avatar' 34import { AvatarModel } from '../avatar/avatar'
35import { VideoPlaylistModel } from './video-playlist' 35import { VideoPlaylistModel } from './video-playlist'
36import * as Bluebird from 'bluebird'
37import {
38 MChannelAccountDefault,
39 MChannelActor,
40 MChannelActorAccountDefault,
41 MChannelActorAccountDefaultVideos
42} from '../../typings/models/video'
36 43
37// FIXME: Define indexes here because there is an issue with TS and Sequelize.literal when called directly in the annotation 44// FIXME: Define indexes here because there is an issue with TS and Sequelize.literal when called directly in the annotation
38const indexes: ModelIndexesOptions[] = [ 45const indexes: ModelIndexesOptions[] = [
@@ -47,7 +54,7 @@ const indexes: ModelIndexesOptions[] = [
47] 54]
48 55
49export enum ScopeNames { 56export enum ScopeNames {
50 AVAILABLE_FOR_LIST = 'AVAILABLE_FOR_LIST', 57 FOR_API = 'FOR_API',
51 WITH_ACCOUNT = 'WITH_ACCOUNT', 58 WITH_ACCOUNT = 'WITH_ACCOUNT',
52 WITH_ACTOR = 'WITH_ACTOR', 59 WITH_ACTOR = 'WITH_ACTOR',
53 WITH_VIDEOS = 'WITH_VIDEOS', 60 WITH_VIDEOS = 'WITH_VIDEOS',
@@ -74,10 +81,10 @@ export type SummaryOptions = {
74@Scopes(() => ({ 81@Scopes(() => ({
75 [ScopeNames.SUMMARY]: (options: SummaryOptions = {}) => { 82 [ScopeNames.SUMMARY]: (options: SummaryOptions = {}) => {
76 const base: FindOptions = { 83 const base: FindOptions = {
77 attributes: [ 'name', 'description', 'id', 'actorId' ], 84 attributes: [ 'id', 'name', 'description', 'actorId' ],
78 include: [ 85 include: [
79 { 86 {
80 attributes: [ 'preferredUsername', 'url', 'serverId', 'avatarId' ], 87 attributes: [ 'id', 'preferredUsername', 'url', 'serverId', 'avatarId' ],
81 model: ActorModel.unscoped(), 88 model: ActorModel.unscoped(),
82 required: true, 89 required: true,
83 include: [ 90 include: [
@@ -106,7 +113,7 @@ export type SummaryOptions = {
106 113
107 return base 114 return base
108 }, 115 },
109 [ScopeNames.AVAILABLE_FOR_LIST]: (options: AvailableForListOptions) => { 116 [ScopeNames.FOR_API]: (options: AvailableForListOptions) => {
110 // Only list local channels OR channels that are on an instance followed by actorId 117 // Only list local channels OR channels that are on an instance followed by actorId
111 const inQueryInstanceFollow = buildServerIdsFollowedBy(options.actorId) 118 const inQueryInstanceFollow = buildServerIdsFollowedBy(options.actorId)
112 119
@@ -268,7 +275,7 @@ export class VideoChannelModel extends Model<VideoChannelModel> {
268 } 275 }
269 276
270 const scopes = { 277 const scopes = {
271 method: [ ScopeNames.AVAILABLE_FOR_LIST, { actorId } as AvailableForListOptions ] 278 method: [ ScopeNames.FOR_API, { actorId } as AvailableForListOptions ]
272 } 279 }
273 return VideoChannelModel 280 return VideoChannelModel
274 .scope(scopes) 281 .scope(scopes)
@@ -278,7 +285,7 @@ export class VideoChannelModel extends Model<VideoChannelModel> {
278 }) 285 })
279 } 286 }
280 287
281 static listLocalsForSitemap (sort: string) { 288 static listLocalsForSitemap (sort: string): Bluebird<MChannelActor[]> {
282 const query = { 289 const query = {
283 attributes: [ ], 290 attributes: [ ],
284 offset: 0, 291 offset: 0,
@@ -331,7 +338,7 @@ export class VideoChannelModel extends Model<VideoChannelModel> {
331 } 338 }
332 339
333 const scopes = { 340 const scopes = {
334 method: [ ScopeNames.AVAILABLE_FOR_LIST, { actorId: options.actorId } as AvailableForListOptions ] 341 method: [ ScopeNames.FOR_API, { actorId: options.actorId } as AvailableForListOptions ]
335 } 342 }
336 return VideoChannelModel 343 return VideoChannelModel
337 .scope(scopes) 344 .scope(scopes)
@@ -369,13 +376,13 @@ export class VideoChannelModel extends Model<VideoChannelModel> {
369 }) 376 })
370 } 377 }
371 378
372 static loadByIdAndPopulateAccount (id: number) { 379 static loadByIdAndPopulateAccount (id: number): Bluebird<MChannelActorAccountDefault> {
373 return VideoChannelModel.unscoped() 380 return VideoChannelModel.unscoped()
374 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ]) 381 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
375 .findByPk(id) 382 .findByPk(id)
376 } 383 }
377 384
378 static loadByIdAndAccount (id: number, accountId: number) { 385 static loadByIdAndAccount (id: number, accountId: number): Bluebird<MChannelActorAccountDefault> {
379 const query = { 386 const query = {
380 where: { 387 where: {
381 id, 388 id,
@@ -388,13 +395,13 @@ export class VideoChannelModel extends Model<VideoChannelModel> {
388 .findOne(query) 395 .findOne(query)
389 } 396 }
390 397
391 static loadAndPopulateAccount (id: number) { 398 static loadAndPopulateAccount (id: number): Bluebird<MChannelActorAccountDefault> {
392 return VideoChannelModel.unscoped() 399 return VideoChannelModel.unscoped()
393 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ]) 400 .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
394 .findByPk(id) 401 .findByPk(id)
395 } 402 }
396 403
397 static loadByUrlAndPopulateAccount (url: string) { 404 static loadByUrlAndPopulateAccount (url: string): Bluebird<MChannelAccountDefault> {
398 const query = { 405 const query = {
399 include: [ 406 include: [
400 { 407 {
@@ -420,7 +427,7 @@ export class VideoChannelModel extends Model<VideoChannelModel> {
420 return VideoChannelModel.loadByNameAndHostAndPopulateAccount(name, host) 427 return VideoChannelModel.loadByNameAndHostAndPopulateAccount(name, host)
421 } 428 }
422 429
423 static loadLocalByNameAndPopulateAccount (name: string) { 430 static loadLocalByNameAndPopulateAccount (name: string): Bluebird<MChannelActorAccountDefault> {
424 const query = { 431 const query = {
425 include: [ 432 include: [
426 { 433 {
@@ -439,7 +446,7 @@ export class VideoChannelModel extends Model<VideoChannelModel> {
439 .findOne(query) 446 .findOne(query)
440 } 447 }
441 448
442 static loadByNameAndHostAndPopulateAccount (name: string, host: string) { 449 static loadByNameAndHostAndPopulateAccount (name: string, host: string): Bluebird<MChannelActorAccountDefault> {
443 const query = { 450 const query = {
444 include: [ 451 include: [
445 { 452 {
@@ -464,7 +471,7 @@ export class VideoChannelModel extends Model<VideoChannelModel> {
464 .findOne(query) 471 .findOne(query)
465 } 472 }
466 473
467 static loadAndPopulateAccountAndVideos (id: number) { 474 static loadAndPopulateAccountAndVideos (id: number): Bluebird<MChannelActorAccountDefaultVideos> {
468 const options = { 475 const options = {
469 include: [ 476 include: [
470 VideoModel 477 VideoModel