PeerTube/server/models/requests.js

56 lines
1.4 KiB
JavaScript
Raw Normal View History

'use strict'
2016-02-04 21:10:33 +01:00
2016-03-16 22:29:27 +01:00
const mongoose = require('mongoose')
2016-02-04 21:10:33 +01:00
2016-03-16 22:29:27 +01:00
const logger = require('../helpers/logger')
2016-02-04 21:10:33 +01:00
// ---------------------------------------------------------------------------
2016-02-04 21:10:33 +01:00
const requestsSchema = mongoose.Schema({
type: String,
id: String, // Special id to find duplicates (video created we want to remove...)
request: mongoose.Schema.Types.Mixed
})
const RequestsDB = mongoose.model('requests', requestsSchema)
2016-02-04 21:10:33 +01:00
// ---------------------------------------------------------------------------
2016-02-04 21:10:33 +01:00
const Requests = {
create: create,
findById: findById,
list: list,
removeRequestById: removeRequestById,
removeRequests: removeRequests
}
2016-02-04 21:10:33 +01:00
function create (id, type, request, callback) {
RequestsDB.create({ id: id, type: type, request: request }, callback)
}
2016-02-04 21:10:33 +01:00
function findById (id, callback) {
RequestsDB.findOne({ id: id }, callback)
}
2016-02-04 21:10:33 +01:00
function list (callback) {
RequestsDB.find({}, { _id: 1, type: 1, request: 1 }, callback)
}
2016-02-04 21:10:33 +01:00
function removeRequestById (id, callback) {
RequestsDB.remove({ id: id }, callback)
}
function removeRequests (ids) {
RequestsDB.remove({ _id: { $in: ids } }, function (err) {
if (err) {
logger.error('Cannot remove requests from the requests database.', { error: err })
return // Abort
}
2016-02-04 21:10:33 +01:00
logger.info('Pool requests flushed.')
})
}
2016-02-04 21:10:33 +01:00
// ---------------------------------------------------------------------------
2016-02-04 21:10:33 +01:00
module.exports = Requests