Gitter migration: Setup redirects (rollout pt. 3)
[gitter.git] / server / web / prerender-chat-helper.js
blob0abdfae349c4edb9a00e887e57f031cfe610d032
1 'use strict';
3 /* Would be nice if we could just fold this into prerender-helper, but at the moment
4 * async helpers for express-hbs only take a single parameter and we can't use them
5 * Also, this way is much faster, so it's not so bad
6 */
8 var _ = require('lodash');
9 var compileTemplate = require('./compile-web-template');
10 var timeFormat = require('gitter-web-shared/time/time-format');
11 const generatePermalink = require('gitter-web-shared/chat/generate-permalink');
12 // var fullTimeFormat = require('gitter-web-shared/time/full-time-format');
13 const getProfileUrlFromVirtualUser = require('gitter-web-shared/get-profile-url-from-virtual-user');
15 var chatWrapper = compileTemplate.compileString(
16 '<div class="chat-item model-id-{{id}} {{burstClass}} {{unreadClass}} {{deletedClass}} {{threadedConversationClass}}" role="listitem">{{{inner}}}</div>'
19 var chatItemTemplate = compileTemplate('/js/views/chat/tmpl/chatItemView.hbs');
20 var statusItemTemplate = compileTemplate('/js/views/chat/tmpl/statusItemView.hbs');
22 function getFormattedTime(model, lang, tz, tzOffset, isArchive) {
23 /* In the chat environment, we don't want to prerender the time
24 * when we don't know what timezone the user is in: we'll just wait
25 * until the javascript kicks in and render it then.
27 * However, in the archive environment, we don't want to do this
28 * for SEO and also because the chats are not re-rendered, so no
29 * times will be shown.
31 if (!tz && !isArchive) {
32 return '';
35 return timeFormat(model.sent, { lang: lang, tzOffset: tzOffset, forceUtc: isArchive });
38 module.exports = exports = function(model, params) {
39 var deletedClass;
41 var root = params.data.root;
43 const isArchive = params.hash.type === 'archive';
45 var troupeName = root.troupeName;
46 var lang = root.lang;
47 var locale = root.locale;
48 var tz = root.tz;
49 var tzOffset = root.tzOffset;
51 var text = model.text || '';
52 var html = model.html || model.text || '';
54 // Handle empty messages as deleted
55 if (html.length === 0) {
56 html = '<i>This message was deleted</i>';
57 deletedClass = 'deleted';
60 const sentTimeFormatted = getFormattedTime(model, lang, tz, tzOffset, isArchive);
61 // TODO: add sentTimeFull
63 var m = _.extend({}, model, {
64 displayName: model.fromUser && model.fromUser.displayName,
65 username: model.fromUser && model.fromUser.username,
66 sentTimeFormatted: sentTimeFormatted,
67 text: text,
68 html: html,
69 lang: lang,
70 locale: locale,
71 tz: tz,
72 tzOffset: tzOffset,
73 permalinkUrl: generatePermalink(troupeName, model.id, model.sent, isArchive),
74 showItemActions: !isArchive
75 });
77 if (m.virtualUser) {
78 m.virtualUser.isMatrix = m.virtualUser.type === 'matrix';
79 m.virtualUser.profileUrl = getProfileUrlFromVirtualUser(m.virtualUser);
82 var result;
84 if (m.status) {
85 result = statusItemTemplate(m);
86 } else {
87 result = chatItemTemplate(m);
90 var unreadClass = model.unread ? 'unread' : 'read';
91 var burstClass = model.burstStart ? 'burstStart' : 'burstContinued';
92 const threadedConversationClass = model.parentId ? 'threaded-conversation-chat-item' : '';
94 return chatWrapper({
95 id: model.id,
96 burstClass,
97 unreadClass,
98 deletedClass,
99 threadedConversationClass,
100 locale: locale,
101 inner: result