3 var roomContextService
= require('gitter-web-rooms/lib/room-context-service');
4 var debug
= require('debug')('gitter:app:uri-context-resolver-middleware');
6 function normaliseUrl(params
) {
7 if (params
.roomPart3
) {
8 return params
.roomPart1
+ '/' + params
.roomPart2
+ '/' + params
.roomPart3
;
11 if (params
.roomPart2
) {
12 return params
.roomPart1
+ '/' + params
.roomPart2
;
15 return params
.roomPart1
;
18 function uriContextResolverMiddleware(req
, res
, next
) {
19 var uri
= normaliseUrl(req
.params
);
20 debug('Looking up normalised uri %s', uri
);
22 return roomContextService
23 .findContextForUri(req
.user
, uri
)
24 .then(function(uriContext
) {
25 if (uriContext
.ownUrl
) {
26 res
.relativeRedirect('/home/explore');
30 req
.troupe
= uriContext
.troupe
;
31 req
.group
= uriContext
.group
;
32 req
.uriContext
= uriContext
;
40 module
.exports
= uriContextResolverMiddleware
;