mirror of
https://github.com/sudoxnym/habitica.git
synced 2026-08-01 23:40:25 +00:00
move API mount to new serverRoutes function, express.use()'d before derby-auth
This commit is contained in:
parent
332080ca94
commit
554c2750c4
3 changed files with 41 additions and 36 deletions
|
|
@ -9,6 +9,7 @@ MongoStore = require('connect-mongo')(express)
|
|||
auth = require 'derby-auth'
|
||||
priv = require './private'
|
||||
habitrpgStore = require('./store')
|
||||
serverRoutes = require './serverRoutes'
|
||||
|
||||
## RACER CONFIGURATION ##
|
||||
|
||||
|
|
@ -101,6 +102,7 @@ mongo_store = new MongoStore {url: process.env.NODE_DB_URI}, ->
|
|||
model.set '_view', _view
|
||||
next()
|
||||
|
||||
.use(serverRoutes.API())
|
||||
.use(auth(store, strategies, options))
|
||||
# Creates an express middleware from the app's routes
|
||||
.use(app.router())
|
||||
|
|
@ -108,7 +110,7 @@ mongo_store = new MongoStore {url: process.env.NODE_DB_URI}, ->
|
|||
.use(serverError root)
|
||||
|
||||
priv.routes(expressApp)
|
||||
require('./serverRoutes')(expressApp, root, derby)
|
||||
serverRoutes.serverPages(expressApp, root, derby)
|
||||
|
||||
# Errors
|
||||
expressApp.all '*', (req) ->
|
||||
|
|
|
|||
|
|
@ -1,32 +1,23 @@
|
|||
scoring = require('../app/scoring')
|
||||
_ = require('underscore')
|
||||
icalendar = require('icalendar')
|
||||
mount = require('express')()
|
||||
|
||||
module.exports = (expressApp, root, derby) ->
|
||||
|
||||
# ---------- Static Pages ------------
|
||||
staticPages = derby.createStatic root
|
||||
|
||||
expressApp.get '/privacy', (req, res) ->
|
||||
staticPages.render 'privacy', res
|
||||
|
||||
expressApp.get '/terms', (req, res) ->
|
||||
staticPages.render 'terms', res
|
||||
|
||||
expressApp.get '/v1/users/:uid/calendar.ics', (req, res, next) ->
|
||||
return next() #disable for now
|
||||
module.exports.API = ->
|
||||
mount.get '/v1/users/:uid/calendar.ics', (req, res) ->
|
||||
# return next() #disable for now
|
||||
{uid} = req.params
|
||||
{apiToken} = req.query
|
||||
|
||||
# model = req.getModel()
|
||||
# query = model.query('users').withIdAndToken(uid, apiToken)
|
||||
# model.fetch query, (err, result) ->
|
||||
require('mongoskin').db(process.env.NODE_DB_URI, {safe:true}).collection('users').find({_id:uid, apiToken:apiToken}).toArray (err, result) ->
|
||||
model = req.getModel()
|
||||
query = model.query('users').withIdAndToken(uid, apiToken)
|
||||
query.fetch (err, result) ->
|
||||
# require('mongoskin').db(process.env.NODE_DB_URI, {safe:true}).collection('users').find({_id:uid, apiToken:apiToken}).toArray (err, result) ->
|
||||
return res.send(500, err) if err
|
||||
# tasks = result.at(0).get('tasks')
|
||||
return next() if _.isEmpty(result)
|
||||
tasks = result[0].tasks
|
||||
tasks = result.at(0).get('tasks')
|
||||
# tasks = result[0].tasks
|
||||
tasksWithDates = _.filter tasks, (task) -> !!task.date
|
||||
return res.send(500, "No events found") if _.isEmpty(tasksWithDates)
|
||||
|
||||
ical = new icalendar.iCalendar()
|
||||
_.each tasksWithDates, (task) ->
|
||||
|
|
@ -39,12 +30,13 @@ module.exports = (expressApp, root, derby) ->
|
|||
res.type('ics')
|
||||
res.send(200, ical.toString())
|
||||
|
||||
|
||||
# ---------- Deprecated Paths ------------
|
||||
|
||||
deprecatedMessage = 'This API is no longer supported, see https://github.com/lefnire/habitrpg/wiki/API for new protocol'
|
||||
expressApp.get '/:uid/up/:score?', (req, res) -> res.send(500, deprecatedMessage)
|
||||
expressApp.get '/:uid/down/:score?', (req, res) -> res.send(500, deprecatedMessage)
|
||||
expressApp.post '/users/:uid/tasks/:taskId/:direction', (req, res) -> res.send(500, deprecatedMessage)
|
||||
mount.get '/:uid/up/:score?', (req, res) -> res.send(500, deprecatedMessage)
|
||||
mount.get '/:uid/down/:score?', (req, res) -> res.send(500, deprecatedMessage)
|
||||
mount.post '/users/:uid/tasks/:taskId/:direction', (req, res) -> res.send(500, deprecatedMessage)
|
||||
|
||||
# ---------- v1 API ------------
|
||||
|
||||
|
|
@ -52,7 +44,7 @@ module.exports = (expressApp, root, derby) ->
|
|||
v1 API. Requires user-id and apiToken, task-id, direction. Test with:
|
||||
curl -X POST -H "Content-Type:application/json" -d '{"apiToken":"{TOKEN}"}' localhost:3000/v1/users/{UID}/tasks/productivity/up
|
||||
###
|
||||
expressApp.post '/v1/users/:uid/tasks/:taskId/:direction', (req, res) ->
|
||||
mount.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'
|
||||
|
|
@ -71,14 +63,14 @@ module.exports = (expressApp, root, derby) ->
|
|||
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)
|
||||
|
||||
|
||||
# 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 {
|
||||
model.at('_habitList').push
|
||||
id: taskId
|
||||
type: 'habit'
|
||||
text: (title || taskId)
|
||||
|
|
@ -86,12 +78,23 @@ module.exports = (expressApp, root, derby) ->
|
|||
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."
|
||||
}, (a,b,c) ->
|
||||
console.log {a:a,b:b,c:c}
|
||||
|
||||
|
||||
scoring.setModel(model)
|
||||
delta = scoring.score(taskId, direction)
|
||||
result = model.get ('_user.stats')
|
||||
result.delta = delta
|
||||
res.send(result)
|
||||
res.send(result)
|
||||
|
||||
|
||||
return mount
|
||||
|
||||
module.exports.serverPages = (expressApp, root, derby) ->
|
||||
|
||||
# ---------- Static Pages ------------
|
||||
staticPages = derby.createStatic root
|
||||
|
||||
expressApp.get '/privacy', (req, res) ->
|
||||
staticPages.render 'privacy', res
|
||||
|
||||
expressApp.get '/terms', (req, res) ->
|
||||
staticPages.render 'terms', res
|
||||
|
|
@ -340,8 +340,8 @@ do a find for the string after "→"
|
|||
{#if _user.history.todos}
|
||||
<a x-bind=click:toggleChart data-toggle-id="todos-chart" data-history-path="_user.history.todos" rel=tooltip title="Progress"><i class=icon-signal></i></a>
|
||||
{/}
|
||||
<!-- <a href="/v1/users/{{_user.id}}/calendar.ics?apiToken={{_user.apiToken}}"><img src='/img/calendar_ical.png' title="iCal" alt="iCal" rel=tooltip /></a>
|
||||
<a target="_blank" href="https://www.google.com/calendar/render?cid={{encodeiCalLink(_user.id, _user.apiToken)}}"><img src='/img/calendar_google.png' title="Google Calendar" alt="Google Calendar" rel=tooltip /></a>-->
|
||||
<a href="/v1/users/{{_user.id}}/calendar.ics?apiToken={{_user.apiToken}}"><img src='/img/calendar_ical.png' title="iCal" alt="iCal" rel=tooltip /></a>
|
||||
<a target="_blank" href="https://www.google.com/calendar/render?cid={{encodeiCalLink(_user.id, _user.apiToken)}}"><img src='/img/calendar_google.png' title="Google Calendar" alt="Google Calendar" rel=tooltip /></a>
|
||||
</span>
|
||||
<div id="todos-chart" style="display:none;"></div>
|
||||
<ul class="nav nav-tabs">
|
||||
|
|
@ -609,10 +609,10 @@ do a find for the string after "→"
|
|||
</label>
|
||||
</div>
|
||||
{else if equal(:task.type, 'todo')}
|
||||
<!--<div class=control-group>
|
||||
<div class=control-group>
|
||||
<label>Date</label>
|
||||
<input type="date" value="{:task.date}" />
|
||||
</div>-->
|
||||
</div>
|
||||
{/}
|
||||
<button type=submit class="btn">Save & Close</button>
|
||||
</form>
|
||||
|
|
|
|||
Loading…
Reference in a new issue