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