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 an endpoint to register logged-in device.

Prakhar Pratyush070974ea1859
diff --git a/api_docs/include/rest-endpoints.md b/api_docs/include/rest-endpoints.md
index c55652bfe63..13831d5caaf 100644
--- a/api_docs/include/rest-endpoints.md
+++ b/api_docs/include/rest-endpoints.md
@@ -160,6 +160,7 @@
* [Fetch an API key (production)](/api/fetch-api-key)
* [Fetch an API key (development only)](/api/dev-fetch-api-key)
+* [Register a logged-in device](/api/register-client-device)
* [Send an E2EE test notification to mobile device(s)](/api/e2ee-test-notify)
* [Register E2EE push device](/api/register-push-device)
* [Register E2EE push device to bouncer](/api/register-remote-push-device)
diff --git a/api_docs/unmerged.d/ZF-42fbcd.md b/api_docs/unmerged.d/ZF-42fbcd.md
new file mode 100644
index 00000000000..6a4ff8a0333
--- /dev/null
+++ b/api_docs/unmerged.d/ZF-42fbcd.md
@@ -0,0 +1,2 @@
+* [`POST /register_client_device`](/api/register-client-device):
+ Added a new endpoint to register a logged-in device.
diff --git a/zerver/openapi/python_examples.py b/zerver/openapi/python_examples.py
index d0c74660503..dcaf40b8578 100644
--- a/zerver/openapi/python_examples.py
+++ b/zerver/openapi/python_examples.py
@@ -1723,6 +1723,16 @@ def register_push_device(client: Client) -> None:
validate_against_openapi_schema(result, "/mobile_push/register", "post", "200")
+@openapi_test_function("/register_client_device:post")
+def register_device(client: Client) -> None:
+ # {code_example|start}
+ # Register a logged-in device.
+ result = client.call_endpoint(url="/register_client_device", method="POST")
+ # {code_example|end}
+ assert_success_response(result)
+ validate_against_openapi_schema(result, "/register_client_device", "post", "200")
+
+
@openapi_test_function("/typing:post")
def set_typing_status(client: Client) -> None:
ensure_users([10, 11], ["hamlet", "iago"])
@@ -2051,6 +2061,7 @@ def test_users(client: Client, owner_client: Client) -> None:
add_fcm_token(client)
remove_fcm_token(client)
register_push_device(client)
+ register_device(client)
def test_streams(client: Client, nonadmin_client: Client) -> None:
diff --git a/zerver/openapi/zulip.yaml b/zerver/openapi/zulip.yaml
index fc0ed5dc866..b77b812fba1 100644
--- a/zerver/openapi/zulip.yaml
+++ b/zerver/openapi/zulip.yaml
@@ -13035,6 +13035,38 @@ paths:
description: |
An example JSON response for when no realm is registered for
the authenticated server on the bouncer for the given `realm_uuid`:
+ /register_client_device:
+ post:
+ operationId: register-client-device
+ summary: Register a logged-in device
+ tags: ["mobile"]
+ description: |
+ Logged-in mobile devices use this endpoint as an initial step to
+ register themselves, before registering for E2EE push notifications.
+
+ This endpoint is currently not useful for clients other than mobile.
+
+ **Changes**: New in Zulip 12.0 (feature level ZF-42fbcd).
+ responses:
+ "200":
+ description: Success.
+ content:
+ application/json:
+ schema:
+ allOf:
+ - $ref: "#/components/schemas/JsonSuccessBase"
+ - additionalProperties: false
+ required:
+ - device_id
+ properties:
+ result: {}
+ msg: {}
+ ignored_parameters_unsupported: {}
+ device_id:
+ type: integer
+ description: |
+ The ID of the newly registered device.
+ example: {"msg": "", "device_id": 2, "result": "success"}
/user_topics:
post:
operationId: update-user-topic
diff --git a/zerver/tests/test_devices.py b/zerver/tests/test_devices.py
new file mode 100644
index 00000000000..a12a3e90c10
--- /dev/null
+++ b/zerver/tests/test_devices.py
@@ -0,0 +1,16 @@
+from zerver.lib.test_classes import ZulipTestCase
+from zerver.models.devices import Device
+
+
+class TestDeviceRegistration(ZulipTestCase):
+ def test_register_device(self) -> None:
+ user = self.example_user("hamlet")
+
+ self.assertEqual(Device.objects.count(), 0)
+
+ result = self.api_post(user, "/api/v1/register_client_device")
+ data = self.assert_json_success(result)
+ self.assertIn("device_id", data)
+
+ device = Device.objects.get(id=data["device_id"])
+ self.assertEqual(device.user_id, user.id)
diff --git a/zerver/views/devices.py b/zerver/views/devices.py
new file mode 100644
index 00000000000..dee99ca38a0
--- /dev/null
+++ b/zerver/views/devices.py
@@ -0,0 +1,12 @@
+from django.http import HttpRequest, HttpResponse
+
+from zerver.lib.response import json_success
+from zerver.lib.typed_endpoint import typed_endpoint_without_parameters
+from zerver.models.devices import Device
+from zerver.models.users import UserProfile
+
+
+@typed_endpoint_without_parameters
+def register_device(request: HttpRequest, user_profile: UserProfile) -> HttpResponse:
+ device = Device.objects.create(user=user_profile)
+ return json_success(request, data={"device_id": device.id})
diff --git a/zproject/urls.py b/zproject/urls.py
index 1211e57b8f2..4d9731d9d35 100644
--- a/zproject/urls.py
+++ b/zproject/urls.py
@@ -61,6 +61,7 @@ from zerver.views.custom_profile_fields import (
update_realm_custom_profile_field,
update_user_custom_profile_data,
)
+from zerver.views.devices import register_device
from zerver.views.digest import digest_page
from zerver.views.documentation import MarkdownDirectoryView, integrations_catalog, integrations_doc
from zerver.views.drafts import create_drafts, delete_draft, edit_draft, fetch_drafts
@@ -610,6 +611,7 @@ v1_api_and_json_patterns = [
rest_path("export/realm", POST=export_realm, GET=get_realm_exports),
rest_path("export/realm/<int:export_id>", DELETE=delete_realm_export),
rest_path("export/realm/consents", GET=get_users_export_consents),
+ rest_path("register_client_device", POST=register_device),
]
# These views serve pages (HTML). As such, their internationalization