]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/videos.js
Fix client linting
[github/Chocobozzz/PeerTube.git] / server / models / videos.js
CommitLineData
9f10b292
C
1'use strict'
2
f0f5567b 3const config = require('config')
f0f5567b 4const mongoose = require('mongoose')
9f10b292 5
f0f5567b 6const logger = require('../helpers/logger')
9f10b292 7
f0f5567b
C
8const http = config.get('webserver.https') === true ? 'https' : 'http'
9const host = config.get('webserver.host')
10const port = config.get('webserver.port')
9f10b292
C
11
12// ---------------------------------------------------------------------------
13
f0f5567b 14const videosSchema = mongoose.Schema({
9f10b292
C
15 name: String,
16 namePath: String,
17 description: String,
18 magnetUri: String,
0c1cbbfe 19 podUrl: String,
3a8a8b51 20 author: String,
cbe2f7c3
C
21 duration: Number,
22 thumbnail: String
9f10b292 23})
f0f5567b 24const VideosDB = mongoose.model('videos', videosSchema)
9f10b292
C
25
26// ---------------------------------------------------------------------------
27
f0f5567b 28const Videos = {
9f10b292
C
29 add: add,
30 addRemotes: addRemotes,
31 get: get,
9f10b292 32 list: list,
cbe2f7c3
C
33 listFromUrl: listFromUrl,
34 listFromUrls: listFromUrls,
35 listFromUrlAndMagnets: listFromUrlAndMagnets,
36 listFromRemotes: listFromRemotes,
9f10b292
C
37 listOwned: listOwned,
38 removeOwned: removeOwned,
cbe2f7c3 39 removeByIds: removeByIds,
9f10b292
C
40 search: search
41}
42
43function add (video, callback) {
44 logger.info('Adding %s video to database.', video.name)
45
f0f5567b 46 const params = video
9f10b292
C
47 params.podUrl = http + '://' + host + ':' + port
48
49 VideosDB.create(params, function (err, video) {
50 if (err) {
51 logger.error('Cannot insert this video into database.')
52 return callback(err)
53 }
54
55 callback(null)
c173e565 56 })
9f10b292 57}
8c308c2b 58
9f10b292 59function addRemotes (videos, callback) {
cbe2f7c3
C
60 videos.forEach(function (video) {
61 // Ensure they are remote videos
62 video.namePath = null
9f10b292 63 })
cbe2f7c3
C
64
65 VideosDB.create(videos, callback)
9f10b292 66}
c45f7f84 67
9f10b292
C
68function get (id, callback) {
69 VideosDB.findById(id, function (err, video) {
70 if (err) {
71 logger.error('Cannot get this video.')
72 return callback(err)
73 }
c173e565 74
9f10b292
C
75 return callback(null, video)
76 })
77}
c173e565 78
9f10b292
C
79function list (callback) {
80 VideosDB.find(function (err, videos_list) {
81 if (err) {
82 logger.error('Cannot get the list of the videos.')
83 return callback(err)
84 }
c173e565 85
9f10b292
C
86 return callback(null, videos_list)
87 })
88}
c45f7f84 89
cbe2f7c3
C
90function listFromUrl (fromUrl, callback) {
91 VideosDB.find({ podUrl: fromUrl }, callback)
92}
93
94function listFromUrls (fromUrls, callback) {
95 VideosDB.find({ podUrl: { $in: fromUrls } }, callback)
96}
97
98function listFromUrlAndMagnets (fromUrl, magnets, callback) {
99 VideosDB.find({ podUrl: fromUrl, magnetUri: { $in: magnets } }, callback)
100}
101
102function listFromRemotes (callback) {
103 VideosDB.find({ namePath: null }, callback)
104}
105
9f10b292
C
106function listOwned (callback) {
107 // If namePath is not null this is *our* video
108 VideosDB.find({ namePath: { $ne: null } }, function (err, videos_list) {
109 if (err) {
110 logger.error('Cannot get the list of owned videos.')
111 return callback(err)
112 }
113
114 return callback(null, videos_list)
115 })
116}
117
cbe2f7c3 118// Return the video in the callback
9f10b292 119function removeOwned (id, callback) {
cbe2f7c3 120 VideosDB.findByIdAndRemove(id, callback)
9f10b292
C
121}
122
123// Use the magnet Uri because the _id field is not the same on different servers
cbe2f7c3
C
124function removeByIds (ids, callback) {
125 VideosDB.remove({ _id: { $in: ids } }, callback)
9f10b292 126}
8c308c2b 127
9f10b292
C
128function search (name, callback) {
129 VideosDB.find({ name: new RegExp(name) }, function (err, videos) {
130 if (err) {
131 logger.error('Cannot search the videos.')
132 return callback(err)
133 }
8c308c2b 134
9f10b292
C
135 return callback(null, videos)
136 })
137}
8c308c2b 138
9f10b292 139// ---------------------------------------------------------------------------
c45f7f84 140
9f10b292 141module.exports = Videos