diff --git a/zerver/context_processors.py b/zerver/context_processors.py index 4613b6f614..abe2b9a4ff 100644 --- a/zerver/context_processors.py +++ b/zerver/context_processors.py @@ -17,7 +17,7 @@ from zerver.decorator import get_client_name from zerver.lib.send_email import FromAddress from zerver.lib.subdomains import get_subdomain from zerver.lib.realm_icon import get_realm_icon_url -from zerver.lib.realm_description import get_realm_rendered_description +from zerver.lib.realm_description import get_realm_rendered_description, get_realm_text_description from version import ZULIP_VERSION, LATEST_RELEASE_VERSION, LATEST_MAJOR_VERSION, \ LATEST_RELEASE_ANNOUNCEMENT @@ -154,6 +154,10 @@ def login_context(request: HttpRequest) -> Dict[str, Any]: 'two_factor_authentication_enabled': settings.TWO_FACTOR_AUTHENTICATION_ENABLED, } # type: Dict[str, Any] + if realm is not None and realm.description: + context['OPEN_GRAPH_TITLE'] = realm.name + context['OPEN_GRAPH_DESCRIPTION'] = get_realm_text_description(realm) + # Add the keys for our standard authentication backends. no_auth_enabled = True social_backends = [] diff --git a/zerver/lib/cache.py b/zerver/lib/cache.py index 4b29d0da46..528a9d53b9 100644 --- a/zerver/lib/cache.py +++ b/zerver/lib/cache.py @@ -446,6 +446,9 @@ def realm_alert_words_automaton_cache_key(realm: 'Realm') -> str: def realm_rendered_description_cache_key(realm: 'Realm') -> str: return "realm_rendered_description:%s" % (realm.string_id,) +def realm_text_description_cache_key(realm: 'Realm') -> str: + return "realm_text_description:%s" % (realm.string_id,) + # Called by models.py to flush the stream cache whenever we save a stream # object. def flush_stream(sender: Any, **kwargs: Any) -> None: diff --git a/zerver/lib/realm_description.py b/zerver/lib/realm_description.py index 04329be065..5db959add7 100644 --- a/zerver/lib/realm_description.py +++ b/zerver/lib/realm_description.py @@ -1,9 +1,16 @@ from zerver.models import Realm -from zerver.lib.cache import cache_with_key, realm_rendered_description_cache_key +from zerver.lib.cache import cache_with_key, realm_rendered_description_cache_key, \ + realm_text_description_cache_key from zerver.lib.bugdown import convert as bugdown_convert +from zerver.lib.html_to_text import html_to_text @cache_with_key(realm_rendered_description_cache_key, timeout=3600*24*7) def get_realm_rendered_description(realm: Realm) -> str: realm_description_raw = realm.description or "The coolest place in the universe." return bugdown_convert(realm_description_raw, message_realm=realm, no_previews=True) + +@cache_with_key(realm_text_description_cache_key, timeout=3600*24*7) +def get_realm_text_description(realm: Realm) -> str: + html_description = get_realm_rendered_description(realm) + return html_to_text(html_description) diff --git a/zerver/tests/test_middleware.py b/zerver/tests/test_middleware.py index 930c89bff2..18bdf289f5 100644 --- a/zerver/tests/test_middleware.py +++ b/zerver/tests/test_middleware.py @@ -5,8 +5,7 @@ from bs4 import BeautifulSoup from django.test import override_settings from unittest.mock import Mock, patch from zerver.lib.test_classes import ZulipTestCase -from zerver.middleware import is_slow_query -from zerver.middleware import write_log_line +from zerver.middleware import is_slow_query, write_log_line class SlowQueryTest(ZulipTestCase): SLOW_QUERY_TIME = 10 @@ -139,3 +138,13 @@ class OpenGraphTest(ZulipTestCase): [], # Test that our open graph logic doesn't throw a 500 404) + + def test_login_page_simple_description(self) -> None: + name = 'Zulip Dev' + description = "The Zulip development environment default organization. It's great for testing!" + + self.check_title_and_description( + '/login/', + name, + [description], + [])