1
0
mirror of https://github.com/ihabunek/toot synced 2024-12-23 07:27:12 +01:00

Merge pull request #454 from lexiwinter/use-preferences-visibility

tui: honour user's default visibility preference
This commit is contained in:
Ivan Habunek 2023-12-31 19:28:15 +01:00 committed by GitHub
commit 2e55ddbe7e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 6 deletions

View File

@ -618,6 +618,10 @@ def get_instance(base_url: str) -> Response:
return http.anon_get(url)
def get_preferences(app, user) -> Response:
return http.get(app, user, '/api/v1/preferences')
def get_lists(app, user):
return http.get(app, user, "/api/v1/lists").json()

View File

@ -1,7 +1,7 @@
import click
from typing import Optional
from toot.cli import TUI_COLORS, Context, cli, pass_context
from toot.cli import TUI_COLORS, VISIBILITY_CHOICES, Context, cli, pass_context
from toot.cli.validators import validate_tui_colors
from toot.tui.app import TUI, TuiOptions
@ -24,12 +24,18 @@ COLOR_OPTIONS = ", ".join(TUI_COLORS.keys())
help=f"""Number of colors to use, one of {COLOR_OPTIONS}, defaults to 16 if
using --color, and 1 if using --no-color."""
)
@click.option(
"-v", "--default-visibility",
type=click.Choice(VISIBILITY_CHOICES),
help="Default visibility when posting new toots; overrides the server-side preference"
)
@pass_context
def tui(
ctx: Context,
colors: Optional[int],
media_viewer: Optional[str],
relative_datetimes: bool,
default_visibility: Optional[str]
):
"""Launches the toot terminal user interface"""
if colors is None:
@ -39,6 +45,7 @@ def tui(
colors=colors,
media_viewer=media_viewer,
relative_datetimes=relative_datetimes,
default_visibility=default_visibility
)
tui = TUI.create(ctx.app, ctx.user, options)
tui.run()

View File

@ -31,6 +31,7 @@ class TuiOptions(NamedTuple):
colors: int
media_viewer: Optional[str]
relative_datetimes: bool
default_visibility: Optional[bool]
class Header(urwid.WidgetWrap):
@ -137,11 +138,13 @@ class TUI(urwid.Frame):
self.can_translate = False
self.account = None
self.followed_accounts = []
self.preferences = {}
super().__init__(self.body, header=self.header, footer=self.footer)
def run(self):
self.loop.set_alarm_in(0, lambda *args: self.async_load_instance())
self.loop.set_alarm_in(0, lambda *args: self.async_load_preferences())
self.loop.set_alarm_in(0, lambda *args: self.async_load_timeline(
is_initial=True, timeline_name="home"))
self.loop.set_alarm_in(0, lambda *args: self.async_load_followed_accounts())
@ -326,6 +329,19 @@ class TUI(urwid.Frame):
return self.run_in_thread(_load_instance, done_callback=_done)
def async_load_preferences(self):
"""
Attempt to update user preferences from instance.
https://docs.joinmastodon.org/methods/preferences/
"""
def _load_preferences():
return api.get_preferences(self.app, self.user).json()
def _done(preferences):
self.preferences = preferences
return self.run_in_thread(_load_preferences, done_callback=_done)
def async_load_followed_accounts(self):
def _load_accounts():
try:
@ -400,7 +416,15 @@ class TUI(urwid.Frame):
def _post(timeline, *args):
self.post_status(*args)
composer = StatusComposer(self.max_toot_chars, self.user.username, in_reply_to)
# If the user specified --default-visibility, use that; otherwise,
# try to use the server-side default visibility. If that fails, fall
# back to get_default_visibility().
visibility = (self.options.default_visibility or
self.preferences.get('posting:default:visibility',
get_default_visibility()))
composer = StatusComposer(self.max_toot_chars, self.user.username,
visibility, in_reply_to)
urwid.connect_signal(composer, "close", _close)
urwid.connect_signal(composer, "post", _post)
self.open_overlay(composer, title="Compose status")

View File

@ -1,8 +1,6 @@
import urwid
import logging
from toot.cli import get_default_visibility
from .constants import VISIBILITY_OPTIONS
from .widgets import Button, EditBox
@ -15,7 +13,7 @@ class StatusComposer(urwid.Frame):
"""
signals = ["close", "post"]
def __init__(self, max_chars, username, in_reply_to=None):
def __init__(self, max_chars, username, visibility, in_reply_to=None):
self.in_reply_to = in_reply_to
self.max_chars = max_chars
self.username = username
@ -34,7 +32,7 @@ class StatusComposer(urwid.Frame):
on_press=self.remove_content_warning)
self.visibility = (
in_reply_to.visibility if in_reply_to else get_default_visibility()
in_reply_to.visibility if in_reply_to else visibility
)
self.visibility_button = Button("Visibility: {}".format(self.visibility),
on_press=self.choose_visibility)