Merge branch 'hotfix/21.56.9' into master
[gitter.git] / server / handlers / renderers / vue / mixin-vue-left-menu-data.js
bloba24f7d76af10411fa28835971763e3b3b1b8513a
1 'use strict';
2 const Promise = require('bluebird');
3 const vueRenderToString = require('../vue-ssr-renderer');
4 const restful = require('../../../services/restful');
5 const restSerializer = require('../../../serializers/rest-serializer');
6 const contextGenerator = require('../../../web/context-generator');
7 const generateUserThemeSnapshot = require('../../snapshots/user-theme-snapshot');
9 async function mixinHbsDataForVueLeftMenu(req, existingData) {
10   const user = req.user;
11   const userId = user && user.id;
13   const uriContext = req.uriContext;
14   const currentRoom = uriContext && uriContext.troupe;
16   const [
17     serializedUser,
18     serializedCurrentRoom,
19     serializedGroups,
20     serializedRooms,
21     baseTroupeContext,
22     userThemeSnapshot
23   ] = await Promise.all([
24     restSerializer.serializeObject(
25       user,
26       new restSerializer.UserStrategy({ includeProviders: true })
27     ),
28     restSerializer.serializeObject(currentRoom, new restSerializer.TroupeStrategy()),
29     restful.serializeGroupsForUserId(userId),
30     restful.serializeTroupesForUser(userId),
31     contextGenerator.generateTroupeContext(req),
32     generateUserThemeSnapshot(req)
33   ]);
35   const serializedRoomMap = {};
37   // the roomMap will contain the current room
38   if (serializedCurrentRoom) {
39     serializedRoomMap[serializedCurrentRoom.id] = serializedCurrentRoom;
40   }
42   serializedRooms.forEach(serializedRoom => {
43     serializedRoomMap[serializedRoom.id] = serializedRoom;
44   });
46   const isMobile = req.isPhone;
48   const storeData = {
49     isMobile,
50     isLoggedIn: !!serializedUser,
51     user: serializedUser,
52     darkTheme: userThemeSnapshot.theme === 'gitter-dark',
54     roomMap: serializedRoomMap,
55     displayedRoomId: serializedCurrentRoom && serializedCurrentRoom.id,
57     leftMenuPinnedState: !isMobile,
58     leftMenuExpandedState: false
59   };
61   const vueLeftMenuHtmlOutput = await vueRenderToString({
62     moduleToRender: 'left-menu',
63     storeData
64   });
66   const threadMessageFeedHtmlOutput = await vueRenderToString({
67     moduleToRender: 'thread-message-feed',
68     storeData
69   });
71   return {
72     ...existingData,
74     layout: 'chat-layout',
75     isMobile,
76     leftMenuHtml: vueLeftMenuHtmlOutput,
77     threadMessageFeedHtml: threadMessageFeedHtmlOutput,
79     troupeContext: {
80       ...baseTroupeContext,
81       ...(existingData.troupeContext || {}),
82       snapshots: {
83         ...((existingData.troupeContext || {}).snapshots || {}),
84         allRooms: serializedRooms,
85         groups: serializedGroups
86       }
87     }
88   };
91 module.exports = mixinHbsDataForVueLeftMenu;