From 3deffa57de8f94a300835ef89ec00b7092e24043 Mon Sep 17 00:00:00 2001 From: Manuel Cortez Date: Thu, 10 Nov 2022 15:32:49 -0600 Subject: [PATCH] Added content warnings to templates as $safe_text. Content warnings will be shown by default on GUI for now --- src/controller/buffers/mastodon/base.py | 2 +- src/mastodon.defaults | 2 +- src/sessions/mastodon/compose.py | 6 +++--- src/sessions/mastodon/templates.py | 20 +++++++++++++------- 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/controller/buffers/mastodon/base.py b/src/controller/buffers/mastodon/base.py index d42d4a13..95ca0740 100644 --- a/src/controller/buffers/mastodon/base.py +++ b/src/controller/buffers/mastodon/base.py @@ -379,7 +379,7 @@ class BaseBuffer(base.Buffer): if url == '': toot = self.get_item() if toot.reblog != None: - urls = utils.find_urls(toot.REBLOG) + urls = utils.find_urls(toot.reblog) else: urls = utils.find_urls(toot) if len(urls) == 1: diff --git a/src/mastodon.defaults b/src/mastodon.defaults index 14a3578e..24c1857c 100644 --- a/src/mastodon.defaults +++ b/src/mastodon.defaults @@ -44,7 +44,7 @@ braille_reporting = boolean(default=True) speech_reporting = boolean(default=True) [templates] -toot = string(default="$display_name, $text $image_descriptions $date. $source") +toot = string(default="$display_name, $safe_text $image_descriptions $date. $source") person = string(default="$display_name (@$screen_name). $followers followers, $following following, $toots toots. Joined $created_at.") [filters] diff --git a/src/sessions/mastodon/compose.py b/src/sessions/mastodon/compose.py index 6e38a466..17b7ccf8 100644 --- a/src/sessions/mastodon/compose.py +++ b/src/sessions/mastodon/compose.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- import arrow -from . import utils +from . import utils, templates def compose_toot(toot, db, relative_times, show_screen_names): if show_screen_names == False: @@ -15,9 +15,9 @@ def compose_toot(toot, db, relative_times, show_screen_names): else: ts = original_date.shift(hours=db["utc_offset"]).format(_("dddd, MMMM D, YYYY H:m:s"), locale="es") if toot.reblog != None: - text = _("Boosted from @{}: {}").format(toot.reblog.account.acct, utils.html_filter(toot.reblog.content)) + text = _("Boosted from @{}: {}").format(toot.reblog.account.acct, templates.process_text(toot.reblog)) else: - text = utils.html_filter(toot.content) + text = templates.process_text(toot) source = toot.get("application", "") # "" means remote user, None for legacy apps so we should cover both sides. if source != None and source != "": diff --git a/src/sessions/mastodon/templates.py b/src/sessions/mastodon/templates.py index 15277e51..02485829 100644 --- a/src/sessions/mastodon/templates.py +++ b/src/sessions/mastodon/templates.py @@ -8,7 +8,8 @@ from . import utils # Define variables that would be available for all template objects. # This will be used for the edit template dialog. # Available variables for toot objects. -toot_variables = ["date", "display_name", "screen_name", "source", "lang", "text", "image_descriptions"] +# safe_text will be the content warning in case a toot contains one, text will always be the full text, no matter if has a content warning or not. +toot_variables = ["date", "display_name", "screen_name", "source", "lang", "safe_text", "text", "image_descriptions"] person_variables = ["display_name", "screen_name", "description", "followers", "following", "favorites", "toots", "created_at"] # Default, translatable templates. @@ -24,9 +25,11 @@ def process_date(field, relative_times=True, offset_hours=0): ts = original_date.shift(hours=offset_hours).format(_("dddd, MMMM D, YYYY H:m:s"), locale=languageHandler.curLang[:2]) return ts -def process_text(toot): +def process_text(toot, safe=True): # text = utils.clean_mentions(utils.StripChars(text)) - return utils.html_filter(toot.content) + if safe == True and toot.sensitive == True and toot.spoiler_text != "": + return _("Content warning: {}").format(toot.spoiler_text) + return utils.html_filter(toot.content) def process_image_descriptions(media_attachments): """ Attempt to extract information for image descriptions. """ @@ -52,7 +55,8 @@ def render_toot(toot, template, relative_times=False, offset_hours=0): $screen_name: User screen name, this is the same name used to reference the user in Twitter. $ source: Source client from where the current tweet was sent. $lang: Two letter code for the automatically detected language for the tweet. This detection is performed by Twitter. - $text: Tweet text. + $safe_text: Safe text to display. If a content warning is applied in toots, display those instead of the whole toot. + $text: Toot text. This always displays the full text, even if there is a content warning present. $image_descriptions: Information regarding image descriptions added by twitter users. """ global toot_variables @@ -69,10 +73,12 @@ def render_toot(toot, template, relative_times=False, offset_hours=0): if hasattr(toot, "application") and toot.application != None: available_data.update(source=toot.application.get("name")) if toot.reblog != None: - text = _("Boosted from @{}: {}").format(toot.reblog.account.acct, process_text(toot.reblog), ) + text = _("Boosted from @{}: {}").format(toot.reblog.account.acct, process_text(toot.reblog, safe=False), ) + safe_text = _("Boosted from @{}: {}").format(toot.reblog.account.acct, process_text(toot.reblog), ) else: - text = process_text(toot) - available_data.update(lang=toot.language, text=text) + text = process_text(toot, safe=False) + safe_text = process_text(toot) + available_data.update(lang=toot.language, text=text, safe_text=safe_text) # process image descriptions image_descriptions = "" if toot.reblog != None: