streams: Add API endpoint to get stream email.

This commit adds new API endpoint to get stream email which is
used by the web-app as well to get the email when a user tries
to open the stream email modal.

The stream email is returned only to the users who have access
to it. Specifically for private streams only subscribed users
have access to its email. And for public streams, all non-guest
users and only subscribed guests have access to its email.
All users can access email of web-public streams.
This commit is contained in:
Sahil Batra
2023-09-29 23:24:03 +05:30
committed by Alex Vandiver
parent 0a3800332f
commit 6e119842bd
11 changed files with 260 additions and 64 deletions

View File

@@ -15687,6 +15687,55 @@ paths:
description: |
An example JSON response for when invalid combination of stream permission
parameters are passed.
/streams/{stream_id}/email_address:
get:
operationId: get-stream-email-address
summary: Get the email address of a stream
tags: ["streams"]
description: |
Get email address of a stream.
**Changes**: New in Zulip 7.5 (feature level 186).
parameters:
- $ref: "#/components/parameters/StreamIdInPath"
responses:
"200":
description: Success.
content:
application/json:
schema:
allOf:
- $ref: "#/components/schemas/JsonSuccessBase"
- additionalProperties: false
properties:
result: {}
msg: {}
ignored_parameters_unsupported: {}
email:
type: string
description: |
Email address of the stream.
example:
{
"result": "success",
"msg": "",
"email": "test_stream.af64447e9e39374841063747ade8e6b0.show-sender@testserver",
}
"400":
description: Bad request.
content:
application/json:
schema:
allOf:
- $ref: "#/components/schemas/JsonError"
- example:
{
"code": "BAD_REQUEST",
"msg": "Invalid stream ID",
"result": "error",
}
description: |
An example JSON response for when the supplied stream does not exist:
/streams/{stream_id}/delete_topic:
post:
operationId: delete-topic

View File

@@ -31,12 +31,14 @@ from zerver.actions.streams import (
bulk_remove_subscriptions,
deactivated_streams_by_old_name,
do_change_stream_group_based_setting,
do_change_stream_permission,
do_change_stream_post_policy,
do_deactivate_stream,
do_unarchive_stream,
)
from zerver.actions.user_groups import add_subgroups_to_user_group, check_add_user_group
from zerver.actions.users import do_change_user_role, do_deactivate_user
from zerver.lib.email_mirror_helpers import encode_email_address_helper
from zerver.lib.exceptions import JsonableError
from zerver.lib.message import UnreadStreamInfo, aggregate_unread_data, get_raw_unread_data
from zerver.lib.response import json_success
@@ -5611,6 +5613,54 @@ class GetStreamsTest(ZulipTestCase):
self.assertEqual(json["stream"]["name"], "private_stream")
self.assertEqual(json["stream"]["stream_id"], private_stream.id)
def test_get_stream_email_address(self) -> None:
self.login("hamlet")
hamlet = self.example_user("hamlet")
iago = self.example_user("iago")
polonius = self.example_user("polonius")
realm = get_realm("zulip")
denmark_stream = get_stream("Denmark", realm)
result = self.client_get(f"/json/streams/{denmark_stream.id}/email_address")
json = self.assert_json_success(result)
denmark_email = encode_email_address_helper(
denmark_stream.name, denmark_stream.email_token, show_sender=True
)
self.assertEqual(json["email"], denmark_email)
self.login("polonius")
result = self.client_get(f"/json/streams/{denmark_stream.id}/email_address")
self.assert_json_error(result, "Invalid stream ID")
self.subscribe(polonius, "Denmark")
result = self.client_get(f"/json/streams/{denmark_stream.id}/email_address")
json = self.assert_json_success(result)
self.assertEqual(json["email"], denmark_email)
do_change_stream_permission(
denmark_stream,
invite_only=True,
history_public_to_subscribers=True,
is_web_public=False,
acting_user=iago,
)
self.login("hamlet")
result = self.client_get(f"/json/streams/{denmark_stream.id}/email_address")
json = self.assert_json_success(result)
self.assertEqual(json["email"], denmark_email)
self.unsubscribe(hamlet, "Denmark")
result = self.client_get(f"/json/streams/{denmark_stream.id}/email_address")
self.assert_json_error(result, "Invalid stream ID")
self.login("iago")
result = self.client_get(f"/json/streams/{denmark_stream.id}/email_address")
json = self.assert_json_success(result)
self.assertEqual(json["email"], denmark_email)
self.unsubscribe(iago, "Denmark")
result = self.client_get(f"/json/streams/{denmark_stream.id}/email_address")
self.assert_json_error(result, "Invalid stream ID")
class StreamIdTest(ZulipTestCase):
def test_get_stream_id(self) -> None:

View File

@@ -47,6 +47,7 @@ from zerver.decorator import (
require_post,
require_realm_admin,
)
from zerver.lib.email_mirror_helpers import encode_email_address
from zerver.lib.exceptions import (
ErrorCode,
JsonableError,
@@ -1073,3 +1074,18 @@ def update_subscription_properties_backend(
)
return json_success(request)
@has_request_variables
def get_stream_email_address(
request: HttpRequest,
user_profile: UserProfile,
stream_id: int = REQ("stream", converter=to_non_negative_int, path_only=True),
) -> HttpResponse:
(stream, sub) = access_stream_by_id(
user_profile,
stream_id,
)
stream_email = encode_email_address(stream, show_sender=True)
return json_success(request, data={"email": stream_email})