]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/video/video-playlist-element.ts
Implement remote runner jobs in server
[github/Chocobozzz/PeerTube.git] / server / models / video / video-playlist-element.ts
index ba92e129ac5b8c8efe88b33be5ac4a7749784f20..b832f9768c04a66c0160ac03c47cafc3f8a59e37 100644 (file)
@@ -1,3 +1,4 @@
+import { AggregateOptions, Op, ScopeOptions, Sequelize, Transaction } from 'sequelize'
 import {
   AllowNull,
   BelongsTo,
@@ -13,18 +14,8 @@ import {
   Table,
   UpdatedAt
 } from 'sequelize-typescript'
-import { ForAPIOptions, ScopeNames as VideoScopeNames, VideoModel } from './video'
-import { VideoPlaylistModel } from './video-playlist'
-import { getSort, throwIfNotValid } from '../utils'
-import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
-import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
-import { PlaylistElementObject } from '../../../shared/models/activitypub/objects/playlist-element-object'
 import validator from 'validator'
-import { AggregateOptions, Op, ScopeOptions, Sequelize, Transaction } from 'sequelize'
-import { VideoPlaylistElement, VideoPlaylistElementType } from '../../../shared/models/videos/playlist/video-playlist-element.model'
-import { AccountModel } from '../account/account'
-import { VideoPrivacy } from '../../../shared/models/videos'
-import * as Bluebird from 'bluebird'
+import { MUserAccountId } from '@server/types/models'
 import {
   MVideoPlaylistElement,
   MVideoPlaylistElementAP,
@@ -32,7 +23,17 @@ import {
   MVideoPlaylistElementVideoUrlPlaylistPrivacy,
   MVideoPlaylistVideoThumbnail
 } from '@server/types/models/video/video-playlist-element'
-import { MUserAccountId } from '@server/types/models'
+import { forceNumber } from '@shared/core-utils'
+import { AttributesOnly } from '@shared/typescript-utils'
+import { PlaylistElementObject } from '../../../shared/models/activitypub/objects/playlist-element-object'
+import { VideoPrivacy } from '../../../shared/models/videos'
+import { VideoPlaylistElement, VideoPlaylistElementType } from '../../../shared/models/videos/playlist/video-playlist-element.model'
+import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
+import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
+import { AccountModel } from '../account/account'
+import { getSort, throwIfNotValid } from '../shared'
+import { ForAPIOptions, ScopeNames as VideoScopeNames, VideoModel } from './video'
+import { VideoPlaylistModel } from './video-playlist'
 
 @Table({
   tableName: 'videoPlaylistElement',
@@ -43,25 +44,21 @@ import { MUserAccountId } from '@server/types/models'
     {
       fields: [ 'videoId' ]
     },
-    {
-      fields: [ 'videoPlaylistId', 'videoId' ],
-      unique: true
-    },
     {
       fields: [ 'url' ],
       unique: true
     }
   ]
 })
-export class VideoPlaylistElementModel extends Model<VideoPlaylistElementModel> {
+export class VideoPlaylistElementModel extends Model<Partial<AttributesOnly<VideoPlaylistElementModel>>> {
   @CreatedAt
   createdAt: Date
 
   @UpdatedAt
   updatedAt: Date
 
-  @AllowNull(false)
-  @Is('VideoPlaylistUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
+  @AllowNull(true)
+  @Is('VideoPlaylistUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url', true))
   @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_PLAYLISTS.URL.max))
   url: string
 
@@ -170,7 +167,7 @@ export class VideoPlaylistElementModel extends Model<VideoPlaylistElementModel>
     ]).then(([ total, data ]) => ({ total, data }))
   }
 
-  static loadByPlaylistAndVideo (videoPlaylistId: number, videoId: number): Bluebird<MVideoPlaylistElement> {
+  static loadByPlaylistAndVideo (videoPlaylistId: number, videoId: number): Promise<MVideoPlaylistElement> {
     const query = {
       where: {
         videoPlaylistId,
@@ -181,16 +178,17 @@ export class VideoPlaylistElementModel extends Model<VideoPlaylistElementModel>
     return VideoPlaylistElementModel.findOne(query)
   }
 
-  static loadById (playlistElementId: number | string): Bluebird<MVideoPlaylistElement> {
+  static loadById (playlistElementId: number | string): Promise<MVideoPlaylistElement> {
     return VideoPlaylistElementModel.findByPk(playlistElementId)
   }
 
-  static loadByPlaylistAndVideoForAP (
+  static loadByPlaylistAndElementIdForAP (
     playlistId: number | string,
-    videoId: number | string
-  ): Bluebird<MVideoPlaylistElementVideoUrlPlaylistPrivacy> {
-    const playlistWhere = validator.isUUID('' + playlistId) ? { uuid: playlistId } : { id: playlistId }
-    const videoWhere = validator.isUUID('' + videoId) ? { uuid: videoId } : { id: videoId }
+    playlistElementId: number
+  ): Promise<MVideoPlaylistElementVideoUrlPlaylistPrivacy> {
+    const playlistWhere = validator.isUUID('' + playlistId)
+      ? { uuid: playlistId }
+      : { id: playlistId }
 
     const query = {
       include: [
@@ -201,35 +199,43 @@ export class VideoPlaylistElementModel extends Model<VideoPlaylistElementModel>
         },
         {
           attributes: [ 'url' ],
-          model: VideoModel.unscoped(),
-          where: videoWhere
+          model: VideoModel.unscoped()
         }
-      ]
+      ],
+      where: {
+        id: playlistElementId
+      }
     }
 
     return VideoPlaylistElementModel.findOne(query)
   }
 
   static listUrlsOfForAP (videoPlaylistId: number, start: number, count: number, t?: Transaction) {
-    const query = {
-      attributes: [ 'url' ],
-      offset: start,
-      limit: count,
-      order: getSort('position'),
-      where: {
-        videoPlaylistId
-      },
-      transaction: t
+    const getQuery = (forCount: boolean) => {
+      return {
+        attributes: forCount
+          ? []
+          : [ 'url' ],
+        offset: start,
+        limit: count,
+        order: getSort('position'),
+        where: {
+          videoPlaylistId
+        },
+        transaction: t
+      }
     }
 
-    return VideoPlaylistElementModel
-      .findAndCountAll(query)
-      .then(({ rows, count }) => {
-        return { total: count, data: rows.map(e => e.url) }
-      })
+    return Promise.all([
+      VideoPlaylistElementModel.count(getQuery(true)),
+      VideoPlaylistElementModel.findAll(getQuery(false))
+    ]).then(([ total, rows ]) => ({
+      total,
+      data: rows.map(e => e.url)
+    }))
   }
 
-  static loadFirstElementWithVideoThumbnail (videoPlaylistId: number): Bluebird<MVideoPlaylistVideoThumbnail> {
+  static loadFirstElementWithVideoThumbnail (videoPlaylistId: number): Promise<MVideoPlaylistVideoThumbnail> {
     const query = {
       order: getSort('position'),
       where: {
@@ -259,13 +265,15 @@ export class VideoPlaylistElementModel extends Model<VideoPlaylistElementModel>
       .then(position => position ? position + 1 : 1)
   }
 
-  static reassignPositionOf (
-    videoPlaylistId: number,
-    firstPosition: number,
-    endPosition: number,
-    newPosition: number,
+  static reassignPositionOf (options: {
+    videoPlaylistId: number
+    firstPosition: number
+    endPosition: number
+    newPosition: number
     transaction?: Transaction
-  ) {
+  }) {
+    const { videoPlaylistId, firstPosition, endPosition, newPosition, transaction } = options
+
     const query = {
       where: {
         videoPlaylistId,
@@ -278,13 +286,13 @@ export class VideoPlaylistElementModel extends Model<VideoPlaylistElementModel>
       validate: false // We use a literal to update the position
     }
 
-    return VideoPlaylistElementModel.update({ position: Sequelize.literal(`${newPosition} + "position" - ${firstPosition}`) }, query)
+    const positionQuery = Sequelize.literal(`${forceNumber(newPosition)} + "position" - ${forceNumber(firstPosition)}`)
+    return VideoPlaylistElementModel.update({ position: positionQuery }, query)
   }
 
   static increasePositionOf (
     videoPlaylistId: number,
     fromPosition: number,
-    toPosition?: number,
     by = 1,
     transaction?: Transaction
   ) {
@@ -301,7 +309,23 @@ export class VideoPlaylistElementModel extends Model<VideoPlaylistElementModel>
     return VideoPlaylistElementModel.increment({ position: by }, query)
   }
 
-  getType (this: MVideoPlaylistElementFormattable, displayNSFW?: boolean, accountId?: number) {
+  toFormattedJSON (
+    this: MVideoPlaylistElementFormattable,
+    options: { accountId?: number } = {}
+  ): VideoPlaylistElement {
+    return {
+      id: this.id,
+      position: this.position,
+      startTimestamp: this.startTimestamp,
+      stopTimestamp: this.stopTimestamp,
+
+      type: this.getType(options.accountId),
+
+      video: this.getVideoElement(options.accountId)
+    }
+  }
+
+  getType (this: MVideoPlaylistElementFormattable, accountId?: number) {
     const video = this.Video
 
     if (!video) return VideoPlaylistElementType.DELETED
@@ -315,40 +339,23 @@ export class VideoPlaylistElementModel extends Model<VideoPlaylistElementModel>
     if (video.privacy === VideoPrivacy.PRIVATE || video.privacy === VideoPrivacy.INTERNAL) return VideoPlaylistElementType.PRIVATE
 
     if (video.isBlacklisted() || video.isBlocked()) return VideoPlaylistElementType.UNAVAILABLE
-    if (video.nsfw === true && displayNSFW === false) return VideoPlaylistElementType.UNAVAILABLE
 
     return VideoPlaylistElementType.REGULAR
   }
 
-  getVideoElement (this: MVideoPlaylistElementFormattable, displayNSFW?: boolean, accountId?: number) {
+  getVideoElement (this: MVideoPlaylistElementFormattable, accountId?: number) {
     if (!this.Video) return null
-    if (this.getType(displayNSFW, accountId) !== VideoPlaylistElementType.REGULAR) return null
+    if (this.getType(accountId) !== VideoPlaylistElementType.REGULAR) return null
 
     return this.Video.toFormattedJSON()
   }
 
-  toFormattedJSON (
-    this: MVideoPlaylistElementFormattable,
-    options: { displayNSFW?: boolean, accountId?: number } = {}
-  ): VideoPlaylistElement {
-    return {
-      id: this.id,
-      position: this.position,
-      startTimestamp: this.startTimestamp,
-      stopTimestamp: this.stopTimestamp,
-
-      type: this.getType(options.displayNSFW, options.accountId),
-
-      video: this.getVideoElement(options.displayNSFW, options.accountId)
-    }
-  }
-
   toActivityPubObject (this: MVideoPlaylistElementAP): PlaylistElementObject {
     const base: PlaylistElementObject = {
       id: this.url,
       type: 'PlaylistElement',
 
-      url: this.Video.url,
+      url: this.Video?.url || null,
       position: this.position
     }