]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/video/video-abuse.ts
Fix video upload and videos list
[github/Chocobozzz/PeerTube.git] / server / models / video / video-abuse.ts
index f5b4debe6964ad1a2b01e21031532692e23f2719..f3fdeab524807cc7fee5bb65cc191ca2c0ad934e 100644 (file)
@@ -1,11 +1,10 @@
 import * as Sequelize from 'sequelize'
 
 import { CONFIG } from '../../initializers'
-import { isVideoAbuseReporterUsernameValid, isVideoAbuseReasonValid } from '../../helpers'
+import { isVideoAbuseReasonValid } from '../../helpers'
 
 import { addMethodsToModel, getSort } from '../utils'
 import {
-  VideoAbuseClass,
   VideoAbuseInstance,
   VideoAbuseAttributes,
 
@@ -13,26 +12,17 @@ import {
 } from './video-abuse-interface'
 
 let VideoAbuse: Sequelize.Model<VideoAbuseInstance, VideoAbuseAttributes>
+let toFormattedJSON: VideoAbuseMethods.ToFormattedJSON
 let listForApi: VideoAbuseMethods.ListForApi
 
 export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
   VideoAbuse = sequelize.define<VideoAbuseInstance, VideoAbuseAttributes>('VideoAbuse',
     {
-      reporterUsername: {
-        type: DataTypes.STRING,
-        allowNull: false,
-        validate: {
-          reporterUsernameValid: function (value) {
-            const res = isVideoAbuseReporterUsernameValid(value)
-            if (res === false) throw new Error('Video abuse reporter username is not valid.')
-          }
-        }
-      },
       reason: {
         type: DataTypes.STRING,
         allowNull: false,
         validate: {
-          reasonValid: function (value) {
+          reasonValid: value => {
             const res = isVideoAbuseReasonValid(value)
             if (res === false) throw new Error('Video abuse reason is not valid.')
           }
@@ -45,7 +35,7 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da
           fields: [ 'videoId' ]
         },
         {
-          fields: [ 'reporterPodId' ]
+          fields: [ 'reporterAccountId' ]
         }
       ]
     }
@@ -57,7 +47,7 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da
     listForApi
   ]
   const instanceMethods = [
-    toFormatedJSON
+    toFormattedJSON
   ]
   addMethodsToModel(VideoAbuse, classMethods, instanceMethods)
 
@@ -66,22 +56,24 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da
 
 // ------------------------------ METHODS ------------------------------
 
-function toFormatedJSON (this: VideoAbuseInstance) {
-  let reporterPodHost
+toFormattedJSON = function (this: VideoAbuseInstance) {
+  let reporterServerHost
 
-  if (this.Pod) {
-    reporterPodHost = this.Pod.host
+  if (this.Account.Server) {
+    reporterServerHost = this.Account.Server.host
   } else {
     // It means it's our video
-    reporterPodHost = CONFIG.WEBSERVER.HOST
+    reporterServerHost = CONFIG.WEBSERVER.HOST
   }
 
   const json = {
     id: this.id,
-    reporterPodHost,
     reason: this.reason,
-    reporterUsername: this.reporterUsername,
-    videoId: this.videoId,
+    reporterUsername: this.Account.name,
+    reporterServerHost,
+    videoId: this.Video.id,
+    videoUUID: this.Video.uuid,
+    videoName: this.Video.name,
     createdAt: this.createdAt
   }
 
@@ -91,12 +83,12 @@ function toFormatedJSON (this: VideoAbuseInstance) {
 // ------------------------------ STATICS ------------------------------
 
 function associate (models) {
-  VideoAbuse.belongsTo(models.Pod, {
+  VideoAbuse.belongsTo(models.Account, {
     foreignKey: {
-      name: 'reporterPodId',
+      name: 'reporterAccountId',
       allowNull: true
     },
-    onDelete: 'cascade'
+    onDelete: 'CASCADE'
   })
 
   VideoAbuse.belongsTo(models.Video, {
@@ -104,28 +96,34 @@ function associate (models) {
       name: 'videoId',
       allowNull: false
     },
-    onDelete: 'cascade'
+    onDelete: 'CASCADE'
   })
 }
 
-listForApi = function (start, count, sort, callback) {
+listForApi = function (start: number, count: number, sort: string) {
   const query = {
     offset: start,
     limit: count,
     order: [ getSort(sort) ],
     include: [
       {
-        model: VideoAbuse['sequelize'].models.Pod,
-        required: false
+        model: VideoAbuse['sequelize'].models.Account,
+        required: true,
+        include: [
+          {
+            model: VideoAbuse['sequelize'].models.Server,
+            required: false
+          }
+        ]
+      },
+      {
+        model: VideoAbuse['sequelize'].models.Video,
+        required: true
       }
     ]
   }
 
-  return VideoAbuse.findAndCountAll(query).asCallback(function (err, result) {
-    if (err) return callback(err)
-
-    return callback(null, result.rows, result.count)
+  return VideoAbuse.findAndCountAll(query).then(({ rows, count }) => {
+    return { total: count, data: rows }
   })
 }
-
-