Skip to content
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

Fix rich push notifications for single-instance situations #2296

Merged
merged 3 commits into from
Dec 10, 2022
Merged

Fix rich push notifications for single-instance situations #2296

merged 3 commits into from
Dec 10, 2022

Conversation

tbroyer
Copy link
Contributor

@tbroyer tbroyer commented Dec 4, 2022

No description provided.

@nolanlawson
Copy link
Owner

@tbroyer Thanks for opening this PR! Could you provide some more detail on what bug this fixes exactly? I don't use push notifications myself, so I'm not super familiar with this part of the code. Thanks!

@nolanlawson
Copy link
Owner

Ah I see, I missed #1663. I will study to see what is going on here.

@nolanlawson
Copy link
Owner

I'm confused about what's going on here and what this fixes. Maybe my brain is dead because it's Sunday morning, but I just don't get #1663 at all. #1663 (comment)

src/service-worker.js Outdated Show resolved Hide resolved
const knownInstances = await getKnownInstances()
if (knownInstances.length !== 1) {
// TODO: find a way to determine the pushing instance from the PushEvent
await showSimpleNotification(data)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like we are eagerly showing a simple notification rather than a rich one, whereas the original logic was to show a rich one and fallback to a simple one. Why would we prefer to always show a simple notification rather than a rich one, when there is only one instance in the known instances list?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More details in #1663 (comment)

The problem is that there's no way to compute the origin from the event alone.

Copy link
Owner

@nolanlawson nolanlawson Dec 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So since event.target.origin is undefined, that means that showRichNotification is going to fail 100% of the time. Why even keep this code around?

const notification = await get(`${origin}/api/v1/notifications/${data.notification_id}`, {
Authorization: `Bearer ${data.access_token}`
}, { timeout: 2000 })
await showRichNotification(data, notification)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea here is that:

  • if we only know about a single instance, the notification should come from it so we use it (line 181) and show a rich notification
  • otherwise, we have no way to determine the origin, so fallback to a simple notification.

Pending deeper changes on the way enabling notifications is handled (e.g. only allow enabling notifications on a single instance) or changes on Mastodon's side (including the URL to the notification, or at least the instance, in the PushEvent), this PR only fixes things in the case where the user logged in to a single instance. Not ideal but that's the simplest change to at least get rich notifications for some users (most of them? a good proportion of them? I'm certainly one of them).

@@ -169,8 +171,14 @@ self.addEventListener('fetch', event => {
self.addEventListener('push', event => {
event.waitUntil((async () => {
const data = event.data.json()
const { origin } = event.target
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK so this code was literally doing nothing because event.target.origin is undefined.


const origin = basename(knownInstances[0])
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an unfortunate solution because a user might log into 2 instances and enable notifications on the second one.

It seems like a better system would be for us to log somewhere which instance they've enabled notifications for. I.e. we can enforce the "you can only have notifications for one instance at once time" system ourselves.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed it's not ideal. For the case where a user logged into 2 instances, they'd have "simple" notifications even if they did enable notifications only on one of them.

Note that this line gets the single element from knownInstances, as the case where there are more than one (more accurately, not exactly one) is handled above; so it's not a matter of "enabling notifications on the second one".

src/service-worker.js Outdated Show resolved Hide resolved
Copy link
Owner

@nolanlawson nolanlawson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is better than the status quo, even if it's not ideal.

@nolanlawson nolanlawson merged commit f5f3395 into nolanlawson:master Dec 10, 2022
@tbroyer tbroyer deleted the rich-push-notifications branch December 12, 2022 15:36
alice-werefox pushed a commit to alice-werefox/sema-werefox-cafe that referenced this pull request Apr 3, 2023
…nlawson#2296)

Partially addresses nolanlawson#1663.

Co-authored-by: Nolan Lawson <nolan@nolanlawson.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants