From 69f2bc78dbbe1dbe365724c03a62c47a3579f2e6 Mon Sep 17 00:00:00 2001 From: Deimos Date: Thu, 13 Sep 2018 14:53:26 -0600 Subject: [PATCH] Display tweet content on topic comments page --- tildes/scss/modules/_topic.scss | 4 ++++ tildes/tildes/lib/url.py | 6 ++++++ tildes/tildes/models/topic/topic.py | 23 +++++++++++++++-------- tildes/tildes/templates/topic.jinja2 | 4 ++++ 4 files changed, 29 insertions(+), 8 deletions(-) diff --git a/tildes/scss/modules/_topic.scss b/tildes/scss/modules/_topic.scss index d15d7b4..ce0a6ab 100644 --- a/tildes/scss/modules/_topic.scss +++ b/tildes/scss/modules/_topic.scss @@ -240,6 +240,10 @@ word-break: break-all; } +.topic-full-content { + margin-top: 1rem; +} + .topic-full-text { overflow: auto; } diff --git a/tildes/tildes/lib/url.py b/tildes/tildes/lib/url.py index ed88bed..023a476 100644 --- a/tildes/tildes/lib/url.py +++ b/tildes/tildes/lib/url.py @@ -17,3 +17,9 @@ def get_domain_from_url(url: str, strip_www: bool = True) -> str: domain = domain[4:] return domain + + +def is_tweet(url: str) -> bool: + """Return whether a url is a link to a tweet.""" + domain = get_domain_from_url(url) + return domain == "twitter.com" and "/status/" in url diff --git a/tildes/tildes/models/topic/topic.py b/tildes/tildes/models/topic/topic.py index afe61bc..64adf3d 100644 --- a/tildes/tildes/models/topic/topic.py +++ b/tildes/tildes/models/topic/topic.py @@ -31,7 +31,7 @@ from tildes.lib.datetime import utc_from_timestamp, utc_now from tildes.lib.id import id_to_id36 from tildes.lib.markdown import convert_markdown_to_safe_html from tildes.lib.string import convert_to_url_slug -from tildes.lib.url import get_domain_from_url +from tildes.lib.url import get_domain_from_url, is_tweet from tildes.metrics import incr_counter from tildes.models import DatabaseModel from tildes.models.group import Group @@ -393,13 +393,7 @@ class Topic(DatabaseModel): if self.is_text_type: return self.get_content_metadata("excerpt") - # if it looks like it's a link to a tweet - if ( - self.is_link_type - and self.link_domain == "twitter.com" - and self.link - and "/status/" in self.link - ): + if self.link and is_tweet(self.link): authors = self.get_content_metadata("authors") tweet = self.get_content_metadata("description") @@ -415,3 +409,16 @@ class Topic(DatabaseModel): return self.content_excerpt.endswith("...") return False + + @property + def additional_content_html(self) -> Optional[str]: + """Additional HTML related to the content that can be displayed.""" + if not self.is_link_type: + return None + + if self.link and is_tweet(self.link): + authors = self.get_content_metadata("authors") + tweet = self.get_content_metadata("description") + return f"@{authors[0]}:
{tweet}
" + + return None diff --git a/tildes/tildes/templates/topic.jinja2 b/tildes/tildes/templates/topic.jinja2 index a6562f9..8c55b16 100644 --- a/tildes/tildes/templates/topic.jinja2 +++ b/tildes/tildes/templates/topic.jinja2 @@ -56,6 +56,10 @@ {% if request.user.open_new_tab_external %}target="_blank"{% endif %} >{{ topic.link }} + + {% if topic.additional_content_html %} +
{{ topic.additional_content_html|safe }}
+ {% endif %} {% endif %} {% endif %}