From 8219e8e428b43feb8df3935ce26e73a9a3b69a26 Mon Sep 17 00:00:00 2001 From: Kasalehlia <kasalehlia@clonejo.de> Date: Tue, 17 Nov 2015 20:34:03 +0100 Subject: [PATCH] hardened brand module --- modules/brand.js | 34 ++++++++++++++++++++++++--------- modules/brand/status.mustache | 3 +++ modules/brand/template.mustache | 6 +----- 3 files changed, 29 insertions(+), 14 deletions(-) create mode 100644 modules/brand/status.mustache diff --git a/modules/brand.js b/modules/brand.js index 089d6e7..863e9e3 100644 --- a/modules/brand.js +++ b/modules/brand.js @@ -2,18 +2,29 @@ var httpreq = require('httpreq'); var Mustache = require('mustache'); var URL = 'https://status.stratum0.org/status.json'; -var TEMPLATE = ''; +var TEMPLATES = ''; require('fs').readFile('modules/brand/template.mustache', 'utf-8', function (err, data) { - TEMPLATE = data; + TEMPLATES.template = data; +}); +require('fs').readFile('modules/brand/status.mustache', 'utf-8', function (err, data) { + TEMPLATES.status = data; }); var status = {}; -function renderStatus () { - return Mustache.render(TEMPLATE, status); +function renderStatus(sock, everything) { + var sendInner = function () { + sock.emit('brand.status', Mustache.render(TEMPLATES.status, status)); + } + if (everything) { + sock.emit('brand', Mustache.render(TEMPLATES.template, status)); + setTimeout(sendInner, 3000); + } else { + sendInner(); + } } -function fetchStatus (cb) { +function fetchStatus(cb) { httpreq.get(URL, function (err, res) { var state = JSON.parse(res.body).state; cb(state); @@ -22,19 +33,24 @@ function fetchStatus (cb) { module.exports = function (io) { function update() { + var firstTime = true; fetchStatus(function (state) { if (status.lastchange != state.lastchange) { var d = new Date(state.lastchange*1000); state.since = DOW[d.getDay()]+', '+pad(d.getHours(),2)+':' +pad(d.getMinutes(),2); status = state; - io.emit('brand', renderStatus()); + renderStatus(); + } + if (firstTime && status) { + io.on('connection', function (sock) { + renderStatus(sock, true); + }); + renderStatus(io, true); + firstTime = false; } }); } - io.on('connection', function (sock) { - sock.emit('brand', renderStatus()); - }); setInterval(update, 2*60*1000); //every 2 minutes update(); } diff --git a/modules/brand/status.mustache b/modules/brand/status.mustache new file mode 100644 index 0000000..a81fcab --- /dev/null +++ b/modules/brand/status.mustache @@ -0,0 +1,3 @@ +Space is {{#open}}<span class="open">open</span> on <span class="person">{{trigger_person}}</span>{{/open}} +{{^open}}<span class="closed">closed</span>{{/open}} +<br>since {{since}} diff --git a/modules/brand/template.mustache b/modules/brand/template.mustache index 9eca35b..b2be527 100644 --- a/modules/brand/template.mustache +++ b/modules/brand/template.mustache @@ -1,6 +1,2 @@ <img src="/modules/brand/stratum0_logo.svg"><br> -<h3>Space is - {{#open}}<span class="open">open</span> on <span class="person">{{trigger_person}}</span>{{/open}} - {{^open}}<span class="closed">closed</span>{{/open}} - <br>since {{since}} -</h3> +<h3 data-infodisplay-outlet="status"></h3> -- GitLab