PeerTube/models/poolRequests.js

75 lines
2.1 KiB
JavaScript
Raw Normal View History

2016-02-04 21:10:33 +01:00
;(function () {
'use strict'
var mongoose = require('mongoose')
var logger = require('../helpers/logger')
// ---------------------------------------------------------------------------
var poolRequestsSchema = mongoose.Schema({
type: String,
id: String, // Special id to find duplicates (video created we want to remove...)
request: mongoose.Schema.Types.Mixed
})
var PoolRequestsDB = mongoose.model('poolRequests', poolRequestsSchema)
// ---------------------------------------------------------------------------
var PoolRequests = {
addRequest: addRequest,
list: list,
removeRequests: removeRequests
}
function addRequest (id, type, request) {
logger.debug('Add request to the pool requests.', { id: id, type: type, request: request })
PoolRequestsDB.findOne({ id: id }, function (err, entity) {
2016-02-05 19:02:05 +01:00
if (err) {
logger.error('Cannot find one pool request.', { error: err })
return // Abort
}
2016-02-04 21:10:33 +01:00
if (entity) {
if (entity.type === type) {
2016-02-05 19:02:05 +01:00
logger.error('Cannot insert two same requests.')
return // Abort
2016-02-04 21:10:33 +01:00
}
// Remove the request of the other type
PoolRequestsDB.remove({ id: id }, function (err) {
2016-02-05 19:02:05 +01:00
if (err) {
logger.error('Cannot remove a pool request.', { error: err })
return // Abort
}
2016-02-04 21:10:33 +01:00
})
} else {
PoolRequestsDB.create({ id: id, type: type, request: request }, function (err) {
2016-02-05 19:02:05 +01:00
logger.error('Cannot create a pool request.', { error: err })
return // Abort
2016-02-04 21:10:33 +01:00
})
}
})
}
function list (callback) {
PoolRequestsDB.find({}, { _id: 1, type: 1, request: 1 }, callback)
}
function removeRequests (ids) {
PoolRequestsDB.remove({ _id: { $in: ids } }, function (err) {
if (err) {
logger.error('Cannot remove requests from the pool requests database.', { error: err })
2016-02-05 19:02:05 +01:00
return // Abort
2016-02-04 21:10:33 +01:00
}
logger.info('Pool requests flushed.')
})
}
// ---------------------------------------------------------------------------
module.exports = PoolRequests
})()