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

MSC4076: Add disable_badge_count to pusher configuration #17975

Merged
merged 11 commits into from
Dec 3, 2024
1 change: 1 addition & 0 deletions changelog.d/17975.feature
anoadragon453 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[MSC4076](https://github.com/matrix-org/matrix-spec-proposals/pull/4076): Add `disable_badge_count` to pusher configuration.
15 changes: 11 additions & 4 deletions synapse/push/httppusher.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ def __init__(self, hs: "HomeServer", pusher_config: PusherConfig):
if self.data is None:
raise PusherConfigException("'data' key can not be null for HTTP pusher")

# Check if badge counts should be disabled for this push gateway
self.disable_badge_count = bool(
self.data.get("org.matrix.msc4076.disable_badge_count", False)
)

self.name = "%s/%s/%s" % (
pusher_config.user_name,
pusher_config.app_id,
Expand Down Expand Up @@ -461,9 +466,10 @@ async def dispatch_push_event(
content: JsonDict = {
"event_id": event.event_id,
"room_id": event.room_id,
"counts": {"unread": badge},
"prio": priority,
}
if not self.disable_badge_count:
content["counts"] = {"unread": badge}
# event_id_only doesn't include the tweaks, so override them.
tweaks = {}
else:
Expand All @@ -478,11 +484,12 @@ async def dispatch_push_event(
"type": event.type,
"sender": event.user_id,
"prio": priority,
"counts": {
}
if not self.disable_badge_count:
content["counts"] = {
"unread": badge,
# 'missed_calls': 2
manuroe marked this conversation as resolved.
Show resolved Hide resolved
},
}
}
if event.type == "m.room.member" and event.is_state():
content["membership"] = event.content["membership"]
content["user_is_target"] = event.state_key == self.user_id
Expand Down
177 changes: 177 additions & 0 deletions tests/push/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -1085,3 +1085,180 @@ def test_jitter(self) -> None:
self.pump()

self.assertEqual(len(self.push_attempts), 11)

def test_badge_count_disabled(self) -> None:
anoadragon453 marked this conversation as resolved.
Show resolved Hide resolved
"""
Test that when disable_badge_count is set to True, the counts field is omitted
from the notification.
"""
# Register the user who gets notified
user_id = self.register_user("user", "pass")
access_token = self.login("user", "pass")

# Register the user who sends the message
other_user_id = self.register_user("otheruser", "pass")
other_access_token = self.login("otheruser", "pass")

# Register the pusher with disable_badge_count set to True
user_tuple = self.get_success(
self.hs.get_datastores().main.get_user_by_access_token(access_token)
)
assert user_tuple is not None
device_id = user_tuple.device_id

self.get_success(
self.hs.get_pusherpool().add_or_update_pusher(
user_id=user_id,
device_id=device_id,
kind="http",
app_id="m.http",
app_display_name="HTTP Push Notifications",
device_display_name="pushy push",
pushkey="[email protected]",
lang=None,
data={
"url": "http://example.com/_matrix/push/v1/notify",
"org.matrix.msc4076.disable_badge_count": True,
},
)
)

# Create a room
room = self.helper.create_room_as(user_id, tok=access_token)

# The other user joins
self.helper.join(room=room, user=other_user_id, tok=other_access_token)

# The other user sends a message
self.helper.send(room, body="Hi!", tok=other_access_token)

# Advance time a bit, so the pusher will register something has happened
self.pump()

# One push was attempted to be sent
self.assertEqual(len(self.push_attempts), 1)
self.assertEqual(
self.push_attempts[0][1], "http://example.com/_matrix/push/v1/notify"
)

# Verify that the notification doesn't contain a counts field
self.assertNotIn("counts", self.push_attempts[0][2]["notification"])

def test_badge_count_disabled_event_id_only(self) -> None:
"""
Test that when disable_badge_count is set to True and format is event_id_only,
the counts field is omitted from the notification.
"""
# Register the user who gets notified
user_id = self.register_user("user", "pass")
access_token = self.login("user", "pass")

# Register the user who sends the message
other_user_id = self.register_user("otheruser", "pass")
other_access_token = self.login("otheruser", "pass")

# Register the pusher with disable_badge_count set to True and format set to event_id_only
user_tuple = self.get_success(
self.hs.get_datastores().main.get_user_by_access_token(access_token)
)
assert user_tuple is not None
device_id = user_tuple.device_id

self.get_success(
self.hs.get_pusherpool().add_or_update_pusher(
user_id=user_id,
device_id=device_id,
kind="http",
app_id="m.http",
app_display_name="HTTP Push Notifications",
device_display_name="pushy push",
pushkey="[email protected]",
lang=None,
data={
"url": "http://example.com/_matrix/push/v1/notify",
"format": "event_id_only",
"org.matrix.msc4076.disable_badge_count": True,
},
)
)

# Create a room
room = self.helper.create_room_as(user_id, tok=access_token)

# The other user joins
self.helper.join(room=room, user=other_user_id, tok=other_access_token)

# The other user sends a message
self.helper.send(room, body="Hi!", tok=other_access_token)

# Advance time a bit, so the pusher will register something has happened
self.pump()

# One push was attempted to be sent
self.assertEqual(len(self.push_attempts), 1)
self.assertEqual(
self.push_attempts[0][1], "http://example.com/_matrix/push/v1/notify"
)

# Verify that the notification doesn't contain a counts field
self.assertNotIn("counts", self.push_attempts[0][2]["notification"])

def test_badge_count_enabled(self) -> None:
"""
Test that when disable_badge_count is set to False, the counts field is included
in the notification.
"""
# Register the user who gets notified
user_id = self.register_user("user", "pass")
access_token = self.login("user", "pass")

# Register the user who sends the message
other_user_id = self.register_user("otheruser", "pass")
other_access_token = self.login("otheruser", "pass")

# Register the pusher with disable_badge_count set to False
user_tuple = self.get_success(
self.hs.get_datastores().main.get_user_by_access_token(access_token)
)
assert user_tuple is not None
device_id = user_tuple.device_id

self.get_success(
self.hs.get_pusherpool().add_or_update_pusher(
user_id=user_id,
device_id=device_id,
kind="http",
app_id="m.http",
app_display_name="HTTP Push Notifications",
device_display_name="pushy push",
pushkey="[email protected]",
lang=None,
data={
"url": "http://example.com/_matrix/push/v1/notify",
},
)
)

# Create a room
room = self.helper.create_room_as(user_id, tok=access_token)

# The other user joins
self.helper.join(room=room, user=other_user_id, tok=other_access_token)

# The other user sends a message
self.helper.send(room, body="Hi!", tok=other_access_token)

# Advance time a bit, so the pusher will register something has happened
self.pump()

# One push was attempted to be sent
self.assertEqual(len(self.push_attempts), 1)
self.assertEqual(
self.push_attempts[0][1], "http://example.com/_matrix/push/v1/notify"
)

# Verify that the notification contains a counts field
self.assertIn("counts", self.push_attempts[0][2]["notification"])
self.assertEqual(
self.push_attempts[0][2]["notification"]["counts"]["unread"], 1
)
Loading