Skip to content

Commit

Permalink
Verify desktop notification permission in webview
Browse files Browse the repository at this point in the history
  • Loading branch information
peterzen authored and ukane-philemon committed Dec 27, 2023
1 parent e4b08f0 commit 074c103
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
11 changes: 6 additions & 5 deletions client/webserver/site/src/js/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type DesktopNtfnSettingLabel = {
[x: string]: string
}

type DesktopNtfnSetting = {
export type DesktopNtfnSetting = {
[x: string]: boolean
}

Expand Down Expand Up @@ -86,7 +86,7 @@ class BrowserNotifier {
}

static sendDesktopNotification (title: string, body?: string) {
if (window.Notification.permission !== 'granted') return
if (!BrowserNotifier.ntfnPermissionGranted() || !desktopNtfnSettings.browserNtfnEnabled) return
const ntfn = new window.Notification(title, {
body: body,
icon: '/img/softened-icon.png'
Expand All @@ -113,6 +113,7 @@ class OSDesktopNotifier {
}

static sendDesktopNotification (title: string, body?: string): void {
if (!desktopNtfnSettings.browserNtfnEnabled) return
// this calls a function exported via webview.Bind()
const w = (window as any)
w.sendOSNotification(title, body)
Expand All @@ -133,17 +134,17 @@ export function desktopNotify (note: CoreNote) {
Notifier.sendDesktopNotification(note.subject, note.details)
}

export async function fetchDesktopNtfnSettings (): Promise<DesktopNtfnSetting> {
export function fetchDesktopNtfnSettings (): DesktopNtfnSetting {
if (desktopNtfnSettings !== undefined) {
return desktopNtfnSettings
}
const k = desktopNtfnSettingsKey()
desktopNtfnSettings = (await State.fetchLocal(k) ?? {}) as DesktopNtfnSetting
desktopNtfnSettings = (State.fetchLocal(k) ?? {}) as DesktopNtfnSetting
return desktopNtfnSettings
}

export async function updateNtfnSetting (noteType: string, enabled: boolean) {
await fetchDesktopNtfnSettings()
fetchDesktopNtfnSettings()
desktopNtfnSettings[noteType] = enabled
State.storeLocal(desktopNtfnSettingsKey(), desktopNtfnSettings)
}
14 changes: 6 additions & 8 deletions client/webserver/site/src/js/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,24 +175,22 @@ export default class SettingsPage extends BasePage {
this.renderDesktopNtfnSettings()
}

async updateNtfnSetting (e: Event) {
updateNtfnSetting (e: Event) {
const checkbox = e.target as HTMLInputElement
const noteType = checkbox.getAttribute('name')
if (noteType === null) return
const enabled = checkbox.checked
await ntfn.updateNtfnSetting(noteType, enabled)
ntfn.updateNtfnSetting(noteType, enabled)
}

async getBrowserNtfnSettings (form: HTMLElement) {
const loaded = app().loading(form)
const permissions = await ntfn.fetchDesktopNtfnSettings()
loaded()
getBrowserNtfnSettings (): ntfn.DesktopNtfnSetting {
const permissions = ntfn.fetchDesktopNtfnSettings()
return permissions
}

async renderDesktopNtfnSettings () {
const page = this.page
const ntfnSettings = await this.getBrowserNtfnSettings(page.browserNotificationsForm)
const ntfnSettings = this.getBrowserNtfnSettings()
const labels = ntfn.desktopNtfnLabels
const tmpl = page.browserNtfnCheckboxTemplate
tmpl.removeAttribute('id')
Expand All @@ -219,7 +217,7 @@ export default class SettingsPage extends BasePage {
await ntfn.requestNtfnPermission()
checkbox.checked = !ntfn.ntfnPermissionDenied()
}
await this.updateNtfnSetting(e)
this.updateNtfnSetting(e)
checkbox.dispatchEvent(new Event('change'))
})

Expand Down

0 comments on commit 074c103

Please sign in to comment.