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.

llms_txt: URL-encode the narrow in the worked example.

Agnish Baurife48d247fb7b
diff --git a/zerver/tests/test_llms_txt.py b/zerver/tests/test_llms_txt.py
index b0e9e61bb9a..178f92b5b35 100644
--- a/zerver/tests/test_llms_txt.py
+++ b/zerver/tests/test_llms_txt.py
@@ -38,6 +38,13 @@ class LlmsTxtTest(ZulipTestCase):
# can build narrows directly.
rome_id = get_stream("Rome", realm).id
self.assertIn(f"Rome ({rome_id})", content)
+ # The worked example must URL-encode the entire narrow value; a raw
+ # JSON array in the query string is not a valid request.
+ self.assertIn(
+ "narrow=%5B%7B%22operator%22%3A%22channels%22%2C%22operand%22%3A%22web-public%22%7D",
+ content,
+ )
+ self.assertNotIn("narrow=[", content)
def test_llms_txt_spectator_access_disabled(self) -> None:
"""
diff --git a/zerver/views/llms_txt.py b/zerver/views/llms_txt.py
index d6ad3158ab0..f3520ae1957 100644
--- a/zerver/views/llms_txt.py
+++ b/zerver/views/llms_txt.py
@@ -1,3 +1,6 @@
+import json
+from urllib.parse import urlencode
+
from django.http import HttpRequest, HttpResponse
from zerver.context_processors import get_valid_realm_from_request
@@ -30,6 +33,17 @@ def llms_txt(request: HttpRequest) -> HttpResponse:
server_url = realm.url
example_channel_id = streams[0][0]
+ example_narrow = json.dumps(
+ [
+ {"operator": "channels", "operand": "web-public"},
+ {"operator": "channel", "operand": example_channel_id},
+ {"operator": "topic", "operand": "TOPIC_NAME"},
+ ],
+ separators=(",", ":"),
+ )
+ example_url = f"{server_url}/json/messages?" + urlencode(
+ {"anchor": "oldest", "num_before": 0, "num_after": 100, "narrow": example_narrow}
+ )
channel_list = "\n".join(f"- {name} ({channel_id})" for channel_id, name in streams)
content = f"""\
@@ -51,7 +65,8 @@ Required query parameters:
- `anchor` — message ID or keyword (`oldest`, `newest`, `first_unread`)
- `num_before` — number of messages before anchor (e.g. `0`)
- `num_after` — number of messages after anchor (e.g. `100`)
-- `narrow` — JSON array of filter operators, e.g.:
+- `narrow` — JSON array of filter operators; URL-encode the entire
+ value in the query string. For example:
`[{{"operator":"channels","operand":"web-public"}},{{"operator":"channel","operand":CHANNEL_ID}},{{"operator":"topic","operand":"TOPIC_NAME"}}]`
**Important**: the narrow must include `{{"operator":"channels","operand":"web-public"}}`
@@ -72,7 +87,7 @@ request returned the entire conversation.
Fetch the 100 oldest messages from a topic:
- GET {server_url}/json/messages?anchor=oldest&num_before=0&num_after=100&narrow=[{{"operator":"channels","operand":"web-public"}},{{"operator":"channel","operand":{example_channel_id}}},{{"operator":"topic","operand":"TOPIC_NAME"}}]
+ GET {example_url}
## Fetching messages in specific conversations / views