Mastodon: Improved character count to match Mastodon's backend implementation. Remote users are counted only by username (domains are not taken into account), content warning text counts against character limit, and emoji&CJK characters are counted as 1

This commit is contained in:
Manuel Cortez 2023-02-06 04:09:58 -06:00
parent 45cffd6a0b
commit 63ae496c39
No known key found for this signature in database
GPG Key ID: 9E0735CA15EFE790
2 changed files with 23 additions and 1 deletions

View File

@ -7,6 +7,7 @@ This release focuses on fixing some important bugs that have been reported in th
* Fixed the update system.
* Fixed a bug when attempting to switch between different accounts using the invisible interface, if the focused account is not an active session.
* Mastodon:
* Improved the way TWBlue counts characters in Mastodon. Now it counts only the username part in a remote user (@domain is not counted against character limit), adds content warning text to character count, also emojis and CJK characters are counted as 1 as opposed to 2. ([#511](https://github.com/MCV-Software/TWBlue/issues/511))
* Added notification when a user joins an instance. This notification is only available for administrators.
* Added option to disable Streaming in the account options. This can be useful if TWBlue, for some reason, repeatedly calls the instance API.
* Improved the code that works with the Streaming API to reduce the number of reconnection attempts TWBlue performs.

View File

@ -1,13 +1,28 @@
# -*- coding: utf-8 -*-
import os
import re
import wx
import widgetUtils
import config
import output
from twitter_text import parse_tweet, config
from controller.twitter import messages
from sessions.mastodon import templates
from wxUI.dialogs.mastodon import postDialogs
def character_count(post_text, post_cw, character_limit=500):
# We will use text for counting character limit only.
full_text = post_text+post_cw
# find remote users as Mastodon doesn't count the domain in char limit.
users = re.findall("@[\w\.-]+@[\w\.-]+", full_text)
for user in users:
domain = user.split("@")[-1]
full_text = full_text.replace("@"+domain, "")
options = config.config.get("defaults")
options.update(max_weighted_tweet_length=character_limit, default_weight=100)
parsed = parse_tweet(full_text, options=options)
return parsed.weightedLength
class post(messages.basicTweet):
def __init__(self, session, title, caption, text="", *args, **kwargs):
# take max character limit from session as this might be different for some instances.
@ -19,6 +34,7 @@ class post(messages.basicTweet):
self.message.text.SetInsertionPoint(len(self.message.text.GetValue()))
widgetUtils.connect_event(self.message.spellcheck, widgetUtils.BUTTON_PRESSED, self.spellcheck)
widgetUtils.connect_event(self.message.text, widgetUtils.ENTERED_TEXT, self.text_processor)
widgetUtils.connect_event(self.message.spoiler, widgetUtils.ENTERED_TEXT, self.text_processor)
widgetUtils.connect_event(self.message.translate, widgetUtils.BUTTON_PRESSED, self.translate)
widgetUtils.connect_event(self.message.add, widgetUtils.BUTTON_PRESSED, self.on_attach)
widgetUtils.connect_event(self.message.remove_attachment, widgetUtils.BUTTON_PRESSED, self.remove_attachment)
@ -49,7 +65,12 @@ class post(messages.basicTweet):
return self.thread
def text_processor(self, *args, **kwargs):
super(post, self).text_processor(*args, **kwargs)
text = self.message.text.GetValue()
cw = self.message.spoiler.GetValue()
results = character_count(text, cw, character_limit=self.max)
self.message.SetTitle(_("%s - %s of %d characters") % (self.title, results, self.max))
if results > self.max:
self.session.sound.play("max_length.ogg")
if len(self.thread) > 0:
if hasattr(self.message, "posts"):
self.message.posts.Enable(True)