2016-11-23 19:41:35 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
var async = require('async');
|
|
|
|
|
var winston = require('winston');
|
|
|
|
|
var db = require('./database');
|
|
|
|
|
var user = require('./user');
|
|
|
|
|
var analytics = require('./analytics');
|
|
|
|
|
var topics = require('./topics');
|
|
|
|
|
var posts = require('./posts');
|
|
|
|
|
var utils = require('../public/src/utils');
|
2016-11-30 20:34:06 -05:00
|
|
|
var _ = require('underscore');
|
2016-11-23 19:41:35 -05:00
|
|
|
|
|
|
|
|
var Flags = {
|
|
|
|
|
_defaults: {
|
2016-11-25 12:43:10 -05:00
|
|
|
state: 'open',
|
|
|
|
|
assignee: null
|
2016-11-23 19:41:35 -05:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2016-11-25 14:17:51 -05:00
|
|
|
Flags.get = function (flagId, callback) {
|
|
|
|
|
async.waterfall([
|
2016-11-29 22:10:51 -05:00
|
|
|
// First stage
|
2016-11-25 14:17:51 -05:00
|
|
|
async.apply(async.parallel, {
|
|
|
|
|
base: async.apply(db.getObject.bind(db), 'flag:' + flagId),
|
|
|
|
|
history: async.apply(db.getSortedSetRevRange.bind(db), 'flag:' + flagId + ':history', 0, -1),
|
2016-11-30 20:34:06 -05:00
|
|
|
notes: async.apply(Flags.getNotes, flagId)
|
2016-11-25 14:17:51 -05:00
|
|
|
}),
|
|
|
|
|
function (data, next) {
|
2016-11-29 22:10:51 -05:00
|
|
|
// Second stage
|
|
|
|
|
async.parallel({
|
|
|
|
|
userObj: async.apply(user.getUserFields, data.base.uid, ['username', 'picture']),
|
|
|
|
|
targetObj: async.apply(Flags.getTarget, data.base.type, data.base.targetId, data.base.uid)
|
|
|
|
|
}, function (err, payload) {
|
|
|
|
|
// Final object return construction
|
2016-11-25 14:17:51 -05:00
|
|
|
next(err, Object.assign(data.base, {
|
2016-11-29 22:10:51 -05:00
|
|
|
datetimeISO: new Date(data.base.datetime).toISOString(),
|
|
|
|
|
target_readable: data.base.type.charAt(0).toUpperCase() + data.base.type.slice(1) + ' ' + data.base.targetId,
|
|
|
|
|
target: payload.targetObj,
|
2016-11-25 14:17:51 -05:00
|
|
|
history: data.history,
|
|
|
|
|
notes: data.notes,
|
|
|
|
|
reporter: {
|
2016-11-29 22:10:51 -05:00
|
|
|
username: payload.userObj.username,
|
|
|
|
|
picture: payload.userObj.picture,
|
|
|
|
|
'icon:bgColor': payload.userObj['icon:bgColor'],
|
|
|
|
|
'icon:text': payload.userObj['icon:text']
|
2016-11-25 14:17:51 -05:00
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
], callback);
|
|
|
|
|
};
|
|
|
|
|
|
2016-11-25 12:43:10 -05:00
|
|
|
Flags.list = function (filters, callback) {
|
|
|
|
|
if (typeof filters === 'function' && !callback) {
|
|
|
|
|
callback = filters;
|
|
|
|
|
filters = {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async.waterfall([
|
|
|
|
|
async.apply(db.getSortedSetRevRange.bind(db), 'flags:datetime', 0, 19),
|
|
|
|
|
function (flagIds, next) {
|
|
|
|
|
async.map(flagIds, function (flagId, next) {
|
|
|
|
|
async.waterfall([
|
|
|
|
|
async.apply(db.getObject, 'flag:' + flagId),
|
|
|
|
|
function (flagObj, next) {
|
|
|
|
|
user.getUserFields(flagObj.uid, ['username', 'picture'], function (err, userObj) {
|
|
|
|
|
next(err, Object.assign(flagObj, {
|
2016-11-25 14:17:51 -05:00
|
|
|
reporter: {
|
2016-11-25 12:43:10 -05:00
|
|
|
username: userObj.username,
|
|
|
|
|
picture: userObj.picture,
|
|
|
|
|
'icon:bgColor': userObj['icon:bgColor'],
|
|
|
|
|
'icon:text': userObj['icon:text']
|
|
|
|
|
}
|
|
|
|
|
}));
|
2016-11-25 15:09:52 -05:00
|
|
|
});
|
2016-11-25 12:43:10 -05:00
|
|
|
}
|
|
|
|
|
], function (err, flagObj) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return next(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch(flagObj.state) {
|
|
|
|
|
case 'open':
|
|
|
|
|
flagObj.labelClass = 'info';
|
|
|
|
|
break;
|
|
|
|
|
case 'wip':
|
|
|
|
|
flagObj.labelClass = 'warning';
|
|
|
|
|
break;
|
|
|
|
|
case 'resolved':
|
|
|
|
|
flagObj.labelClass = 'success';
|
|
|
|
|
break;
|
|
|
|
|
case 'rejected':
|
|
|
|
|
flagObj.labelClass = 'danger';
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
next(null, Object.assign(flagObj, {
|
2016-11-25 14:17:51 -05:00
|
|
|
target_readable: flagObj.type.charAt(0).toUpperCase() + flagObj.type.slice(1) + ' ' + flagObj.targetId,
|
2016-11-25 12:43:10 -05:00
|
|
|
datetimeISO: new Date(parseInt(flagObj.datetime, 10)).toISOString()
|
|
|
|
|
}));
|
|
|
|
|
});
|
|
|
|
|
}, next);
|
|
|
|
|
}
|
|
|
|
|
], function (err, flags) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return callback(null, flags);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2016-11-29 22:10:51 -05:00
|
|
|
Flags.getTarget = function (type, id, uid, callback) {
|
|
|
|
|
switch (type) {
|
|
|
|
|
case 'post':
|
|
|
|
|
async.waterfall([
|
|
|
|
|
async.apply(posts.getPostsByPids, [id], uid),
|
|
|
|
|
function (posts, next) {
|
|
|
|
|
topics.addPostData(posts, uid, next);
|
|
|
|
|
}
|
|
|
|
|
], function (err, posts) {
|
|
|
|
|
callback(err, posts[0]);
|
|
|
|
|
});
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'user':
|
2016-12-01 09:24:49 -05:00
|
|
|
user.getUsersData([id], function (err, users) {
|
|
|
|
|
callback(err, users ? users[0] : undefined);
|
|
|
|
|
});
|
2016-11-29 22:10:51 -05:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2016-11-30 20:34:06 -05:00
|
|
|
Flags.getNotes = function (flagId, callback) {
|
|
|
|
|
async.waterfall([
|
|
|
|
|
async.apply(db.getSortedSetRevRangeWithScores.bind(db), 'flag:' + flagId + ':notes', 0, -1),
|
|
|
|
|
function (notes, next) {
|
|
|
|
|
var uids = [];
|
|
|
|
|
var noteObj;
|
|
|
|
|
notes = notes.map(function (note) {
|
|
|
|
|
try {
|
|
|
|
|
noteObj = JSON.parse(note.value);
|
|
|
|
|
uids.push(noteObj[0]);
|
|
|
|
|
return {
|
|
|
|
|
uid: noteObj[0],
|
|
|
|
|
content: noteObj[1],
|
|
|
|
|
datetime: note.score,
|
|
|
|
|
datetimeISO: new Date(note.score).toISOString()
|
|
|
|
|
};
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return next(e);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
next(null, notes, uids);
|
|
|
|
|
},
|
|
|
|
|
function (notes, uids, next) {
|
|
|
|
|
user.getUsersData(uids, function (err, users) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return next(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
next(null, notes.map(function (note, idx) {
|
|
|
|
|
note.user = users[idx];
|
|
|
|
|
return note;
|
|
|
|
|
}));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
], callback);
|
|
|
|
|
};
|
|
|
|
|
|
2016-11-23 19:41:35 -05:00
|
|
|
Flags.create = function (type, id, uid, reason, callback) {
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
// Sanity checks
|
|
|
|
|
async.parallel([
|
|
|
|
|
async.apply(Flags.exists, type, id, uid),
|
|
|
|
|
async.apply(Flags.targetExists, type, id)
|
|
|
|
|
], function (err, checks) {
|
2016-11-25 15:09:52 -05:00
|
|
|
if (err) {
|
|
|
|
|
return next(err);
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-23 19:41:35 -05:00
|
|
|
if (checks[0]) {
|
|
|
|
|
return next(new Error('[[error:already-flagged]]'));
|
|
|
|
|
} else if (!checks[1]) {
|
|
|
|
|
return next(new Error('[[error:invalid-data]]'));
|
|
|
|
|
} else {
|
|
|
|
|
next();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
},
|
2016-11-25 14:17:51 -05:00
|
|
|
async.apply(db.incrObjectField, 'global', 'nextFlagId'),
|
|
|
|
|
function (flagId, next) {
|
2016-11-23 19:41:35 -05:00
|
|
|
async.parallel([
|
2016-11-25 12:43:10 -05:00
|
|
|
async.apply(db.setObject.bind(db), 'flag:' + flagId, Object.assign({}, Flags._defaults, {
|
2016-11-25 14:17:51 -05:00
|
|
|
flagId: flagId,
|
2016-11-25 12:43:10 -05:00
|
|
|
type: type,
|
2016-11-25 14:17:51 -05:00
|
|
|
targetId: id,
|
2016-11-25 12:43:10 -05:00
|
|
|
description: reason,
|
|
|
|
|
uid: uid,
|
|
|
|
|
datetime: Date.now()
|
|
|
|
|
})),
|
2016-11-25 14:17:51 -05:00
|
|
|
async.apply(db.sortedSetAdd.bind(db), 'flags:datetime', Date.now(), flagId),
|
|
|
|
|
async.apply(db.setObjectField.bind(db), 'flagHash:flagId', [type, id, uid].join(':'), flagId)
|
2016-11-25 15:09:52 -05:00
|
|
|
], function (err, data) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return next(err);
|
|
|
|
|
}
|
2016-11-23 19:41:35 -05:00
|
|
|
|
2016-11-25 15:09:52 -05:00
|
|
|
next(null, flagId);
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
async.apply(Flags.get)
|
|
|
|
|
], callback);
|
2016-11-23 19:41:35 -05:00
|
|
|
// if (!parseInt(uid, 10) || !reason) {
|
|
|
|
|
// return callback();
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// async.waterfall([
|
|
|
|
|
// function (next) {
|
|
|
|
|
// async.parallel({
|
|
|
|
|
// hasFlagged: async.apply(Flags.isFlaggedByUser, post.pid, uid),
|
|
|
|
|
// exists: async.apply(Posts.exists, post.pid)
|
|
|
|
|
// }, next);
|
|
|
|
|
// },
|
|
|
|
|
// function (results, next) {
|
|
|
|
|
// if (!results.exists) {
|
|
|
|
|
// return next(new Error('[[error:no-post]]'));
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// if (results.hasFlagged) {
|
|
|
|
|
// return next(new Error('[[error:already-flagged]]'));
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// var now = Date.now();
|
|
|
|
|
// async.parallel([
|
|
|
|
|
// function (next) {
|
|
|
|
|
// db.sortedSetAdd('posts:flagged', now, post.pid, next);
|
|
|
|
|
// },
|
|
|
|
|
// function (next) {
|
|
|
|
|
// db.sortedSetIncrBy('posts:flags:count', 1, post.pid, next);
|
|
|
|
|
// },
|
|
|
|
|
// function (next) {
|
|
|
|
|
// db.incrObjectField('post:' + post.pid, 'flags', next);
|
|
|
|
|
// },
|
|
|
|
|
// function (next) {
|
|
|
|
|
// db.sortedSetAdd('pid:' + post.pid + ':flag:uids', now, uid, next);
|
|
|
|
|
// },
|
|
|
|
|
// function (next) {
|
|
|
|
|
// db.sortedSetAdd('pid:' + post.pid + ':flag:uid:reason', 0, uid + ':' + reason, next);
|
|
|
|
|
// },
|
|
|
|
|
// function (next) {
|
|
|
|
|
// if (parseInt(post.uid, 10)) {
|
|
|
|
|
// async.parallel([
|
|
|
|
|
// async.apply(db.sortedSetIncrBy, 'users:flags', 1, post.uid),
|
|
|
|
|
// async.apply(db.incrObjectField, 'user:' + post.uid, 'flags'),
|
|
|
|
|
// async.apply(db.sortedSetAdd, 'uid:' + post.uid + ':flag:pids', now, post.pid)
|
|
|
|
|
// ], next);
|
|
|
|
|
// } else {
|
|
|
|
|
// next();
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// ], next);
|
|
|
|
|
// },
|
|
|
|
|
// function (data, next) {
|
2016-11-25 12:43:10 -05:00
|
|
|
// openNewFlag(post.pid, uid, next); // removed, used to just update flag to open state if new flag
|
2016-11-23 19:41:35 -05:00
|
|
|
// }
|
|
|
|
|
// ], function (err) {
|
|
|
|
|
// if (err) {
|
|
|
|
|
// return callback(err);
|
|
|
|
|
// }
|
|
|
|
|
// analytics.increment('flags');
|
|
|
|
|
// callback();
|
|
|
|
|
// });
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Flags.exists = function (type, id, uid, callback) {
|
|
|
|
|
db.isObjectField('flagHash:flagId', [type, id, uid].join(':'), callback);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Flags.targetExists = function (type, id, callback) {
|
|
|
|
|
switch (type) {
|
|
|
|
|
case 'topic':
|
|
|
|
|
topics.exists(id, callback);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'post':
|
|
|
|
|
posts.exists(id, callback);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Flags.dismiss = function (pid, callback) {
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
db.getObjectFields('post:' + pid, ['pid', 'uid', 'flags'], next);
|
|
|
|
|
},
|
|
|
|
|
function (postData, next) {
|
|
|
|
|
if (!postData.pid) {
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
|
|
|
|
async.parallel([
|
|
|
|
|
function (next) {
|
|
|
|
|
if (parseInt(postData.uid, 10)) {
|
|
|
|
|
if (parseInt(postData.flags, 10) > 0) {
|
|
|
|
|
async.parallel([
|
|
|
|
|
async.apply(db.sortedSetIncrBy, 'users:flags', -postData.flags, postData.uid),
|
|
|
|
|
async.apply(db.incrObjectFieldBy, 'user:' + postData.uid, 'flags', -postData.flags)
|
|
|
|
|
], next);
|
|
|
|
|
} else {
|
|
|
|
|
next();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
next();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
function (next) {
|
|
|
|
|
db.sortedSetsRemove([
|
|
|
|
|
'posts:flagged',
|
|
|
|
|
'posts:flags:count',
|
|
|
|
|
'uid:' + postData.uid + ':flag:pids'
|
|
|
|
|
], pid, next);
|
|
|
|
|
},
|
|
|
|
|
function (next) {
|
|
|
|
|
async.series([
|
|
|
|
|
function (next) {
|
|
|
|
|
db.getSortedSetRange('pid:' + pid + ':flag:uids', 0, -1, function (err, uids) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return next(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async.each(uids, function (uid, next) {
|
|
|
|
|
var nid = 'post_flag:' + pid + ':uid:' + uid;
|
|
|
|
|
async.parallel([
|
|
|
|
|
async.apply(db.delete, 'notifications:' + nid),
|
|
|
|
|
async.apply(db.sortedSetRemove, 'notifications', 'post_flag:' + pid + ':uid:' + uid)
|
|
|
|
|
], next);
|
|
|
|
|
}, next);
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
async.apply(db.delete, 'pid:' + pid + ':flag:uids')
|
|
|
|
|
], next);
|
|
|
|
|
},
|
|
|
|
|
async.apply(db.deleteObjectField, 'post:' + pid, 'flags'),
|
|
|
|
|
async.apply(db.delete, 'pid:' + pid + ':flag:uid:reason'),
|
|
|
|
|
async.apply(db.deleteObjectFields, 'post:' + pid, ['flag:state', 'flag:assignee', 'flag:notes', 'flag:history'])
|
|
|
|
|
], next);
|
|
|
|
|
},
|
|
|
|
|
function (results, next) {
|
|
|
|
|
db.sortedSetsRemoveRangeByScore(['users:flags'], '-inf', 0, next);
|
|
|
|
|
}
|
|
|
|
|
], callback);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Pretty sure we don't need this method...
|
|
|
|
|
Flags.dismissAll = function (callback) {
|
|
|
|
|
db.getSortedSetRange('posts:flagged', 0, -1, function (err, pids) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
async.eachSeries(pids, Flags.dismiss, callback);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Flags.dismissByUid = function (uid, callback) {
|
|
|
|
|
db.getSortedSetRange('uid:' + uid + ':flag:pids', 0, -1, function (err, pids) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
async.eachSeries(pids, Flags.dismiss, callback);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2016-12-01 11:42:06 -05:00
|
|
|
Flags.update = function (flagId, uid, changeset, callback) {
|
2016-11-23 19:41:35 -05:00
|
|
|
// Retrieve existing flag data to compare for history-saving purposes
|
2016-12-01 11:42:06 -05:00
|
|
|
var fields = ['state', 'assignee'];
|
2016-11-23 19:41:35 -05:00
|
|
|
|
2016-12-01 11:42:06 -05:00
|
|
|
async.waterfall([
|
|
|
|
|
async.apply(db.getObjectFields.bind(db), 'flag:' + flagId, fields),
|
|
|
|
|
function (current, next) {
|
|
|
|
|
for(var prop in changeset) {
|
|
|
|
|
if (changeset.hasOwnProperty(prop)) {
|
|
|
|
|
if (current[prop] === changeset[prop]) {
|
|
|
|
|
delete changeset[prop];
|
2016-11-23 19:41:35 -05:00
|
|
|
}
|
2016-12-01 11:42:06 -05:00
|
|
|
}
|
|
|
|
|
}
|
2016-11-23 19:41:35 -05:00
|
|
|
|
2016-12-01 11:42:06 -05:00
|
|
|
if (!Object.keys(changeset).length) {
|
|
|
|
|
// No changes
|
|
|
|
|
return next();
|
2016-11-23 19:41:35 -05:00
|
|
|
}
|
|
|
|
|
|
2016-12-01 11:42:06 -05:00
|
|
|
async.parallel([
|
|
|
|
|
// Save new object to db (upsert)
|
|
|
|
|
async.apply(db.setObject, 'flag:' + flagId, changeset),
|
|
|
|
|
// Append history
|
|
|
|
|
async.apply(Flags.appendHistory, flagId, uid, Object.keys(changeset))
|
|
|
|
|
], next);
|
2016-11-23 19:41:35 -05:00
|
|
|
}
|
2016-12-01 11:42:06 -05:00
|
|
|
], callback);
|
2016-11-23 19:41:35 -05:00
|
|
|
};
|
|
|
|
|
|
2016-12-01 11:42:06 -05:00
|
|
|
Flags.appendHistory = function (flagId, uid, changeset, callback) {
|
|
|
|
|
return callback();
|
|
|
|
|
};
|
2016-11-23 19:41:35 -05:00
|
|
|
|
2016-12-01 11:42:06 -05:00
|
|
|
Flags.appendNote = function (flagId, uid, note, callback) {
|
|
|
|
|
var payload;
|
|
|
|
|
try {
|
|
|
|
|
payload = JSON.stringify([uid, note]);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return callback(e);
|
|
|
|
|
}
|
2016-11-23 19:41:35 -05:00
|
|
|
|
2016-12-01 11:42:06 -05:00
|
|
|
async.waterfall([
|
|
|
|
|
async.apply(db.sortedSetAdd, 'flag:' + flagId + ':notes', Date.now(), payload),
|
|
|
|
|
async.apply(Flags.getNotes, flagId)
|
|
|
|
|
], callback);
|
2016-11-23 19:41:35 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = Flags;
|