Skip to content

Commit

Permalink
ES6: Migrated "announce" (#899)
Browse files Browse the repository at this point in the history
  • Loading branch information
bennycode authored Mar 15, 2017
1 parent 5a02957 commit 2e20f9d
Show file tree
Hide file tree
Showing 8 changed files with 248 additions and 158 deletions.
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.

133 changes: 133 additions & 0 deletions app/script/announce/AnnounceRepository.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* 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;
}

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.

57 changes: 57 additions & 0 deletions app/script/announce/AnnounceService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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';
}
}

get_announcements() {
return new Promise((resolve, reject) => {
$.get(this.url).done((data) => {
resolve(data['result']);
}).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',
};
Loading

0 comments on commit 2e20f9d

Please sign in to comment.