aboutsummaryrefslogtreecommitdiffhomepage
path: root/controllers/api/v1/remoteVideos.js
blob: d72db983663c5a3511d3ea9a591167192b54efdc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
;(function () {
  'use strict'

  var express = require('express')
  var pluck = require('lodash-node/compat/collection/pluck')

  var middleware = require('../../../middlewares')
  var miscMiddleware = middleware.misc
  var reqValidator = middleware.reqValidators.remote
  var videos = require('../../../models/videos')

  var router = express.Router()

  router.post('/add',
    reqValidator.secureRequest,
    miscMiddleware.decryptBody,
    reqValidator.remoteVideosAdd,
    miscMiddleware.cache(false),
    addRemoteVideos
  )

  router.post('/remove',
    reqValidator.secureRequest,
    miscMiddleware.decryptBody,
    reqValidator.remoteVideosRemove,
    miscMiddleware.cache(false),
    removeRemoteVideo
  )

  // ---------------------------------------------------------------------------

  module.exports = router

  // ---------------------------------------------------------------------------

  function addRemoteVideos (req, res, next) {
    videos.addRemotes(req.body.data, function (err, videos) {
      if (err) return next(err)

      res.json(videos)
    })
  }

  function removeRemoteVideo (req, res, next) {
    var url = req.body.signature.url
    var magnetUris = pluck(req.body.data, 'magnetUri')

    videos.removeRemotesOfByMagnetUris(url, magnetUris, function (err) {
      if (err) return next(err)

      res.sendStatus(204)
    })
  }
})()