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.

openapi: Document remove-attachment endpoint.

Suyash Vardhan Mathurd2c50678392f
diff --git a/templates/zerver/help/include/rest-endpoints.md b/templates/zerver/help/include/rest-endpoints.md
index a2c13868ce2..9353c8e439b 100644
--- a/templates/zerver/help/include/rest-endpoints.md
+++ b/templates/zerver/help/include/rest-endpoints.md
@@ -54,6 +54,7 @@
* [Set "typing" status](/api/set-typing-status)
* [Get user presence](/api/get-user-presence)
* [Get attachments](/api/get-attachments)
+* [Delete an attachment](/api/remove-attachment)
* [Update settings](/api/update-settings)
* [Get user groups](/api/get-user-groups)
* [Create a user group](/api/create-user-group)
diff --git a/zerver/openapi/curl_param_value_generators.py b/zerver/openapi/curl_param_value_generators.py
index 5ffb052ff7d..04eb7f104f9 100644
--- a/zerver/openapi/curl_param_value_generators.py
+++ b/zerver/openapi/curl_param_value_generators.py
@@ -20,6 +20,7 @@ from zerver.lib.actions import (
from zerver.lib.events import do_events_register
from zerver.lib.initial_password import initial_password
from zerver.lib.test_classes import ZulipTestCase
+from zerver.lib.upload import upload_message_file
from zerver.lib.users import get_api_key
from zerver.models import Client, Message, UserGroup, UserPresence, get_realm, get_user
@@ -333,3 +334,12 @@ def deactivate_own_user() -> Dict[str, object]:
# change authentication line to allow test_client to delete itself.
AUTHENTICATION_LINE[0] = f"{deactivate_test_user.email}:{test_user_api_key}"
return {}
+
+
+@openapi_param_value_generator(["/attachments/{attachment_id}:delete"])
+def remove_attachment() -> Dict[str, object]:
+ user_profile = helpers.example_user("iago")
+ url = upload_message_file("dummy.txt", len(b"zulip!"), "text/plain", b"zulip!", user_profile)
+ attachment_id = url.replace("/user_uploads/", "").split("/")[0]
+
+ return {"attachment_id": attachment_id}
diff --git a/zerver/openapi/python_examples.py b/zerver/openapi/python_examples.py
index 6b7cc13db5d..408db0b8517 100644
--- a/zerver/openapi/python_examples.py
+++ b/zerver/openapi/python_examples.py
@@ -861,13 +861,28 @@ def get_raw_message(client: Client, message_id: int) -> None:
@openapi_test_function("/attachments:get")
-def get_attachments(client: Client) -> None:
+def get_attachments(client: Client) -> int:
# {code_example|start}
# Get your attachments.
result = client.get_attachments()
# {code_example|end}
validate_against_openapi_schema(result, "/attachments", "get", "200")
+ return result["attachments"][0]["id"]
+
+
+@openapi_test_function("/attachments/{attachment_id}:delete")
+def remove_attachment(client: Client, attachment_id: int) -> None:
+ # {code_example|start}
+ # Delete the attachment with given attachment_id.
+
+ url = "attachments/" + str(attachment_id)
+ result = client.call_endpoint(
+ url=url,
+ method="DELETE",
+ )
+ # {code_example|end}
+ validate_against_openapi_schema(result, "/attachments/{attachment_id}", "delete", "200")
@openapi_test_function("/messages:post")
@@ -1465,7 +1480,8 @@ def test_users(client: Client, owner_client: Client) -> None:
get_profile(client)
update_settings(client)
upload_file(client)
- get_attachments(client)
+ attachment_id = get_attachments(client)
+ remove_attachment(client, attachment_id)
set_typing_status(client)
update_presence(client)
get_user_presence(client)
diff --git a/zerver/openapi/zulip.yaml b/zerver/openapi/zulip.yaml
index 1d99aeda112..8d3b6b80149 100644
--- a/zerver/openapi/zulip.yaml
+++ b/zerver/openapi/zulip.yaml
@@ -4342,6 +4342,65 @@ paths:
],
"upload_space_used": 571946,
}
+ /attachments/{attachment_id}:
+ delete:
+ operationId: remove-attachment
+ summary: Delete an attachment
+ tags: ["users"]
+ description: |
+ Delete an uploaded file given its attachment ID.
+
+ Note that uploaded files that have been referenced in at least
+ one message are automatically deleted once the last message
+ containing a link to them is deleted (whether directly or via
+ a [message retention policy](/help/message-retention-policy)).
+
+ Uploaded files that are never used in a message are
+ automatically deleted a few weeks after being uploaded.
+
+ Attachment IDs can be contained from [GET /attachments](/api/get-attachments).
+ parameters:
+ - name: attachment_id
+ in: path
+ description: |
+ The ID of the attachment to be deleted.
+ schema:
+ type: string
+ example: "1"
+ required: true
+ responses:
+ "200":
+ $ref: "#/components/responses/SimpleSuccess"
+ "400":
+ description: Bad request.
+ content:
+ application/json:
+ schema:
+ oneOf:
+ - allOf:
+ - $ref: "#/components/schemas/CodedError"
+ - example:
+ {
+ "result": "error",
+ "msg": "Invalid attachment",
+ "code": "BAD_REQUEST",
+ }
+ description: |
+ A typical failed JSON response for when the `attachment_id` is invalid
+ "401":
+ description: Error.
+ content:
+ application/json:
+ schema:
+ allOf:
+ - $ref: "#/components/schemas/JsonError"
+ - example:
+ {
+ "result": "error",
+ "msg": "Not logged in: API authentication or user session required",
+ }
+ description: |
+ A typical failed JSON response for when the user is not logged in
/drafts:
get:
operationId: get-drafts
diff --git a/zerver/tests/test_openapi.py b/zerver/tests/test_openapi.py
index f7cfbc07ea2..f0312e7466e 100644
--- a/zerver/tests/test_openapi.py
+++ b/zerver/tests/test_openapi.py
@@ -197,8 +197,6 @@ class OpenAPIArgumentsTest(ZulipTestCase):
## And this one isn't, and isn't really representable
# "/user_uploads/{realm_id_str}/{filename}",
#### These realm administration settings are valuable to document:
- # Delete a file uploaded by current user.
- "/attachments/{attachment_id}",
# List data exports for organization (GET) or request one (POST)
"/export/realm",
# Delete a data export.