-
Notifications
You must be signed in to change notification settings - Fork 292
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
Changes from 10 commits
93e54d4
f30ccea
7785415
af5c52d
01915fd
79ce6a0
f6e6b57
9165930
5929b5a
f7659bd
1d15171
6f95df4
9f3bf76
30f1bd3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
/* | ||
* 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; | ||
} | ||
|
||
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; | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
})(); |
This file was deleted.
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']); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we want to use fetch for this???
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One thing at a time :D There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. means touching it twice There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tests fail if I use There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I will see what I can do here... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like it needs a reader: Maybe we should use a wrapper which wraps the fetch API. Like: https://github.com/typicode/fetchival There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)); | ||
}); | ||
}); | ||
} | ||
}; | ||
})(); |
This file was deleted.
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', | ||
}; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure
There was a problem hiding this comment.
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).