]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/video/video-change-ownership.ts
align ownership change video list table with moderation tables
[github/Chocobozzz/PeerTube.git] / server / models / video / video-change-ownership.ts
index c9cff50545144eac09d4a7402e12ebb132ab57f8..ac0ab7e8b9088c1a3158b4696cb5c2bc7dd6c3a0 100644 (file)
@@ -1,49 +1,59 @@
 import { AllowNull, BelongsTo, Column, CreatedAt, ForeignKey, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript'
 import { AccountModel } from '../account/account'
-import { VideoModel } from './video'
+import { ScopeNames as VideoScopeNames, VideoModel } from './video'
 import { VideoChangeOwnership, VideoChangeOwnershipStatus } from '../../../shared/models/videos'
 import { getSort } from '../utils'
-import { VideoFileModel } from './video-file'
+import { MVideoChangeOwnershipFormattable, MVideoChangeOwnershipFull } from '@server/types/models/video/video-change-ownership'
+import * as Bluebird from 'bluebird'
 
 enum ScopeNames {
-  FULL = 'FULL'
+  WITH_ACCOUNTS = 'WITH_ACCOUNTS',
+  WITH_VIDEO = 'WITH_VIDEO'
 }
 
 @Table({
   tableName: 'videoChangeOwnership',
   indexes: [
     {
-      fields: ['videoId']
+      fields: [ 'videoId' ]
     },
     {
-      fields: ['initiatorAccountId']
+      fields: [ 'initiatorAccountId' ]
     },
     {
-      fields: ['nextOwnerAccountId']
+      fields: [ 'nextOwnerAccountId' ]
     }
   ]
 })
-@Scopes({
-  [ScopeNames.FULL]: {
+@Scopes(() => ({
+  [ScopeNames.WITH_ACCOUNTS]: {
     include: [
       {
-        model: () => AccountModel,
+        model: AccountModel,
         as: 'Initiator',
         required: true
       },
       {
-        model: () => AccountModel,
+        model: AccountModel,
         as: 'NextOwner',
         required: true
-      },
+      }
+    ]
+  },
+  [ScopeNames.WITH_VIDEO]: {
+    include: [
       {
-        model: () => VideoModel,
-        required: true,
-        include: [{ model: () => VideoFileModel }]
+        model: VideoModel.scope([
+          VideoScopeNames.WITH_THUMBNAILS,
+          VideoScopeNames.WITH_WEBTORRENT_FILES,
+          VideoScopeNames.WITH_STREAMING_PLAYLISTS,
+          VideoScopeNames.WITH_ACCOUNT_DETAILS
+        ]),
+        required: true
       }
     ]
   }
-})
+}))
 export class VideoChangeOwnershipModel extends Model<VideoChangeOwnershipModel> {
   @CreatedAt
   createdAt: Date
@@ -94,33 +104,33 @@ export class VideoChangeOwnershipModel extends Model<VideoChangeOwnershipModel>
   Video: VideoModel
 
   static listForApi (nextOwnerId: number, start: number, count: number, sort: string) {
-    return VideoChangeOwnershipModel.scope(ScopeNames.FULL).findAndCountAll({
+    const query = {
       offset: start,
       limit: count,
       order: getSort(sort),
       where: {
         nextOwnerAccountId: nextOwnerId
       }
-    })
-      .then(({ rows, count }) => ({ total: count, data: rows }))
+    }
+
+    return Promise.all([
+      VideoChangeOwnershipModel.scope(ScopeNames.WITH_ACCOUNTS).count(query),
+      VideoChangeOwnershipModel.scope([ ScopeNames.WITH_ACCOUNTS, ScopeNames.WITH_VIDEO ]).findAll<MVideoChangeOwnershipFull>(query)
+    ]).then(([ count, rows ]) => ({ total: count, data: rows }))
   }
 
-  static load (id: number) {
-    return VideoChangeOwnershipModel.scope(ScopeNames.FULL).findById(id)
+  static load (id: number): Bluebird<MVideoChangeOwnershipFull> {
+    return VideoChangeOwnershipModel.scope([ ScopeNames.WITH_ACCOUNTS, ScopeNames.WITH_VIDEO ])
+                                    .findByPk(id)
   }
 
-  toFormattedJSON (): VideoChangeOwnership {
+  toFormattedJSON (this: MVideoChangeOwnershipFormattable): VideoChangeOwnership {
     return {
       id: this.id,
       status: this.status,
       initiatorAccount: this.Initiator.toFormattedJSON(),
       nextOwnerAccount: this.NextOwner.toFormattedJSON(),
-      video: {
-        id: this.Video.id,
-        uuid: this.Video.uuid,
-        url: this.Video.url,
-        name: this.Video.name
-      },
+      video: this.Video.toFormattedJSON(),
       createdAt: this.createdAt
     }
   }