Mastodon: Started implementation of read preferences from instance. Currently only content warnings are displayed by taking into accounts values from instance preferences

This commit is contained in:
2022-12-23 13:58:10 -06:00
parent 460cea702b
commit 18a7a42b5a
10 changed files with 90 additions and 28 deletions

View File

@@ -3,7 +3,7 @@ import arrow
import languageHandler
from . import utils, templates
def compose_post(post, db, relative_times, show_screen_names):
def compose_post(post, db, relative_times, show_screen_names, safe=True):
if show_screen_names == False:
user = post.account.get("display_name")
if user == "":
@@ -16,9 +16,9 @@ def compose_post(post, db, relative_times, show_screen_names):
else:
ts = original_date.shift(hours=db["utc_offset"]).format(_("dddd, MMMM D, YYYY H:m"), locale=languageHandler.curLang[:2])
if post.reblog != None:
text = _("Boosted from @{}: {}").format(post.reblog.account.acct, templates.process_text(post.reblog))
text = _("Boosted from @{}: {}").format(post.reblog.account.acct, templates.process_text(post.reblog, safe=safe))
else:
text = templates.process_text(post)
text = templates.process_text(post, safe=safe)
source = post.get("application", "")
# "" means remote user, None for legacy apps so we should cover both sides.
if source != None and source != "":
@@ -27,7 +27,7 @@ def compose_post(post, db, relative_times, show_screen_names):
source = ""
return [user+", ", text, ts+", ", source]
def compose_user(user, db, relative_times=True, show_screen_names=False):
def compose_user(user, db, relative_times=True, show_screen_names=False, safe=False):
original_date = arrow.get(user.created_at)
if relative_times:
ts = original_date.humanize(locale=languageHandler.curLang[:2])
@@ -38,7 +38,7 @@ def compose_user(user, db, relative_times=True, show_screen_names=False):
name = user.get("username")
return [_("%s (@%s). %s followers, %s following, %s posts. Joined %s") % (name, user.acct, user.followers_count, user.following_count, user.statuses_count, ts)]
def compose_conversation(conversation, db, relative_times, show_screen_names):
def compose_conversation(conversation, db, relative_times, show_screen_names, safe=False):
users = []
for account in conversation.accounts:
if account.display_name != "":
@@ -50,7 +50,7 @@ def compose_conversation(conversation, db, relative_times, show_screen_names):
text = _("Last message from {}: {}").format(last_post[0], last_post[1])
return [users, text, last_post[-2], last_post[-1]]
def compose_notification(notification, db, relative_times, show_screen_names):
def compose_notification(notification, db, relative_times, show_screen_names, safe=False):
if show_screen_names == False:
user = notification.account.get("display_name")
if user == "":
@@ -64,15 +64,15 @@ def compose_notification(notification, db, relative_times, show_screen_names):
ts = original_date.shift(hours=db["utc_offset"]).format(_("dddd, MMMM D, YYYY H:m"), locale=languageHandler.curLang[:2])
text = "Unknown: %r" % (notification)
if notification.type == "mention":
text = _("{username} has mentionned you: {status}").format(username=user, status=",".join(compose_post(notification.status, db, relative_times, show_screen_names)))
text = _("{username} has mentionned you: {status}").format(username=user, status=",".join(compose_post(notification.status, db, relative_times, show_screen_names, safe=safe)))
elif notification.type == "reblog":
text = _("{username} has boosted: {status}").format(username=user, status=",".join(compose_post(notification.status, db, relative_times, show_screen_names)))
text = _("{username} has boosted: {status}").format(username=user, status=",".join(compose_post(notification.status, db, relative_times, show_screen_names, safe=safe)))
elif notification.type == "favourite":
text = _("{username} has added to favorites: {status}").format(username=user, status=",".join(compose_post(notification.status, db, relative_times, show_screen_names)))
text = _("{username} has added to favorites: {status}").format(username=user, status=",".join(compose_post(notification.status, db, relative_times, show_screen_names, safe=safe)))
elif notification.type == "follow":
text = _("{username} has followed you.").format(username=user)
elif notification.type == "poll":
text = _("A poll in which you have voted has expired: {status}").format(status=",".join(compose_post(notification.status, db, relative_times, show_screen_names)))
text = _("A poll in which you have voted has expired: {status}").format(status=",".join(compose_post(notification.status, db, relative_times, show_screen_names, safe=safe)))
elif notification.type == "follow_request":
text = _("{username} wants to follow you.").format(username=user)
return [user, text, ts]

View File

@@ -29,6 +29,8 @@ class Session(base.baseSession):
self.type = "mastodon"
self.db["pagination_info"] = dict()
self.char_limit = 500
self.post_visibility = "public"
self.expand_spoilers = False
pub.subscribe(self.on_status, "mastodon.status_received")
pub.subscribe(self.on_status_updated, "mastodon.status_updated")
pub.subscribe(self.on_notification, "mastodon.notification_received")
@@ -98,14 +100,18 @@ class Session(base.baseSession):
offset = time.timezone if (time.localtime().tm_isdst == 0) else time.altzone
offset = offset / 60 / 60 * -1
self.db["utc_offset"] = offset
instance = self.api.instance()
if len(self.supported_languages) == 0:
self.supported_languages = self.api.instance().languages
self.supported_languages = instance.languages
self.get_lists()
self.get_muted_users()
# determine instance custom characters limit.
instance = self.api.instance()
if hasattr(instance, "configuration") and hasattr(instance.configuration, "statuses") and hasattr(instance.configuration.statuses, "max_characters"):
self.char_limit = instance.configuration.statuses.max_characters
# User preferences for some things.
preferences = self.api.preferences()
self.post_visibility = preferences.get("posting:default:visibility")
self.expand_spoilers = preferences.get("reading:expand:spoilers")
self.settings.write()
def get_lists(self):