Mastodon: Implemented hide emojis for usernames.

This commit is contained in:
2023-04-05 09:17:37 -06:00
parent 0764679164
commit 972b851b93
10 changed files with 79 additions and 86 deletions

View File

@@ -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]

View File

@@ -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

View File

@@ -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 theyve defined it. Not necessarily a persons 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)

View File

@@ -1,4 +1,5 @@
import re
import demoji
from html.parser import HTMLParser
url_re = re.compile('<a\s*href=[\'|"](.*?)[\'"].*?>')
@@ -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
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