push_notifs: Remove "canonical" handling in FCM API.

We believe this to already be obsolete and dead code and is about to be
removed with the migration to the FCM HTTP v1 API, where the concept
doesn't exist anymore.
This commit is contained in:
Mateusz Mandera 2024-06-13 20:07:28 +02:00 committed by Tim Abbott
parent 81ea09be19
commit 4591202032
2 changed files with 0 additions and 96 deletions

View File

@ -519,37 +519,6 @@ def send_android_push_notification(
else:
DeviceTokenClass = PushDeviceToken
# res.canonical will contain results when there are duplicate registrations for the same
# device. The "canonical" registration is the latest registration made by the device.
# Ref: https://developer.android.com/google/gcm/adv.html#canonical
if "canonical" in res:
for reg_id, new_reg_id in res["canonical"].items():
if reg_id == new_reg_id:
# I'm not sure if this should happen. In any case, not really actionable.
logger.warning("GCM: Got canonical ref but it already matches our ID %s!", reg_id)
elif not DeviceTokenClass._default_manager.filter(
token=new_reg_id, kind=DeviceTokenClass.GCM
).exists():
# This case shouldn't happen; any time we get a canonical ref it should have been
# previously registered in our system.
#
# That said, recovery is easy: just update the current PDT object to use the new ID.
logger.warning(
"GCM: Got canonical ref %s replacing %s but new ID not registered! Updating.",
new_reg_id,
reg_id,
)
DeviceTokenClass._default_manager.filter(
token=reg_id, kind=DeviceTokenClass.GCM
).update(token=new_reg_id)
else:
# Since we know the new ID is registered in our system we can just drop the old one.
logger.info("GCM: Got canonical ref %s, dropping %s", new_reg_id, reg_id)
DeviceTokenClass._default_manager.filter(
token=reg_id, kind=DeviceTokenClass.GCM
).delete()
if "errors" in res:
for error, reg_ids in res["errors"].items():
if error in ["NotRegistered", "InvalidRegistration"]:

View File

@ -4840,71 +4840,6 @@ class GCMSendTest(PushNotificationTest):
self.assertEqual([log_msg1, log_msg2, log_msg3], logger.output)
mock_warning.assert_not_called()
def test_canonical_equal(self, mock_gcm: mock.MagicMock) -> None:
res = {}
res["canonical"] = {1: 1}
mock_gcm.json_request.return_value = res
data = self.get_gcm_data()
with self.assertLogs("zerver.lib.push_notifications", level="WARNING") as logger:
send_android_push_notification_to_user(self.user_profile, data, {})
self.assertEqual(
f"WARNING:zerver.lib.push_notifications:GCM: Got canonical ref but it already matches our ID {1}!",
logger.output[0],
)
def test_canonical_pushdevice_not_present(self, mock_gcm: mock.MagicMock) -> None:
res = {}
t1 = hex_to_b64("1111")
t2 = hex_to_b64("3333")
res["canonical"] = {t1: t2}
mock_gcm.json_request.return_value = res
def get_count(hex_token: str) -> int:
token = hex_to_b64(hex_token)
return PushDeviceToken.objects.filter(token=token, kind=PushDeviceToken.GCM).count()
self.assertEqual(get_count("1111"), 1)
self.assertEqual(get_count("3333"), 0)
data = self.get_gcm_data()
with self.assertLogs("zerver.lib.push_notifications", level="WARNING") as logger:
send_android_push_notification_to_user(self.user_profile, data, {})
msg = f"WARNING:zerver.lib.push_notifications:GCM: Got canonical ref {t2} replacing {t1} but new ID not registered! Updating."
self.assertEqual(msg, logger.output[0])
self.assertEqual(get_count("1111"), 0)
self.assertEqual(get_count("3333"), 1)
def test_canonical_pushdevice_different(self, mock_gcm: mock.MagicMock) -> None:
res = {}
old_token = hex_to_b64("1111")
new_token = hex_to_b64("2222")
res["canonical"] = {old_token: new_token}
mock_gcm.json_request.return_value = res
def get_count(hex_token: str) -> int:
token = hex_to_b64(hex_token)
return PushDeviceToken.objects.filter(token=token, kind=PushDeviceToken.GCM).count()
self.assertEqual(get_count("1111"), 1)
self.assertEqual(get_count("2222"), 1)
data = self.get_gcm_data()
with self.assertLogs("zerver.lib.push_notifications", level="INFO") as logger:
send_android_push_notification_to_user(self.user_profile, data, {})
self.assertEqual(
f"INFO:zerver.lib.push_notifications:GCM: Sending notification for local user <id:{self.user_profile.id}> to 2 devices",
logger.output[0],
)
self.assertEqual(
f"INFO:zerver.lib.push_notifications:GCM: Got canonical ref {new_token}, dropping {old_token}",
logger.output[1],
)
self.assertEqual(get_count("1111"), 0)
self.assertEqual(get_count("2222"), 1)
def test_not_registered(self, mock_gcm: mock.MagicMock) -> None:
res = {}
token = hex_to_b64("1111")