]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/video.js
Server: add video abuse support
[github/Chocobozzz/PeerTube.git] / server / models / video.js
index 0e84e89867f722d88e0c6d236986b4faa6646d15..4c197a83502d8aad01a2c429c42d81e48929a9aa 100644 (file)
@@ -20,7 +20,6 @@ const customVideosValidators = require('../helpers/custom-validators').videos
 // ---------------------------------------------------------------------------
 
 module.exports = function (sequelize, DataTypes) {
-  // TODO: add indexes on searchable columns
   const Video = sequelize.define('Video',
     {
       id: {
@@ -111,10 +110,10 @@ module.exports = function (sequelize, DataTypes) {
         getDurationFromFile,
         list,
         listForApi,
-        listByHostAndRemoteId,
         listOwnedAndPopulateAuthorAndTags,
         listOwnedByAuthor,
         load,
+        loadByHostAndRemoteId,
         loadAndPopulateAuthor,
         loadAndPopulateAuthorAndPodAndTags,
         searchAndPopulateAuthorAndPodAndTags
@@ -127,7 +126,8 @@ module.exports = function (sequelize, DataTypes) {
         getTorrentName,
         isOwned,
         toFormatedJSON,
-        toRemoteJSON
+        toAddRemoteJSON,
+        toUpdateRemoteJSON
       },
       hooks: {
         beforeValidate,
@@ -248,6 +248,14 @@ function associate (models) {
     through: models.VideoTag,
     onDelete: 'cascade'
   })
+
+  this.hasMany(models.VideoAbuse, {
+    foreignKey: {
+      name: 'videoId',
+      allowNull: false
+    },
+    onDelete: 'cascade'
+  })
 }
 
 function generateMagnetUri () {
@@ -328,13 +336,14 @@ function toFormatedJSON () {
     duration: this.duration,
     tags: map(this.Tags, 'name'),
     thumbnailPath: constants.STATIC_PATHS.THUMBNAILS + '/' + this.getThumbnailName(),
-    createdAt: this.createdAt
+    createdAt: this.createdAt,
+    updatedAt: this.updatedAt
   }
 
   return json
 }
 
-function toRemoteJSON (callback) {
+function toAddRemoteJSON (callback) {
   const self = this
 
   // Get thumbnail data to send to the other pod
@@ -355,6 +364,7 @@ function toRemoteJSON (callback) {
       thumbnailData: thumbnailData.toString('binary'),
       tags: map(self.Tags, 'name'),
       createdAt: self.createdAt,
+      updatedAt: self.updatedAt,
       extname: self.extname
     }
 
@@ -362,6 +372,23 @@ function toRemoteJSON (callback) {
   })
 }
 
+function toUpdateRemoteJSON (callback) {
+  const json = {
+    name: this.name,
+    description: this.description,
+    infoHash: this.infoHash,
+    remoteId: this.id,
+    author: this.Author.name,
+    duration: this.duration,
+    tags: map(this.Tags, 'name'),
+    createdAt: this.createdAt,
+    updatedAt: this.updatedAt,
+    extname: this.extname
+  }
+
+  return json
+}
+
 // ------------------------------ STATICS ------------------------------
 
 function generateThumbnailFromData (video, thumbnailData, callback) {
@@ -411,7 +438,7 @@ function listForApi (start, count, sort, callback) {
   })
 }
 
-function listByHostAndRemoteId (fromHost, remoteId, callback) {
+function loadByHostAndRemoteId (fromHost, remoteId, callback) {
   const query = {
     where: {
       remoteId: remoteId
@@ -432,7 +459,7 @@ function listByHostAndRemoteId (fromHost, remoteId, callback) {
     ]
   }
 
-  return this.findAll(query).asCallback(callback)
+  return this.findOne(query).asCallback(callback)
 }
 
 function listOwnedAndPopulateAuthorAndTags (callback) {