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.

remote_server: Upload data to advertise in Zulip communities directory.

Prakhar Pratyush9e4f5e1833af
diff --git a/zerver/lib/remote_server.py b/zerver/lib/remote_server.py
index 7da3370742f..8ff9f8e55fa 100644
--- a/zerver/lib/remote_server.py
+++ b/zerver/lib/remote_server.py
@@ -29,6 +29,8 @@ from zerver.lib.exceptions import (
)
from zerver.lib.outgoing_http import OutgoingSession
from zerver.lib.queue import queue_event_on_commit
+from zerver.lib.realm_description import get_realm_text_description
+from zerver.lib.realm_icon import get_realm_icon_url
from zerver.lib.redis_utils import get_redis_client
from zerver.lib.types import AnalyticsDataUploadLevel
from zerver.models import Realm, RealmAuditLog
@@ -80,6 +82,17 @@ class RealmAuditLogDataForAnalytics(BaseModel):
event_type: int
+class AdvertiseRealmData(BaseModel):
+ model_config = ConfigDict(extra="forbid")
+
+ description: str | None = None
+ icon_url: str
+ invite_required: bool
+ emails_restricted_to_domains: bool
+ has_web_public_streams: bool
+ is_demo_organization: bool
+
+
class RealmDataForAnalytics(BaseModel):
model_config = ConfigDict(extra="forbid")
@@ -94,6 +107,10 @@ class RealmDataForAnalytics(BaseModel):
authentication_methods: dict[str, bool] = Field(default_factory=dict)
+ # Sent only for realms that have opted in to get listed
+ # in the Zulip communities directory (zulip.com/communities).
+ advertise_realm_data: AdvertiseRealmData | None = None
+
uuid: UUID4
uuid_owner_secret: str
@@ -359,6 +376,31 @@ def build_analytics_data(
return realm_count_data, installation_count_data, zerver_realmauditlog
+def get_advertise_realm_data(realm: Realm) -> AdvertiseRealmData | None:
+ """Metadata the bouncer needs to list a realm in the Zulip communities
+ directory.
+
+ Uploaded only for realms that have opted in, on servers that have
+ enabled ZULIP_SERVICE_ADVERTISE_REALMS.
+ """
+ if not (
+ settings.ZULIP_SERVICE_ADVERTISE_REALMS and realm.want_advertise_in_communities_directory
+ ):
+ return None
+
+ return AdvertiseRealmData(
+ # A realm with no description is not eligible to be listed, so
+ # avoid sending the placeholder that get_realm_text_description
+ # substitutes for an empty one.
+ description=get_realm_text_description(realm) if realm.description else None,
+ icon_url=urljoin(realm.url, get_realm_icon_url(realm)),
+ invite_required=realm.invite_required,
+ emails_restricted_to_domains=realm.emails_restricted_to_domains,
+ has_web_public_streams=realm.has_web_public_streams(),
+ is_demo_organization=realm.demo_organization_scheduled_deletion_date is not None,
+ )
+
+
def get_realms_info_for_push_bouncer(realm_id: int | None = None) -> list[RealmDataForAnalytics]:
realms = Realm.objects.order_by("id")
if realm_id is not None: # nocoverage
@@ -377,6 +419,7 @@ def get_realms_info_for_push_bouncer(realm_id: int | None = None) -> list[RealmD
name=realm.name,
authentication_methods=realm.authentication_methods_dict(),
is_system_bot_realm=realm.string_id == settings.SYSTEM_BOT_REALM,
+ advertise_realm_data=get_advertise_realm_data(realm),
)
for realm in realms
]
diff --git a/zerver/tests/test_zilencer_analytics.py b/zerver/tests/test_zilencer_analytics.py
index 6aa7a9081f7..2ee0c9830e1 100644
--- a/zerver/tests/test_zilencer_analytics.py
+++ b/zerver/tests/test_zilencer_analytics.py
@@ -10,6 +10,7 @@ import responses
import time_machine
from django.conf import settings
from django.db.models import F
+from django.test import override_settings
from django.utils.timezone import now
from requests.exceptions import ConnectionError
from typing_extensions import override
@@ -24,6 +25,7 @@ from zerver.actions.realm_settings import (
do_change_realm_org_type,
do_deactivate_realm,
do_set_realm_authentication_methods,
+ do_set_realm_property,
)
from zerver.lib import redis_utils
from zerver.lib.remote_server import (
@@ -31,6 +33,7 @@ from zerver.lib.remote_server import (
AnalyticsRequest,
PushNotificationBouncerRetryLaterError,
build_analytics_data,
+ get_advertise_realm_data,
get_realms_info_for_push_bouncer,
record_push_notifications_recently_working,
redis_client,
@@ -1464,3 +1467,71 @@ class AnalyticsBouncerTest(BouncerTestCase):
audit_log.event_type, AuditLogEventType.REMOTE_REALM_LOCALLY_DELETED_RESTORED
)
self.assertEqual(audit_log.remote_realm, remote_realm_for_deleted_realm)
+
+
+class AdvertiseRealmDataTest(BouncerTestCase):
+ def test_uploaded_only_when_server_and_realm_both_opt_in(self) -> None:
+ realm = get_realm("zulip")
+
+ # Neither the server nor the realm has opted in.
+ self.assertIsNone(get_advertise_realm_data(realm))
+
+ # The realm wants to be listed, but the server has not
+ # enabled the service.
+ do_set_realm_property(
+ realm, "want_advertise_in_communities_directory", True, acting_user=None
+ )
+ self.assertIsNone(get_advertise_realm_data(realm))
+
+ with self.settings(ZULIP_SERVICE_ADVERTISE_REALMS=True):
+ self.assertIsNotNone(get_advertise_realm_data(realm))
+
+ do_set_realm_property(
+ realm, "want_advertise_in_communities_directory", False, acting_user=None
+ )
+ self.assertIsNone(get_advertise_realm_data(realm))
+
+ @override_settings(ZULIP_SERVICE_ADVERTISE_REALMS=True)
+ def test_empty_description_is_not_replaced_by_placeholder(self) -> None:
+ realm = get_realm("zulip")
+ do_set_realm_property(
+ realm, "want_advertise_in_communities_directory", True, acting_user=None
+ )
+ do_set_realm_property(realm, "description", "", acting_user=None)
+
+ data = get_advertise_realm_data(realm)
+ assert data is not None
+ self.assertIsNone(data.description)
+
+ @override_settings(ZULIP_SERVICE_ADVERTISE_REALMS=True)
+ def test_advertise_realm_data_success(self) -> None:
+ realm = get_realm("zulip")
+ do_set_realm_property(
+ realm, "want_advertise_in_communities_directory", True, acting_user=None
+ )
+
+ realms_data = get_realms_info_for_push_bouncer()
+ self.assertTrue(
+ any(realm_data.advertise_realm_data is not None for realm_data in realms_data)
+ )
+
+ request = AnalyticsRequest.model_construct(
+ realm_counts=[],
+ installation_counts=[],
+ realmauditlog_rows=[],
+ realms=realms_data,
+ version=None,
+ merge_base=None,
+ api_feature_level=None,
+ )
+ result = self.uuid_post(
+ self.server_uuid,
+ "/api/v1/remotes/server/analytics",
+ request.model_dump(
+ round_trip=True, exclude={"version", "merge_base", "api_feature_level"}
+ ),
+ subdomain="",
+ )
+ self.assert_json_success(result)
+
+ # TODO: Extend it when implementing bouncer side of this feature.