aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/remote/videos.js
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-01-04 20:59:23 +0100
committerChocobozzz <florian.bigard@gmail.com>2017-01-04 21:05:13 +0100
commit55fa55a9be566cca2ba95322f2ae23b434aed62a (patch)
treef51ef35c120ce8a928917a659418079538cdb8dc /server/controllers/api/remote/videos.js
parenta6fd2b30bf717eec14972a2175354781f5f43e77 (diff)
downloadPeerTube-55fa55a9be566cca2ba95322f2ae23b434aed62a.tar.gz
PeerTube-55fa55a9be566cca2ba95322f2ae23b434aed62a.tar.zst
PeerTube-55fa55a9be566cca2ba95322f2ae23b434aed62a.zip
Server: add video abuse support
Diffstat (limited to 'server/controllers/api/remote/videos.js')
-rw-r--r--server/controllers/api/remote/videos.js68
1 files changed, 50 insertions, 18 deletions
diff --git a/server/controllers/api/remote/videos.js b/server/controllers/api/remote/videos.js
index 87c49bff9..d02da4463 100644
--- a/server/controllers/api/remote/videos.js
+++ b/server/controllers/api/remote/videos.js
@@ -7,15 +7,16 @@ const waterfall = require('async/waterfall')
7const db = require('../../../initializers/database') 7const db = require('../../../initializers/database')
8const middlewares = require('../../../middlewares') 8const middlewares = require('../../../middlewares')
9const secureMiddleware = middlewares.secure 9const secureMiddleware = middlewares.secure
10const validators = middlewares.validators.remote 10const videosValidators = middlewares.validators.remote.videos
11const signatureValidators = middlewares.validators.remote.signature
11const logger = require('../../../helpers/logger') 12const logger = require('../../../helpers/logger')
12 13
13const router = express.Router() 14const router = express.Router()
14 15
15router.post('/', 16router.post('/',
16 validators.signature, 17 signatureValidators.signature,
17 secureMiddleware.checkSignature, 18 secureMiddleware.checkSignature,
18 validators.remoteVideos, 19 videosValidators.remoteVideos,
19 remoteVideos 20 remoteVideos
20) 21)
21 22
@@ -32,19 +33,23 @@ function remoteVideos (req, res, next) {
32 // We need to process in the same order to keep consistency 33 // We need to process in the same order to keep consistency
33 // TODO: optimization 34 // TODO: optimization
34 eachSeries(requests, function (request, callbackEach) { 35 eachSeries(requests, function (request, callbackEach) {
35 const videoData = request.data 36 const data = request.data
36 37
37 switch (request.type) { 38 switch (request.type) {
38 case 'add': 39 case 'add':
39 addRemoteVideo(videoData, fromPod, callbackEach) 40 addRemoteVideo(data, fromPod, callbackEach)
40 break 41 break
41 42
42 case 'update': 43 case 'update':
43 updateRemoteVideo(videoData, fromPod, callbackEach) 44 updateRemoteVideo(data, fromPod, callbackEach)
44 break 45 break
45 46
46 case 'remove': 47 case 'remove':
47 removeRemoteVideo(videoData, fromPod, callbackEach) 48 removeRemoteVideo(data, fromPod, callbackEach)
49 break
50
51 case 'report-abuse':
52 reportAbuseRemoteVideo(data, fromPod, callbackEach)
48 break 53 break
49 54
50 default: 55 default:
@@ -164,13 +169,8 @@ function updateRemoteVideo (videoAttributesToUpdate, fromPod, finalCallback) {
164 }, 169 },
165 170
166 function findVideo (t, callback) { 171 function findVideo (t, callback) {
167 db.Video.loadByHostAndRemoteId(fromPod.host, videoAttributesToUpdate.remoteId, function (err, videoInstance) { 172 fetchVideo(fromPod.host, videoAttributesToUpdate.remoteId, function (err, videoInstance) {
168 if (err || !videoInstance) { 173 return callback(err, t, videoInstance)
169 logger.error('Cannot load video from host and remote id.', { error: err.message })
170 return callback(err)
171 }
172
173 return callback(null, t, videoInstance)
174 }) 174 })
175 }, 175 },
176 176
@@ -225,13 +225,45 @@ function updateRemoteVideo (videoAttributesToUpdate, fromPod, finalCallback) {
225 225
226function removeRemoteVideo (videoToRemoveData, fromPod, callback) { 226function removeRemoteVideo (videoToRemoveData, fromPod, callback) {
227 // We need the instance because we have to remove some other stuffs (thumbnail etc) 227 // We need the instance because we have to remove some other stuffs (thumbnail etc)
228 db.Video.loadByHostAndRemoteId(fromPod.host, videoToRemoveData.remoteId, function (err, video) { 228 fetchVideo(fromPod.host, videoToRemoveData.remoteId, function (err, video) {
229 if (err) return callback(err)
230
231 logger.debug('Removing remote video %s.', video.remoteId)
232 video.destroy().asCallback(callback)
233 })
234}
235
236function reportAbuseRemoteVideo (reportData, fromPod, callback) {
237 db.Video.load(reportData.videoRemoteId, function (err, video) {
229 if (err || !video) { 238 if (err || !video) {
230 logger.error('Cannot load video from host and remote id.', { error: err.message }) 239 if (!err) err = new Error('video not found')
240
241 logger.error('Cannot load video from host and remote id.', { error: err })
231 return callback(err) 242 return callback(err)
232 } 243 }
233 244
234 logger.debug('Removing remote video %s.', video.remoteId) 245 logger.debug('Reporting remote abuse for video %s.', video.id)
235 video.destroy().asCallback(callback) 246
247 const videoAbuseData = {
248 reporterUsername: reportData.reporterUsername,
249 reason: reportData.reportReason,
250 reporterPodId: fromPod.id,
251 videoId: video.id
252 }
253
254 db.VideoAbuse.create(videoAbuseData).asCallback(callback)
255 })
256}
257
258function fetchVideo (podHost, remoteId, callback) {
259 db.Video.loadByHostAndRemoteId(podHost, remoteId, function (err, video) {
260 if (err || !video) {
261 if (!err) err = new Error('video not found')
262
263 logger.error('Cannot load video from host and remote id.', { error: err })
264 return callback(err)
265 }
266
267 return callback(null, video)
236 }) 268 })
237} 269}