Skip to content

Commit

Permalink
Set notification permission flag when extension refreshed, chrome -> …
Browse files Browse the repository at this point in the history
…browser object change
  • Loading branch information
ioay committed Dec 5, 2023
1 parent 02ff7de commit eede991
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 21 deletions.
13 changes: 7 additions & 6 deletions background/services/island/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Eligible, ReferrerStats } from "./types"
import BaseService from "../base"
import { getFileHashProspect, getClaimFromFileHash } from "./utils"
import ChainService from "../chain"
import { DOGGO, ETHEREUM } from "../../constants"
import { DOGGO, ETHEREUM, HOUR } from "../../constants"
import { sameNetwork } from "../../networks"
import {
ClaimWithFriends,
Expand All @@ -25,7 +25,7 @@ import { normalizeEVMAddress } from "../../lib/utils"
import { FeatureFlags, isDisabled, isEnabled } from "../../features"
import { SmartContractFungibleAsset } from "../../assets"

const NOTIFICATIONS_XP_DROP_THRESHOLD_MS = 86_400_000 // 24h
const NOTIFICATIONS_XP_DROP_THRESHOLD = 24 * HOUR

export {
TESTNET_TAHO,
Expand All @@ -51,7 +51,7 @@ interface Events extends ServiceLifecycleEvents {
export default class IslandService extends BaseService<Events> {
private isRelevantMonitoringAlreadyEnabled = false

private lastXpDropNotificationInMs = Date.now()
private lastXpDropNotificationInMs?: number

static create: ServiceCreatorFunction<
Events,
Expand Down Expand Up @@ -244,9 +244,10 @@ export default class IslandService extends BaseService<Events> {
}

private checkXPDrop() {
const shouldShowXpDropNotifications =
Date.now() >
this.lastXpDropNotificationInMs + NOTIFICATIONS_XP_DROP_THRESHOLD_MS
const shouldShowXpDropNotifications = this.lastXpDropNotificationInMs
? Date.now() >
this.lastXpDropNotificationInMs + NOTIFICATIONS_XP_DROP_THRESHOLD
: true

if (shouldShowXpDropNotifications) {
this.lastXpDropNotificationInMs = Date.now()
Expand Down
18 changes: 12 additions & 6 deletions background/services/notifications/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import BaseService from "../base"
import PreferenceService from "../preferences"
import { ServiceCreatorFunction, ServiceLifecycleEvents } from "../types"

const TAHO_ICON_URL =
"https://taho.xyz/icons/icon-144x144.png?v=41306c4d4e6795cdeaecc31bd794f68e"

type Events = ServiceLifecycleEvents & {
notificationDisplayed: string
notificationSuppressed: string
Expand Down Expand Up @@ -120,7 +123,7 @@ export default class NotificationsService extends BaseService<Events> {
title: string
message: string
contextMessage?: string
type?: chrome.notifications.TemplateType
type?: browser.Notifications.TemplateType
}
callback?: () => void
}) {
Expand All @@ -130,14 +133,17 @@ export default class NotificationsService extends BaseService<Events> {
const notificationId = uniqueId("notification-")

const notificationOptions = {
type: "basic" as chrome.notifications.TemplateType,
iconUrl:
"https://taho.xyz/icons/icon-144x144.png?v=41306c4d4e6795cdeaecc31bd794f68e",
type: "basic" as browser.Notifications.TemplateType,
iconUrl: TAHO_ICON_URL,
...options,
}

chrome.notifications.clear(notificationId, () => {
chrome.notifications.create(notificationId, notificationOptions, callback)
browser.notifications.clear(notificationId).then(() => {
browser.notifications
.create(notificationId, notificationOptions)
.then(() => {
callback?.()
})
})
}
}
23 changes: 14 additions & 9 deletions background/services/preferences/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ export default class PreferenceService extends BaseService<Events> {
"initializeShownDismissableItems",
await this.getShownDismissableItems(),
)
this.setShouldShowNotifications()
}

protected override async internalStopService(): Promise<void> {
Expand Down Expand Up @@ -269,25 +270,29 @@ export default class PreferenceService extends BaseService<Events> {
return (await this.db.getPreferences()).shouldShowNotifications
}

async setShouldShowNotifications(shouldShowNotifications: boolean) {
async setShouldShowNotifications(shouldShowNotifications?: boolean) {
if (shouldShowNotifications === undefined) {
const granted = await this.getShouldShowNotifications()
this.emitter.emit("setNotificationsPermission", granted)
return granted
}
const permissionRequest: Promise<boolean> = new Promise((resolve) => {
if (shouldShowNotifications) {
chrome.permissions.request(
{
browser.permissions
.request({
permissions: ["notifications"],
},
(granted) => {
})
.then((granted) => {
resolve(granted)
},
)
})
} else {
resolve(false)
resolve(shouldShowNotifications)
}
})

return permissionRequest.then(async (granted) => {
await this.db.setShouldShowNotifications(granted)
this.emitter.emit("setNotificationsPermission", granted)
await this.db.setShouldShowNotifications(granted)

return granted
})
Expand Down

0 comments on commit eede991

Please sign in to comment.