diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-02-04 21:10:33 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-02-04 21:10:33 +0100 |
commit | c173e56520b0fe4206b9ea8049b6add40bfeabcd (patch) | |
tree | 264c6cbf1bf81a6522685b4be5771bbeef4cd5dc /models/poolRequests.js | |
parent | c45f7f84001c2731909db04dd82e1c1f290386eb (diff) | |
download | PeerTube-c173e56520b0fe4206b9ea8049b6add40bfeabcd.tar.gz PeerTube-c173e56520b0fe4206b9ea8049b6add40bfeabcd.tar.zst PeerTube-c173e56520b0fe4206b9ea8049b6add40bfeabcd.zip |
Split models
Diffstat (limited to 'models/poolRequests.js')
-rw-r--r-- | models/poolRequests.js | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/models/poolRequests.js b/models/poolRequests.js new file mode 100644 index 000000000..0f488ef04 --- /dev/null +++ b/models/poolRequests.js | |||
@@ -0,0 +1,67 @@ | |||
1 | ;(function () { | ||
2 | 'use strict' | ||
3 | |||
4 | var mongoose = require('mongoose') | ||
5 | |||
6 | var logger = require('../helpers/logger') | ||
7 | |||
8 | // --------------------------------------------------------------------------- | ||
9 | |||
10 | var poolRequestsSchema = mongoose.Schema({ | ||
11 | type: String, | ||
12 | id: String, // Special id to find duplicates (video created we want to remove...) | ||
13 | request: mongoose.Schema.Types.Mixed | ||
14 | }) | ||
15 | var PoolRequestsDB = mongoose.model('poolRequests', poolRequestsSchema) | ||
16 | |||
17 | // --------------------------------------------------------------------------- | ||
18 | |||
19 | var PoolRequests = { | ||
20 | addRequest: addRequest, | ||
21 | list: list, | ||
22 | removeRequests: removeRequests | ||
23 | } | ||
24 | |||
25 | function addRequest (id, type, request) { | ||
26 | logger.debug('Add request to the pool requests.', { id: id, type: type, request: request }) | ||
27 | |||
28 | PoolRequestsDB.findOne({ id: id }, function (err, entity) { | ||
29 | if (err) logger.error(err) | ||
30 | |||
31 | if (entity) { | ||
32 | if (entity.type === type) { | ||
33 | logger.error(new Error('Cannot insert two same requests.')) | ||
34 | return | ||
35 | } | ||
36 | |||
37 | // Remove the request of the other type | ||
38 | PoolRequestsDB.remove({ id: id }, function (err) { | ||
39 | if (err) logger.error(err) | ||
40 | }) | ||
41 | } else { | ||
42 | PoolRequestsDB.create({ id: id, type: type, request: request }, function (err) { | ||
43 | if (err) logger.error(err) | ||
44 | }) | ||
45 | } | ||
46 | }) | ||
47 | } | ||
48 | |||
49 | function list (callback) { | ||
50 | PoolRequestsDB.find({}, { _id: 1, type: 1, request: 1 }, callback) | ||
51 | } | ||
52 | |||
53 | function removeRequests (ids) { | ||
54 | PoolRequestsDB.remove({ _id: { $in: ids } }, function (err) { | ||
55 | if (err) { | ||
56 | logger.error('Cannot remove requests from the pool requests database.', { error: err }) | ||
57 | return | ||
58 | } | ||
59 | |||
60 | logger.info('Pool requests flushed.') | ||
61 | }) | ||
62 | } | ||
63 | |||
64 | // --------------------------------------------------------------------------- | ||
65 | |||
66 | module.exports = PoolRequests | ||
67 | })() | ||