mirror of
https://github.com/sudoxnym/habitica.git
synced 2026-08-01 15:31:16 +00:00
redirect deprecated task-scoring to new API, some cleanup to score function to use switz auth
This commit is contained in:
parent
264119b755
commit
4b77daff0a
2 changed files with 45 additions and 41 deletions
|
|
@ -6,7 +6,6 @@ _ = require 'underscore'
|
|||
validator = require 'derby-auth/node_modules/validator'
|
||||
check = validator.check
|
||||
sanitize = validator.sanitize
|
||||
icalendar = require 'icalendar'
|
||||
|
||||
NO_TOKEN_OR_UID = err: "You must include a token and uid (user id) in your request"
|
||||
NO_USER_FOUND = err: "No user found."
|
||||
|
|
@ -114,4 +113,42 @@ router.get '/user/tasks', auth, (req, res) ->
|
|||
|
||||
res.json 200, tasks
|
||||
|
||||
###
|
||||
This is called form deprecated.coffee's score function, and the req.headers are setup properly to handle the login
|
||||
###
|
||||
scoreTask = (req, res, next) ->
|
||||
auth req, res, ->
|
||||
{taskId, direction} = req.params
|
||||
{title, service, icon} = req.body
|
||||
|
||||
# Send error responses for improper API call
|
||||
return res.send(500, ':taskId required') unless taskId
|
||||
return res.send(500, ":direction must be 'up' or 'down'") unless direction in ['up','down']
|
||||
|
||||
model = req.getModel()
|
||||
{user, userObj} = req
|
||||
|
||||
model.ref('_user', user)
|
||||
|
||||
# Create task if doesn't exist
|
||||
# TODO add service & icon to task
|
||||
unless model.get("_user.tasks.#{taskId}")
|
||||
model.refList "_habitList", "_user.tasks", "_user.habitIds"
|
||||
model.at('_habitList').push
|
||||
id: taskId
|
||||
type: 'habit'
|
||||
text: (title || taskId)
|
||||
value: 0
|
||||
up: true
|
||||
down: true
|
||||
notes: "This task was created by a third-party service. Feel free to edit, it won't harm the connection to that service. Additionally, multiple services may piggy-back off this task."
|
||||
|
||||
delta = scoring.score(model, taskId, direction)
|
||||
result = model.get ('_user.stats')
|
||||
result.delta = delta
|
||||
res.send(result)
|
||||
router.post '/user/tasks/:taskId/:direction', scoreTask
|
||||
|
||||
module.exports = router
|
||||
module.exports.scoreTask = scoreTask # export so deprecated can call it
|
||||
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ router = new express.Router()
|
|||
scoring = require '../app/scoring'
|
||||
_ = require 'underscore'
|
||||
icalendar = require('icalendar')
|
||||
api = require './api'
|
||||
|
||||
# ---------- Deprecated Paths ------------
|
||||
|
||||
|
|
@ -13,45 +14,11 @@ router.get '/:uid/up/:score?', (req, res) -> res.send(500, deprecatedMessage)
|
|||
router.get '/:uid/down/:score?', (req, res) -> res.send(500, deprecatedMessage)
|
||||
router.post '/users/:uid/tasks/:taskId/:direction', (req, res) -> res.send(500, deprecatedMessage)
|
||||
|
||||
router.post '/v1/users/:uid/tasks/:taskId/:direction', (req, res) ->
|
||||
{uid, taskId, direction} = req.params
|
||||
{apiToken, title, service, icon} = req.body
|
||||
console.log {params:req.params, body:req.body} if process.env.NODE_ENV == 'development'
|
||||
|
||||
# Send error responses for improper API call
|
||||
return res.send(500, 'request body "apiToken" required') unless apiToken
|
||||
return res.send(500, ':uid required') unless uid
|
||||
return res.send(500, ':taskId required') unless taskId
|
||||
return res.send(500, ":direction must be 'up' or 'down'") unless direction in ['up','down']
|
||||
|
||||
model = req.getModel()
|
||||
model.fetch model.query('users').withIdAndToken(uid, apiToken), (err, result) ->
|
||||
return res.send(500, err) if err
|
||||
user = result
|
||||
userObj = user.get()
|
||||
if _.isEmpty(userObj)
|
||||
return res.send(500, "User with uid=#{uid}, token=#{apiToken} not found. Make sure you're not using your username, but your User Id")
|
||||
|
||||
model.ref('_user', user)
|
||||
req._isServer = true
|
||||
|
||||
# Create task if doesn't exist
|
||||
# TODO add service & icon to task
|
||||
unless model.get("_user.tasks.#{taskId}")
|
||||
model.refList "_habitList", "_user.tasks", "_user.habitIds"
|
||||
model.at('_habitList').push
|
||||
id: taskId
|
||||
type: 'habit'
|
||||
text: (title || taskId)
|
||||
value: 0
|
||||
up: true
|
||||
down: true
|
||||
notes: "This task was created by a third-party service. Feel free to edit, it won't harm the connection to that service. Additionally, multiple services may piggy-back off this task."
|
||||
|
||||
delta = scoring.score(model, taskId, direction)
|
||||
result = model.get ('_user.stats')
|
||||
result.delta = delta
|
||||
res.send(result)
|
||||
# Redirect to new API
|
||||
router.post '/v1/users/:uid/tasks/:taskId/:direction', (req, res, next) ->
|
||||
req.headers['x-api-user'] = req.params.uid
|
||||
req.headers['x-api-key'] = req.body.apiToken
|
||||
api.scoreTask(req, res, next)
|
||||
|
||||
router.get '/v1/users/:uid/calendar.ics', (req, res) ->
|
||||
#return next() #disable for now
|
||||
|
|
@ -80,4 +47,4 @@ router.get '/v1/users/:uid/calendar.ics', (req, res) ->
|
|||
formattedIcal = ical.toString().replace(/DTSTART\:/g, 'DTSTART;VALUE=DATE:')
|
||||
res.send(200, formattedIcal)
|
||||
|
||||
module.exports = router
|
||||
module.exports = router
|
||||
Loading…
Reference in a new issue