2016-12-01 11:42:06 -05:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
var async = require('async');
|
|
|
|
|
|
|
|
|
|
var user = require('../user');
|
|
|
|
|
var flags = require('../flags');
|
|
|
|
|
|
2017-05-09 14:31:32 -04:00
|
|
|
var SocketFlags = module.exports;
|
2016-12-01 11:42:06 -05:00
|
|
|
|
|
|
|
|
SocketFlags.create = function (socket, data, callback) {
|
|
|
|
|
if (!socket.uid) {
|
|
|
|
|
return callback(new Error('[[error:not-logged-in]]'));
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-06 16:11:56 -05:00
|
|
|
if (!data || !data.type || !data.id || !data.reason) {
|
2016-12-01 11:42:06 -05:00
|
|
|
return callback(new Error('[[error:invalid-data]]'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async.waterfall([
|
2016-12-06 16:11:56 -05:00
|
|
|
async.apply(flags.validate, {
|
|
|
|
|
uid: socket.uid,
|
|
|
|
|
type: data.type,
|
2017-02-24 12:47:46 -05:00
|
|
|
id: data.id,
|
2016-12-06 16:11:56 -05:00
|
|
|
}),
|
2016-12-01 11:42:06 -05:00
|
|
|
function (next) {
|
2016-12-06 16:11:56 -05:00
|
|
|
// If we got here, then no errors occurred
|
|
|
|
|
flags.create(data.type, data.id, socket.uid, data.reason, next);
|
2017-02-24 12:47:46 -05:00
|
|
|
},
|
2016-12-13 12:11:51 -05:00
|
|
|
], function (err, flagObj) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
flags.notify(flagObj, socket.uid);
|
|
|
|
|
callback(null, flagObj);
|
|
|
|
|
});
|
2016-12-01 11:42:06 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
SocketFlags.update = function (socket, data, callback) {
|
|
|
|
|
if (!data || !(data.flagId && data.data)) {
|
|
|
|
|
return callback(new Error('[[error:invalid-data]]'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var payload = {};
|
|
|
|
|
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
async.parallel([
|
|
|
|
|
async.apply(user.isAdminOrGlobalMod, socket.uid),
|
2017-02-24 12:47:46 -05:00
|
|
|
async.apply(user.isModeratorOfAnyCategory, socket.uid),
|
2016-12-01 11:42:06 -05:00
|
|
|
], function (err, results) {
|
|
|
|
|
next(err, results[0] || results[1]);
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
function (allowed, next) {
|
|
|
|
|
if (!allowed) {
|
|
|
|
|
return next(new Error('[[no-privileges]]'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Translate form data into object
|
|
|
|
|
payload = data.data.reduce(function (memo, cur) {
|
|
|
|
|
memo[cur.name] = cur.value;
|
|
|
|
|
return memo;
|
|
|
|
|
}, payload);
|
|
|
|
|
|
|
|
|
|
flags.update(data.flagId, socket.uid, payload, next);
|
2016-12-01 16:22:10 -05:00
|
|
|
},
|
2017-02-24 12:47:46 -05:00
|
|
|
async.apply(flags.getHistory, data.flagId),
|
2016-12-01 11:42:06 -05:00
|
|
|
], callback);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
SocketFlags.appendNote = function (socket, data, callback) {
|
|
|
|
|
if (!data || !(data.flagId && data.note)) {
|
|
|
|
|
return callback(new Error('[[error:invalid-data]]'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
async.parallel([
|
|
|
|
|
async.apply(user.isAdminOrGlobalMod, socket.uid),
|
2017-02-24 12:47:46 -05:00
|
|
|
async.apply(user.isModeratorOfAnyCategory, socket.uid),
|
2016-12-01 11:42:06 -05:00
|
|
|
], function (err, results) {
|
|
|
|
|
next(err, results[0] || results[1]);
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
function (allowed, next) {
|
|
|
|
|
if (!allowed) {
|
|
|
|
|
return next(new Error('[[no-privileges]]'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
flags.appendNote(data.flagId, socket.uid, data.note, next);
|
2016-12-01 16:22:10 -05:00
|
|
|
},
|
|
|
|
|
function (next) {
|
|
|
|
|
async.parallel({
|
2017-02-24 12:47:46 -05:00
|
|
|
notes: async.apply(flags.getNotes, data.flagId),
|
|
|
|
|
history: async.apply(flags.getHistory, data.flagId),
|
2016-12-01 16:22:10 -05:00
|
|
|
}, next);
|
2017-02-24 12:47:46 -05:00
|
|
|
},
|
2016-12-01 11:42:06 -05:00
|
|
|
], callback);
|
|
|
|
|
};
|