Some checks failed
Code scanning / CodeQL (push) Failing after 15s
Zulip production suite / Ubuntu 22.04 production build (push) Failing after 26s
Zulip production suite / Debian 12 production install with custom db name and user (push) Has been skipped
Zulip production suite / Ubuntu 22.04 production install and PostgreSQL upgrade with pgroonga (push) Has been skipped
Zulip production suite / Ubuntu 24.04 production install (push) Has been skipped
Zulip production suite / Ubuntu 26.04 production install (push) Has been skipped
Zulip production suite / Debian 13 production install (push) Has been skipped
Zulip production suite / 7.0 Version Upgrade (push) Has been skipped
Zulip production suite / 8.0 Version Upgrade (push) Has been skipped
Zulip production suite / 6.0 Version Upgrade (push) Has been skipped
Zulip production suite / 10.0 Version Upgrade (push) Has been skipped
Zulip production suite / 9.0 Version Upgrade (push) Has been skipped
Zulip production suite / 12.0 Version Upgrade (push) Has been skipped
Zulip production suite / 11.0 Version Upgrade (push) Has been skipped
GitHub Actions Security Analysis with zizmor / zizmor (push) Failing after 3s
Zulip CI / Debian 12 (Python 3.11, backend + documentation) (push) Failing after 26s
Zulip CI / Ubuntu 22.04 (Python 3.10, backend + frontend) (push) Failing after 3s
Zulip CI / Ubuntu 24.04 (Python 3.12, backend) (push) Failing after 25s
Zulip CI / Ubuntu 26.04 (Python 3.14, backend) (push) Failing after 25s
Zulip CI / Debian 13 (Python 3.13, backend) (push) Failing after 28s
Zulip production suite / Required jobs (push) Has been cancelled
Zulip CI / Required jobs (push) Has been cancelled
API Documentation Update Check / check-feature-level-updated (push) Has been cancelled
API Documentation Update Check / notify-if-api-docs-changed (push) Has been cancelled
76 lines
2.4 KiB
Python
76 lines
2.4 KiB
Python
import hashlib
|
|
from collections import defaultdict
|
|
|
|
from django.db import migrations
|
|
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
|
|
from django.db.migrations.state import StateApps
|
|
|
|
APNS_KIND = 1
|
|
|
|
|
|
def compute_token_id_int(token: str) -> int:
|
|
# Equivalent to zerver.lib.push_registration.compute_token_id_int,
|
|
# inlined to avoid importing live code from a migration.
|
|
hash_bytes = hashlib.sha256(token.encode()).digest()
|
|
return int.from_bytes(hash_bytes[0:8], byteorder="big", signed=True)
|
|
|
|
|
|
def cleanup_case_mismatched_legacy_apns_tokens(
|
|
apps: StateApps, schema_editor: BaseDatabaseSchemaEditor
|
|
) -> None:
|
|
"""One-time cleanup for legacy PushDeviceToken APNs rows that
|
|
`cleanup_legacy_push_device_token` left behind because it compared
|
|
tokens case-sensitively prior to the previous commit.
|
|
"""
|
|
Device = apps.get_model("zerver", "Device")
|
|
PushDeviceToken = apps.get_model("zerver", "PushDeviceToken")
|
|
|
|
e2ee_apns_token_ids_by_user: dict[int, set[int]] = defaultdict(set)
|
|
for user_id, push_token_id in (
|
|
Device.objects.filter(
|
|
push_token_kind="apns",
|
|
push_token_id__isnull=False,
|
|
)
|
|
.values_list("user_id", "push_token_id")
|
|
.iterator()
|
|
):
|
|
e2ee_apns_token_ids_by_user[user_id].add(push_token_id)
|
|
|
|
if not e2ee_apns_token_ids_by_user:
|
|
return
|
|
|
|
legacy_ids_to_delete: list[int] = []
|
|
for legacy_id, user_id, token in (
|
|
PushDeviceToken.objects.filter(
|
|
kind=APNS_KIND,
|
|
user_id__in=e2ee_apns_token_ids_by_user,
|
|
)
|
|
.values_list("id", "user_id", "token")
|
|
.iterator()
|
|
):
|
|
e2ee_token_ids = e2ee_apns_token_ids_by_user[user_id]
|
|
candidate_hashes = {
|
|
compute_token_id_int(token),
|
|
compute_token_id_int(token.lower()),
|
|
compute_token_id_int(token.upper()),
|
|
}
|
|
if not candidate_hashes.isdisjoint(e2ee_token_ids):
|
|
legacy_ids_to_delete.append(legacy_id)
|
|
|
|
if legacy_ids_to_delete:
|
|
PushDeviceToken.objects.filter(id__in=legacy_ids_to_delete).delete()
|
|
|
|
|
|
class Migration(migrations.Migration):
|
|
dependencies = [
|
|
("zerver", "0799_remove_realm_emoji_from_audit_log_extra_data"),
|
|
]
|
|
|
|
operations = [
|
|
migrations.RunPython(
|
|
cleanup_case_mismatched_legacy_apns_tokens,
|
|
reverse_code=migrations.RunPython.noop,
|
|
elidable=True,
|
|
),
|
|
]
|