Read toot lenght limit from mastodon instances

fixes #369
This commit is contained in:
Ivan Habunek 2023-06-24 10:18:10 +02:00
parent 12d84ea05e
commit 0662a7616b
No known key found for this signature in database
GPG key ID: CDBD63C43A30BB95
2 changed files with 33 additions and 6 deletions

View file

@ -14,13 +14,16 @@ from .overlays import ExceptionStackTrace, GotoMenu, Help, StatusSource, StatusL
from .overlays import StatusDeleteConfirmation, Account
from .poll import Poll
from .timeline import Timeline
from .utils import parse_content_links, show_media, copy_to_clipboard
from .utils import get_max_toot_chars, parse_content_links, show_media, copy_to_clipboard
logger = logging.getLogger(__name__)
urwid.set_encoding('UTF-8')
DEFAULT_MAX_TOOT_CHARS = 500
class Header(urwid.WidgetWrap):
def __init__(self, app, user):
self.app = app
@ -112,7 +115,7 @@ class TUI(urwid.Frame):
self.footer.set_status("Loading...")
# Default max status length, updated on startup
self.max_toot_chars = 500
self.max_toot_chars = DEFAULT_MAX_TOOT_CHARS
self.timeline = None
self.overlay = None
@ -289,8 +292,8 @@ class TUI(urwid.Frame):
return api.get_instance(self.app.base_url)
def _done(instance):
if "max_toot_chars" in instance:
self.max_toot_chars = instance["max_toot_chars"]
self.max_toot_chars = get_max_toot_chars(instance, DEFAULT_MAX_TOOT_CHARS)
logger.info(f"Max toot chars set to: {self.max_toot_chars}")
if "translation" in instance:
# instance is advertising translation service

View file

@ -1,13 +1,14 @@
import base64
import urwid
from html.parser import HTMLParser
import math
import os
import re
import shutil
import subprocess
import urwid
from datetime import datetime, timezone
from functools import reduce
from html.parser import HTMLParser
HASHTAG_PATTERN = re.compile(r'(?<!\w)(#\w+)\b')
SECOND = 1
@ -163,3 +164,26 @@ def copy_to_clipboard(screen: urwid.raw_display.Screen, text: str):
screen.write(f"\033]52;c;{b64_text}\a")
screen.flush()
def get_max_toot_chars(instance, default=500):
# Mastodon
# https://docs.joinmastodon.org/entities/Instance/#max_characters
max_toot_chars = deep_get(instance, ["configuration", "statuses", "max_characters"])
if isinstance(max_toot_chars, int):
return max_toot_chars
# Pleroma
max_toot_chars = instance.get("max_toot_chars")
if isinstance(max_toot_chars, int):
return max_toot_chars
return default
def deep_get(adict: dict, path: list[str], default=None):
return reduce(
lambda d, key: d.get(key, default) if isinstance(d, dict) else default,
path,
adict
)