Skip to content
Snippets Groups Projects
Commit e1d9deee authored by echicken's avatar echicken :chicken:
Browse files

Moved event subscription/handling stuff from index.xjs to common.js.

Tidied up common.js, removed telegram polling, switched to telegram event subscription and handler.
parent c76628b3
Branches
Tags
No related merge requests found
var last_run = 0;
var frequency = 15;
function cycle() {
if (user.alias === settings.guest) return;
if (time() - last_run <= frequency) return;
last_run = time();
const tg = system.get_telegram(user.number);
if (tg !== null) emit({ event: 'telegram', data: JSON.stringify(tg) });
}
this;
......@@ -98,9 +98,6 @@
<?xjs if (file_exists(settings.web_root + 'css/custom.css')) { ?>
<link href="./css/custom.css" rel="stylesheet">
<?xjs } ?>
<script type="text/javascript">
const _sbbs_events = {};
</script>
</head>
<body>
......@@ -238,13 +235,6 @@
);
}
);
const _evtqs = Object.keys(_sbbs_events).reduce(function (a, c, i) {
return a + (i == 0 ? '?' : '&') + 'subscribe=' + c; }, ''
);
const _es = new EventSource('/api/events.ssjs' + _evtqs);
Object.keys(_sbbs_events).forEach(function (e) {
_es.addEventListener(e, _sbbs_events[e]);
});
</script>
</body>
......
// How often to check for unread mail, new telegrams (milliseconds)
var updateInterval = 60000;
const _sbbs_events = {};
function login(evt) {
if ($('#input-username').val() === '' ||
$('#input-password').val() === ''
) {
if ($('#input-username').val() == '' || $('#input-password').val() == '') {
return;
}
if (typeof evt !== 'undefined') evt.preventDefault();
$.ajax(
{ 'url' : './api/auth.ssjs',
'method' : 'POST',
'data' : {
username : $('#input-username').val(),
password : $('#input-password').val()
}
$.ajax({
url: './api/auth.ssjs',
method: 'POST',
data: {
username : $('#input-username').val(),
password : $('#input-password').val()
}
).done(
function (data) {
if (data.authenticated) {
window.location.reload(true);
} else {
$('#login-form').append(
'<p class="text-danger">Login failed</p>'
);
}
}).done(function (data) {
if (data.authenticated) {
window.location.reload(true);
} else {
$('#login-form').append('<p class="text-danger">Login failed</p>');
}
);
});
}
function logout() {
$.ajax(
{ 'url' : './api/auth.ssjs',
'method' : 'GET',
'data' : {
'logout' : true
}
}
).done(
function (data) { if (!data.authenticated) window.location.href = '/'; }
);
$.ajax({
url: './api/auth.ssjs',
method: 'GET',
data: { logout: true }
}).done(function (data) {
if (!data.authenticated) window.location.href = '/';
});
}
function scrollUp() {
......@@ -49,15 +40,10 @@ function scrollUp() {
}
function getMailUnreadCount() {
$.getJSON(
'./api/forum.ssjs?call=get-mail-unread-count',
function (data) {
$('#badge-unread-mail').text(data.count < 1 ? '' : data.count);
$('#badge-unread-mail-inner').text(
data.count < 1 ? '' : data.count
);
}
);
$.getJSON('./api/forum.ssjs?call=get-mail-unread-count', function (data) {
$('#badge-unread-mail').text(data.count < 1 ? '' : data.count);
$('#badge-unread-mail-inner').text(data.count < 1 ? '' : data.count);
});
}
function sendTelegram(alias) {
......@@ -67,72 +53,60 @@ function sendTelegram(alias) {
'placeholder="My message" name="telegram" id="telegram">'
);
$('#popUpModalActionButton').show();
$('#popUpModalActionButton').click(
function () {
$.getJSON(
'./api/system.ssjs?call=send-telegram&user=' +
alias + '&telegram=' + $('#telegram').val(),
function(data) {}
);
$('#popUpModal').modal('hide');
}
);
$('#popUpModalActionButton').click(function () {
$.getJSON(
'./api/system.ssjs?call=send-telegram&user=' +
alias + '&telegram=' + $('#telegram').val(),
function(data) {}
);
$('#popUpModal').modal('hide');
});
$('#popUpModal').modal('show');
}
function getTelegram() {
$.getJSON(
'./api/system.ssjs?call=get-telegram',
function (data) {
if (typeof data.telegram === 'undefined' ||
data.telegram === null
) {
return;
}
var tg = data.telegram.replace(
/\1./g, ''
).replace(
/\r?\n/g, '<br>'
);
$('#popUpModalTitle').html('New telegram(s) received');
$('#popUpModalBody').append(tg);
$('#popUpModalActionButton').hide();
$('#popUpModal').modal('show');
}
);
}
window.onload = function () {
$('#button-logout').click(logout);
$('#button-login').click(login);
$('#form-login').submit(login);
$('#popUpModal').on(
'hidden.bs.modal',
function (e) {
$('#popUpModalActionButton').off('click');
$('#popUpModalTitle').empty();
$('#popUpModalBody').empty();
}
);
$("#popUpModalCloseButton").click(
function () {
$('#popUpModal').modal('hide');
}
);
$('#popUpModal').on('hidden.bs.modal', function (e) {
$('#popUpModalActionButton').off('click');
$('#popUpModalTitle').empty();
$('#popUpModalBody').empty();
});
$("#popUpModalCloseButton").click(function () {
$('#popUpModal').modal('hide');
});
setTimeout(scrollUp, 25);
window.onhashchange = scrollUp;
if ($('#button-logout').length > 0) {
getMailUnreadCount();
setInterval(getMailUnreadCount, updateInterval);
// Write backing event module
// Switch to event subscription & callback
getMailUnreadCount();
setInterval(getMailUnreadCount, updateInterval);
getTelegram();
setInterval(getTelegram, updateInterval);
_sbbs_events.telegram = function (e) {
const tg = JSON.parse(e.data).replace(/\1./g, '').replace(
/\r?\n/g, '<br>'
);
$('#popUpModalTitle').html('New telegram(s) received');
$('#popUpModalBody').append(tg);
$('#popUpModalActionButton').hide();
$('#popUpModal').modal('show');
}
}
}
const _evtqs = Object.keys(_sbbs_events).reduce(function (a, c, i) {
return a + (i == 0 ? '?' : '&') + 'subscribe=' + c; }, ''
);
const _es = new EventSource('/api/events.ssjs' + _evtqs);
Object.keys(_sbbs_events).forEach(function (e) {
_es.addEventListener(e, _sbbs_events[e]);
});
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment