|
|
|
@@ -1,12 +1,15 @@
|
|
|
|
|
// Screen route registration and dashboard wiring.
|
|
|
|
|
|
|
|
|
|
module.exports = function registerScreensRoutes(app, deps) {
|
|
|
|
|
const pool = deps.pool;
|
|
|
|
|
const common = deps.common;
|
|
|
|
|
const pages = deps.pages;
|
|
|
|
|
const buildDashboardState = deps.buildDashboardState;
|
|
|
|
|
const getScreenDeleteBlockMessage = deps.getScreenDeleteBlockMessage;
|
|
|
|
|
const { buildPagination } = require('../../../lib/pagination');
|
|
|
|
|
const requirePermission = deps.requirePermission;
|
|
|
|
|
|
|
|
|
|
const LIST_PAGE_SIZE = 10;
|
|
|
|
|
const LIST_PAGE_SIZE = 25;
|
|
|
|
|
|
|
|
|
|
function requireQueryPermission(readPermissionKey, editPermissionKey) {
|
|
|
|
|
return function (req, res, next) {
|
|
|
|
@@ -18,6 +21,7 @@ module.exports = function registerScreensRoutes(app, deps) {
|
|
|
|
|
function applyConnectionCounts(screens, dashboardScreens) {
|
|
|
|
|
const countsById = new Map();
|
|
|
|
|
const countsBySlug = new Map();
|
|
|
|
|
const dashboardScreenBySlug = new Map();
|
|
|
|
|
|
|
|
|
|
(dashboardScreens || []).forEach(function (screen) {
|
|
|
|
|
const count = Number(screen && screen.player_connection_count || 0);
|
|
|
|
@@ -29,18 +33,24 @@ module.exports = function registerScreensRoutes(app, deps) {
|
|
|
|
|
}
|
|
|
|
|
if (slug) {
|
|
|
|
|
countsBySlug.set(slug, count);
|
|
|
|
|
dashboardScreenBySlug.set(slug, screen);
|
|
|
|
|
}
|
|
|
|
|
if (id) {
|
|
|
|
|
dashboardScreenBySlug.set(id, screen);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return (screens || []).map(function (screen) {
|
|
|
|
|
const slug = String(screen && screen.slug || '').trim();
|
|
|
|
|
const id = screen && screen.id !== undefined && screen.id !== null ? String(screen.id) : '';
|
|
|
|
|
const dashboardScreen = dashboardScreenBySlug.get(slug) || dashboardScreenBySlug.get(id) || null;
|
|
|
|
|
const playerConnectionCount = countsBySlug.has(slug)
|
|
|
|
|
? countsBySlug.get(slug)
|
|
|
|
|
: countsById.get(id) || 0;
|
|
|
|
|
|
|
|
|
|
return Object.assign({}, screen, {
|
|
|
|
|
player_connection_count: playerConnectionCount
|
|
|
|
|
player_connection_count: playerConnectionCount,
|
|
|
|
|
player_url: dashboardScreen && dashboardScreen.player_url ? dashboardScreen.player_url : screen.player_url || null
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
@@ -52,6 +62,7 @@ module.exports = function registerScreensRoutes(app, deps) {
|
|
|
|
|
if (!screen) {
|
|
|
|
|
return res.status(404).send('Screen not found');
|
|
|
|
|
}
|
|
|
|
|
screen.inUse = Boolean(await getScreenDeleteBlockMessage(pool, screen, deps.getScreenConnections));
|
|
|
|
|
const editData = await common.fetchScreenEditData(pool);
|
|
|
|
|
return res.send(pages.renderScreenEditPage(screen, editData, req.query.message ? String(req.query.message) : '', req.currentUser));
|
|
|
|
|
}
|
|
|
|
@@ -60,9 +71,18 @@ module.exports = function registerScreensRoutes(app, deps) {
|
|
|
|
|
const sort = common.getSortQuery(req);
|
|
|
|
|
const direction = common.getSortDirectionQuery(req);
|
|
|
|
|
const dashboardState = await buildDashboardState(pool);
|
|
|
|
|
const playerUrlsBySlug = typeof common.fetchScreenPlayerUrls === 'function'
|
|
|
|
|
? await common.fetchScreenPlayerUrls(pool)
|
|
|
|
|
: {};
|
|
|
|
|
const data = await common.fetchScreensPage(pool, page, LIST_PAGE_SIZE, search, sort, direction);
|
|
|
|
|
res.send(pages.renderScreensPage({
|
|
|
|
|
screens: applyConnectionCounts(data.screens || [], dashboardState.screens || []),
|
|
|
|
|
screens: applyConnectionCounts(data.screens || [], dashboardState.screens || []).map(function (screen) {
|
|
|
|
|
const slug = String(screen && screen.slug || '').trim();
|
|
|
|
|
const registryUrl = slug && playerUrlsBySlug[slug] ? String(playerUrlsBySlug[slug]).trim() : '';
|
|
|
|
|
return Object.assign({}, screen, {
|
|
|
|
|
player_url: screen.player_url || registryUrl || null
|
|
|
|
|
});
|
|
|
|
|
}),
|
|
|
|
|
pagination: buildPagination(data.totalItems, data.currentPage, 'page', { search: search, sort: sort, direction: direction }, LIST_PAGE_SIZE, 'screens', 'Screen pages')
|
|
|
|
|
}, req.query.message ? String(req.query.message) : '', req.currentUser));
|
|
|
|
|
} catch (error) {
|
|
|
|
@@ -76,6 +96,11 @@ module.exports = function registerScreensRoutes(app, deps) {
|
|
|
|
|
if (!screen) {
|
|
|
|
|
return res.status(404).send('Screen not found');
|
|
|
|
|
}
|
|
|
|
|
const playerUrlsBySlug = typeof common.fetchScreenPlayerUrls === 'function'
|
|
|
|
|
? await common.fetchScreenPlayerUrls(pool)
|
|
|
|
|
: {};
|
|
|
|
|
screen.player_url = screen.slug && playerUrlsBySlug[screen.slug] ? String(playerUrlsBySlug[screen.slug]).trim() : null;
|
|
|
|
|
screen.inUse = Boolean(await getScreenDeleteBlockMessage(pool, screen, deps.getScreenConnections));
|
|
|
|
|
const editData = await common.fetchScreenEditData(pool);
|
|
|
|
|
return res.send(pages.renderScreenEditPage(screen, editData, req.query.message ? String(req.query.message) : '', req.currentUser));
|
|
|
|
|
} catch (error) {
|
|
|
|
|