60 lines
2.3 KiB
JavaScript
60 lines
2.3 KiB
JavaScript
const { collectLocalControlUsers } = require('../../local-control-users');
|
|
|
|
const TASK = {
|
|
key: 'player-control-sync',
|
|
title: 'Player Local Control auth refresh',
|
|
category: 'player-sync',
|
|
trigger: 'scheduled recurring task, every fifteen minutes',
|
|
purpose: 'push eligible Client Control users to connected players.',
|
|
taskType: 'recurring-run',
|
|
intervalMs: 15 * 60 * 1000
|
|
};
|
|
|
|
function registerPlayerControlSyncTask(options) {
|
|
const pool = options && options.pool;
|
|
const common = options && options.common;
|
|
const backgroundTaskQueue = options && options.backgroundTaskQueue;
|
|
const forwardPlayerCommandToDevice = options && options.forwardPlayerCommandToDevice;
|
|
const localPlayerInternalUrl = String(options && options.localPlayerInternalUrl || '').trim().replace(/\/$/, '');
|
|
|
|
if (!pool || !common || !backgroundTaskQueue || typeof common.fetchPlayerRegistrations !== 'function' || typeof forwardPlayerCommandToDevice !== 'function') {
|
|
throw new Error('registerPlayerControlSyncTask requires player control sync dependencies.');
|
|
}
|
|
|
|
backgroundTaskQueue.registerRecurringTask({
|
|
key: TASK.key,
|
|
title: TASK.title,
|
|
category: TASK.category,
|
|
intervalMs: TASK.intervalMs,
|
|
metadata: {},
|
|
run: async function () {
|
|
const cachedUsers = await collectLocalControlUsers(pool);
|
|
const players = await common.fetchPlayerRegistrations(pool);
|
|
const results = await Promise.all((Array.isArray(players) ? players : []).filter(function (player) {
|
|
const playerInternalUrl = String(player && player.internal_base_url || '').trim().replace(/\/$/, '');
|
|
return !localPlayerInternalUrl || playerInternalUrl !== localPlayerInternalUrl;
|
|
}).map(async function (player) {
|
|
const deviceId = String(player && (player.identifier || player.device_id) || '').trim();
|
|
if (!deviceId) {
|
|
return { ok: false, skipped: true };
|
|
}
|
|
try {
|
|
await forwardPlayerCommandToDevice(deviceId, {
|
|
command: 'sync-local-control',
|
|
users: cachedUsers
|
|
});
|
|
return { ok: true };
|
|
} catch (_error) {
|
|
return { ok: false };
|
|
}
|
|
}));
|
|
return {
|
|
playerCount: results.length,
|
|
syncedCount: results.filter(function (result) { return result.ok; }).length
|
|
};
|
|
}
|
|
});
|
|
}
|
|
|
|
module.exports = { registerPlayerControlSyncTask };
|