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 %}