• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

NodeBB / NodeBB / 22913939149

10 Mar 2026 04:50PM UTC coverage: 85.545% (-0.03%) from 85.572%
22913939149

push

github

barisusakli
chore: up themes

13376 of 18294 branches covered (73.12%)

28246 of 33019 relevant lines covered (85.54%)

3335.19 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.88
/src/middleware/render.js
1
'use strict';
2

3
const _ = require('lodash');
4✔
4
const nconf = require('nconf');
4✔
5
const validator = require('validator');
4✔
6
const jsesc = require('jsesc');
4✔
7
const winston = require('winston');
4✔
8
const semver = require('semver');
4✔
9

10
const db = require('../database');
4✔
11
const navigation = require('../navigation');
4✔
12
const translator = require('../translator');
4✔
13
const privileges = require('../privileges');
4✔
14
const languages = require('../languages');
4✔
15
const plugins = require('../plugins');
4✔
16
const user = require('../user');
4✔
17
const topics = require('../topics');
4✔
18
const messaging = require('../messaging');
4✔
19
const flags = require('../flags');
4✔
20
const meta = require('../meta');
4✔
21
const widgets = require('../widgets');
4✔
22
const utils = require('../utils');
4✔
23
const helpers = require('./helpers');
4✔
24
const versions = require('../admin/versions');
4✔
25
const controllersHelpers = require('../controllers/helpers');
4✔
26

27
const relative_path = nconf.get('relative_path');
4✔
28

29
module.exports = function (middleware) {
4✔
30
        middleware.processRender = function processRender(req, res, next) {
4✔
31
                // res.render post-processing, modified from here: https://gist.github.com/mrlannigan/5051687
32
                const { render } = res;
5,577✔
33

34
                res.render = async function renderOverride(template, options, fn) {
5,577✔
35
                        const self = this;
1,410✔
36
                        const { req } = this;
1,410✔
37
                        async function renderMethod(template, options, fn) {
38
                                options = options || {};
1,410!
39
                                if (typeof options === 'function') {
1,410!
40
                                        fn = options;
×
41
                                        options = {};
×
42
                                }
43

44
                                options.loggedIn = req.uid > 0;
1,410✔
45
                                options.loggedInUser = await getLoggedInUser(req);
1,410✔
46
                                options.relative_path = relative_path;
1,410✔
47
                                options.template = { name: template, [template]: true };
1,410✔
48
                                options.url = options.url || (req.baseUrl + req.path.replace(/^\/api/, ''));
1,410✔
49
                                options.bodyClass = helpers.buildBodyClass(req, res, options);
1,410✔
50

51
                                if (req.loggedIn) {
1,410✔
52
                                        res.set('cache-control', 'private');
1,062✔
53
                                }
54

55
                                const buildResult = await plugins.hooks.fire(`filter:${template}.build`, {
1,410✔
56
                                        req: req,
57
                                        res: res,
58
                                        templateData: options,
59
                                });
60
                                if (res.headersSent) {
1,410!
61
                                        return;
×
62
                                }
63
                                const templateToRender = buildResult.templateData.templateToRender || template;
1,410✔
64

65
                                const renderResult = await plugins.hooks.fire('filter:middleware.render', {
1,410✔
66
                                        req: req,
67
                                        res: res,
68
                                        templateData: buildResult.templateData,
69
                                });
70
                                if (res.headersSent) {
1,410!
71
                                        return;
×
72
                                }
73
                                options = renderResult.templateData;
1,410✔
74
                                options._header = {
1,410✔
75
                                        tags: await meta.tags.parse(req, renderResult, res.locals.metaTags, res.locals.linkTags),
76
                                };
77
                                options.widgets = await widgets.render(req.uid, {
1,410✔
78
                                        template: `${template}.tpl`,
79
                                        url: options.url,
80
                                        templateData: options,
81
                                        req: req,
82
                                        res: res,
83
                                });
84
                                res.locals.template = template;
1,410✔
85
                                options._locals = undefined;
1,410✔
86

87
                                if (res.locals.isAPI) {
1,410✔
88
                                        if (req.route && req.route.path === '/api/') {
1,112✔
89
                                                options.title = '[[pages:home]]';
4✔
90
                                        }
91
                                        req.app.set('json spaces', process.env.NODE_ENV === 'development' || req.query.pretty ? 4 : 0);
1,112✔
92
                                        return res.json(options);
1,112✔
93
                                }
94
                                const optionsString = JSON.stringify(options).replace(/<\//g, '<\\/');
298✔
95
                                const headerFooterData = await loadHeaderFooterData(req, res, options);
298✔
96
                                const results = await utils.promiseParallel({
298✔
97
                                        header: renderHeaderFooter('renderHeader', req, res, options, headerFooterData),
98
                                        content: renderContent(render, templateToRender, req, res, options),
99
                                        footer: renderHeaderFooter('renderFooter', req, res, options, headerFooterData),
100
                                });
101

102
                                const str = `${results.header +
298✔
103
                                        (res.locals.postHeader || '') +
596✔
104
                                        results.content
105
                                }<script id="ajaxify-data" type="application/json">${
106
                                        optionsString
107
                                }</script>${
108
                                        res.locals.preFooter || ''
596✔
109
                                }${results.footer}`;
110

111
                                if (typeof fn !== 'function') {
298!
112
                                        self.send(str);
298✔
113
                                } else {
114
                                        fn(null, str);
×
115
                                }
116
                        }
117

118
                        try {
1,410✔
119
                                await renderMethod(template, options, fn);
1,410✔
120
                        } catch (err) {
121
                                next(err);
×
122
                        }
123
                };
124

125
                next();
5,577✔
126
        };
127

128
        async function getLoggedInUser(req) {
129
                if (req.user) {
1,410✔
130
                        return await user.getUserData(req.uid);
1,062✔
131
                }
132
                return {
348✔
133
                        uid: req.uid === -1 ? -1 : 0,
348!
134
                        username: '[[global:guest]]',
135
                        picture: user.getDefaultAvatar(),
136
                        'icon:text': '?',
137
                        'icon:bgColor': '#aaa',
138
                };
139
        }
140

141
        async function loadHeaderFooterData(req, res, options) {
142
                if (res.locals.renderHeader) {
298✔
143
                        return await loadClientHeaderFooterData(req, res, options);
230✔
144
                } else if (res.locals.renderAdminHeader) {
68!
145
                        return await loadAdminHeaderFooterData(req, res, options);
68✔
146
                }
147
                return null;
×
148
        }
149

150
        async function loadClientHeaderFooterData(req, res, options) {
151
                const registrationType = meta.config.registrationType || 'normal';
230!
152
                res.locals.config = res.locals.config || {};
230!
153
                const userLang = res.locals.config.userLang || meta.config.userLang || 'en-GB';
230!
154
                const templateValues = {
230✔
155
                        title: meta.config.title || '',
230!
156
                        'title:url': meta.config['title:url'] || '',
460✔
157
                        description: meta.config.description || '',
460✔
158
                        'cache-buster': meta.config['cache-buster'] || '',
230!
159
                        'brand:logo': meta.config['brand:logo'] || '',
460✔
160
                        'brand:logo:url': meta.config['brand:logo:url'] || '',
460✔
161
                        'brand:logo:alt': meta.config['brand:logo:alt'] || '',
460✔
162
                        'brand:logo:display': meta.config['brand:logo'] ? '' : 'hide',
230!
163
                        allowRegistration: registrationType === 'normal',
164
                        searchEnabled: plugins.hooks.hasListeners('filter:search.query'),
165
                        postQueueEnabled: !!meta.config.postQueue,
166
                        registrationQueueEnabled: meta.config.registrationApprovalType !== 'normal' || (meta.config.registrationType === 'invite-only' || meta.config.registrationType === 'admin-invite-only'),
690✔
167
                        config: res.locals.config,
168
                        relative_path,
169
                        bodyClass: options.bodyClass,
170
                        widgets: options.widgets,
171
                };
172

173
                templateValues.configJSON = jsesc(JSON.stringify(res.locals.config), { isScriptContext: true });
230✔
174

175
                const title = translator.unescape(utils.stripHTMLTags(options.title || ''));
230✔
176
                const results = await utils.promiseParallel({
230✔
177
                        isAdmin: user.isAdministrator(req.uid),
178
                        isGlobalMod: user.isGlobalModerator(req.uid),
179
                        isModerator: user.isModeratorOfAnyCategory(req.uid),
180
                        privileges: privileges.global.get(req.uid),
181
                        blocks: user.blocks.list(req.uid),
182
                        user: user.getUserData(req.uid),
183
                        isEmailConfirmSent: req.uid <= 0 ? false : await user.email.isValidationPending(req.uid),
230✔
184
                        languageDirection: translator.translate('[[language:dir]]', userLang),
185
                        timeagoCode: languages.userTimeagoCode(userLang),
186
                        browserTitle: translator.translate(controllersHelpers.buildTitle(title), userLang),
187
                        navigation: navigation.get(req.uid),
188
                        roomIds: req.uid > 0 ? db.getSortedSetRevRange(`uid:${req.uid}:chat:rooms`, 0, 0) : [],
230✔
189
                });
190

191
                const unreadData = {
230✔
192
                        '': {},
193
                        new: {},
194
                        watched: {},
195
                        unreplied: {},
196
                };
197

198
                results.user.unreadData = unreadData;
230✔
199
                results.user.isAdmin = results.isAdmin;
230✔
200
                results.user.isGlobalMod = results.isGlobalMod;
230✔
201
                results.user.isMod = !!results.isModerator;
230✔
202
                results.user.privileges = results.privileges;
230✔
203
                results.user.blocks = results.blocks;
230✔
204
                results.user.timeagoCode = results.timeagoCode;
230✔
205
                results.user[results.user.status] = true;
230✔
206
                results.user.lastRoomId = results.roomIds.length ? results.roomIds[0] : null;
230!
207

208
                results.user.email = String(results.user.email);
230✔
209
                results.user['email:confirmed'] = results.user['email:confirmed'] === 1;
230✔
210
                results.user.isEmailConfirmSent = !!results.isEmailConfirmSent;
230✔
211

212
                templateValues.bootswatchSkin = res.locals.config.bootswatchSkin || '';
230✔
213
                templateValues.browserTitle = results.browserTitle;
230✔
214
                ({
230✔
215
                        navigation: templateValues.navigation,
216
                        unreadCount: templateValues.unreadCount,
217
                } = await appendUnreadCounts({
218
                        uid: req.uid,
219
                        query: req.query,
220
                        navigation: results.navigation,
221
                        unreadData,
222
                }));
223
                templateValues.isAdmin = results.user.isAdmin;
230✔
224
                templateValues.isGlobalMod = results.user.isGlobalMod;
230✔
225
                templateValues.showModMenu = results.user.isAdmin || results.user.isGlobalMod || results.user.isMod;
230✔
226
                templateValues.canChat = (results.privileges.chat || results.privileges['chat:privileged']) && meta.config.disableChat !== 1;
230✔
227
                templateValues.user = results.user;
230✔
228
                templateValues.userJSON = jsesc(JSON.stringify(results.user), { isScriptContext: true });
230✔
229
                templateValues.useCustomCSS = meta.config.useCustomCSS && meta.config.customCSS;
230!
230
                templateValues.customCSS = templateValues.useCustomCSS ? (meta.config.renderedCustomCSS || '') : '';
230!
231
                templateValues.useCustomHTML = meta.config.useCustomHTML;
230✔
232
                templateValues.customHTML = templateValues.useCustomHTML ? meta.config.customHTML : '';
230!
233
                templateValues.maintenanceHeader = meta.config.maintenanceMode && !results.isAdmin;
230✔
234
                templateValues.defaultLang = meta.config.defaultLang || 'en-GB';
230!
235
                templateValues.userLang = res.locals.config.userLang;
230✔
236
                templateValues.languageDirection = results.languageDirection;
230✔
237
                if (req.query.noScriptMessage) {
230!
238
                        templateValues.noScriptMessage = validator.escape(String(req.query.noScriptMessage));
×
239
                }
240

241
                templateValues.template = { name: res.locals.template };
230✔
242
                templateValues.template[res.locals.template] = true;
230✔
243

244
                if (options.hasOwnProperty('_header')) {
230!
245
                        templateValues.metaTags = options._header.tags.meta;
230✔
246
                        templateValues.linkTags = options._header.tags.link;
230✔
247
                }
248

249
                if (req.route && req.route.path === '/') {
230✔
250
                        modifyTitle(templateValues);
10✔
251
                }
252
                return templateValues;
230✔
253
        }
254

255
        async function loadAdminHeaderFooterData(req, res, options) {
256
                const custom_header = {
68✔
257
                        plugins: [],
258
                        authentication: [],
259
                };
260
                res.locals.config = res.locals.config || {};
68!
261

262
                const results = await utils.promiseParallel({
68✔
263
                        userData: user.getUserFields(req.uid, ['username', 'userslug', 'email', 'picture', 'email:confirmed']),
264
                        scripts: getAdminScripts(),
265
                        custom_header: plugins.hooks.fire('filter:admin.header.build', custom_header),
266
                        configs: meta.configs.list(),
267
                        latestVersion: getLatestVersion(),
268
                        privileges: privileges.admin.get(req.uid),
269
                        tags: meta.tags.parse(req, {}, [], []),
270
                        languageDirection: translator.translate('[[language:dir]]', res.locals.config.acpLang),
271
                });
272

273
                const { userData } = results;
68✔
274
                userData.uid = req.uid;
68✔
275
                userData['email:confirmed'] = userData['email:confirmed'] === 1;
68✔
276
                userData.privileges = results.privileges;
68✔
277

278
                let acpPath = req.path.slice(1).split('/');
68✔
279
                acpPath.forEach((path, i) => {
68✔
280
                        acpPath[i] = path.charAt(0).toUpperCase() + path.slice(1);
200✔
281
                });
282
                acpPath = acpPath.join(' > ');
68✔
283

284
                const version = nconf.get('version');
68✔
285

286
                res.locals.config.userLang = res.locals.config.acpLang || res.locals.config.userLang;
68!
287
                res.locals.config.isRTL = results.languageDirection === 'rtl';
68✔
288
                const templateValues = {
68✔
289
                        config: res.locals.config,
290
                        configJSON: jsesc(JSON.stringify(res.locals.config), { isScriptContext: true }),
291
                        relative_path: res.locals.config.relative_path,
292
                        adminConfigJSON: encodeURIComponent(JSON.stringify(results.configs)),
293
                        metaTags: results.tags.meta,
294
                        linkTags: results.tags.link,
295
                        user: userData,
296
                        userJSON: jsesc(JSON.stringify(userData), { isScriptContext: true }),
297
                        plugins: results.custom_header.plugins,
298
                        authentication: results.custom_header.authentication,
299
                        scripts: results.scripts,
300
                        'cache-buster': meta.config['cache-buster'] || '',
68!
301
                        env: !!process.env.NODE_ENV,
302
                        title: `${acpPath || 'Dashboard'} | NodeBB Admin Control Panel`,
68!
303
                        bodyClass: options.bodyClass,
304
                        version: version,
305
                        latestVersion: results.latestVersion,
306
                        upgradeAvailable: results.latestVersion && semver.gt(results.latestVersion, version),
136✔
307
                        showManageMenu: results.privileges.superadmin || ['categories', 'privileges', 'users', 'admins-mods', 'groups', 'tags', 'settings'].some(priv => results.privileges[`admin:${priv}`]),
×
308
                        defaultLang: meta.config.defaultLang || 'en-GB',
68!
309
                        acpLang: res.locals.config.acpLang,
310
                        languageDirection: results.languageDirection,
311
                };
312

313
                templateValues.template = { name: res.locals.template };
68✔
314
                templateValues.template[res.locals.template] = true;
68✔
315
                return templateValues;
68✔
316
        }
317

318
        function renderContent(render, tpl, req, res, options) {
319
                return new Promise((resolve, reject) => {
298✔
320
                        render.call(res, tpl, options, async (err, str) => {
298✔
321
                                if (err) reject(err);
298!
322
                                else resolve(await translate(str, getLang(req, res)));
298✔
323
                        });
324
                });
325
        }
326

327
        async function renderHeader(req, res, options, headerFooterData) {
328
                const hookReturn = await plugins.hooks.fire('filter:middleware.renderHeader', {
230✔
329
                        req: req,
330
                        res: res,
331
                        templateValues: headerFooterData, // TODO: deprecate
332
                        templateData: headerFooterData,
333
                        data: options,
334
                });
335

336
                return await req.app.renderAsync('header', hookReturn.templateData);
230✔
337
        }
338

339
        async function renderFooter(req, res, options, headerFooterData) {
340
                const hookReturn = await plugins.hooks.fire('filter:middleware.renderFooter', {
230✔
341
                        req,
342
                        res,
343
                        templateValues: headerFooterData, // TODO: deprecate
344
                        templateData: headerFooterData,
345
                        data: options,
346
                });
347

348
                const scripts = await plugins.hooks.fire('filter:scripts.get', []);
230✔
349

350
                hookReturn.templateData.scripts = scripts.map(script => ({ src: script }));
230✔
351

352
                hookReturn.templateData.useCustomJS = meta.config.useCustomJS;
230✔
353
                hookReturn.templateData.customJS = hookReturn.templateData.useCustomJS ? meta.config.customJS : '';
230!
354
                hookReturn.templateData.isSpider = req.uid === -1;
230✔
355

356
                return await req.app.renderAsync('footer', hookReturn.templateData);
230✔
357
        }
358

359
        async function renderAdminHeader(req, res, options, headerFooterData) {
360
                const hookReturn = await plugins.hooks.fire('filter:middleware.renderAdminHeader', {
68✔
361
                        req,
362
                        res,
363
                        templateValues: headerFooterData, // TODO: deprecate
364
                        templateData: headerFooterData,
365
                        data: options,
366
                });
367

368
                return await req.app.renderAsync('admin/header', hookReturn.templateData);
68✔
369
        }
370

371
        async function renderAdminFooter(req, res, options, headerFooterData) {
372
                const hookReturn = await plugins.hooks.fire('filter:middleware.renderAdminFooter', {
68✔
373
                        req,
374
                        res,
375
                        templateValues: headerFooterData, // TODO: deprecate
376
                        templateData: headerFooterData,
377
                        data: options,
378
                });
379

380
                return await req.app.renderAsync('admin/footer', hookReturn.templateData);
68✔
381
        }
382

383
        async function renderHeaderFooter(method, req, res, options, headerFooterData) {
384
                let str = '';
596✔
385
                if (res.locals.renderHeader) {
596✔
386
                        if (method === 'renderHeader') {
460✔
387
                                str = await renderHeader(req, res, options, headerFooterData);
230✔
388
                        } else if (method === 'renderFooter') {
230!
389
                                str = await renderFooter(req, res, options, headerFooterData);
230✔
390
                        }
391
                } else if (res.locals.renderAdminHeader) {
136!
392
                        if (method === 'renderHeader') {
136✔
393
                                str = await renderAdminHeader(req, res, options, headerFooterData);
68✔
394
                        } else if (method === 'renderFooter') {
68!
395
                                str = await renderAdminFooter(req, res, options, headerFooterData);
68✔
396
                        }
397
                }
398
                return await translate(str, getLang(req, res));
596✔
399
        }
400

401
        function getLang(req, res) {
402
                let language = (res.locals.config && res.locals.config.userLang) || 'en-GB';
894!
403
                if (res.locals.renderAdminHeader) {
894✔
404
                        language = (res.locals.config && res.locals.config.acpLang) || 'en-GB';
204!
405
                }
406
                return req.query.lang ? validator.escape(String(req.query.lang)) : language;
894!
407
        }
408

409
        async function translate(str, language) {
410
                const translated = await translator.translate(str, language);
894✔
411
                return translator.unescape(translated);
894✔
412
        }
413

414
        async function appendUnreadCounts({ uid, navigation, unreadData, query }) {
415
                const originalRoutes = navigation.map(nav => nav.originalRoute);
230✔
416
                const calls = {
230✔
417
                        unreadData: topics.getUnreadData({ uid: uid, query: query }),
418
                        unreadChatCount: messaging.getUnreadCount(uid),
419
                        unreadNotificationCount: user.notifications.getUnreadCount(uid),
420
                        unreadFlagCount: (async function () {
421
                                if (originalRoutes.includes('/flags') && await user.isPrivileged(uid)) {
230!
422
                                        return flags.getCount({
×
423
                                                uid,
424
                                                query,
425
                                                filters: {
426
                                                        quick: 'unresolved',
427
                                                        cid: (await user.isAdminOrGlobalMod(uid)) ? [] : (await user.getModeratedCids(uid)),
×
428
                                                },
429
                                        });
430
                                }
431
                                return 0;
230✔
432
                        }()),
433
                };
434
                const results = await utils.promiseParallel(calls);
230✔
435

436
                const unreadCounts = results.unreadData.counts;
230✔
437
                const unreadCount = {
230✔
438
                        topic: unreadCounts[''] || 0,
426✔
439
                        newTopic: unreadCounts.new || 0,
426✔
440
                        watchedTopic: unreadCounts.watched || 0,
456✔
441
                        unrepliedTopic: unreadCounts.unreplied || 0,
426✔
442
                        mobileUnread: 0,
443
                        unreadUrl: '/unread',
444
                        chat: results.unreadChatCount || 0,
460✔
445
                        notification: results.unreadNotificationCount || 0,
432✔
446
                        flags: results.unreadFlagCount || 0,
460✔
447
                };
448

449
                Object.keys(unreadCount).forEach((key) => {
230✔
450
                        if (unreadCount[key] > 99) {
2,070!
451
                                unreadCount[key] = '99+';
×
452
                        }
453
                });
454

455
                const { tidsByFilter } = results.unreadData;
230✔
456
                navigation = navigation.map((item) => {
230✔
457
                        function modifyNavItem(item, route, filter, content) {
458
                                if (item && item.originalRoute === route) {
×
459
                                        unreadData[filter] = _.zipObject(tidsByFilter[filter], tidsByFilter[filter].map(() => true));
×
460
                                        item.content = content;
×
461
                                        unreadCount.mobileUnread = content;
×
462
                                        unreadCount.unreadUrl = route;
×
463
                                        if (unreadCounts[filter] > 0) {
×
464
                                                item.iconClass += ' unread-count';
×
465
                                        }
466
                                }
467
                        }
468
                        modifyNavItem(item, '/unread', '', unreadCount.topic);
×
469
                        modifyNavItem(item, '/unread?filter=new', 'new', unreadCount.newTopic);
×
470
                        modifyNavItem(item, '/unread?filter=watched', 'watched', unreadCount.watchedTopic);
×
471
                        modifyNavItem(item, '/unread?filter=unreplied', 'unreplied', unreadCount.unrepliedTopic);
×
472

473
                        ['flags'].forEach((prop) => {
×
474
                                if (item && item.originalRoute === `/${prop}` && unreadCount[prop] > 0) {
×
475
                                        item.iconClass += ' unread-count';
×
476
                                        item.content = unreadCount.flags;
×
477
                                }
478
                        });
479

480
                        return item;
×
481
                });
482

483
                return { navigation, unreadCount };
230✔
484
        }
485

486

487
        function modifyTitle(obj) {
488
                const title = controllersHelpers.buildTitle(meta.config.homePageTitle || '[[pages:home]]');
10✔
489
                obj.browserTitle = title;
10✔
490

491
                if (obj.metaTags) {
10!
492
                        obj.metaTags.forEach((tag, i) => {
10✔
493
                                if (tag.property === 'og:title') {
130✔
494
                                        obj.metaTags[i].content = title;
10✔
495
                                }
496
                        });
497
                }
498

499
                return title;
10✔
500
        }
501

502
        async function getAdminScripts() {
503
                const scripts = await plugins.hooks.fire('filter:admin.scripts.get', []);
68✔
504
                return scripts.map(script => ({ src: script }));
68✔
505
        }
506

507
        async function getLatestVersion() {
508
                try {
68✔
509
                        return await versions.getLatestVersion();
68✔
510
                } catch (err) {
511
                        winston.error(`[acp] Failed to fetch latest version${err.stack}`);
×
512
                }
513
                return null;
×
514
        }
515
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc