moved flag history expansion to its own method, showing usernames in assignee history event

This commit is contained in:
Julian Lam
2016-09-15 10:53:24 -04:00
parent d04a10225c
commit 4e6b2555d0
3 changed files with 41 additions and 14 deletions

View File

@@ -195,24 +195,13 @@ module.exports = function(Posts) {
if (post) {
post.flagReasons = reasons[index];
// Expand flag history
try {
history = JSON.parse(post['flag:history'] || '[]');
history.map(function(event) {
event.timestampISO = new Date(event.timestamp).toISOString();
return event;
});
post['flag:history'] = history;
} catch (e) {
winston.warn('[posts/getFlags] Unable to deserialise post flag history, likely malformed data');
}
}
});
next(null, results.posts);
});
}
},
async.apply(Posts.expandFlagHistory)
], callback);
}
@@ -305,4 +294,39 @@ module.exports = function(Posts) {
Posts.setPostFields(pid, changeset, callback);
});
};
Posts.expandFlagHistory = function(posts, callback) {
// Expand flag history
async.map(posts, function(post, next) {
try {
var history = JSON.parse(post['flag:history'] || '[]');
} catch (e) {
winston.warn('[posts/getFlags] Unable to deserialise post flag history, likely malformed data');
callback(e);
}
async.map(history, function(event, next) {
event.timestampISO = new Date(event.timestamp).toISOString();
if (event.type === 'assignee') {
user.getUserField(parseInt(event.value, 10), 'username', function(err, username) {
if (err) {
return next(err);
}
event.label = username || 'Unknown user';
next(null, event);
});
} else if (event.type === 'state') {
event.label = '[[topic:flag_manage_state_' + event.value + ']]';
setImmediate(next.bind(null, null, event));
} else {
setImmediate(next.bind(null, null, event));
}
}, function(err, history) {
post['flag:history'] = history;
next(null, post);
});
}, callback);
}
};