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

notif: Ignore notifications for logged out accounts #1349

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

tomlin7
Copy link
Contributor

@tomlin7 tomlin7 commented Feb 12, 2025

Fixes: #1264

Changes

  • Ignores notifications received for accounts that were logged out while the app was offline
  • Removes notifications (if there exist) of the user when the user logs out

@gnprice
Copy link
Member

gnprice commented Feb 12, 2025

Thanks @tomlin7 for the contribution! Before we can review this, it will need a test — see the repo's README file.

@PIG208 PIG208 added the maintainer review PR ready for review by Zulip maintainers label Feb 14, 2025
@tomlin7 tomlin7 force-pushed the ignore-notification-removed-account branch 4 times, most recently from 6c31bb2 to 51c72de Compare February 15, 2025 20:27
Copy link
Collaborator

@chrisbobbe chrisbobbe left a comment

Choose a reason for hiding this comment

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

Thanks! Comments below from a brief review.

@@ -11,6 +12,8 @@ Future<void> logOutAccount(GlobalStore globalStore, int accountId) async {
// Unawaited, to not block removing the account on this request.
unawaited(unregisterToken(globalStore, accountId));

await NotificationDisplayManager.removeNotificationsForAccount(account.realmUrl, account.userId);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Our tests for logOutAccount should check that the notifications are removed.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Also, what's the effect when this runs on iOS? Does it error or do nothing?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Also let's have this be unawaited, like unregisterToken above; we still want to do the removeAccount, as soon as possible, if this fails or takes a long time for some reason.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@chrisbobbe The tests are passing, I can't test on iOS atm since I'm on Android :)

Copy link
Collaborator

@chrisbobbe chrisbobbe Feb 21, 2025

Choose a reason for hiding this comment

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

Yeah; I see this output in the console when I log out on iOS:

[ERROR:flutter/runtime/dart_vm_initializer.cc(40)] Unhandled Exception: PlatformException(channel-error, Unable to establish connection on channel: "dev.flutter.pigeon.zulip.AndroidNotificationHostApi.getActiveNotifications"., null, null)
#0      AndroidNotificationHostApi.getActiveNotifications (package:zulip/host/android_notifications.g.dart:721:7)
<asynchronous suspension>
#1      NotificationDisplayManager.removeNotificationsForAccount (package:zulip/notifications/display.dart:531:33)
<asynchronous suspension>

I'll start a CZO discussion on what to do for iOS here. Edit: here: https://chat.zulip.org/#narrow/channel/243-mobile-team/topic/NotificationDisplayManager.20Android-only/near/2099143

Copy link
Collaborator

@chrisbobbe chrisbobbe Feb 25, 2025

Choose a reason for hiding this comment

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

Following that discussion, yeah, we will need to put the new call inside an Android platform conditional.

Then as an optional, independent improvement, you could add a commit that does this:

Could be helpful to put asserts at the top of [NotificationDisplayManager's] public methods, checking the target platform is indeed Android — because that's basically an assumption the implementation is making

@@ -518,6 +525,16 @@ class NotificationDisplayManager {
}
return null;
}

static Future<void> removeNotificationsForAccount(Uri realmUri, int userId) async {
final groupKey = "$realmUri|$userId";
Copy link
Collaborator

Choose a reason for hiding this comment

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

We have other code for computing a group key; let's be sure to use shared code for that, so the implementations stay in sync.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@chrisbobbe but the current implementation of _groupKey() takes a subtype of FcmMessageWithIdentity and I don't have sufficient data at this point. What should be done?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I see two options:

  1. Change _groupKey to take a realm URL and user ID instead of an FcmMessageWithIdentity.
  2. Keep _groupKey's signature but add a separate helper that takes a realm URL and user ID; have _groupKey and this new function call that new helper.

Option 1 is less code; let's try that for now, and perhaps Greg will have thoughts in his review.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@chrisbobbe I noticed _personKey does exactly what I needed here, and it works fine. What do you think about using it instead?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmm, I'd appreciate @gnprice's thoughts on that question.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I found some 🙂—https://chat.zulip.org/#narrow/channel/516-mobile-dev-help/topic/Removing.20notifications.20for.20logged.20out.20account/near/2088813 :

As a matter of code organization, we'll want to keep centralized in one place the details of how the group key is formatted — the fact that it's the realm URL, then user ID, and separated by |. That's currently in this method:

  static String _groupKey(FcmMessageWithIdentity data) {
    // The realm URL can't contain a `|`, because `|` is not a URL code point:
    //   https://url.spec.whatwg.org/#url-code-points
    return "${data.realmUrl}|${data.userId}";
  }

So you'll want to refactor that a bit so that it can be used by this code path as well as the existing code paths that use it.

I read that as suggesting my option (1) above.

@tomlin7 tomlin7 force-pushed the ignore-notification-removed-account branch 2 times, most recently from 4bc60b5 to 90daa5f Compare February 21, 2025 19:52
@tomlin7 tomlin7 force-pushed the ignore-notification-removed-account branch from 90daa5f to bbd7f14 Compare February 21, 2025 20:01
@chrisbobbe chrisbobbe self-assigned this Feb 21, 2025
@chrisbobbe
Copy link
Collaborator

I believe the two questions above that were open now have answers:

So you should be able to proceed, incorporating those.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maintainer review PR ready for review by Zulip maintainers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Don't show notifications for logged-out accounts, on Android
4 participants