3 var assert = require('assert');
4 var Promise = require('bluebird');
5 var _ = require('lodash');
6 const debug = require('debug')('gitter:app:context-generator');
7 var restSerializer = require('../serializers/rest-serializer');
8 var userService = require('gitter-web-users');
9 var roomMetaService = require('gitter-web-rooms/lib/room-meta-service');
10 var contextGeneratorRequest = require('./context-generator-request');
12 function serializeUser(user) {
13 var strategy = new restSerializer.UserStrategy({
14 exposeRawDisplayName: true,
16 includePermissions: true,
17 showPremiumStatus: true,
18 includeProviders: true
21 return restSerializer.serializeObject(user, strategy);
24 function serializeTroupe(troupe, user) {
25 var strategy = new restSerializer.TroupeStrategy({
26 currentUserId: user ? user.id : null,
28 includePermissions: true,
32 includeAssociatedRepo: true
35 return restSerializer.serializeObject(troupe, strategy);
38 function serializeGroup(group, user) {
39 var strategy = new restSerializer.GroupStrategy({
41 currentUserId: user && user._id
44 return restSerializer.serializeObject(group, strategy);
47 function serializeTroupeId(troupeId, user) {
48 var strategy = new restSerializer.TroupeIdStrategy({
49 currentUserId: user ? user.id : null,
51 includePermissions: true,
55 includeAssociatedRepo: true
58 return restSerializer.serializeObject(troupeId, strategy);
62 * Returns the promise of a mini-context
64 function generateMainMenuContext(req) {
66 var uriContext = req.uriContext;
68 var troupe = uriContext && uriContext.troupe;
69 const group = uriContext && uriContext.group;
70 var roomMember = uriContext && uriContext.roomMember;
73 contextGeneratorRequest(req),
74 user ? serializeUser(user) : null,
75 serializeGroup(group, user),
76 troupe ? serializeTroupe(troupe, user) : undefined
77 ]).spread(function(reqContextHash, serializedUser, serializedGroup, serializedTroupe) {
78 var serializedContext = _.extend({}, reqContextHash, {
79 roomMember: roomMember,
81 group: serializedGroup,
82 troupe: serializedTroupe
85 return serializedContext;
89 async function generateOrgContext(req) {
91 const { uriContext } = req;
93 const { group } = uriContext;
96 const [basicContext, serializedGroup] = await Promise.all([
97 generateBasicContext(req),
98 serializeGroup(group, user)
101 return _.extend({}, basicContext, {
102 group: serializedGroup
106 function generateBasicContext(req) {
109 return Promise.all([contextGeneratorRequest(req), user ? serializeUser(user) : null]).spread(
110 function(reqContextHash, serializedUser) {
111 return _.extend({}, reqContextHash, {
119 * Generates a context for sending over a bayeux connection
121 function generateSocketContext(userId, troupeId) {
123 if (!userId) return Promise.resolve(null);
124 return userService.findById(userId);
128 .then(function(user) {
129 return [user && serializeUser(user), troupeId && serializeTroupeId(troupeId, user)];
131 .spread(function(serializedUser, serializedTroupe) {
133 user: serializedUser || undefined,
134 troupe: serializedTroupe || undefined
140 * Generates the context to send for a main-frame
142 function generateTroupeContext(req, extras) {
144 var uriContext = req.uriContext;
145 var troupe = uriContext && uriContext.troupe;
146 var roomMember = uriContext && uriContext.roomMember;
149 `generateTroupeContext -> roomMember=${roomMember} ${
150 troupe ? `troupe=${troupe.lcUri}(${troupe._id})` : ''
151 } ${user ? `user=${user.username}(${user._id})` : ''}`
155 contextGeneratorRequest(req),
156 user ? serializeUser(user) : null,
157 troupe ? serializeTroupe(troupe, user) : undefined,
158 // TODO: if we keep serializing troupes with metadata, we can replace following with troupe.meta.welcomeMessage
160 ? roomMetaService.findMetaByTroupeId(troupe._id, ['welcomeMessage'])
162 ]).spread(function(reqContextHash, serializedUser, serializedTroupe, { welcomeMessage }) {
163 var roomHasWelcomeMessage = !!(
165 welcomeMessage.text &&
166 welcomeMessage.text.length
173 roomMember: roomMember,
174 user: serializedUser,
175 troupe: serializedTroupe,
176 roomHasWelcomeMessage: roomHasWelcomeMessage
184 generateBasicContext,
185 generateMainMenuContext,
186 generateSocketContext,
187 generateTroupeContext,