filo_caspar/api/content/routes.mjs

107 lines
2.2 KiB
JavaScript
Raw Normal View History

import _ from 'lodash'
2016-04-10 08:37:05 +00:00
2016-04-14 04:01:51 +00:00
export const active = { }
function getSocket(ctx, all) {
if (all === true) return ctx.io
return ctx.socket
}
2018-06-26 18:35:12 +00:00
/*
* Event: 'content.display'
*
* Display a specific graphic content
*/
export async function display(ctx, data) {
let compiled = _.template(data.graphic.settings.html)
2016-04-14 04:01:51 +00:00
let html = compiled(data.data)
2016-04-10 08:37:05 +00:00
// let old = await Content.getSingle(data.graphic.name)
let playing = ctx.db.get('playing')
let old = playing.find({ name: data.graphic.name }).value()
2018-06-26 18:35:12 +00:00
if (old) {
await playing.removeById(old.id).write()
2018-06-26 18:35:12 +00:00
}
2016-04-14 04:01:51 +00:00
let payload = {
graphic: data.graphic,
2018-06-26 18:35:12 +00:00
name: data.graphic.name,
html: html || '',
css: data.graphic.settings.css || '',
2016-04-14 04:01:51 +00:00
data: data.data,
2018-06-26 18:35:12 +00:00
is_deleted: false,
2016-04-14 04:01:51 +00:00
}
await playing.insert(payload).write()
2018-06-26 18:35:12 +00:00
ctx.io.emit('client.display', playing.find({ name: data.graphic.name }).value())
2016-04-14 04:01:51 +00:00
list(ctx, true)
2016-04-10 08:37:05 +00:00
}
2018-06-26 18:35:12 +00:00
/*
* Event: 'content.hide'
*
* Hide a specific graphic content
*/
export async function hide(ctx, data) {
let playing = ctx.db.get('playing')
let old = playing.find({ name: data.name }).value()
2018-06-26 18:35:12 +00:00
if (!old) return
2018-06-26 18:35:12 +00:00
await playing.removeById(old.id).write()
2016-04-14 04:01:51 +00:00
2016-04-10 08:37:05 +00:00
ctx.io.emit('client.hide', {
2016-04-14 04:01:51 +00:00
name: data.name,
2016-04-10 08:37:05 +00:00
})
2016-04-14 04:01:51 +00:00
list(ctx, true)
}
function generateDisplayText(item) {
2018-06-26 18:35:12 +00:00
// if (item.graphic.engine === 'countdown') {
// return `${item.data.text} - ${item.data.finished}`
// }
try {
return _.template(item.graphic.settings.main)(item.data)
2018-06-26 18:35:12 +00:00
} catch (e) {
return `Error creating display: ${e.message}`
2016-04-14 04:01:51 +00:00
}
}
2018-06-26 18:35:12 +00:00
/*
* Event: 'content.list'
* Runs on start of every new connection
*
* Send a name list of all active graphics
*/
export async function list(ctx, all) {
let allContent = ctx.db.get('playing').value()
2018-06-26 18:35:12 +00:00
let payload = await Promise.all(allContent.map(function(item) {
return {
name: item.name,
display: generateDisplayText(item),
}
}))
2016-04-14 04:01:51 +00:00
getSocket(ctx, all).emit('content.list', payload)
}
2018-06-26 18:35:12 +00:00
/*
* Event: 'content.list'
* Runs on start of every new connection
*
* Send actual graphics of all active graphics
*/
export async function reset(ctx) {
let allContent = ctx.db.get('playing').value()
2018-06-26 18:35:12 +00:00
ctx.socket.emit('client.reset', allContent)
2016-04-10 08:37:05 +00:00
}