mirror of
https://github.com/sudoxnym/habitica.git
synced 2026-08-05 12:02:13 +00:00
apiv2: reapplying cleanup fix of b88c2ad
This commit is contained in:
parent
41ff34d1d6
commit
0ae0c57858
2 changed files with 51 additions and 81 deletions
|
|
@ -28,7 +28,7 @@ score = (model, user, taskId, direction, cb) ->
|
||||||
misc.batchTxn model, (uObj, paths) ->
|
misc.batchTxn model, (uObj, paths) ->
|
||||||
tObj = uObj.tasks[taskId]
|
tObj = uObj.tasks[taskId]
|
||||||
delta = algos.score(uObj, tObj, direction, {paths})
|
delta = algos.score(uObj, tObj, direction, {paths})
|
||||||
, {user, cb}
|
, {user, done:cb}
|
||||||
delta
|
delta
|
||||||
|
|
||||||
# ---------- /api/v1 API ------------
|
# ---------- /api/v1 API ------------
|
||||||
|
|
@ -216,7 +216,7 @@ router.put '/user/task/:id', auth, validateTask, (req, res) ->
|
||||||
DELETE /user/task/:id
|
DELETE /user/task/:id
|
||||||
###
|
###
|
||||||
router.delete '/user/task/:id', auth, validateTask, (req, res) ->
|
router.delete '/user/task/:id', auth, validateTask, (req, res) ->
|
||||||
deleteTask user, req.task.type, req.task.id
|
deleteTask req.user, req.task.type, req.task.id
|
||||||
res.send 204
|
res.send 204
|
||||||
|
|
||||||
###
|
###
|
||||||
|
|
@ -306,7 +306,7 @@ scoreTask = (req, res, next) ->
|
||||||
addTask user, task
|
addTask user, task
|
||||||
|
|
||||||
# TODO - could modify batchTxn to conform to this better
|
# TODO - could modify batchTxn to conform to this better
|
||||||
delta = score model, req.user, taskId, direction, ->
|
delta = score model, user, taskId, direction, ->
|
||||||
result = user.get('stats')
|
result = user.get('stats')
|
||||||
result.delta = delta
|
result.delta = delta
|
||||||
res.json result
|
res.json result
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
express = require 'express'
|
express = require 'express'
|
||||||
router = new express.Router()
|
router = new express.Router()
|
||||||
util = require('util')
|
util = require 'util'
|
||||||
|
|
||||||
_ = require 'lodash'
|
_ = require 'lodash'
|
||||||
algos = require 'habitrpg-shared/script/algos'
|
algos = require 'habitrpg-shared/script/algos'
|
||||||
|
|
@ -9,13 +9,9 @@ validator = require 'derby-auth/node_modules/validator'
|
||||||
check = validator.check
|
check = validator.check
|
||||||
sanitize = validator.sanitize
|
sanitize = validator.sanitize
|
||||||
misc = require '../app/misc'
|
misc = require '../app/misc'
|
||||||
|
api = require './api'
|
||||||
|
|
||||||
NO_TOKEN_OR_UID =
|
# ---------- /api/v2 API ------------
|
||||||
err: "You must include a token and uid (user id) in your request"
|
|
||||||
NO_USER_FOUND =
|
|
||||||
err: "No user found."
|
|
||||||
|
|
||||||
# ---------- /api/v1 API ------------
|
|
||||||
# Every url added beneath router is prefaced by /api/v2
|
# Every url added beneath router is prefaced by /api/v2
|
||||||
|
|
||||||
###
|
###
|
||||||
|
|
@ -24,94 +20,68 @@ NO_USER_FOUND =
|
||||||
router.get '/status', (req, res) ->
|
router.get '/status', (req, res) ->
|
||||||
res.json status: 'up'
|
res.json status: 'up'
|
||||||
|
|
||||||
###
|
|
||||||
beforeEach auth interceptor
|
|
||||||
###
|
|
||||||
auth = (req, res, next) ->
|
|
||||||
uid = req.headers['x-api-user']
|
|
||||||
token = req.headers['x-api-key']
|
|
||||||
console.log uid, token
|
|
||||||
return res.json 401, NO_TOKEN_OR_UID unless uid || token
|
|
||||||
|
|
||||||
model = req.getModel()
|
|
||||||
|
|
||||||
model.query('users').withIdAndToken(uid, token).fetch (err, user) ->
|
|
||||||
return res.json err: err if err
|
|
||||||
req.user = user
|
|
||||||
req.userObj = user.get()
|
|
||||||
return res.json 401, NO_USER_FOUND if !req.userObj || _.isEmpty(req.userObj)
|
|
||||||
req._isServer = true
|
|
||||||
model.ref('_user', user)
|
|
||||||
next()
|
|
||||||
|
|
||||||
###
|
###
|
||||||
POST new actions
|
POST new actions
|
||||||
###
|
###
|
||||||
router.post '/', auth, (req, res) ->
|
router.post '/', api.auth, (req, res, next) ->
|
||||||
model = req.getModel()
|
model = req.getModel()
|
||||||
user = req.user
|
{user} = req
|
||||||
actions = req.body
|
actions = req.body
|
||||||
console.log util.inspect req.body
|
#console.log util.inspect req.body
|
||||||
|
|
||||||
|
doneCount = 0
|
||||||
|
done = (err) ->
|
||||||
|
return next(err) if err
|
||||||
|
if --doneCount is 0
|
||||||
|
uObj = misc.hydrate user.get()
|
||||||
|
#transform user structure FROM user.tasks{} + user.habitIds[] TO user.habits[] + user.todos[] etc.
|
||||||
|
_.each ['habit','daily','todo','reward'], (type) ->
|
||||||
|
uObj["#{type}s"] = _.where(uObj.tasks, {type}); true
|
||||||
|
delete uObj["#{type}Ids"]
|
||||||
|
delete uObj.tasks
|
||||||
|
res.json 200, uObj
|
||||||
|
console.log "Reply sent"
|
||||||
|
|
||||||
misc.batchTxn model, (uObj, paths) ->
|
misc.batchTxn model, (uObj, paths) ->
|
||||||
|
doneCount++
|
||||||
# habitrpg-shared/algos requires uObj.habits, uObj.dailys etc instead of uObj.tasks
|
# habitrpg-shared/algos requires uObj.habits, uObj.dailys etc instead of uObj.tasks
|
||||||
_.each ['habit','daily','todo','reward'], (type) -> uObj["#{type}s"] = _.where(uObj.tasks, {type}); true
|
_.each ['habit','daily','todo','reward'], (type) -> uObj["#{type}s"] = _.where(uObj.tasks, {type}); true
|
||||||
algos.cron uObj, {paths}
|
algos.cron uObj, {paths}
|
||||||
,{cron:true}
|
, {user, done, cron:true}
|
||||||
|
|
||||||
|
|
||||||
_.each ['habit', 'daily', 'todo', 'reward'], (type) ->
|
|
||||||
model.refList "_#{type}List", "_user.tasks", "_user.#{type}Ids"
|
|
||||||
|
|
||||||
if _.isArray actions
|
if _.isArray actions
|
||||||
actions.forEach (action)->
|
actions.forEach (action)->
|
||||||
task = {}
|
doneCount++
|
||||||
if action.task? then task = action.task
|
|
||||||
|
|
||||||
if action.op == "score"
|
task = action.task ? {}
|
||||||
if task.type == "daily" || task.type == "todo"
|
|
||||||
|
switch action.op
|
||||||
|
when "score"
|
||||||
|
if task.type in ["daily","todo"]
|
||||||
# switch completed state. Since checkbox is not binded to model unlike when you click through Derby website.
|
# switch completed state. Since checkbox is not binded to model unlike when you click through Derby website.
|
||||||
completed = if action.dir == "up" then true else false
|
completed = if action.dir is "up" then true else false
|
||||||
user.set("tasks.#{task.id}.completed", completed)
|
user.set "tasks.#{task.id}.completed", completed, done
|
||||||
misc.score(model, task.id, action.dir, true)
|
doneCount++
|
||||||
|
api.score model, user, task.id, action.dir, done
|
||||||
|
|
||||||
if action.op == "sortTask"
|
when "sortTask"
|
||||||
path = action.task.type + "Ids"
|
path = action.task.type + "Ids"
|
||||||
a = user.get(path)
|
a = user.get(path)
|
||||||
a.splice(action.to, 0, a.splice(action.from, 1)[0])
|
a.splice(action.to, 0, a.splice(action.from, 1)[0])
|
||||||
user.set(path, a)
|
user.set(path, a)
|
||||||
|
|
||||||
if action.op == "addTask"
|
when "addTask"
|
||||||
model.unshift "_#{task.type}List", task
|
api.addTask user, task, done
|
||||||
|
|
||||||
if action.op == "delTask"
|
|
||||||
# to make sure we update DOM on Derby client
|
|
||||||
ids = user.get(task.type + 'Ids')
|
|
||||||
ids.splice(ids.indexOf(task.id), 1);
|
|
||||||
user.set(task.type + 'Ids', ids)
|
|
||||||
|
|
||||||
# Actually delete the task
|
|
||||||
user.del ("tasks." + task.id)
|
|
||||||
|
|
||||||
|
when "delTask"
|
||||||
|
api.deleteTask user, task, done
|
||||||
|
|
||||||
# this API is only working with string or number variables. It should return error if object given or object is at the path.
|
# this API is only working with string or number variables. It should return error if object given or object is at the path.
|
||||||
if action.op == "set"
|
when "set"
|
||||||
oldValue = user.get(action.path);
|
oldValue = user.get(action.path)
|
||||||
if typeof action.value != 'object'
|
if _.isObject(action.value) or _.isObject(oldValue)
|
||||||
user.set(action.path, action.value)
|
console.error "action.value was an object, which isn't currently supported. Tyler - double check this"
|
||||||
|
else
|
||||||
|
user.set action.path, action.value, done
|
||||||
user = misc.hydrate user.get()
|
|
||||||
|
|
||||||
#transform user structure FROM user.tasks{} + user.habitIds[] TO user.habits[] + user.todos[] etc.
|
|
||||||
["habit", "daily", "todo", "reward"].forEach (type) ->
|
|
||||||
user[type + 's'] = []
|
|
||||||
user[type + 'Ids'].forEach (id)->
|
|
||||||
user[type + 's'].push(user.tasks[id])
|
|
||||||
delete user[type + 'Ids']
|
|
||||||
delete user.tasks
|
|
||||||
res.json 200, user
|
|
||||||
console.log "Reply sent"
|
|
||||||
|
|
||||||
module.exports = router
|
module.exports = router
|
||||||
Loading…
Reference in a new issue