]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/models/videos.js
Extends the search feature by customizing the search field (name,
[github/Chocobozzz/PeerTube.git] / server / models / videos.js
... / ...
CommitLineData
1'use strict'
2
3const async = require('async')
4const config = require('config')
5const mongoose = require('mongoose')
6
7const logger = require('../helpers/logger')
8
9const http = config.get('webserver.https') === true ? 'https' : 'http'
10const host = config.get('webserver.host')
11const port = config.get('webserver.port')
12
13// ---------------------------------------------------------------------------
14
15const videosSchema = mongoose.Schema({
16 name: String,
17 namePath: String,
18 description: String,
19 magnetUri: String,
20 podUrl: String,
21 author: String,
22 duration: Number,
23 thumbnail: String,
24 createdDate: {
25 type: Date,
26 default: Date.now
27 }
28})
29const VideosDB = mongoose.model('videos', videosSchema)
30
31// ---------------------------------------------------------------------------
32
33const Videos = {
34 add: add,
35 addRemotes: addRemotes,
36 get: get,
37 list: list,
38 listFromUrl: listFromUrl,
39 listFromUrls: listFromUrls,
40 listFromUrlAndMagnets: listFromUrlAndMagnets,
41 listFromRemotes: listFromRemotes,
42 listOwned: listOwned,
43 removeOwned: removeOwned,
44 removeByIds: removeByIds,
45 search: search
46}
47
48function add (video, callback) {
49 logger.info('Adding %s video to database.', video.name)
50
51 const params = video
52 params.podUrl = http + '://' + host + ':' + port
53
54 VideosDB.create(params, function (err, insertedVideo) {
55 if (err) {
56 logger.error('Cannot insert this video into database.')
57 return callback(err)
58 }
59
60 callback(null, insertedVideo)
61 })
62}
63
64function addRemotes (videos, callback) {
65 videos.forEach(function (video) {
66 // Ensure they are remote videos
67 video.namePath = null
68 })
69
70 VideosDB.create(videos, callback)
71}
72
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 }
79
80 return callback(null, video)
81 })
82}
83
84function list (start, count, sort, callback) {
85 const query = {}
86 return findWithCount(query, start, count, sort, callback)
87}
88
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
105function listOwned (callback) {
106 // If namePath is not null this is *our* video
107 VideosDB.find({ namePath: { $ne: null } }, function (err, videosList) {
108 if (err) {
109 logger.error('Cannot get the list of owned videos.')
110 return callback(err)
111 }
112
113 return callback(null, videosList)
114 })
115}
116
117// Return the video in the callback
118function removeOwned (id, callback) {
119 VideosDB.findByIdAndRemove(id, callback)
120}
121
122// Use the magnet Uri because the _id field is not the same on different servers
123function removeByIds (ids, callback) {
124 VideosDB.remove({ _id: { $in: ids } }, callback)
125}
126
127function search (value, field, start, count, sort, callback) {
128 const query = {}
129 // Make an exact search with the magnet
130 if (field === 'magnetUri') {
131 query[field] = value
132 } else {
133 query[field] = new RegExp(value)
134 }
135
136 findWithCount(query, start, count, sort, callback)
137}
138
139// ---------------------------------------------------------------------------
140
141module.exports = Videos
142
143// ---------------------------------------------------------------------------
144
145function findWithCount (query, start, count, sort, callback) {
146 async.parallel([
147 function (asyncCallback) {
148 VideosDB.find(query).skip(start).limit(start + count).sort(sort).exec(asyncCallback)
149 },
150 function (asyncCallback) {
151 VideosDB.count(query, asyncCallback)
152 }
153 ], function (err, results) {
154 if (err) return callback(err)
155
156 const videos = results[0]
157 const totalVideos = results[1]
158 return callback(null, videos, totalVideos)
159 })
160}