open graph: Use uploaded realm icon for open graph images.

This commit is contained in:
Puneeth Chaganti 2019-04-26 18:32:16 +05:30 committed by Tim Abbott
parent 9e8cfbea3b
commit f3f172991b
3 changed files with 29 additions and 1 deletions

View File

@ -16,7 +16,12 @@
<meta property="og:title" content="The world's most productive team chat"> <meta property="og:title" content="The world's most productive team chat">
<meta property="og:description" content="Zulip combines the immediacy of real-time chat with an email threading model. With Zulip, you can catch up on important conversations while ignoring irrelevant ones."> <meta property="og:description" content="Zulip combines the immediacy of real-time chat with an email threading model. With Zulip, you can catch up on important conversations while ignoring irrelevant ones.">
{% endif %} {% endif %}
{% if OPEN_GRAPH_IMAGE %}
<meta property="og:image" content="{{ OPEN_GRAPH_IMAGE }}">
{% else %}
<meta property="og:image" content="{{ realm_uri }}/static/images/logo/zulip-icon-128x128.png"> <meta property="og:image" content="{{ realm_uri }}/static/images/logo/zulip-icon-128x128.png">
{% endif %}
<!-- Twitter Meta Tags --> <!-- Twitter Meta Tags -->
<meta name="twitter:card" content="summary"> <meta name="twitter:card" content="summary">
@ -27,5 +32,8 @@
<meta property="twitter:title" content="The world's most productive team chat"> <meta property="twitter:title" content="The world's most productive team chat">
<meta name="twitter:description" content="Zulip combines the immediacy of real-time chat with an email threading model. With Zulip, you can catch up on important conversations while ignoring irrelevant ones."> <meta name="twitter:description" content="Zulip combines the immediacy of real-time chat with an email threading model. With Zulip, you can catch up on important conversations while ignoring irrelevant ones.">
{% endif %} {% endif %}
{% if OPEN_GRAPH_IMAGE %}
<meta name="twitter:image" content="{{ OPEN_GRAPH_IMAGE }}">
{% else %}
<meta name="twitter:image" content="{{ realm_uri }}/static/images/logo/zulip-icon-128x128.png"> <meta name="twitter:image" content="{{ realm_uri }}/static/images/logo/zulip-icon-128x128.png">
{% endif %}

View File

@ -133,6 +133,9 @@ def zulip_default_context(request: HttpRequest) -> Dict[str, Any]:
'allow_search_engine_indexing': allow_search_engine_indexing, 'allow_search_engine_indexing': allow_search_engine_indexing,
} }
if realm is not None and realm.icon_source == realm.ICON_UPLOADED:
context['OPEN_GRAPH_IMAGE'] = '%s%s' % (realm_uri, realm_icon)
return context return context
def login_context(request: HttpRequest) -> Dict[str, Any]: def login_context(request: HttpRequest) -> Dict[str, Any]:

View File

@ -4,6 +4,7 @@ from typing import List
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
from django.test import override_settings from django.test import override_settings
from unittest.mock import Mock, patch from unittest.mock import Mock, patch
from zerver.lib.realm_icon import get_realm_icon_url
from zerver.lib.test_classes import ZulipTestCase from zerver.lib.test_classes import ZulipTestCase
from zerver.middleware import is_slow_query, write_log_line from zerver.middleware import is_slow_query, write_log_line
from zerver.models import get_realm from zerver.models import get_realm
@ -167,3 +168,19 @@ class OpenGraphTest(ZulipTestCase):
['Welcome to Clojurians Zulip - the place where the Clojure community meets', ['Welcome to Clojurians Zulip - the place where the Clojure community meets',
'note-1', 'note-2', 'note-3', 'Enjoy!'], 'note-1', 'note-2', 'note-3', 'Enjoy!'],
[]) [])
def test_login_page_realm_icon(self) -> None:
realm = get_realm('zulip')
realm.icon_source = 'U'
realm.save(update_fields=['icon_source'])
realm_icon = get_realm_icon_url(realm)
response = self.client_get('/login/')
self.assertEqual(response.status_code, 200)
decoded = response.content.decode('utf-8')
bs = BeautifulSoup(decoded, features='lxml')
open_graph_image = bs.select_one('meta[property="og:image"]').get('content')
twitter_image = bs.select_one('meta[name="twitter:image"]').get('content')
self.assertTrue(open_graph_image.endswith(realm_icon))
self.assertTrue(twitter_image.endswith(realm_icon))