Browse Source

Display tweets as "excerpts" in topic listings

merge-requests/37/head
Deimos 6 years ago
parent
commit
a0e308e19a
  1. 29
      tildes/tildes/models/topic/topic.py
  2. 19
      tildes/tildes/templates/macros/topics.jinja2

29
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

19
tildes/tildes/templates/macros/topics.jinja2

@ -49,7 +49,7 @@
{% endif %}
</div>
{% 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 %}
>
<summary><span>{{ excerpt }}</span></summary>
{{ topic.rendered_html|safe }}
{{ full_text }}
</details>
{% else %}
<p class="topic-text-excerpt">{{ topic.get_content_metadata('excerpt') }}</p>
<p class="topic-text-excerpt">{{ excerpt }}</p>
{% endif %}
{% endmacro %}

Loading…
Cancel
Save