From 4e6b2555d0d633511e98fbb7c4dc803cfccfc1cb Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Thu, 15 Sep 2016 10:53:24 -0400 Subject: [PATCH] moved flag history expansion to its own method, showing usernames in assignee history event --- public/language/en_GB/topic.json | 3 ++ src/posts/flags.js | 50 +++++++++++++++++++++++--------- src/views/admin/manage/flags.tpl | 2 +- 3 files changed, 41 insertions(+), 14 deletions(-) diff --git a/public/language/en_GB/topic.json b/public/language/en_GB/topic.json index e38f4b1d84..e3479cb989 100644 --- a/public/language/en_GB/topic.json +++ b/public/language/en_GB/topic.json @@ -47,6 +47,9 @@ "flag_manage_state_rejected": "Rejected", "flag_manage_notes": "Shared Notes", "flag_manage_update": "Update Flag Status", + "flag_manage_history_assignee": "Assigned to %1", + "flag_manage_history_state": "Updated state to %1", + "flag_manage_history_notes": "Updates flag notes", "deleted_message": "This topic has been deleted. Only users with topic management privileges can see it.", diff --git a/src/posts/flags.js b/src/posts/flags.js index 6a9ad6ecfd..62956ee161 100644 --- a/src/posts/flags.js +++ b/src/posts/flags.js @@ -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); + } }; diff --git a/src/views/admin/manage/flags.tpl b/src/views/admin/manage/flags.tpl index 40416d3a70..1ffb5856b1 100644 --- a/src/views/admin/manage/flags.tpl +++ b/src/views/admin/manage/flags.tpl @@ -151,7 +151,7 @@
  • - Updated {../type} to {../value} + [[topic:flag_manage_history_{../type}, {../label}]]