Skip to content

Commit

Permalink
refactor: change notification class to extend window.Notification (f…
Browse files Browse the repository at this point in the history
  • Loading branch information
SpecialAro authored and ponces committed Apr 30, 2024
1 parent adbd62f commit 0a52bbd
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 43 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/builds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -387,3 +387,9 @@ jobs:
CSC_IDENTITY_AUTO_DISCOVERY: false
run: pnpm build --publish always
shell: bash
- name: Upload build artifacts
if: success() || failure()
uses: actions/upload-artifact@v4
with:
name: windows
path: out
3 changes: 2 additions & 1 deletion src/stores/ServicesStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,8 @@ export default class ServicesStore extends TypedStore {

if (service.isNotificationEnabled) {
let title: string;
options.icon = service.iconUrl;
options.icon =
options.icon || service.customIconUrl || service.iconUrl;
if (this.stores.settings.all.app.privateNotifications === true) {
// Remove message data from notification in private mode
options.body = '';
Expand Down
59 changes: 18 additions & 41 deletions src/webview/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,52 +31,29 @@ export class NotificationsHandler {
}

export const notificationsClassDefinition = `(() => {
class Notification {
static permission = 'granted';
constructor(title = '', options = {}) {
this.title = title;
this.options = options;
try {
window.ferdium.displayNotification(title, options)
.then(() => {
if (typeof (this.onClick) === 'function') {
this.onClick();
}
});
} catch(error) {
this.options.onClick = null;
window.ferdium.displayNotification(title, options)
.then(() => {
if (typeof (this.onClick) === 'function') {
this.onClick();
}
});
}
}
static requestPermission(cb = null) {
if (!cb) {
return new Promise((resolve) => {
resolve(Notification.permission);
});
}
if (typeof (cb) === 'function') {
return cb(Notification.permission);
}
return Notification.permission;
}
class Notification extends window.Notification {
static permission = 'granted';
constructor(title = '', options = {}) {
super(title, options);
window.ferdium.displayNotification(title, options).then(() => {
// Continue to dispatch the event to the original Notification class
super.dispatchEvent(new Event('click'));
});
}
onNotify(data) {
return data;
static requestPermission(cb) {
if (typeof cb === 'function') {
cb(Notification.permission);
}
onClick() {}
return Promise.resolve(Notification.permission);
}
close() {}
onNotify(data) {
return data;
}
}
window.Notification = Notification;
})();`;
2 changes: 1 addition & 1 deletion src/webview/recipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ contextBridge.exposeInMainWorld('ferdium', {
setDialogTitle: (title: string | null | undefined) =>
dialogTitleHandler.setDialogTitle(title),
displayNotification: (title: string, options: any) => {
notificationsHandler.displayNotification(
return notificationsHandler.displayNotification(
title,
// The following line is needed so that a proper clone of the "options" object is made.
// This line was causing issues with some services.
Expand Down

0 comments on commit 0a52bbd

Please sign in to comment.