From 28c788f38e6d7aedfcd12553b7643bf64ed9ffb7 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Mon, 4 Feb 2019 23:55:21 +0000 Subject: [PATCH 1/4] Remove calls to /version, use /diagnosis instead --- src/js/yunohost/controllers/home.js | 2 ++ src/js/yunohost/controllers/monitor.js | 6 +++--- src/js/yunohost/controllers/tools.js | 4 ++-- src/js/yunohost/events.js | 3 --- src/js/yunohost/main.js | 7 ------- 5 files changed, 7 insertions(+), 15 deletions(-) diff --git a/src/js/yunohost/controllers/home.js b/src/js/yunohost/controllers/home.js index fb3159fc..03dd69ee 100644 --- a/src/js/yunohost/controllers/home.js +++ b/src/js/yunohost/controllers/home.js @@ -78,6 +78,8 @@ }); 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')); } diff --git a/src/js/yunohost/controllers/monitor.js b/src/js/yunohost/controllers/monitor.js index a7e8c664..527e466c 100644 --- a/src/js/yunohost/controllers/monitor.js +++ b/src/js/yunohost/controllers/monitor.js @@ -30,8 +30,8 @@ delete monitorData.network.usage.lo; // Get YunoHost versions too - c.api('/version', function(versions) { - monitorData.versions = versions; + c.api('/diagnosis', function(diagnosis) { + monitorData.versions = diagnosis.packages; c.view('tools/tools_monitoring', monitorData); }); }); @@ -47,4 +47,4 @@ }, 'GET'); }); -})(); \ No newline at end of file +})(); diff --git a/src/js/yunohost/controllers/tools.js b/src/js/yunohost/controllers/tools.js index d24566db..09f8687a 100644 --- a/src/js/yunohost/controllers/tools.js +++ b/src/js/yunohost/controllers/tools.js @@ -223,8 +223,8 @@ // Packages version app.get('#/tools/versions', function (c) { - c.api('/version', function(versions) { - c.view('tools/tools_versions', {'versions' : versions}); + c.api('/diagnosis', function(diagnosis) { + c.view('tools/tools_versions', {'versions' : diagnosis.packages }); }); }); diff --git a/src/js/yunohost/events.js b/src/js/yunohost/events.js index cde91e94..505bb140 100644 --- a/src/js/yunohost/events.js +++ b/src/js/yunohost/events.js @@ -8,9 +8,6 @@ * */ app.bind('login', function(e, data) { - this.api('/version', function(versions) { - $('#yunohost-version').html(y18n.t('footer_version', [versions.yunohost.version, versions.yunohost.repo])); - }); }); app.bind('logout', function(e, data) { diff --git a/src/js/yunohost/main.js b/src/js/yunohost/main.js index 8ca70af2..c1e14ecf 100644 --- a/src/js/yunohost/main.js +++ b/src/js/yunohost/main.js @@ -149,13 +149,6 @@ // Store url sam.store.set('url', window.location.hostname + '/yunohost/api'); - // Get YunoHost version - if (sam.store.get('connected')) { - this.api('/version', function(versions) { - $('#yunohost-version').html(y18n.t('footer_version', [versions.yunohost.version, versions.yunohost.repo])); - }); - } - // Flash messages var flashMessage = $('#flashMessage'); $('#toggle-btn', flashMessage).click(function(e) { From 7480d4f7bb5ee5bc9b135079189449699844b107 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Tue, 5 Feb 2019 00:13:58 +0000 Subject: [PATCH 2/4] Simplify code by moving checks at login time to events.js --- src/js/yunohost/controllers/home.js | 83 ++--------------------------- src/js/yunohost/events.js | 70 ++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 80 deletions(-) diff --git a/src/js/yunohost/controllers/home.js b/src/js/yunohost/controllers/home.js index 03dd69ee..ac315c21 100644 --- a/src/js/yunohost/controllers/home.js +++ b/src/js/yunohost/controllers/home.js @@ -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) + - ' ('+y18n.t('read_more')+')'; - 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 * diff --git a/src/js/yunohost/events.js b/src/js/yunohost/events.js index 505bb140..3f85c9e8 100644 --- a/src/js/yunohost/events.js +++ b/src/js/yunohost/events.js @@ -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) + + ' ('+y18n.t('read_more')+')'; + 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) { From f0a4428f564d5331b6d6e71b6bd486a7b29634b0 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Tue, 5 Feb 2019 00:24:17 +0000 Subject: [PATCH 3/4] In fact we do need this so that the footer gets the correct value even if opening a new tab while already logged in --- src/js/yunohost/main.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/js/yunohost/main.js b/src/js/yunohost/main.js index c1e14ecf..ea00fe6c 100644 --- a/src/js/yunohost/main.js +++ b/src/js/yunohost/main.js @@ -149,6 +149,13 @@ // Store url sam.store.set('url', window.location.hostname + '/yunohost/api'); + if (sam.store.get('connected')) { + this.api('/diagnosis', function(diagnosis) { + versions = diagnosis.packages; + $('#yunohost-version').html(y18n.t('footer_version', [versions.yunohost.version, versions.yunohost.repo])); + }); + } + // Flash messages var flashMessage = $('#flashMessage'); $('#toggle-btn', flashMessage).click(function(e) { From ac4d240a9b51e74f46c78af072354b23c957e863 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Tue, 5 Feb 2019 15:28:10 +0000 Subject: [PATCH 4/4] Not sure why but pacman was still hanging --- src/js/yunohost/events.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/js/yunohost/events.js b/src/js/yunohost/events.js index 3f85c9e8..478edecc 100644 --- a/src/js/yunohost/events.js +++ b/src/js/yunohost/events.js @@ -66,7 +66,6 @@ }); }) .fail(function(stuff) { - console.log(stuff); c.flash('fail', y18n.t('error_retrieve_feed', [securityFeed])); }); @@ -76,6 +75,7 @@ if (data.security["CVE-2017-5754"].vulnerable) { c.flash('danger', y18n.t('meltdown')); } + $('div.loader').remove(); }); }); });