diff --git a/doc/changelog.md b/doc/changelog.md index 3f002df1..37b94d82 100644 --- a/doc/changelog.md +++ b/doc/changelog.md @@ -7,7 +7,8 @@ During the development of the current TWBlue version, Twitter has cut out access * TWBlue should be able to display variables within templates (for example, now it is possible to send a template inside a post's text). Before, it was removing $variables so it was difficult to show how to edit templates from the client. ([#515](https://github.com/MCV-Software/TWBlue/issues/515)) * Mastodon: * it is possible to add descriptions for all media available on Mastodon (audio, photos, video and Givs). ([#516](https://github.com/MCV-Software/TWBlue/issues/516)) - * Added an experimetal feature to recover from connection errors. When making a post, if the post cannot be published due to any kind of error, TWBlue will bring up the dialog where the post was composed, so you can give the post a second chance or save the post's text. This feature should work for threads, posts with attachments, polls and replies. ([#527,](https://github.com/MCV-Software/TWBlue/issues/527) [#526,](https://github.com/MCV-Software/TWBlue/issues/526) [#377,](https://github.com/MCV-Software/TWBlue/issues/377) [#137,](https://github.com/MCV-Software/TWBlue/issues/137) [#108](https://github.com/MCV-Software/TWBlue/issues/108)) + * Implemented "Hide emojis on usernames" in both GUI and invisible interface. + * Added an experimental feature to recover from connection errors. When making a post, if the post cannot be published due to any kind of error, TWBlue will bring up the dialog where the post was composed, so you can give the post a second chance or save the post's text. This feature should work for threads, posts with attachments, polls and replies. ([#527,](https://github.com/MCV-Software/TWBlue/issues/527) [#526,](https://github.com/MCV-Software/TWBlue/issues/526) [#377,](https://github.com/MCV-Software/TWBlue/issues/377) [#137,](https://github.com/MCV-Software/TWBlue/issues/137) [#108](https://github.com/MCV-Software/TWBlue/issues/108)) * Fixed an error on mentions buffer that was making TWBlue unable to load posts if there were mentions from a blocked or deleted account. ## Changes on version 2023.2.6 diff --git a/src/controller/buffers/mastodon/base.py b/src/controller/buffers/mastodon/base.py index 3d7d18e4..a169c865 100644 --- a/src/controller/buffers/mastodon/base.py +++ b/src/controller/buffers/mastodon/base.py @@ -78,7 +78,7 @@ class BaseBuffer(base.Buffer): safe = True if self.session.settings["general"]["read_preferences_from_instance"]: safe = self.session.expand_spoilers == False - return self.compose_function(self.get_item(), self.session.db, self.session.settings["general"]["relative_times"], self.session.settings["general"]["show_screen_names"], safe=safe)[1] + return self.compose_function(self.get_item(), self.session.db, self.session.settings, self.session.settings["general"]["relative_times"], self.session.settings["general"]["show_screen_names"], safe=safe)[1] def get_message(self): post = self.get_item() @@ -91,7 +91,7 @@ class BaseBuffer(base.Buffer): template = template.replace("$safe_text", "$text") elif self.session.expand_spoilers == False and "$text" in template: template = template.replace("$text", "$safe_text") - t = templates.render_post(post, template, relative_times=self.session.settings["general"]["relative_times"], offset_hours=self.session.db["utc_offset"]) + t = templates.render_post(post, template, self.session.settings, relative_times=self.session.settings["general"]["relative_times"], offset_hours=self.session.db["utc_offset"]) return t def start_stream(self, mandatory=False, play_sound=True, avoid_autoreading=False): @@ -138,7 +138,7 @@ class BaseBuffer(base.Buffer): safe = True if self.session.settings["general"]["read_preferences_from_instance"]: safe = self.session.expand_spoilers == False - output.speak(" ".join(self.compose_function(post, self.session.db, self.session.settings["general"]["relative_times"], self.session.settings["general"]["show_screen_names"], safe=safe))) + output.speak(" ".join(self.compose_function(post, self.session.db, self.session.settings, self.session.settings["general"]["relative_times"], self.session.settings["general"]["show_screen_names"], safe=safe))) elif number_of_items > 1 and self.name in self.session.settings["other_buffers"]["autoread_buffers"] and self.name not in self.session.settings["other_buffers"]["muted_buffers"] and self.session.settings["sound"]["session_mute"] == False: output.speak(_("{0} new posts in {1}.").format(number_of_items, self.get_buffer_name())) @@ -169,11 +169,11 @@ class BaseBuffer(base.Buffer): safe = self.session.expand_spoilers == False if self.session.settings["general"]["reverse_timelines"] == False: for i in elements: - post = self.compose_function(i, self.session.db, self.session.settings["general"]["relative_times"], self.session.settings["general"]["show_screen_names"], safe=safe) + post = self.compose_function(i, self.session.db, self.session.settings, self.session.settings["general"]["relative_times"], self.session.settings["general"]["show_screen_names"], safe=safe) self.buffer.list.insert_item(True, *post) else: for i in elements: - post = self.compose_function(i, self.session.db, self.session.settings["general"]["relative_times"], self.session.settings["general"]["show_screen_names"], safe=safe) + post = self.compose_function(i, self.session.db, self.session.settings, self.session.settings["general"]["relative_times"], self.session.settings["general"]["show_screen_names"], safe=safe) self.buffer.list.insert_item(False, *post) self.buffer.list.select_item(selection) output.speak(_(u"%s items retrieved") % (str(len(elements))), True) @@ -207,20 +207,20 @@ class BaseBuffer(base.Buffer): safe = self.session.expand_spoilers == False if self.buffer.list.get_count() == 0: for i in list_to_use: - post = self.compose_function(i, self.session.db, self.session.settings["general"]["relative_times"], self.session.settings["general"]["show_screen_names"], safe=safe) + post = self.compose_function(i, self.session.db, self.session.settings, self.session.settings["general"]["relative_times"], self.session.settings["general"]["show_screen_names"], safe=safe) self.buffer.list.insert_item(False, *post) self.buffer.set_position(self.session.settings["general"]["reverse_timelines"]) elif self.buffer.list.get_count() > 0 and number_of_items > 0: if self.session.settings["general"]["reverse_timelines"] == False: items = list_to_use[len(list_to_use)-number_of_items:] for i in items: - post = self.compose_function(i, self.session.db, self.session.settings["general"]["relative_times"], self.session.settings["general"]["show_screen_names"], safe=safe) + post = self.compose_function(i, self.session.db, self.session.settings, self.session.settings["general"]["relative_times"], self.session.settings["general"]["show_screen_names"], safe=safe) self.buffer.list.insert_item(False, *post) else: items = list_to_use[0:number_of_items] items.reverse() for i in items: - post = self.compose_function(i, self.session.db, self.session.settings["general"]["relative_times"], self.session.settings["general"]["show_screen_names"], safe=safe) + post = self.compose_function(i, self.session.db, self.session.settings, self.session.settings["general"]["relative_times"], self.session.settings["general"]["show_screen_names"], safe=safe) self.buffer.list.insert_item(True, *post) log.debug("Now the list contains %d items " % (self.buffer.list.get_count(),)) @@ -228,7 +228,7 @@ class BaseBuffer(base.Buffer): safe = True if self.session.settings["general"]["read_preferences_from_instance"]: safe = self.session.expand_spoilers == False - post = self.compose_function(item, self.session.db, self.session.settings["general"]["relative_times"], self.session.settings["general"]["show_screen_names"], safe=safe) + post = self.compose_function(item, self.session.db, self.session.settings, self.session.settings["general"]["relative_times"], self.session.settings["general"]["show_screen_names"], safe=safe) if self.session.settings["general"]["reverse_timelines"] == False: self.buffer.list.insert_item(False, *post) else: @@ -240,7 +240,7 @@ class BaseBuffer(base.Buffer): safe = True if self.session.settings["general"]["read_preferences_from_instance"]: safe = self.session.expand_spoilers == False - post = self.compose_function(item, self.session.db, self.session.settings["general"]["relative_times"], self.session.settings["general"]["show_screen_names"], safe=safe) + post = self.compose_function(item, self.session.db, self.session.settings, self.session.settings["general"]["relative_times"], self.session.settings["general"]["show_screen_names"], safe=safe) self.buffer.list.list.SetItem(position, 1, post[1]) def bind_events(self): @@ -412,6 +412,7 @@ class BaseBuffer(base.Buffer): item = self.get_item() if item == None: return + print(item) urls = utils.get_media_urls(item) if len(urls) == 1: url=urls[0] diff --git a/src/controller/buffers/mastodon/conversations.py b/src/controller/buffers/mastodon/conversations.py index 06b5c8ef..037efbab 100644 --- a/src/controller/buffers/mastodon/conversations.py +++ b/src/controller/buffers/mastodon/conversations.py @@ -28,7 +28,7 @@ class ConversationListBuffer(BaseBuffer): return self.session.db[self.name][index] def get_formatted_message(self): - return self.compose_function(self.get_conversation(), self.session.db, self.session.settings["general"]["relative_times"], self.session.settings["general"]["show_screen_names"])[1] + return self.compose_function(self.get_conversation(), self.session.db, self.session.settings, self.session.settings["general"]["relative_times"], self.session.settings["general"]["show_screen_names"])[1] def get_message(self): conversation = self.get_conversation() @@ -36,7 +36,7 @@ class ConversationListBuffer(BaseBuffer): return template = self.session.settings["templates"]["conversation"] post_template = self.session.settings["templates"]["post"] - t = templates.render_conversation(conversation=conversation, template=template, post_template=post_template, relative_times=self.session.settings["general"]["relative_times"], offset_hours=self.session.db["utc_offset"]) + t = templates.render_conversation(conversation=conversation, template=template, post_template=post_template, settings=self.session.settings, relative_times=self.session.settings["general"]["relative_times"], offset_hours=self.session.db["utc_offset"]) return t def start_stream(self, mandatory=False, play_sound=True, avoid_autoreading=False): @@ -89,11 +89,11 @@ class ConversationListBuffer(BaseBuffer): log.debug("Retrieved %d items from cursored search in function %s." % (len(elements), self.function)) if self.session.settings["general"]["reverse_timelines"] == False: for i in elements: - conversation = self.compose_function(i, self.session.db, self.session.settings["general"]["relative_times"], self.session.settings["general"]["show_screen_names"]) + conversation = self.compose_function(i, self.session.db, self.session.settings, self.session.settings["general"]["relative_times"], self.session.settings["general"]["show_screen_names"]) self.buffer.list.insert_item(True, *conversation) else: for i in elements: - conversation = self.compose_function(i, self.session.db, self.session.settings["general"]["relative_times"], self.session.settings["general"]["show_screen_names"]) + conversation = self.compose_function(i, self.session.db, self.session.settings, self.session.settings["general"]["relative_times"], self.session.settings["general"]["show_screen_names"]) self.buffer.list.insert_item(False, *conversation) self.buffer.list.select_item(selection) output.speak(_(u"%s items retrieved") % (str(len(elements))), True) diff --git a/src/controller/buffers/mastodon/mentions.py b/src/controller/buffers/mastodon/mentions.py index 33afbc5d..1eec84a0 100644 --- a/src/controller/buffers/mastodon/mentions.py +++ b/src/controller/buffers/mastodon/mentions.py @@ -69,11 +69,11 @@ class MentionsBuffer(BaseBuffer): safe = self.session.expand_spoilers == False if self.session.settings["general"]["reverse_timelines"] == False: for i in elements: - post = self.compose_function(i.status, self.session.db, self.session.settings["general"]["relative_times"], self.session.settings["general"]["show_screen_names"], safe=safe) + post = self.compose_function(i.status, self.session.db, self.session.settings, self.session.settings["general"]["relative_times"], self.session.settings["general"]["show_screen_names"], safe=safe) self.buffer.list.insert_item(True, *post) else: for i in elements: - post = self.compose_function(i.status, self.session.db, self.session.settings["general"]["relative_times"], self.session.settings["general"]["show_screen_names"], safe=safe) + post = self.compose_function(i.status, self.session.db, self.session.settings, self.session.settings["general"]["relative_times"], self.session.settings["general"]["show_screen_names"], safe=safe) self.buffer.list.insert_item(False, *post) self.buffer.list.select_item(selection) output.speak(_(u"%s items retrieved") % (str(len(elements))), True) @@ -88,20 +88,20 @@ class MentionsBuffer(BaseBuffer): safe = self.session.expand_spoilers == False if self.buffer.list.get_count() == 0: for i in list_to_use: - post = self.compose_function(i.status, self.session.db, self.session.settings["general"]["relative_times"], self.session.settings["general"]["show_screen_names"], safe=safe) + post = self.compose_function(i.status, self.session.db, self.session.settings, self.session.settings["general"]["relative_times"], self.session.settings["general"]["show_screen_names"], safe=safe) self.buffer.list.insert_item(False, *post) self.buffer.set_position(self.session.settings["general"]["reverse_timelines"]) elif self.buffer.list.get_count() > 0 and number_of_items > 0: if self.session.settings["general"]["reverse_timelines"] == False: items = list_to_use[len(list_to_use)-number_of_items:] for i in items: - post = self.compose_function(i.status, self.session.db, self.session.settings["general"]["relative_times"], self.session.settings["general"]["show_screen_names"], safe=safe) + post = self.compose_function(i.status, self.session.db, self.session.settings, self.session.settings["general"]["relative_times"], self.session.settings["general"]["show_screen_names"], safe=safe) self.buffer.list.insert_item(False, *post) else: items = list_to_use[0:number_of_items] items.reverse() for i in items: - post = self.compose_function(i.status, self.session.db, self.session.settings["general"]["relative_times"], self.session.settings["general"]["show_screen_names"], safe=safe) + post = self.compose_function(i.status, self.session.db, self.session.settings, self.session.settings["general"]["relative_times"], self.session.settings["general"]["show_screen_names"], safe=safe) self.buffer.list.insert_item(True, *post) log.debug("Now the list contains %d items " % (self.buffer.list.get_count(),)) @@ -109,7 +109,7 @@ class MentionsBuffer(BaseBuffer): safe = True if self.session.settings["general"]["read_preferences_from_instance"]: safe = self.session.expand_spoilers == False - post = self.compose_function(item.status, self.session.db, self.session.settings["general"]["relative_times"], self.session.settings["general"]["show_screen_names"], safe=safe) + post = self.compose_function(item.status, self.session.db, self.session.settings, self.session.settings["general"]["relative_times"], self.session.settings["general"]["show_screen_names"], safe=safe) if self.session.settings["general"]["reverse_timelines"] == False: self.buffer.list.insert_item(False, *post) else: diff --git a/src/controller/buffers/mastodon/notifications.py b/src/controller/buffers/mastodon/notifications.py index 68f362a3..77b0fa57 100644 --- a/src/controller/buffers/mastodon/notifications.py +++ b/src/controller/buffers/mastodon/notifications.py @@ -18,7 +18,7 @@ class NotificationsBuffer(BaseBuffer): return template = self.session.settings["templates"]["notification"] post_template = self.session.settings["templates"]["post"] - t = templates.render_notification(notification, template, post_template, relative_times=self.session.settings["general"]["relative_times"], offset_hours=self.session.db["utc_offset"]) + t = templates.render_notification(notification, template, post_template, self.session.settings, relative_times=self.session.settings["general"]["relative_times"], offset_hours=self.session.db["utc_offset"]) return t def create_buffer(self, parent, name): diff --git a/src/controller/buffers/mastodon/users.py b/src/controller/buffers/mastodon/users.py index 84f73145..5463017f 100644 --- a/src/controller/buffers/mastodon/users.py +++ b/src/controller/buffers/mastodon/users.py @@ -22,7 +22,7 @@ class UserBuffer(BaseBuffer): if user == None: return template = self.session.settings["templates"]["person"] - t = templates.render_user(user=user, template=template, relative_times=self.session.settings["general"]["relative_times"], offset_hours=self.session.db["utc_offset"]) + t = templates.render_user(user=user, template=template, settings=self.session.settings, relative_times=self.session.settings["general"]["relative_times"], offset_hours=self.session.db["utc_offset"]) return t def bind_events(self): @@ -123,11 +123,11 @@ class UserBuffer(BaseBuffer): log.debug("Retrieved %d items from cursored search in function %s." % (len(elements), self.function)) if self.session.settings["general"]["reverse_timelines"] == False: for i in elements: - post = self.compose_function(i, self.session.db, self.session.settings["general"]["relative_times"], self.session.settings["general"]["show_screen_names"]) + post = self.compose_function(i, self.session.db, self.session.settings, self.session.settings["general"]["relative_times"], self.session.settings["general"]["show_screen_names"]) self.buffer.list.insert_item(True, *post) else: for i in elements: - post = self.compose_function(i, self.session.db, self.session.settings["general"]["relative_times"], self.session.settings["general"]["show_screen_names"]) + post = self.compose_function(i, self.session.db, self.session.settings, self.session.settings["general"]["relative_times"], self.session.settings["general"]["show_screen_names"]) self.buffer.list.insert_item(False, *post) self.buffer.list.select_item(selection) output.speak(_(u"%s items retrieved") % (str(len(elements))), True) diff --git a/src/sessions/mastodon/compose.py b/src/sessions/mastodon/compose.py index d44f5109..a4591618 100644 --- a/src/sessions/mastodon/compose.py +++ b/src/sessions/mastodon/compose.py @@ -3,11 +3,9 @@ import arrow import languageHandler from . import utils, templates -def compose_post(post, db, relative_times, show_screen_names, safe=True): +def compose_post(post, db, settings, relative_times, show_screen_names, safe=True): if show_screen_names == False: - user = post.account.get("display_name") - if user == "": - user = post.account.get("username") + user = utils.get_user_alias(post.account, settings) else: user = post.account.get("acct") original_date = arrow.get(post.created_at) @@ -27,34 +25,30 @@ def compose_post(post, db, relative_times, show_screen_names, safe=True): source = "" return [user+", ", text, ts+", ", source] -def compose_user(user, db, relative_times=True, show_screen_names=False, safe=False): +def compose_user(user, db, settings, 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]) else: ts = original_date.shift(hours=db["utc_offset"]).format(_("dddd, MMMM D, YYYY H:m:s"), locale=languageHandler.curLang[:2]) - name = user.display_name - if name == "": - name = user.get("username") + name = utils.get_user_alias(user, settings) 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, safe=False): +def compose_conversation(conversation, db, settings, relative_times, show_screen_names, safe=False): users = [] for account in conversation.accounts: if account.display_name != "": - users.append(account.display_name) + users.append(utils.get_user_alias(account, settings)) else: users.append(account.username) users = ", ".join(users) - last_post = compose_post(conversation.last_status, db, relative_times, show_screen_names) + last_post = compose_post(conversation.last_status, db, settings, 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, safe=False): +def compose_notification(notification, db, settings, relative_times, show_screen_names, safe=False): if show_screen_names == False: - user = notification.account.get("display_name") - if user == "": - user = notification.account.get("username") + user = utils.get_user_alias(notification.account, settings) else: user = notification.account.get("acct") original_date = arrow.get(notification.created_at) @@ -64,19 +58,19 @@ def compose_notification(notification, db, relative_times, show_screen_names, sa 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 == "status": - text = _("{username} has posted: {status}").format(username=user, status=",".join(compose_post(notification.status, db, relative_times, show_screen_names, safe=safe))) + text = _("{username} has posted: {status}").format(username=user, status=",".join(compose_post(notification.status, db, settings, relative_times, show_screen_names, safe=safe))) elif notification.type == "mention": - text = _("{username} has mentioned you: {status}").format(username=user, status=",".join(compose_post(notification.status, db, relative_times, show_screen_names, safe=safe))) + text = _("{username} has mentioned you: {status}").format(username=user, status=",".join(compose_post(notification.status, db, settings, 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, safe=safe))) + text = _("{username} has boosted: {status}").format(username=user, status=",".join(compose_post(notification.status, db, settings, 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, safe=safe))) + text = _("{username} has added to favorites: {status}").format(username=user, status=",".join(compose_post(notification.status, db, settings, relative_times, show_screen_names, safe=safe))) elif notification.type == "follow": text = _("{username} has followed you.").format(username=user) elif notification.type == "admin.sign_up": text = _("{username} has joined the instance.").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, safe=safe))) + text = _("A poll in which you have voted has expired: {status}").format(status=",".join(compose_post(notification.status, db, settings, 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] \ No newline at end of file diff --git a/src/sessions/mastodon/session.py b/src/sessions/mastodon/session.py index 47f037c2..005b7939 100644 --- a/src/sessions/mastodon/session.py +++ b/src/sessions/mastodon/session.py @@ -123,25 +123,6 @@ class Session(base.baseSession): ### ToDo: Use a function to retrieve all muted users. self.db["muted_users"] = self.api.mutes() - def get_user_alias(self, user): - if user.display_name == None or user.display_name == "": - display_name = user.username - else: - display_name = user.display_name - aliases = self.settings.get("user-aliases") - if aliases == None: - log.error("Aliases are not defined for this config spec.") - return self.demoji_user(display_name) - user_alias = aliases.get(user.id) - if user_alias != None: - return user_alias - return self.demoji_user(display_name) - - def demoji_user(self, name): - if self.settings["general"]["hide_emojis"] == True: - return demoji.replace(name, "") - return name - def order_buffer(self, name, data, ignore_older=False): num = 0 last_id = None diff --git a/src/sessions/mastodon/templates.py b/src/sessions/mastodon/templates.py index 46127072..1b972560 100644 --- a/src/sessions/mastodon/templates.py +++ b/src/sessions/mastodon/templates.py @@ -45,7 +45,7 @@ def process_image_descriptions(media_attachments): idescriptions = idescriptions + _("Media description: {}").format(image) + "\n" return idescriptions -def render_post(post, template, relative_times=False, offset_hours=0): +def render_post(post, template, settings, relative_times=False, offset_hours=0): """ Renders any given post according to the passed template. Available data for posts will be stored in the following variables: $date: Creation date. @@ -63,9 +63,7 @@ def render_post(post, template, relative_times=False, offset_hours=0): created_at = process_date(post.created_at, relative_times, offset_hours) available_data.update(date=created_at) # user. - display_name = post.account.display_name - if display_name == "": - display_name = post.account.username + display_name = utils.get_user_alias(post.account, settings) available_data.update(display_name=display_name, screen_name=post.account.acct) # Source client from where tweet was originated. source = "" @@ -90,7 +88,7 @@ def render_post(post, template, relative_times=False, offset_hours=0): result = Template(_(template)).safe_substitute(**available_data) return result -def render_user(user, template, relative_times=True, offset_hours=0): +def render_user(user, template, settings, relative_times=True, offset_hours=0): """ Renders persons by using the provided template. Available data will be stored in the following variables: $display_name: The name of the user, as they’ve defined it. Not necessarily a person’s name. Typically capped at 50 characters, but subject to change. @@ -102,9 +100,7 @@ def render_user(user, template, relative_times=True, offset_hours=0): $created_at: The date and time that the user account was created on Twitter. """ global person_variables - display_name = user.display_name - if display_name == "": - display_name = user.username + display_name = utils.get_user_alias(user, settings) available_data = dict(display_name=display_name, screen_name=user.acct, followers=user.followers_count, following=user.following_count, posts=user.statuses_count) # Nullable values. nullables = ["description"] @@ -116,20 +112,20 @@ def render_user(user, template, relative_times=True, offset_hours=0): result = Template(_(template)).safe_substitute(**available_data) return result -def render_conversation(conversation, template, post_template, relative_times=False, offset_hours=0): +def render_conversation(conversation, template, settings, post_template, relative_times=False, offset_hours=0): users = [] for account in conversation.accounts: if account.display_name != "": - users.append(account.display_name) + users.append(utils.get_user_alias(account, settings)) else: users.append(account.username) users = ", ".join(users) - last_post = render_post(conversation.last_status, post_template, relative_times=relative_times, offset_hours=offset_hours) + last_post = render_post(conversation.last_status, post_template, settings, relative_times=relative_times, offset_hours=offset_hours) available_data = dict(users=users, last_post=last_post) result = Template(_(template)).safe_substitute(**available_data) return result -def render_notification(notification, template, post_template, relative_times=False, offset_hours=0): +def render_notification(notification, template, post_template, settings, relative_times=False, offset_hours=0): """ Renders any given notification according to the passed template. Available data for notifications will be stored in the following variables: $date: Creation date. @@ -142,29 +138,27 @@ def render_notification(notification, template, post_template, relative_times=Fa created_at = process_date(notification.created_at, relative_times, offset_hours) available_data.update(date=created_at) # user. - display_name = notification.account.display_name - if display_name == "": - display_name = notification.account.username + display_name = utils.get_user_alias(notification.account, settings) available_data.update(display_name=display_name, screen_name=notification.account.acct) text = "Unknown: %r" % (notification) # Remove date from status, so it won't be rendered twice. post_template = post_template.replace("$date", "") if notification.type == "status": - text = _("has posted: {status}").format(status=render_post(notification.status, post_template, relative_times, offset_hours)) + text = _("has posted: {status}").format(status=render_post(notification.status, post_template, settings, relative_times, offset_hours)) elif notification.type == "mention": - text = _("has mentioned you: {status}").format(status=render_post(notification.status, post_template, relative_times, offset_hours)) + text = _("has mentioned you: {status}").format(status=render_post(notification.status, post_template, settings, relative_times, offset_hours)) elif notification.type == "reblog": - text = _("has boosted: {status}").format(status=render_post(notification.status, post_template, relative_times, offset_hours)) + text = _("has boosted: {status}").format(status=render_post(notification.status, post_template, settings, relative_times, offset_hours)) elif notification.type == "favourite": - text = _("has added to favorites: {status}").format(status=render_post(notification.status, post_template, relative_times, offset_hours)) + text = _("has added to favorites: {status}").format(status=render_post(notification.status, post_template, settings, relative_times, offset_hours)) elif notification.type == "update": - text = _("has updated a status: {status}").format(status=render_post(notification.status, post_template, relative_times, offset_hours)) + text = _("has updated a status: {status}").format(status=render_post(notification.status, post_template, settings, relative_times, offset_hours)) elif notification.type == "follow": text = _("has followed you.") elif notification.type == "admin.sign_up": text = _("has joined the instance.") elif notification.type == "poll": - text = _("A poll in which you have voted has expired: {status}").format(status=render_post(notification.status, post_template, relative_times, offset_hours)) + text = _("A poll in which you have voted has expired: {status}").format(status=render_post(notification.status, post_template, settings, relative_times, offset_hours)) elif notification.type == "follow_request": text = _("wants to follow you.") available_data.update(text=text) diff --git a/src/sessions/mastodon/utils.py b/src/sessions/mastodon/utils.py index d42b856d..8f4a9bc7 100644 --- a/src/sessions/mastodon/utils.py +++ b/src/sessions/mastodon/utils.py @@ -1,4 +1,5 @@ import re +import demoji from html.parser import HTMLParser url_re = re.compile('') @@ -68,4 +69,25 @@ def find_urls(post, include_tags=False): for url in urls[::]: if url.lower().endswith("/tags/"+tag["name"]): urls.remove(url) - return urls \ No newline at end of file + return urls + +def get_user_alias(user, settings): + if user.display_name == None or user.display_name == "": + display_name = user.username + else: + display_name = user.display_name + aliases = settings.get("user-aliases") + if aliases == None: + return demoji_user(display_name, settings) + user_alias = aliases.get(user.id) + if user_alias != None: + return user_alias + return demoji_user(display_name, settings) + +def demoji_user(name, settings): + if settings["general"]["hide_emojis"] == True: + user = demoji.replace(name, "") + # Take care of Mastodon instance emojis. + user = re.sub(r":(.*?):", "", user) + return user + return name