]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/pods.js
Server: do not make friends with myself
[github/Chocobozzz/PeerTube.git] / server / models / pods.js
index 59de2d60cb62dd07b17445f24386b1d29c3aba7c..6ab018c1ccc0d107323d573f74960b00453abeb4 100644 (file)
@@ -1,11 +1,14 @@
 '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({
@@ -24,18 +27,18 @@ 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,
+  loadByUrl,
+  removeAll
 }
 
 PodSchema.pre('save', function (next) {
@@ -51,6 +54,17 @@ PodSchema.pre('save', function (next) {
   })
 })
 
+PodSchema.pre('remove', function (next) {
+  // Remove the videos owned by this pod too
+  Video.listByUrl(this.url, function (err, videos) {
+    if (err) return next(err)
+
+    each(videos, function (video, callbackEach) {
+      video.remove(callbackEach)
+    }, next)
+  })
+})
+
 const Pod = mongoose.model('Pod', PodSchema)
 
 // ------------------------------ METHODS ------------------------------