gitoria

tickets

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Main branchmain5d144817Merge branch 'records-t40' into mainTicketsmain/lib/subscriptions-api.hl

2.0 KB

  1. // Thin token-only wrappers; cookies, project keys and ident access keys do not own subscriptions.
  2. import { Response } from 'hl:http1'
  3. import { userOfBearer } from './users.hl'
  4. import { jsonErrorAt } from './jsoncheck.hl'
  5. import { hdr, fail, unauthorizedFor, wantsMarkdown } from './api-helpers.hl'
  6. import { subscriptionsFor, createSubscription, removeSubscription, subscriptionDelta } from './subscriptions.hl'
  7. static answer = (status, data, markdown) => {
  8. return new Response(markdown ? data : JSON.stringify(data), { status = status headers = hdr({ 'Content-Type' = markdown ? 'text/markdown; charset=utf-8' : 'application/json; charset=utf-8' 'Cache-Control' = 'no-store' 'Vary' = 'Accept' }) })
  9. }
  10. static result = (r, status) => { return answer(r.error != null ? (r.forbidden == true ? 403 : (r.status != null ? r.status : 400)) : status, r, false) }
  11. static subscriptionsApi = (route, req) => {
  12. if (req.method != 'GET' && req.method != 'POST') { return fail(405, 'GET or POST') }
  13. u = userOfBearer(req.headers['authorization'])
  14. if (u == null) { return unauthorizedFor('subscriptions') }
  15. if (req.method == 'GET') { return result(subscriptionsFor(u), 200) }
  16. if (req.body == null || req.body == '' || req.body.length > 32768 || jsonErrorAt(req.body) >= 0) { return fail(400, 'invalid JSON body (maximum 32768 characters)') }
  17. return result(createSubscription(u, JSON.parse(req.body)), 201)
  18. }
  19. static deltaApi = (route, req) => {
  20. if (req.method != 'GET') { return fail(405, 'GET only') }
  21. u = userOfBearer(req.headers['authorization'])
  22. if (u == null) { return unauthorizedFor('subscriptions') }
  23. md = wantsMarkdown(req)
  24. q = req.query != null ? req.query : {}
  25. r = subscriptionDelta(route.params.id, u, q.peek == '1', md)
  26. if (r.error != null) { return result(r, 200) }
  27. return answer(200, md ? r.text : r.data, md)
  28. }
  29. static removeSubscriptionApi = (route, req) => {
  30. if (req.method != 'POST') { return fail(405, 'POST only') }
  31. u = userOfBearer(req.headers['authorization'])
  32. if (u == null) { return unauthorizedFor('subscriptions') }
  33. return result(removeSubscription(route.params.id, u), 200)
  34. }

Branches

Latest commits

  • 5d144817Merge branch 'records-t40' into mainTickets
  • 44ee6f06|||PR records: #43 #45 livemre
  • b8fdeee9Merge branch 'p39-routes' into mainTickets
  • a0187a0a|||PR Remove redundant route-order loopmre
  • 369e4b9cMerge branch 'k40-lists' into mainTickets
  • 9af8e780|||PR Use keyed list mutations for live ticket updatesmre
  • 610d8e8aMerge branch 'records-t39' into mainTickets
  • 32bcdf24|||PR records: GO P39 K40mre
  • 4123c6d1Merge branch 'records-t38' into mainTickets
  • cd3641a1|||PR records: day72 deploy live, tickets closedmre
  • 7d44d852Merge branch 'records-t37' into mainTickets
  • 926f7061|||PR records: merges, D36 deploymre
  • bf58534eMerge branch 'l33-final' into mainTickets
  • 759a9ac3|||PR thin login (tickets#46, #45): merge main, route entry cleanupmre
  • 239436d8Merge remote-tracking branch 'origin/main' into l33-finalmre
  • 5822cb3aMerge branch 'o34-clean' into mainTickets
  • 0870653c|||PR offline note from __hlOnline, no polling (tickets#41)mre
  • 14cf96b4Merge branch 'records-t36' into mainTickets
  • 07749fa1|||PR records: day72 + #40 mergedmre
  • ecd7e226|||PR Consolidate login and move handler logic into topic libraries (partial)mre