]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/video/video-playlist-element.ts
Stricter models typing
[github/Chocobozzz/PeerTube.git] / server / models / video / video-playlist-element.ts
index 5530e0492d6463db06e95361fdc4788abad66e09..e6906cb19202c6b70b59c8386a86ae8d8e39adc5 100644 (file)
@@ -1,3 +1,4 @@
+import { AggregateOptions, Op, ScopeOptions, Sequelize, Transaction } from 'sequelize'
 import {
   AllowNull,
   BelongsTo,
@@ -13,13 +14,25 @@ import {
   Table,
   UpdatedAt
 } from 'sequelize-typescript'
-import { VideoModel } from './video'
-import { VideoPlaylistModel } from './video-playlist'
-import * as Sequelize from 'sequelize'
-import { getSort, throwIfNotValid } from '../utils'
-import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
-import { CONSTRAINTS_FIELDS } from '../../initializers'
+import validator from 'validator'
+import { MUserAccountId } from '@server/types/models'
+import {
+  MVideoPlaylistElement,
+  MVideoPlaylistElementAP,
+  MVideoPlaylistElementFormattable,
+  MVideoPlaylistElementVideoUrlPlaylistPrivacy,
+  MVideoPlaylistVideoThumbnail
+} from '@server/types/models/video/video-playlist-element'
 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 '../utils'
+import { ForAPIOptions, ScopeNames as VideoScopeNames, VideoModel } from './video'
+import { VideoPlaylistModel } from './video-playlist'
+import { AttributesOnly } from '@shared/core-utils'
 
 @Table({
   tableName: 'videoPlaylistElement',
@@ -30,29 +43,21 @@ import { PlaylistElementObject } from '../../../shared/models/activitypub/object
     {
       fields: [ 'videoId' ]
     },
-    {
-      fields: [ 'videoPlaylistId', 'videoId' ],
-      unique: true
-    },
-    {
-      fields: [ 'videoPlaylistId', 'position' ],
-      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
 
@@ -93,13 +98,13 @@ export class VideoPlaylistElementModel extends Model<VideoPlaylistElementModel>
 
   @BelongsTo(() => VideoModel, {
     foreignKey: {
-      allowNull: false
+      allowNull: true
     },
-    onDelete: 'CASCADE'
+    onDelete: 'set null'
   })
   Video: VideoModel
 
-  static deleteAllOf (videoPlaylistId: number, transaction?: Sequelize.Transaction) {
+  static deleteAllOf (videoPlaylistId: number, transaction?: Transaction) {
     const query = {
       where: {
         videoPlaylistId
@@ -110,7 +115,58 @@ export class VideoPlaylistElementModel extends Model<VideoPlaylistElementModel>
     return VideoPlaylistElementModel.destroy(query)
   }
 
-  static loadByPlaylistAndVideo (videoPlaylistId: number, videoId: number) {
+  static listForApi (options: {
+    start: number
+    count: number
+    videoPlaylistId: number
+    serverAccount: AccountModel
+    user?: MUserAccountId
+  }) {
+    const accountIds = [ options.serverAccount.id ]
+    const videoScope: (ScopeOptions | string)[] = [
+      VideoScopeNames.WITH_BLACKLISTED
+    ]
+
+    if (options.user) {
+      accountIds.push(options.user.Account.id)
+      videoScope.push({ method: [ VideoScopeNames.WITH_USER_HISTORY, options.user.id ] })
+    }
+
+    const forApiOptions: ForAPIOptions = { withAccountBlockerIds: accountIds }
+    videoScope.push({
+      method: [
+        VideoScopeNames.FOR_API, forApiOptions
+      ]
+    })
+
+    const findQuery = {
+      offset: options.start,
+      limit: options.count,
+      order: getSort('position'),
+      where: {
+        videoPlaylistId: options.videoPlaylistId
+      },
+      include: [
+        {
+          model: VideoModel.scope(videoScope),
+          required: false
+        }
+      ]
+    }
+
+    const countQuery = {
+      where: {
+        videoPlaylistId: options.videoPlaylistId
+      }
+    }
+
+    return Promise.all([
+      VideoPlaylistElementModel.count(countQuery),
+      VideoPlaylistElementModel.findAll(findQuery)
+    ]).then(([ total, data ]) => ({ total, data }))
+  }
+
+  static loadByPlaylistAndVideo (videoPlaylistId: number, videoId: number): Promise<MVideoPlaylistElement> {
     const query = {
       where: {
         videoPlaylistId,
@@ -121,9 +177,15 @@ export class VideoPlaylistElementModel extends Model<VideoPlaylistElementModel>
     return VideoPlaylistElementModel.findOne(query)
   }
 
-  static loadByPlaylistAndVideoForAP (playlistId: number | string, videoId: number | string) {
+  static loadById (playlistElementId: number | string): Promise<MVideoPlaylistElement> {
+    return VideoPlaylistElementModel.findByPk(playlistElementId)
+  }
+
+  static loadByPlaylistAndElementIdForAP (
+    playlistId: number | string,
+    playlistElementId: number
+  ): Promise<MVideoPlaylistElementVideoUrlPlaylistPrivacy> {
     const playlistWhere = validator.isUUID('' + playlistId) ? { uuid: playlistId } : { id: playlistId }
-    const videoWhere = validator.isUUID('' + videoId) ? { uuid: videoId } : { id: videoId }
 
     const query = {
       include: [
@@ -134,16 +196,18 @@ 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) {
+  static listUrlsOfForAP (videoPlaylistId: number, start: number, count: number, t?: Transaction) {
     const query = {
       attributes: [ 'url' ],
       offset: start,
@@ -151,7 +215,8 @@ export class VideoPlaylistElementModel extends Model<VideoPlaylistElementModel>
       order: getSort('position'),
       where: {
         videoPlaylistId
-      }
+      },
+      transaction: t
     }
 
     return VideoPlaylistElementModel
@@ -161,8 +226,26 @@ export class VideoPlaylistElementModel extends Model<VideoPlaylistElementModel>
       })
   }
 
-  static getNextPositionOf (videoPlaylistId: number, transaction?: Sequelize.Transaction) {
+  static loadFirstElementWithVideoThumbnail (videoPlaylistId: number): Promise<MVideoPlaylistVideoThumbnail> {
     const query = {
+      order: getSort('position'),
+      where: {
+        videoPlaylistId
+      },
+      include: [
+        {
+          model: VideoModel.scope(VideoScopeNames.WITH_THUMBNAILS),
+          required: true
+        }
+      ]
+    }
+
+    return VideoPlaylistElementModel
+      .findOne(query)
+  }
+
+  static getNextPositionOf (videoPlaylistId: number, transaction?: Transaction) {
+    const query: AggregateOptions<number> = {
       where: {
         videoPlaylistId
       },
@@ -178,21 +261,22 @@ export class VideoPlaylistElementModel extends Model<VideoPlaylistElementModel>
     firstPosition: number,
     endPosition: number,
     newPosition: number,
-    transaction?: Sequelize.Transaction
+    transaction?: Transaction
   ) {
     const query = {
       where: {
         videoPlaylistId,
         position: {
-          [Sequelize.Op.gte]: firstPosition,
-          [Sequelize.Op.lte]: endPosition
+          [Op.gte]: firstPosition,
+          [Op.lte]: endPosition
         }
       },
       transaction,
       validate: false // We use a literal to update the position
     }
 
-    return VideoPlaylistElementModel.update({ position: Sequelize.literal(`${newPosition} + "position" - ${firstPosition}`) }, query)
+    const positionQuery = Sequelize.literal(`${newPosition} + "position" - ${firstPosition}`)
+    return VideoPlaylistElementModel.update({ position: positionQuery as any }, query)
   }
 
   static increasePositionOf (
@@ -200,13 +284,13 @@ export class VideoPlaylistElementModel extends Model<VideoPlaylistElementModel>
     fromPosition: number,
     toPosition?: number,
     by = 1,
-    transaction?: Sequelize.Transaction
+    transaction?: Transaction
   ) {
     const query = {
       where: {
         videoPlaylistId,
         position: {
-          [Sequelize.Op.gte]: fromPosition
+          [Op.gte]: fromPosition
         }
       },
       transaction
@@ -215,7 +299,49 @@ export class VideoPlaylistElementModel extends Model<VideoPlaylistElementModel>
     return VideoPlaylistElementModel.increment({ position: by }, query)
   }
 
-  toActivityPubObject (): PlaylistElementObject {
+  getType (this: MVideoPlaylistElementFormattable, displayNSFW?: boolean, accountId?: number) {
+    const video = this.Video
+
+    if (!video) return VideoPlaylistElementType.DELETED
+
+    // Owned video, don't filter it
+    if (accountId && video.VideoChannel.Account.id === accountId) return VideoPlaylistElementType.REGULAR
+
+    // Internal video?
+    if (video.privacy === VideoPrivacy.INTERNAL && accountId) return VideoPlaylistElementType.REGULAR
+
+    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) {
+    if (!this.Video) return null
+    if (this.getType(displayNSFW, 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',