From 22284bbc2477c01260c3c1f1cca656311f1244c1 Mon Sep 17 00:00:00 2001 From: Dan Cecile Date: Tue, 6 Oct 2015 21:24:34 -0400 Subject: [PATCH] Issue #2606 - Verify/fix indices during sortTask Because the 'sortTask' operation uses a 'from' index and a 'to' index as parameters, the server needs to check if these indices are valid before performing the client's request. If the task ID doesn't match the task at the 'from' index, then it's clear that the client was looking at a preened list of todos (everything except more-than-3-days-old-non-quest completed tasks). In that case, try to recalculate the indices before continuing. --- common/script/index.coffee | 12 ++++++++++++ website/src/controllers/user.js | 4 +--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/common/script/index.coffee b/common/script/index.coffee index 191c2e12d2..5881a9be6d 100644 --- a/common/script/index.coffee +++ b/common/script/index.coffee @@ -186,6 +186,13 @@ preenHistory = (history) -> newHistory + +### + Preen 3-day past-completed To-Dos from Angular & mobile app +### +api.preenTodos = (tasks) -> + _.where(tasks, (t) -> !t.completed || (t.challenge && t.challenge.id) || moment(t.dateCompleted).isAfter(moment().subtract({days:3}))) + ### Update the in-browser store with new gear. FIXME this was in user.fns, but it was causing strange issues there ### @@ -613,6 +620,11 @@ api.wrap = (user, main=true) -> return cb?({code:404, message: i18n.t('messageTaskNotFound', req.language)}) unless task return cb?('?to=__&from=__ are required') unless to? and from? tasks = user["#{task.type}s"] + if task.type is 'todo' and tasks[from] isnt task # client indices don't match because of preened tasks + preenedTasks = api.preenTodos(tasks); + to = tasks.indexOf(preenedTasks[to]) unless to == -1 # Push To Bottom doesn't require readjustment + from = tasks.indexOf(preenedTasks[from]) + return cb?({code:404, message: i18n.t('messageTaskNotFound', req.language)}) unless tasks[from] is task movedTask = tasks.splice(from, 1)[0] if to == -1 # we've used the Push To Bottom feature tasks.push(movedTask) diff --git a/website/src/controllers/user.js b/website/src/controllers/user.js index 80a8321191..b77d10df7f 100644 --- a/website/src/controllers/user.js +++ b/website/src/controllers/user.js @@ -635,9 +635,7 @@ api.batchUpdate = function(req, res, next) { // Fetch full user object } else if (response.wasModified){ // Preen 3-day past-completed To-Dos from Angular & mobile app - response.todos = _.where(response.todos, function(t) { - return !t.completed || (t.challenge && t.challenge.id) || moment(t.dateCompleted).isAfter(moment().subtract({days:3})); - }); + response.todos = shared.preenTodos(response.todos); res.json(200, response); // return only the version number