aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/video/video-playlist-element.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/models/video/video-playlist-element.ts')
-rw-r--r--server/models/video/video-playlist-element.ts34
1 files changed, 24 insertions, 10 deletions
diff --git a/server/models/video/video-playlist-element.ts b/server/models/video/video-playlist-element.ts
index dd7653533..a28021313 100644
--- a/server/models/video/video-playlist-element.ts
+++ b/server/models/video/video-playlist-element.ts
@@ -21,10 +21,18 @@ import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
21import { PlaylistElementObject } from '../../../shared/models/activitypub/objects/playlist-element-object' 21import { PlaylistElementObject } from '../../../shared/models/activitypub/objects/playlist-element-object'
22import * as validator from 'validator' 22import * as validator from 'validator'
23import { AggregateOptions, Op, ScopeOptions, Sequelize, Transaction } from 'sequelize' 23import { AggregateOptions, Op, ScopeOptions, Sequelize, Transaction } from 'sequelize'
24import { UserModel } from '../account/user'
25import { VideoPlaylistElement, VideoPlaylistElementType } from '../../../shared/models/videos/playlist/video-playlist-element.model' 24import { VideoPlaylistElement, VideoPlaylistElementType } from '../../../shared/models/videos/playlist/video-playlist-element.model'
26import { AccountModel } from '../account/account' 25import { AccountModel } from '../account/account'
27import { VideoPrivacy } from '../../../shared/models/videos' 26import { VideoPrivacy } from '../../../shared/models/videos'
27import * as Bluebird from 'bluebird'
28import {
29 MVideoPlaylistElement,
30 MVideoPlaylistElementAP,
31 MVideoPlaylistElementFormattable,
32 MVideoPlaylistElementVideoUrlPlaylistPrivacy,
33 MVideoPlaylistVideoThumbnail
34} from '@server/typings/models/video/video-playlist-element'
35import { MUserAccountId } from '@server/typings/models'
28 36
29@Table({ 37@Table({
30 tableName: 'videoPlaylistElement', 38 tableName: 'videoPlaylistElement',
@@ -116,7 +124,7 @@ export class VideoPlaylistElementModel extends Model<VideoPlaylistElementModel>
116 count: number, 124 count: number,
117 videoPlaylistId: number, 125 videoPlaylistId: number,
118 serverAccount: AccountModel, 126 serverAccount: AccountModel,
119 user?: UserModel 127 user?: MUserAccountId
120 }) { 128 }) {
121 const accountIds = [ options.serverAccount.id ] 129 const accountIds = [ options.serverAccount.id ]
122 const videoScope: (ScopeOptions | string)[] = [ 130 const videoScope: (ScopeOptions | string)[] = [
@@ -162,7 +170,7 @@ export class VideoPlaylistElementModel extends Model<VideoPlaylistElementModel>
162 ]).then(([ total, data ]) => ({ total, data })) 170 ]).then(([ total, data ]) => ({ total, data }))
163 } 171 }
164 172
165 static loadByPlaylistAndVideo (videoPlaylistId: number, videoId: number) { 173 static loadByPlaylistAndVideo (videoPlaylistId: number, videoId: number): Bluebird<MVideoPlaylistElement> {
166 const query = { 174 const query = {
167 where: { 175 where: {
168 videoPlaylistId, 176 videoPlaylistId,
@@ -173,11 +181,14 @@ export class VideoPlaylistElementModel extends Model<VideoPlaylistElementModel>
173 return VideoPlaylistElementModel.findOne(query) 181 return VideoPlaylistElementModel.findOne(query)
174 } 182 }
175 183
176 static loadById (playlistElementId: number) { 184 static loadById (playlistElementId: number): Bluebird<MVideoPlaylistElement> {
177 return VideoPlaylistElementModel.findByPk(playlistElementId) 185 return VideoPlaylistElementModel.findByPk(playlistElementId)
178 } 186 }
179 187
180 static loadByPlaylistAndVideoForAP (playlistId: number | string, videoId: number | string) { 188 static loadByPlaylistAndVideoForAP (
189 playlistId: number | string,
190 videoId: number | string
191 ): Bluebird<MVideoPlaylistElementVideoUrlPlaylistPrivacy> {
181 const playlistWhere = validator.isUUID('' + playlistId) ? { uuid: playlistId } : { id: playlistId } 192 const playlistWhere = validator.isUUID('' + playlistId) ? { uuid: playlistId } : { id: playlistId }
182 const videoWhere = validator.isUUID('' + videoId) ? { uuid: videoId } : { id: videoId } 193 const videoWhere = validator.isUUID('' + videoId) ? { uuid: videoId } : { id: videoId }
183 194
@@ -218,7 +229,7 @@ export class VideoPlaylistElementModel extends Model<VideoPlaylistElementModel>
218 }) 229 })
219 } 230 }
220 231
221 static loadFirstElementWithVideoThumbnail (videoPlaylistId: number) { 232 static loadFirstElementWithVideoThumbnail (videoPlaylistId: number): Bluebird<MVideoPlaylistVideoThumbnail> {
222 const query = { 233 const query = {
223 order: getSort('position'), 234 order: getSort('position'),
224 where: { 235 where: {
@@ -290,7 +301,7 @@ export class VideoPlaylistElementModel extends Model<VideoPlaylistElementModel>
290 return VideoPlaylistElementModel.increment({ position: by }, query) 301 return VideoPlaylistElementModel.increment({ position: by }, query)
291 } 302 }
292 303
293 getType (displayNSFW?: boolean, accountId?: number) { 304 getType (this: MVideoPlaylistElementFormattable, displayNSFW?: boolean, accountId?: number) {
294 const video = this.Video 305 const video = this.Video
295 306
296 if (!video) return VideoPlaylistElementType.DELETED 307 if (!video) return VideoPlaylistElementType.DELETED
@@ -306,14 +317,17 @@ export class VideoPlaylistElementModel extends Model<VideoPlaylistElementModel>
306 return VideoPlaylistElementType.REGULAR 317 return VideoPlaylistElementType.REGULAR
307 } 318 }
308 319
309 getVideoElement (displayNSFW?: boolean, accountId?: number) { 320 getVideoElement (this: MVideoPlaylistElementFormattable, displayNSFW?: boolean, accountId?: number) {
310 if (!this.Video) return null 321 if (!this.Video) return null
311 if (this.getType(displayNSFW, accountId) !== VideoPlaylistElementType.REGULAR) return null 322 if (this.getType(displayNSFW, accountId) !== VideoPlaylistElementType.REGULAR) return null
312 323
313 return this.Video.toFormattedJSON() 324 return this.Video.toFormattedJSON()
314 } 325 }
315 326
316 toFormattedJSON (options: { displayNSFW?: boolean, accountId?: number } = {}): VideoPlaylistElement { 327 toFormattedJSON (
328 this: MVideoPlaylistElementFormattable,
329 options: { displayNSFW?: boolean, accountId?: number } = {}
330 ): VideoPlaylistElement {
317 return { 331 return {
318 id: this.id, 332 id: this.id,
319 position: this.position, 333 position: this.position,
@@ -326,7 +340,7 @@ export class VideoPlaylistElementModel extends Model<VideoPlaylistElementModel>
326 } 340 }
327 } 341 }
328 342
329 toActivityPubObject (): PlaylistElementObject { 343 toActivityPubObject (this: MVideoPlaylistElementAP): PlaylistElementObject {
330 const base: PlaylistElementObject = { 344 const base: PlaylistElementObject = {
331 id: this.url, 345 id: this.url,
332 type: 'PlaylistElement', 346 type: 'PlaylistElement',