Merge branch 'hotfix/21.56.9' into master
[gitter.git] / server / api / private / resolve.js
blob0a3bc173eb0d8d37d9df2853fe06964912fb21a2
1 'use strict';
3 var roomContextService = require('gitter-web-rooms/lib/room-context-service');
4 var StatusError = require('statuserror');
5 var uriContextAsBrowserState = require('gitter-web-uri-resolver/lib/uri-context-as-browser-state');
7 /**
8 * This API is currently a work in progress and shouldn't be
9 * relied upon until it is ratified.
11 function resolve(req, res, next) {
12 var uri = req.params[0];
14 return roomContextService
15 .findContextForUri(req.user, uri, { ignoreCase: true })
16 .bind({
17 uriContext: null
19 .then(function(uriContext) {
20 if (!uriContext) {
21 throw new StatusError(404);
24 this.uriContext = uriContext;
26 if (uriContext.ownUrl) {
27 return true;
30 return uriContext.policy.canRead();
32 .then(function(access) {
33 if (!access) throw new StatusError(404);
35 var browserState = uriContextAsBrowserState(this.uriContext);
36 if (!browserState) throw new StatusError(404);
39 * TODO: consider adding Cache Control headers to this
40 * API
41 * `res.set('Cache-Control', 'max-age=3600');`
44 res.send(browserState);
46 .catch(next);
49 module.exports = resolve;