From a0e308e19ab755463f906dc5bcb6d313b4933b71 Mon Sep 17 00:00:00 2001 From: Deimos Date: Tue, 11 Sep 2018 14:33:28 -0600 Subject: [PATCH] Display tweets as "excerpts" in topic listings --- tildes/tildes/models/topic/topic.py | 29 ++++++++++++++++++++ tildes/tildes/templates/macros/topics.jinja2 | 19 ++++++++----- 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/tildes/tildes/models/topic/topic.py b/tildes/tildes/models/topic/topic.py index 8632384..ebe0b48 100644 --- a/tildes/tildes/models/topic/topic.py +++ b/tildes/tildes/models/topic/topic.py @@ -376,3 +376,32 @@ class Topic(DatabaseModel): metadata_strings.append(f"{self.link_domain}") return ", ".join(metadata_strings) + + @property + def content_excerpt(self) -> Optional[str]: + """Return the topic's content excerpt (if it has one).""" + 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 + ): + authors = self.get_content_metadata("authors") + tweet = self.get_content_metadata("description") + + if authors and tweet: + return f"@{authors[0]}: {tweet}" + + return None + + @property + def is_content_excerpt_truncated(self) -> bool: + """Return whether the content excerpt has been truncated or not.""" + if self.is_text_type and self.content_excerpt: + return self.content_excerpt.endswith("...") + + return False diff --git a/tildes/tildes/templates/macros/topics.jinja2 b/tildes/tildes/templates/macros/topics.jinja2 index 840526a..e318786 100644 --- a/tildes/tildes/templates/macros/topics.jinja2 +++ b/tildes/tildes/templates/macros/topics.jinja2 @@ -49,7 +49,7 @@ {% endif %} - {% if topic.is_text_type and topic.get_content_metadata('excerpt') %} + {% if topic.content_excerpt %} {{ topic_excerpt_expandable(topic) }} {% endif %} @@ -87,11 +87,16 @@ {% if topic.is_spoiler %} {% set excerpt = 'Warning: this post may contain spoilers' %} {% set is_expandable = True %} - {% else %} - {% set excerpt = topic.get_content_metadata('excerpt') %} - {# if the excerpt is the full text, it doesn't need to be expandable #} - {% set is_expandable = excerpt.endswith('...') %} + {% if topic.is_text_type %} + {% set full_text = topic.rendered_html|safe %} + {% else %} + {% set full_text = topic.content_excerpt %} + {% endif %} + {% else %} + {% set excerpt = topic.content_excerpt %} + {% set is_expandable = topic.is_content_excerpt_truncated %} + {% set full_text = topic.rendered_html|safe %} {% endif %} {% if is_expandable %} @@ -101,10 +106,10 @@ {% endif %} > {{ excerpt }} - {{ topic.rendered_html|safe }} + {{ full_text }} {% else %} -

{{ topic.get_content_metadata('excerpt') }}

+

{{ excerpt }}

{% endif %} {% endmacro %}