Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ES6: Migrated "announce" #899

Merged
merged 14 commits into from
Mar 15, 2017
5 changes: 5 additions & 0 deletions .eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ env:
extends:
"eslint:recommended"

globals:
$: true
amplify: true
z: true

rules:
comma-dangle: [2, always-multiline]
handle-callback-err: 2
Expand Down
89 changes: 0 additions & 89 deletions app/script/announce/AnnounceRepository.coffee

This file was deleted.

134 changes: 134 additions & 0 deletions app/script/announce/AnnounceRepository.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* Wire
* Copyright (C) 2016 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*
*/

'use strict';

(function() {
window.z = window.z || {};
window.z.announce = z.announce || {};

const ANNOUNCE_CONFIG = {
CHECK_INTERVAL: 3 * 60 * 60 * 1000,
CHECK_TIMEOUT: 5 * 60 * 1000,
UPDATE_INTERVAL: 6 * 60 * 60 * 1000,
};

window.z.announce.AnnounceRepository = class AnnounceRepository {
constructor(announce_service) {
this.logger = new z.util.Logger('z.announce.AnnounceRepository', z.config.LOGGER.OPTIONS);
this.announce_service = announce_service;
return this;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

think we dont need to return this or is it chained?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is needed in TestAPI I believe

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the this. It was there because otherwise CoffeeScript would return the last statement from the constructor (instead the instance of the class).

}

init() {
return window.setTimeout(() => {
this.check_announcements();
this.schedule_checks();
}, ANNOUNCE_CONFIG.CHECK_TIMEOUT);
}

check_announcements() {
return this.announce_service.get_announcements().then(this.process_announce_list).catch((error) => {
this.logger.error(`Failed to fetch announcements: ${error}`);
});
}

check_version() {
return this.announce_service.get_version().then((server_version) => {
this.logger.info(`Found new version ${server_version}`);

if (server_version > z.util.Environment.version(false, true)) {
amplify.publish(z.event.WebApp.LIFECYCLE.UPDATE, z.announce.UPDATE_SOURCE.WEBAPP);
}
}).catch((error) => {
this.logger.error(`Failed to fetch version: ${error}`);
});
}

schedule_checks() {
window.setInterval(() => {
this.check_announcements();
}, ANNOUNCE_CONFIG.CHECK_INTERVAL);

window.setInterval(() => {
this.check_version();
}, ANNOUNCE_CONFIG.CHECK_INTERVAL);
}

process_announce_list(announcements_list) {
if (announcements_list) {
for (let announcement in announcements_list) {
if (!z.util.Environment.frontend.is_localhost()) {
if (announcement.version_max && z.util.Environment.version(false) > announcement.version_max) {
continue;
}

if (announcement.version_min && z.util.Environment.version(false) < announcement.version_min) {
continue;
}
}

const key = `${z.storage.StorageKey.ANNOUNCE.ANNOUNCE_KEY}@${announcement.key}`;
if (!z.util.StorageUtil.get_value(key)) {
z.util.StorageUtil.set_value(key, 'read');
if (!z.util.Environment.browser.supports.notifications) {
return;
}

if (window.Notification.permission === z.system_notification.PermissionStatusState.DENIED) {
return;
}

if (z.localization.Localizer.locale !== 'en') {
announcement.title = announcement[`title_${z.localization.Localizer.locale}`] || announcement.title;
announcement.message = announcement[`message_${z.localization.Localizer.locale}`] || announcement.message;
}

const notification = new window.Notification(announcement.title, {
body: announcement.message,
icon: z.util.Environment.electron && z.util.Environment.os.mac ? '' : '/image/logo/notification.png',
sticky: true,
requireInteraction: true,
});

amplify.publish(z.event.WebApp.ANALYTICS.EVENT, z.tracking.EventName.ANNOUNCE.SENT, {campaign: announcement.campaign});
this.logger.info(`Announcement '${announcement.title}' shown`);

notification.onclick = () => {
amplify.publish(z.event.WebApp.ANALYTICS.EVENT, z.tracking.EventName.ANNOUNCE.CLICKED, {campaign: announcement.campaign});
this.logger.info(`Announcement '${announcement.title}' clicked`);

if (announcement.link) {
z.util.safe_window_open(announcement.link);
}

if (announcement.refresh) {
amplify.publish(z.event.WebApp.LIFECYCLE.REFRESH);
}

notification.close();
};

break;
}
}
}
}
};
})();
45 changes: 0 additions & 45 deletions app/script/announce/AnnounceService.coffee

This file was deleted.

58 changes: 58 additions & 0 deletions app/script/announce/AnnounceService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Wire
* Copyright (C) 2016 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*
*/

'use strict';

(function() {
window.z = window.z || {};
window.z.announce = z.announce || {};

const ANNOUNCE_SERVICE_URL = 'api/v1/announce/';

window.z.announce.AnnounceService = class AnnounceService {
constructor() {
this.logger = new z.util.Logger('z.announce.AnnounceService', z.config.LOGGER.OPTIONS);
this.url = `${z.util.Environment.backend.website_url()}${ANNOUNCE_SERVICE_URL}?order=created&active=true`;
if (z.util.Environment.frontend.is_production()) {
this.url += '&production=true';
}
return this;
}

get_announcements() {
return new Promise((resolve, reject) => {
$.get(this.url).done((data) => {
resolve(data['result']);
Copy link
Contributor

@herrmannplatz herrmannplatz Mar 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we want to use fetch for this???

return fetch(this.url).then((response) => response.json()).then((json) => json.result)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing at a time :D

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

means touching it twice

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests fail if I use return fetch(this.url).then((data) => data.result).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there "easy" fixes we should do them in one go.. but of course if the tests fail and stuff then maybe have somewhere a list that we can track of the things that we spotted..

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a good time to have a complete review of the codebase!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I will see what I can do here...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

window.fetch returns a ReadableStream in data.body. So the code needs some adjustments.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it needs a reader: var reader = response.body.getReader(); (https://jakearchibald.com/2015/thats-so-fetch/)

Maybe we should use a wrapper which wraps the fetch API. Like: https://github.com/typicode/fetchival

Copy link
Contributor Author

@bennycode bennycode Mar 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to do the trick:

get_announcements() {
  return window.fetch(this.url).then((response) => response.json()).then((data) => data.result);
}

get_version() {
  return window.fetch('version/').then((response) => response.json()).then((data) => data.version);
}

}).fail((jqXHR, textStatus, errorThrown) => {
reject(new Error(errorThrown));
});
});
}

get_version() {
return new Promise((resolve, reject) => {
$.get('version/').done((data) => {
resolve(data['version']);
}).fail((jqXHR, textStatus, errorThrown) => {
reject(new Error(errorThrown));
});
});
}
};
})();
24 changes: 0 additions & 24 deletions app/script/announce/UpdateSource.coffee

This file was deleted.

28 changes: 28 additions & 0 deletions app/script/announce/UpdateSource.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Wire
* Copyright (C) 2016 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*
*/

'use strict';

window.z = window.z || {};
window.z.announce = z.announce || {};

window.z.announce.UPDATE_SOURCE = {
DESKTOP: 'desktop',
WEBAPP: 'webapp',
};