Simplify code by moving checks at login time to events.js

This commit is contained in:
Alexandre Aubin 2019-02-05 00:13:58 +00:00
parent 28c788f38e
commit 7480d4f7bb
2 changed files with 73 additions and 80 deletions

View file

@ -3,8 +3,6 @@
var app = Sammy.apps['#main'];
var store = app.store;
var initial_checks_already_performed = false;
/**
* Home
*
@ -12,86 +10,11 @@
// Home page
app.get('#/', function (c) {
if (initial_checks_already_performed)
{
c.view("home");
}
else c.api('/users', function(data) {
// Warn admin if no users are created.
if (typeof data.users !== 'undefined' && data.users.length === 0) {
c.flash('warning', y18n.t('warning_first_user'));
}
// Get security feed and display new items
var securityFeed = 'https://yunohost.org/security.rss';
var forumUrl = 'https://forum.yunohost.org';
$.ajax({
url: securityFeed,
// dataType: (jQuery.browser.msie) ? "text" : "xml",
dataType: "xml"
})
.done(function(xml){
// Get viewed security alerts from cookie
var viewedItems = Cookies.get('ynhSecurityViewedItems') || [];
// Get 6 month earlier date
var SixMonthEarlier = new Date();
SixMonthEarlier.setMonth(SixMonthEarlier.getMonth() - 6);
// Loop through items in a reverse order (older first)
$($('item', xml).get().reverse()).each(function(k, v) {
var link = $('link', v).text();
if (typeof link == 'string' && link !== '' && link.charAt(0) == '/') {
link = forumUrl+link;
}
// var description=$('description', v).text();
// description=description.replace('href="/','href="'+forumUrl+'/');
var item = {
guid: $('guid', v).text(),
title: $('title', v).text(),
url: link,
// desc: description,
date: new Date($('pubDate', v).text()),
};
// If item is not already viewed and is not older than 6 month
if (viewedItems.indexOf(item.guid) === -1 && (item.date.getTime() > SixMonthEarlier.getTime())) {
// Show security message to administrator
var warning = item.title + ' - ' +
item.date.toISOString().substring(0, 10) +
' (<a href="'+ item.url +'" class="alert-link" target="_blank">'+y18n.t('read_more')+'</a>)';
c.flash('warning', warning);
// Store viewed item
viewedItems.push(item.guid);
}
});
// Saved viewed items to cookie
Cookies.set('ynhSecurityViewedItems', viewedItems, {
expires: 7
});
})
.fail(function() {
c.flash('fail', y18n.t('error_retrieve_feed', [securityFeed]));
});
c.api("/diagnosis", function(data) {
versions = data.packages;
$('#yunohost-version').html(y18n.t('footer_version', [versions.yunohost.version, versions.yunohost.repo]));
if (data.security["CVE-2017-5754"].vulnerable) {
c.flash('danger', y18n.t('meltdown'));
}
});
initial_checks_already_performed = true;
c.view('home');
});
c.view("home");
// N.B : if you need to run stuff at login time,
// see js/events.js instead
});
/**
* Login
*

View file

@ -8,6 +8,76 @@
*
*/
app.bind('login', function(e, data) {
c.api('/users', function(data) {
// Warn admin if no users are created.
if (typeof data.users !== 'undefined' && data.users.length === 0) {
c.flash('warning', y18n.t('warning_first_user'));
}
// Get security feed and display new items
var securityFeed = 'https://yunohost.org/security.rss';
var forumUrl = 'https://forum.yunohost.org';
$.ajax({
url: securityFeed,
// dataType: (jQuery.browser.msie) ? "text" : "xml",
dataType: "xml"
})
.done(function(xml){
// Get viewed security alerts from cookie
var viewedItems = Cookies.get('ynhSecurityViewedItems') || [];
// Get 6 month earlier date
var SixMonthEarlier = new Date();
SixMonthEarlier.setMonth(SixMonthEarlier.getMonth() - 6);
// Loop through items in a reverse order (older first)
$($('item', xml).get().reverse()).each(function(k, v) {
var link = $('link', v).text();
if (typeof link == 'string' && link !== '' && link.charAt(0) == '/') {
link = forumUrl+link;
}
// var description=$('description', v).text();
// description=description.replace('href="/','href="'+forumUrl+'/');
var item = {
guid: $('guid', v).text(),
title: $('title', v).text(),
url: link,
// desc: description,
date: new Date($('pubDate', v).text()),
};
// If item is not already viewed and is not older than 6 month
if (viewedItems.indexOf(item.guid) === -1 && (item.date.getTime() > SixMonthEarlier.getTime())) {
// Show security message to administrator
var warning = item.title + ' - ' +
item.date.toISOString().substring(0, 10) +
' (<a href="'+ item.url +'" class="alert-link" target="_blank">'+y18n.t('read_more')+'</a>)';
c.flash('warning', warning);
// Store viewed item
viewedItems.push(item.guid);i
}
});
// Saved viewed items to cookie
Cookies.set('ynhSecurityViewedItems', viewedItems, {
expires: 7
});
})
.fail(function(stuff) {
console.log(stuff);
c.flash('fail', y18n.t('error_retrieve_feed', [securityFeed]));
});
c.api("/diagnosis", function(data) {
versions = data.packages;
$('#yunohost-version').html(y18n.t('footer_version', [versions.yunohost.version, versions.yunohost.repo]));
if (data.security["CVE-2017-5754"].vulnerable) {
c.flash('danger', y18n.t('meltdown'));
}
});
});
});
app.bind('logout', function(e, data) {