From 98ac898a03ed7bbb4edec74fe823b3f2d6d4904a Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 29 Dec 2016 11:17:11 +0100 Subject: [PATCH] Server: use video hook to send information to other pods when a video is deleted --- server/controllers/api/pods.js | 2 +- server/controllers/api/users.js | 32 ++------------------------------ server/controllers/api/videos.js | 24 ++++++------------------ server/models/video.js | 14 ++++++++++++++ 4 files changed, 23 insertions(+), 49 deletions(-) diff --git a/server/controllers/api/pods.js b/server/controllers/api/pods.js index 79f3f9d8d..d9279f1d9 100644 --- a/server/controllers/api/pods.js +++ b/server/controllers/api/pods.js @@ -113,7 +113,7 @@ function removePods (req, res, next) { db.Pod.loadByHost(host, callback) }, - function removePod (pod, callback) { + function deletePod (pod, callback) { pod.destroy().asCallback(callback) } ], function (err) { diff --git a/server/controllers/api/users.js b/server/controllers/api/users.js index 890028b36..e4423680c 100644 --- a/server/controllers/api/users.js +++ b/server/controllers/api/users.js @@ -90,39 +90,11 @@ function listUsers (req, res, next) { function removeUser (req, res, next) { waterfall([ - function getUser (callback) { + function loadUser (callback) { db.User.loadById(req.params.id, callback) }, - // TODO: use foreignkey? - function getVideos (user, callback) { - db.Video.listOwnedByAuthor(user.username, function (err, videos) { - return callback(err, user, videos) - }) - }, - - function removeVideosFromDB (user, videos, callback) { - each(videos, function (video, callbackEach) { - video.destroy().asCallback(callbackEach) - }, function (err) { - return callback(err, user, videos) - }) - }, - - function sendInformationToFriends (user, videos, callback) { - videos.forEach(function (video) { - const params = { - name: video.name, - remoteId: video.id - } - - friends.removeVideoToFriends(params) - }) - - return callback(null, user) - }, - - function removeUserFromDB (user, callback) { + function deleteUser (user, callback) { user.destroy().asCallback(callback) } ], function andFinally (err) { diff --git a/server/controllers/api/videos.js b/server/controllers/api/videos.js index 170224634..ddf85d77d 100644 --- a/server/controllers/api/videos.js +++ b/server/controllers/api/videos.js @@ -249,27 +249,15 @@ function removeVideo (req, res, next) { const videoId = req.params.id waterfall([ - function getVideo (callback) { - db.Video.load(videoId, callback) - }, - - function removeFromDB (video, callback) { - video.destroy().asCallback(function (err) { - if (err) return callback(err) - - return callback(null, video) + function loadVideo (callback) { + db.Video.load(videoId, function (err, video) { + return callback(err, video) }) }, - function sendInformationToFriends (video, callback) { - const params = { - name: video.name, - remoteId: video.id - } - - friends.removeVideoToFriends(params) - - return callback(null) + function deleteVideo (video, callback) { + // Informations to other pods will be sent by the afterDestroy video hook + video.destroy().asCallback(callback) } ], function andFinally (err) { if (err) { diff --git a/server/models/video.js b/server/models/video.js index d1595ce51..564e362fd 100644 --- a/server/models/video.js +++ b/server/models/video.js @@ -12,6 +12,7 @@ const values = require('lodash/values') const constants = require('../initializers/constants') const logger = require('../helpers/logger') +const friends = require('../lib/friends') const modelUtils = require('./utils') const customVideosValidators = require('../helpers/custom-validators').videos @@ -205,11 +206,24 @@ function afterDestroy (video, options, next) { function (callback) { removeFile(video, callback) }, + function (callback) { removeTorrent(video, callback) }, + function (callback) { removePreview(video, callback) + }, + + function (callback) { + const params = { + name: video.name, + remoteId: video.id + } + + friends.removeVideoToFriends(params) + + return callback() } ) } -- 2.25.1