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
71 lines
2.5 KiB
Python
71 lines
2.5 KiB
Python
import base64
|
|
import binascii
|
|
|
|
from django.utils.translation import gettext as _
|
|
from typing_extensions import TypedDict
|
|
|
|
from zerver.lib.exceptions import JsonableError
|
|
from zerver.lib.timestamp import datetime_to_timestamp
|
|
from zerver.models.devices import Device
|
|
from zerver.models.users import UserProfile
|
|
|
|
|
|
class DeviceInfoDict(TypedDict):
|
|
push_key_id: int | None
|
|
push_token_id: str | None
|
|
pending_push_token_id: str | None
|
|
push_token_last_updated_timestamp: int | None
|
|
push_registration_error_code: str | None
|
|
|
|
|
|
def b64encode_token_id_int(token_id_int: int) -> str:
|
|
token_id_bytes = token_id_int.to_bytes(8, byteorder="big", signed=True)
|
|
return base64.b64encode(token_id_bytes).decode()
|
|
|
|
|
|
def b64decode_token_id_base64(token_id_base64: str) -> int:
|
|
try:
|
|
token_id_bytes = base64.b64decode(token_id_base64, validate=True)
|
|
except binascii.Error:
|
|
raise JsonableError(_("{var_name} is not Base64 encoded").format(var_name="`token_id`"))
|
|
|
|
return int.from_bytes(token_id_bytes, byteorder="big", signed=True)
|
|
|
|
|
|
def get_devices(user_profile: UserProfile) -> dict[str, DeviceInfoDict]:
|
|
devices = Device.objects.filter(user=user_profile)
|
|
|
|
devices_dict: dict[str, DeviceInfoDict] = dict()
|
|
for device in devices:
|
|
push_token_id_base64 = None
|
|
pending_push_token_id_base64 = None
|
|
push_token_last_updated_timestamp = None
|
|
|
|
if device.push_token_id is not None:
|
|
push_token_id_base64 = b64encode_token_id_int(device.push_token_id)
|
|
if device.pending_push_token_id is not None:
|
|
pending_push_token_id_base64 = b64encode_token_id_int(device.pending_push_token_id)
|
|
if device.push_token_last_updated_timestamp is not None:
|
|
push_token_last_updated_timestamp = datetime_to_timestamp(
|
|
device.push_token_last_updated_timestamp
|
|
)
|
|
|
|
devices_dict[str(device.id)] = DeviceInfoDict(
|
|
push_key_id=device.push_key_id,
|
|
push_token_id=push_token_id_base64,
|
|
pending_push_token_id=pending_push_token_id_base64,
|
|
push_token_last_updated_timestamp=push_token_last_updated_timestamp,
|
|
push_registration_error_code=device.push_registration_error_code,
|
|
)
|
|
|
|
return devices_dict
|
|
|
|
|
|
def check_device_id(device_id: int, user_id: int) -> Device:
|
|
try:
|
|
device = Device.objects.get(id=device_id, user_id=user_id)
|
|
except Device.DoesNotExist:
|
|
raise JsonableError(_("Invalid `device_id`"))
|
|
|
|
return device
|