]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/pods.js
Server: don't be rude when serving unknown video in watch html file
[github/Chocobozzz/PeerTube.git] / server / models / pods.js
index 59de2d60cb62dd07b17445f24386b1d29c3aba7c..49c73472abd6a2e8073ee755edaf02b4739a3e61 100644 (file)
@@ -1,15 +1,18 @@
 'use strict'
 
+const each = require('async/each')
 const mongoose = require('mongoose')
 const map = require('lodash/map')
 const validator = require('express-validator').validator
 
 const constants = require('../initializers/constants')
 
+const Video = mongoose.model('Video')
+
 // ---------------------------------------------------------------------------
 
 const PodSchema = mongoose.Schema({
-  url: String,
+  host: String,
   publicKey: String,
   score: { type: Number, max: constants.FRIEND_SCORE.MAX },
   createdDate: {
@@ -18,30 +21,29 @@ const PodSchema = mongoose.Schema({
   }
 })
 
-// TODO: set options (TLD...)
-PodSchema.path('url').validate(validator.isURL)
+PodSchema.path('host').validate(validator.isURL)
 PodSchema.path('publicKey').required(true)
 PodSchema.path('score').validate(function (value) { return !isNaN(value) })
 
 PodSchema.methods = {
-  toFormatedJSON: toFormatedJSON
+  toFormatedJSON
 }
 
 PodSchema.statics = {
-  countAll: countAll,
-  incrementScores: incrementScores,
-  list: list,
-  listAllIds: listAllIds,
-  listBadPods: listBadPods,
-  load: load,
-  loadByUrl: loadByUrl,
-  removeAll: removeAll
+  countAll,
+  incrementScores,
+  list,
+  listAllIds,
+  listBadPods,
+  load,
+  loadByHost,
+  removeAll
 }
 
 PodSchema.pre('save', function (next) {
   const self = this
 
-  Pod.loadByUrl(this.url, function (err, pod) {
+  Pod.loadByHost(this.host, function (err, pod) {
     if (err) return next(err)
 
     if (pod) return next(new Error('Pod already exists.'))
@@ -51,6 +53,17 @@ PodSchema.pre('save', function (next) {
   })
 })
 
+PodSchema.pre('remove', function (next) {
+  // Remove the videos owned by this pod too
+  Video.listByHost(this.host, function (err, videos) {
+    if (err) return next(err)
+
+    each(videos, function (video, callbackEach) {
+      video.remove(callbackEach)
+    }, next)
+  })
+})
+
 const Pod = mongoose.model('Pod', PodSchema)
 
 // ------------------------------ METHODS ------------------------------
@@ -58,7 +71,7 @@ const Pod = mongoose.model('Pod', PodSchema)
 function toFormatedJSON () {
   const json = {
     id: this._id,
-    url: this.url,
+    host: this.host,
     score: this.score,
     createdDate: this.createdDate
   }
@@ -97,8 +110,8 @@ function load (id, callback) {
   return this.findById(id, callback)
 }
 
-function loadByUrl (url, callback) {
-  return this.findOne({ url: url }, callback)
+function loadByHost (host, callback) {
+  return this.findOne({ host }, callback)
 }
 
 function removeAll (callback) {