Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

devices: Add Device model.

Prakhar Pratyush1aadb74acfe7
diff --git a/zerver/lib/export.py b/zerver/lib/export.py
index c1fe03c4d57..89318211952 100644
--- a/zerver/lib/export.py
+++ b/zerver/lib/export.py
@@ -160,6 +160,7 @@ ALL_ZULIP_TABLES = {
"zerver_defaultstream",
"zerver_defaultstreamgroup",
"zerver_defaultstreamgroup_streams",
+ "zerver_device",
"zerver_draft",
"zerver_emailchangestatus",
"zerver_externalauthid",
@@ -244,6 +245,7 @@ NON_EXPORTED_TABLES = {
"zerver_scheduledmessagenotificationemail",
# When switching servers, clients will need to re-log in and
# reregister for push notifications anyway.
+ "zerver_device",
"zerver_pushdevice",
"zerver_pushdevicetoken",
# We don't use these generated Django tables
diff --git a/zerver/migrations/0779_device.py b/zerver/migrations/0779_device.py
new file mode 100644
index 00000000000..691417e29f3
--- /dev/null
+++ b/zerver/migrations/0779_device.py
@@ -0,0 +1,69 @@
+# Generated by Django 5.2.11 on 2026-02-13 13:11
+
+import django.db.models.deletion
+from django.conf import settings
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("zerver", "0778_realm_rendered_description_version"),
+ ]
+
+ operations = [
+ migrations.CreateModel(
+ name="Device",
+ fields=[
+ (
+ "id",
+ models.BigAutoField(
+ auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
+ ),
+ ),
+ ("push_key", models.BinaryField(null=True)),
+ ("push_key_id", models.PositiveBigIntegerField(null=True)),
+ ("push_token_id", models.BigIntegerField(null=True)),
+ ("pending_push_token_id", models.BigIntegerField(null=True)),
+ ("push_token_last_updated_timestamp", models.DateTimeField(null=True)),
+ (
+ "push_token_kind",
+ models.CharField(
+ choices=[("apns", "APNs"), ("fcm", "FCM")], max_length=4, null=True
+ ),
+ ),
+ (
+ "push_registration_error_code",
+ models.CharField(
+ choices=[
+ ("INVALID_BOUNCER_PUBLIC_KEY", "Invalid Bouncer Public Key"),
+ ("BAD_REQUEST", "Invalid Encrypted Push Registration"),
+ ("REQUEST_EXPIRED", "Request Expired"),
+ ],
+ max_length=100,
+ null=True,
+ ),
+ ),
+ (
+ "user",
+ models.ForeignKey(
+ on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL
+ ),
+ ),
+ ],
+ options={
+ "indexes": [
+ models.Index(
+ condition=models.Q(("push_token_id__isnull", False)),
+ fields=["user", "push_token_id"],
+ name="zerver_device_user_push_token_id_idx",
+ )
+ ],
+ "constraints": [
+ models.CheckConstraint(
+ condition=models.Q(("push_key_id__lte", 4294967295)),
+ name="push_key_id_lte_max_push_key_id",
+ )
+ ],
+ },
+ ),
+ ]
diff --git a/zerver/models/__init__.py b/zerver/models/__init__.py
index 0ac44420acb..9045bdf44ae 100644
--- a/zerver/models/__init__.py
+++ b/zerver/models/__init__.py
@@ -7,6 +7,7 @@ from zerver.models.channel_folders import ChannelFolder as ChannelFolder
from zerver.models.clients import Client as Client
from zerver.models.custom_profile_fields import CustomProfileField as CustomProfileField
from zerver.models.custom_profile_fields import CustomProfileFieldValue as CustomProfileFieldValue
+from zerver.models.devices import Device as Device
from zerver.models.drafts import Draft as Draft
from zerver.models.groups import GroupGroupMembership as GroupGroupMembership
from zerver.models.groups import NamedUserGroup as NamedUserGroup
diff --git a/zerver/models/devices.py b/zerver/models/devices.py
new file mode 100644
index 00000000000..af141859de0
--- /dev/null
+++ b/zerver/models/devices.py
@@ -0,0 +1,67 @@
+from django.db import models
+from django.db.models import Q
+
+from zerver.lib.exceptions import (
+ InvalidBouncerPublicKeyError,
+ InvalidEncryptedPushRegistrationError,
+ RequestExpiredError,
+)
+from zerver.models.users import UserProfile
+
+
+class Device(models.Model):
+ """Core zulip server table storing logged-in devices.
+
+ Currently, only used by mobile apps for E2EE push notifications.
+ """
+
+ # The user on this server to whom this Device belongs.
+ user = models.ForeignKey(UserProfile, on_delete=models.CASCADE)
+
+ # Key to use to encrypt notifications for delivery to this device.
+ # Consists of a 1-byte prefix identifying the symmetric cryptosystem
+ # in use, followed by the secret key.
+ # Prefix Cryptosystem
+ # 0x31 libsodium's `crypto_secretbox_easy`
+ push_key = models.BinaryField(null=True)
+ # ID to reference the `push_key` - unsigned 32-bit integer.
+ push_key_id = models.PositiveBigIntegerField(null=True)
+
+ # ID to reference the token provided by FCM/APNs, registered to bouncer.
+ push_token_id = models.BigIntegerField(null=True)
+ # ID to reference the token provided by FCM/APNs, registration in progress to bouncer.
+ pending_push_token_id = models.BigIntegerField(null=True)
+
+ # The last time when `pending_push_token_id` was set to a new value.
+ push_token_last_updated_timestamp = models.DateTimeField(null=True)
+
+ class PushTokenKind(models.TextChoices):
+ APNS = "apns", "APNs"
+ FCM = "fcm", "FCM"
+
+ push_token_kind = models.CharField(max_length=4, choices=PushTokenKind.choices, null=True)
+
+ class PushRegistrationErrorCode(models.TextChoices):
+ INVALID_BOUNCER_PUBLIC_KEY = InvalidBouncerPublicKeyError.code.name
+ INVALID_ENCRYPTED_PUSH_REGISTRATION = InvalidEncryptedPushRegistrationError.code.name
+ REQUEST_EXPIRED = RequestExpiredError.code.name
+
+ # The error code returned when registration to bouncer fails.
+ push_registration_error_code = models.CharField(
+ max_length=100, choices=PushRegistrationErrorCode.choices, null=True
+ )
+
+ class Meta:
+ constraints = [
+ models.CheckConstraint(
+ condition=Q(push_key_id__lte=2**32 - 1),
+ name="push_key_id_lte_max_push_key_id",
+ )
+ ]
+ indexes = [
+ models.Index(
+ fields=["user", "push_token_id"],
+ condition=Q(push_token_id__isnull=False),
+ name="zerver_device_user_push_token_id_idx",
+ ),
+ ]