From 78e09f05b12d9660633efcbd92fe9235ca683101 Mon Sep 17 00:00:00 2001 From: Deimos Date: Wed, 17 Apr 2019 13:48:18 -0600 Subject: [PATCH] Replace domain with more info for YouTube+Twitter In topic listings, when a link topic is from YouTube or Twitter, this will now display more info about the "source" instead of just the domain. For YouTube, the channel name is displayed, and for Twitter, the author of the tweet is displayed. --- tildes/tildes/models/topic/topic.py | 20 ++++++++++++++++++++ tildes/tildes/templates/macros/topics.jinja2 | 2 +- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/tildes/tildes/models/topic/topic.py b/tildes/tildes/models/topic/topic.py index f92893d..b673c33 100644 --- a/tildes/tildes/models/topic/topic.py +++ b/tildes/tildes/models/topic/topic.py @@ -361,6 +361,26 @@ class Topic(DatabaseModel): # parsing it from the link if it's not present return self.get_content_metadata("domain") or get_domain_from_url(self.link) + @property + def link_source(self) -> str: + """Return the link's "source", which can be defined for certain domains. + + If special behavior isn't defined for a domain, this just falls back to + returning the domain itself. + """ + if not self.is_link_type: + raise ValueError("Non-link topics do not have a link source") + + domain = self.link_domain + authors = self.get_content_metadata("authors") + + if domain == "twitter.com" and authors: + return f"Twitter: @{authors[0]}" + elif domain == "youtube.com" and authors: + return f"YouTube: {authors[0]}" + + return domain + @property def is_spoiler(self) -> bool: """Return whether the topic is marked as a spoiler.""" diff --git a/tildes/tildes/templates/macros/topics.jinja2 b/tildes/tildes/templates/macros/topics.jinja2 index 392743d..4691c65 100644 --- a/tildes/tildes/templates/macros/topics.jinja2 +++ b/tildes/tildes/templates/macros/topics.jinja2 @@ -83,7 +83,7 @@ {% if topic.is_user_treated_as_source %} aria-label="Posted by">{{ username_linked(topic.user.username) }} {% else %} - aria-label="Link domain">{{ topic.link_domain }} + aria-label="Link source">{{ topic.link_source }} {% endif %}