From 1f674aab41721f14d3f789aebb2e83f6bee3d5b8 Mon Sep 17 00:00:00 2001 From: Negue Date: Fri, 8 May 2015 18:52:29 +0200 Subject: [PATCH] /user/anonymized - return user json without personal data --- test/api.mocha.coffee | 6 ++++ website/src/controllers/user.js | 58 +++++++++++++++++++++++++++++++++ website/src/routes/apiv2.coffee | 5 +++ 3 files changed, 69 insertions(+) diff --git a/test/api.mocha.coffee b/test/api.mocha.coffee index 2a853562cf..caf608b14c 100644 --- a/test/api.mocha.coffee +++ b/test/api.mocha.coffee @@ -259,6 +259,12 @@ describe "API", -> expect(body.err).to.be "Task not found." done() + describe "Anonymized User", -> + it "/api/v2/user/anonymized", (done) -> + request.get(baseURL + "/user/anonymized").set("Accept", "application/json").end (res) -> + expect(res.statusCode).to.be 200 + done() + ###* GROUPS ### diff --git a/website/src/controllers/user.js b/website/src/controllers/user.js index 949e4cfb7b..3956cfa540 100644 --- a/website/src/controllers/user.js +++ b/website/src/controllers/user.js @@ -215,6 +215,64 @@ api.getUser = function(req, res, next) { return res.json(200, user); }; +/** + * Get anonymized User + */ +api.getUserAnonymized = function(req, res, next) { + var user = res.locals.user.toJSON(); + user.stats.toNextLevel = shared.tnl(user.stats.lvl); + user.stats.maxHealth = 50; + user.stats.maxMP = res.locals.user._statsComputed.maxMP; + delete user.apiToken; + if (user.auth) { + delete user.auth; + } + + delete user.webhooks; + + _.forEach(user.inbox.messages, function(msg){ + msg.text = "inbox message text"; + }); + + _.forEach(user.tags, function(tag){ + tag.name = "tag"; + tag.challenge = "challenge"; + }); + + function cleanChecklist(task){ + var checklistIndex = 0; + + _.forEach(task.checklist, function(c){ + c.text = "item" + checklistIndex++; + }); + } + + _.forEach(user.habits, function(task){ + task.text = "task text"; + task.notes = "task notes"; + }); + + _.forEach(user.rewards, function(task){ + task.text = "task text"; + task.notes = "task notes"; + }); + + _.forEach(user.dailys, function(task){ + task.text = "task text"; + task.notes = "task notes"; + + cleanChecklist(task); + }); + + _.forEach(user.todos, function(task){ + task.text = "task text"; + task.notes = "task notes"; + + cleanChecklist(task); + }); + + return res.json(200, user); +}; /** * This tells us for which paths users can call `PUT /user` (or batch-update equiv, which use `User.set()` on our client). diff --git a/website/src/routes/apiv2.coffee b/website/src/routes/apiv2.coffee index f30b03cb53..b4ae4a5a1e 100644 --- a/website/src/routes/apiv2.coffee +++ b/website/src/routes/apiv2.coffee @@ -227,6 +227,11 @@ module.exports = (swagger, v2) -> description: "Get the full user object" action: user.getUser + "/user/anonymized": + spec: + description: "Get the user object without any personal data" + action: user.getUserAnonymized + "/user:PUT": spec: path: '/user'