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