From 528a9efa8272532bbd0dafc35c3e05e57c50f61e Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Sat, 18 Jun 2016 16:13:54 +0200 Subject: Try to make a better communication (between pods) module --- server/models/requests.js | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) (limited to 'server/models/requests.js') diff --git a/server/models/requests.js b/server/models/requests.js index 2152ae0e9..e67ccad56 100644 --- a/server/models/requests.js +++ b/server/models/requests.js @@ -7,9 +7,8 @@ const logger = require('../helpers/logger') // --------------------------------------------------------------------------- const requestsSchema = mongoose.Schema({ - type: String, - id: String, // Special id to find duplicates (video created we want to remove...) - request: mongoose.Schema.Types.Mixed + request: mongoose.Schema.Types.Mixed, + to: [ { type: mongoose.Schema.Types.ObjectId, ref: 'users' } ] }) const RequestsDB = mongoose.model('requests', requestsSchema) @@ -19,12 +18,15 @@ const Requests = { create: create, findById: findById, list: list, + removeAll: removeAll, + removePodOf: removePodOf, removeRequestById: removeRequestById, - removeRequests: removeRequests + removeRequests: removeRequests, + removeWithEmptyTo: removeWithEmptyTo } -function create (id, type, request, callback) { - RequestsDB.create({ id: id, type: type, request: request }, callback) +function create (request, to, callback) { + RequestsDB.create({ request: request, to: to }, callback) } function findById (id, callback) { @@ -32,7 +34,17 @@ function findById (id, callback) { } function list (callback) { - RequestsDB.find({}, { _id: 1, type: 1, request: 1 }, callback) + RequestsDB.find({}, { _id: 1, request: 1, to: 1 }, callback) +} + +function removeAll (callback) { + RequestsDB.remove({ }, callback) +} + +function removePodOf (requestsIds, podId, callback) { + if (!callback) callback = function () {} + + RequestsDB.update({ _id: { $in: requestsIds } }, { $pull: { to: podId } }, { multi: true }, callback) } function removeRequestById (id, callback) { @@ -50,6 +62,12 @@ function removeRequests (ids) { }) } +function removeWithEmptyTo (callback) { + if (!callback) callback = function () {} + + RequestsDB.remove({ to: { $size: 0 } }, callback) +} + // --------------------------------------------------------------------------- module.exports = Requests -- cgit v1.2.3