-
Notifications
You must be signed in to change notification settings - Fork 395
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
Do notify: Set up base NotificationService #3666
Merged
Merged
Changes from 9 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
efa624a
Add shouldShowNotifications preference
Shadowfiend f4b3ad6
Add and set up notifications service
Shadowfiend 0737bd1
Add optional notifications permission to manifest.json
Shadowfiend 6ef0df8
Request notifications permission on Subscape banner click
Shadowfiend a70b5f7
Push notifications: added to state & settings
731486b
Settings state connected to background notification service
4d0e740
Request notification permission on open extension
f66a2a8
UI improvements, changes naming conventions: pushNotifications -> not…
ioay 4d798c5
Binding method for event listeners
ioay 554675d
Change thunk naming convention, catch the browser issue
ioay f30fe35
Move protected method to const in the InternalService
ioay a7e74f2
reverted getState logic
ioay 9ba58d9
Merge branch 'main' into do-notify
jagodarybacka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
import { uniqueId } from "lodash" | ||
import BaseService from "../base" | ||
import IslandService from "../island" | ||
import PreferenceService from "../preferences" | ||
import { ServiceCreatorFunction, ServiceLifecycleEvents } from "../types" | ||
|
||
type Events = ServiceLifecycleEvents & { | ||
notificationDisplayed: string | ||
notificationSuppressed: string | ||
} | ||
jagodarybacka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
type NotificationClickHandler = (() => Promise<void>) | (() => void) | ||
|
||
/** | ||
* The NotificationService manages all notifications for the extension. It is | ||
* charged both with managing the actual notification lifecycle (notification | ||
* delivery, dismissal, and reaction to clicks) and delivery (i.e., responding | ||
* to user preferences to deliver vs not deliver notifications), but also is | ||
* charged with actually creating the notifications themselves. | ||
* | ||
* Adding a new notification should involve connecting the appropriate event in | ||
* another service to a method in NotificationService that will generate the | ||
* corresponding notification. In that way, the NotificationService is more part | ||
* of the UI aspect of the extension than the background aspect, as it decides | ||
* on and creates user-visible content directly. | ||
*/ | ||
export default class NotificationsService extends BaseService<Events> { | ||
private isPermissionGranted: boolean | null = null | ||
|
||
private clickHandlers: { | ||
[notificationId: string]: NotificationClickHandler | ||
} = {} | ||
|
||
protected boundHandleNotificationClicks = | ||
this.handleNotificationClicks.bind(this) | ||
|
||
protected boundCleanUpNotificationClickHandler = | ||
this.cleanUpNotificationClickHandler.bind(this) | ||
|
||
jagodarybacka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/* | ||
* Create a new NotificationsService. The service isn't initialized until | ||
* startService() is called and resolved. | ||
*/ | ||
static create: ServiceCreatorFunction< | ||
Events, | ||
NotificationsService, | ||
[Promise<PreferenceService>, Promise<IslandService>] | ||
> = async (preferenceService, islandService) => | ||
new this(await preferenceService, await islandService) | ||
|
||
private constructor( | ||
private preferenceService: PreferenceService, | ||
private islandService: IslandService, | ||
) { | ||
super() | ||
} | ||
|
||
protected override async internalStartService(): Promise<void> { | ||
await super.internalStartService() | ||
|
||
// Preference and listener setup. | ||
// NOTE: Below, we assume if we got `shouldShowNotifications` as true, the | ||
// browser notifications permission has been granted. The preferences service | ||
// does guard this, but if that ends up not being true, browser.notifications | ||
// will be undefined and all of this will explode. | ||
this.isPermissionGranted = | ||
await this.preferenceService.getShouldShowNotifications() | ||
|
||
this.preferenceService.emitter.on( | ||
"setNotificationsPermission", | ||
(isPermissionGranted) => { | ||
if (isPermissionGranted) { | ||
browser.notifications.onClicked.addListener( | ||
this.boundHandleNotificationClicks, | ||
) | ||
browser.notifications.onClosed.addListener( | ||
this.boundCleanUpNotificationClickHandler, | ||
) | ||
} else { | ||
browser.notifications.onClicked.removeListener( | ||
this.boundHandleNotificationClicks, | ||
) | ||
browser.notifications.onClosed.removeListener( | ||
this.boundCleanUpNotificationClickHandler, | ||
) | ||
} | ||
}, | ||
) | ||
|
||
if (this.isPermissionGranted) { | ||
browser.notifications.onClicked.addListener( | ||
this.boundHandleNotificationClicks, | ||
) | ||
browser.notifications.onClosed.addListener( | ||
this.boundCleanUpNotificationClickHandler, | ||
) | ||
} | ||
|
||
/* | ||
* FIXME add below | ||
this.islandService.emitter.on("xpDropped", this.notifyXpDrop.bind(this)) | ||
*/ | ||
} | ||
|
||
// TODO: uncomment when the XP drop is ready | ||
// protected async notifyDrop(/* xpInfos: XpInfo[] */): Promise<void> { | ||
// const callback = () => { | ||
// browser.tabs.create({ | ||
// url: "dapp url for realm claim, XpInfo must include realm id, ideally some way to communicate if the address is right as well", | ||
// }) | ||
// } | ||
// this.notify({ callback }) | ||
// } | ||
|
||
// Fires the click handler for the given notification id. | ||
protected handleNotificationClicks(notificationId: string): void { | ||
this.clickHandlers?.[notificationId]() | ||
jagodarybacka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// Clears the click handler for the given notification id. | ||
protected cleanUpNotificationClickHandler(notificationId: string): void { | ||
delete this.clickHandlers?.[notificationId] | ||
} | ||
|
||
/** | ||
* Issues a notification with the given title, message, and context message. | ||
* The click action, if specified, will be fired when the user clicks on the | ||
* notification. | ||
*/ | ||
protected async notify({ | ||
title = "", | ||
message = "", | ||
contextMessage = "", | ||
callback, | ||
}: { | ||
title?: string | ||
message?: string | ||
contextMessage?: string | ||
callback?: () => void | ||
}) { | ||
if (!this.isPermissionGranted) { | ||
return | ||
} | ||
const notificationId = uniqueId("notification-") | ||
|
||
await browser.notifications.create(notificationId, { | ||
type: "basic", | ||
title, | ||
message, | ||
contextMessage, | ||
isClickable: !!callback, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
is this actually correct? I think UI state will be frozen to the one that we had when
this.store.getState()
was called - is this expected behaviour? (actually, I'm not sure because I haven't checked ifgetState
gives us a copy of the state object or a state object that gets updated 😅)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.
We generally should not expect that
getState
is updated.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.
For a change like this to an existing behavior, let's make sure we have a commit with a commit message that explains the motivation for the change. A lot of the stuff in
main
that interacts with redux is very fiddly because it's syncing the lifecycle of the wallet popup with the background script.This change looks like it can break the desired behavior, which is to check whether the network name changed during the lifetime of the popover. I don't see a clear reason for changing the existing code, and unfortunately the commit message has no details here, so it's hard to know if it's safe without doing a full debugger round.
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 have configured
Prettier
on file saving, so it was changed automatically. It doesn't change logic, so keep it as it is.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.
The prettier settings we have here by default should not do it 🤔 and I'm pretty sure the logic was changed as we noticed above, right?
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.
omg:)) yees, mea culpa! I didn't notice my change here before! :)