2019-08-24 11:20:31 +02:00
|
|
|
import logging
|
|
|
|
import urwid
|
|
|
|
|
|
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
|
|
|
2020-01-26 15:58:40 +01:00
|
|
|
from toot import api, config, __version__
|
2019-08-24 11:20:31 +02:00
|
|
|
|
2019-08-27 10:02:13 +02:00
|
|
|
from .compose import StatusComposer
|
2019-08-24 11:20:31 +02:00
|
|
|
from .constants import PALETTE
|
|
|
|
from .entities import Status
|
2020-01-26 11:13:45 +01:00
|
|
|
from .overlays import ExceptionStackTrace, GotoMenu, Help, StatusSource, StatusLinks
|
2019-09-04 16:16:16 +02:00
|
|
|
from .overlays import StatusDeleteConfirmation
|
2019-08-24 11:20:31 +02:00
|
|
|
from .timeline import Timeline
|
2020-01-26 11:13:45 +01:00
|
|
|
from .utils import parse_content_links, show_media
|
2019-08-24 11:20:31 +02:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2019-09-14 00:03:05 +02:00
|
|
|
urwid.set_encoding('UTF-8')
|
|
|
|
|
2019-08-24 11:20:31 +02:00
|
|
|
|
|
|
|
class Header(urwid.WidgetWrap):
|
|
|
|
def __init__(self, app, user):
|
|
|
|
self.app = app
|
|
|
|
self.user = user
|
|
|
|
|
|
|
|
self.text = urwid.Text("")
|
|
|
|
self.cols = urwid.Columns([
|
|
|
|
("pack", urwid.Text(('header_bold', 'toot'))),
|
2019-09-23 16:51:04 +02:00
|
|
|
("pack", urwid.Text(('header', ' | {}@{}'.format(user.username, app.instance)))),
|
2019-08-24 11:20:31 +02:00
|
|
|
("pack", self.text),
|
|
|
|
])
|
|
|
|
|
|
|
|
widget = urwid.AttrMap(self.cols, 'header')
|
|
|
|
widget = urwid.Padding(widget)
|
|
|
|
self._wrapped_widget = widget
|
|
|
|
|
|
|
|
def clear_text(self, text):
|
|
|
|
self.text.set_text("")
|
|
|
|
|
|
|
|
def set_text(self, text):
|
|
|
|
self.text.set_text(" | " + text)
|
|
|
|
|
|
|
|
|
|
|
|
class Footer(urwid.Pile):
|
|
|
|
def __init__(self):
|
|
|
|
self.status = urwid.Text("")
|
|
|
|
self.message = urwid.Text("")
|
|
|
|
|
|
|
|
return super().__init__([
|
|
|
|
urwid.AttrMap(self.status, "footer_status"),
|
|
|
|
urwid.AttrMap(self.message, "footer_message"),
|
|
|
|
])
|
|
|
|
|
|
|
|
def set_status(self, text):
|
|
|
|
self.status.set_text(text)
|
|
|
|
|
2019-08-24 12:53:55 +02:00
|
|
|
def clear_status(self, text):
|
2019-08-24 13:13:22 +02:00
|
|
|
self.status.set_text("")
|
2019-08-24 12:53:55 +02:00
|
|
|
|
2019-08-24 11:20:31 +02:00
|
|
|
def set_message(self, text):
|
|
|
|
self.message.set_text(text)
|
|
|
|
|
2019-08-26 13:28:37 +02:00
|
|
|
def set_error_message(self, text):
|
|
|
|
self.message.set_text(("footer_message_error", text))
|
|
|
|
|
2019-08-24 12:53:55 +02:00
|
|
|
def clear_message(self):
|
2019-08-24 13:13:22 +02:00
|
|
|
self.message.set_text("")
|
2019-08-24 11:20:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TUI(urwid.Frame):
|
|
|
|
"""Main TUI frame."""
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def create(cls, app, user):
|
|
|
|
"""Factory method, sets up TUI and an event loop."""
|
|
|
|
|
|
|
|
tui = cls(app, user)
|
|
|
|
loop = urwid.MainLoop(
|
|
|
|
tui,
|
|
|
|
palette=PALETTE,
|
|
|
|
event_loop=urwid.AsyncioEventLoop(),
|
|
|
|
unhandled_input=tui.unhandled_input,
|
|
|
|
)
|
|
|
|
tui.loop = loop
|
|
|
|
|
|
|
|
return tui
|
|
|
|
|
|
|
|
def __init__(self, app, user):
|
|
|
|
self.app = app
|
|
|
|
self.user = user
|
2020-01-26 15:58:40 +01:00
|
|
|
self.config = config.load_config()
|
2019-08-24 11:20:31 +02:00
|
|
|
|
|
|
|
self.loop = None # set in `create`
|
|
|
|
self.executor = ThreadPoolExecutor(max_workers=1)
|
2019-08-24 13:13:22 +02:00
|
|
|
self.timeline_generator = api.home_timeline_generator(app, user, limit=40)
|
2019-08-24 11:20:31 +02:00
|
|
|
|
2019-08-26 15:06:40 +02:00
|
|
|
# Show intro screen while toots are being loaded
|
|
|
|
self.body = self.build_intro()
|
2019-08-24 11:20:31 +02:00
|
|
|
self.header = Header(app, user)
|
|
|
|
self.footer = Footer()
|
|
|
|
self.footer.set_status("Loading...")
|
|
|
|
|
2020-01-03 09:15:11 +01:00
|
|
|
# Default max status length, updated on startup
|
|
|
|
self.max_toot_chars = 500
|
|
|
|
|
2019-08-25 14:30:57 +02:00
|
|
|
self.timeline = None
|
|
|
|
self.overlay = None
|
2019-08-26 14:08:41 +02:00
|
|
|
self.exception = None
|
2019-08-25 14:30:57 +02:00
|
|
|
|
2019-08-24 11:20:31 +02:00
|
|
|
super().__init__(self.body, header=self.header, footer=self.footer)
|
|
|
|
|
|
|
|
def run(self):
|
2020-01-03 09:15:11 +01:00
|
|
|
self.loop.set_alarm_in(0, lambda *args: self.async_load_instance())
|
|
|
|
self.loop.set_alarm_in(0, lambda *args: self.async_load_timeline(
|
|
|
|
is_initial=True, timeline_name="home"))
|
2019-08-24 11:20:31 +02:00
|
|
|
self.loop.run()
|
|
|
|
self.executor.shutdown(wait=False)
|
|
|
|
|
2019-08-26 15:06:40 +02:00
|
|
|
def build_intro(self):
|
|
|
|
font = urwid.font.Thin6x6Font()
|
|
|
|
|
|
|
|
# NB: Padding with width="clip" will convert the fixed BigText widget
|
|
|
|
# to a flow widget so it can be used in a Pile.
|
|
|
|
|
|
|
|
big_text = "Toot {}".format(__version__)
|
|
|
|
big_text = urwid.BigText(("intro_bigtext", big_text), font)
|
|
|
|
big_text = urwid.Padding(big_text, align="center", width="clip")
|
|
|
|
|
|
|
|
intro = urwid.Pile([
|
|
|
|
big_text,
|
|
|
|
urwid.Divider(),
|
|
|
|
urwid.Text([
|
|
|
|
"Maintained by ",
|
|
|
|
("intro_smalltext", "@ihabunek"),
|
|
|
|
" and contributors"
|
|
|
|
], align="center"),
|
|
|
|
urwid.Divider(),
|
|
|
|
urwid.Text(("intro_smalltext", "Loading toots..."), align="center"),
|
|
|
|
])
|
|
|
|
|
|
|
|
return urwid.Filler(intro)
|
|
|
|
|
2019-08-26 13:28:37 +02:00
|
|
|
def run_in_thread(self, fn, args=[], kwargs={}, done_callback=None, error_callback=None):
|
|
|
|
"""Runs `fn(*args, **kwargs)` asynchronously in a separate thread.
|
|
|
|
|
|
|
|
On completion calls `done_callback` if `fn` exited cleanly, or
|
|
|
|
`error_callback` if an exception was caught. Callback methods are
|
|
|
|
invoked in the main thread, not the thread in which `fn` is executed.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def _default_error_callback(ex):
|
|
|
|
self.exception = ex
|
|
|
|
self.footer.set_error_message("An exeption occured, press E to view")
|
|
|
|
|
|
|
|
_error_callback = error_callback or _default_error_callback
|
|
|
|
|
|
|
|
def _done(future):
|
|
|
|
try:
|
|
|
|
result = future.result()
|
|
|
|
if done_callback:
|
|
|
|
# Use alarm to invoke callback in main thread
|
|
|
|
self.loop.set_alarm_in(0, lambda *args: done_callback(result))
|
|
|
|
except Exception as ex:
|
|
|
|
exception = ex
|
|
|
|
logger.exception(exception)
|
|
|
|
self.loop.set_alarm_in(0, lambda *args: _error_callback(exception))
|
|
|
|
|
|
|
|
future = self.executor.submit(fn, *args, **kwargs)
|
|
|
|
future.add_done_callback(_done)
|
2019-08-30 14:02:00 +02:00
|
|
|
return future
|
2019-08-26 13:28:37 +02:00
|
|
|
|
2019-08-28 17:00:46 +02:00
|
|
|
def connect_default_timeline_signals(self, timeline):
|
|
|
|
def _compose(*args):
|
|
|
|
self.show_compose()
|
|
|
|
|
2019-09-04 16:16:16 +02:00
|
|
|
def _delete(timeline, status):
|
|
|
|
if status.is_mine:
|
|
|
|
self.show_delete_confirmation(status)
|
|
|
|
|
2019-08-29 11:01:49 +02:00
|
|
|
def _reply(timeline, status):
|
|
|
|
self.show_compose(status)
|
|
|
|
|
2019-08-28 17:00:46 +02:00
|
|
|
def _source(timeline, status):
|
|
|
|
self.show_status_source(status)
|
|
|
|
|
2020-01-26 11:13:45 +01:00
|
|
|
def _links(timeline, status):
|
|
|
|
self.show_links(status)
|
|
|
|
|
2019-08-29 11:47:44 +02:00
|
|
|
def _media(timeline, status):
|
|
|
|
self.show_media(status)
|
|
|
|
|
2019-08-30 12:28:03 +02:00
|
|
|
def _menu(timeline, status):
|
|
|
|
self.show_context_menu(status)
|
|
|
|
|
2019-08-28 17:00:46 +02:00
|
|
|
urwid.connect_signal(timeline, "compose", _compose)
|
2019-09-04 16:16:16 +02:00
|
|
|
urwid.connect_signal(timeline, "delete", _delete)
|
|
|
|
urwid.connect_signal(timeline, "favourite", self.async_toggle_favourite)
|
|
|
|
urwid.connect_signal(timeline, "focus", self.refresh_footer)
|
2019-08-29 11:47:44 +02:00
|
|
|
urwid.connect_signal(timeline, "media", _media)
|
2019-08-30 12:28:03 +02:00
|
|
|
urwid.connect_signal(timeline, "menu", _menu)
|
2019-09-04 16:16:16 +02:00
|
|
|
urwid.connect_signal(timeline, "reblog", self.async_toggle_reblog)
|
|
|
|
urwid.connect_signal(timeline, "reply", _reply)
|
|
|
|
urwid.connect_signal(timeline, "source", _source)
|
2020-01-26 11:13:45 +01:00
|
|
|
urwid.connect_signal(timeline, "links", _links)
|
2019-08-28 17:00:46 +02:00
|
|
|
|
2020-01-26 21:29:08 +01:00
|
|
|
def build_timeline(self, name, statuses, local):
|
2019-08-28 15:32:57 +02:00
|
|
|
def _close(*args):
|
|
|
|
raise urwid.ExitMainLoop()
|
|
|
|
|
|
|
|
def _next(*args):
|
2019-08-30 14:02:00 +02:00
|
|
|
self.async_load_timeline(is_initial=False)
|
2019-08-28 15:32:57 +02:00
|
|
|
|
|
|
|
def _thread(timeline, status):
|
|
|
|
self.show_thread(status)
|
|
|
|
|
2020-01-26 21:52:58 +01:00
|
|
|
def _toggle_save(timeline, status):
|
2020-01-26 21:29:08 +01:00
|
|
|
if not timeline.name.startswith("#"):
|
|
|
|
return
|
|
|
|
hashtag = timeline.name[1:]
|
|
|
|
assert isinstance(local, bool), local
|
2020-01-26 21:52:58 +01:00
|
|
|
timelines = self.config.setdefault("timelines", {})
|
|
|
|
if hashtag in timelines:
|
|
|
|
del timelines[hashtag]
|
2020-01-27 11:50:12 +01:00
|
|
|
self.footer.set_message("#{} unpinned".format(hashtag))
|
2020-01-26 21:52:58 +01:00
|
|
|
else:
|
|
|
|
timelines[hashtag] = {"local": local}
|
2020-01-27 11:50:12 +01:00
|
|
|
self.footer.set_message("#{} pinned".format(hashtag))
|
|
|
|
self.loop.set_alarm_in(5, lambda *args: self.footer.clear_message())
|
2020-01-26 21:29:08 +01:00
|
|
|
config.save_config(self.config)
|
|
|
|
|
2019-08-30 14:02:00 +02:00
|
|
|
timeline = Timeline(name, statuses)
|
2019-08-28 17:00:46 +02:00
|
|
|
|
|
|
|
self.connect_default_timeline_signals(timeline)
|
2019-08-28 15:32:57 +02:00
|
|
|
urwid.connect_signal(timeline, "next", _next)
|
|
|
|
urwid.connect_signal(timeline, "close", _close)
|
|
|
|
urwid.connect_signal(timeline, "thread", _thread)
|
2020-01-26 21:52:58 +01:00
|
|
|
urwid.connect_signal(timeline, "save", _toggle_save)
|
2019-08-28 17:00:46 +02:00
|
|
|
|
2019-08-26 13:28:37 +02:00
|
|
|
return timeline
|
|
|
|
|
2019-09-04 16:16:16 +02:00
|
|
|
def make_status(self, status_data):
|
|
|
|
is_mine = self.user.username == status_data["account"]["acct"]
|
|
|
|
return Status(status_data, is_mine, self.app.instance)
|
|
|
|
|
2019-08-28 15:32:57 +02:00
|
|
|
def show_thread(self, status):
|
|
|
|
def _close(*args):
|
2019-08-28 17:00:46 +02:00
|
|
|
"""When thread is closed, go back to the main timeline."""
|
2019-08-28 15:32:57 +02:00
|
|
|
self.body = self.timeline
|
|
|
|
self.body.refresh_status_details()
|
|
|
|
self.refresh_footer(self.timeline)
|
|
|
|
|
|
|
|
# This is pretty fast, so it's probably ok to block while context is
|
|
|
|
# loaded, can be made async later if needed
|
2019-09-22 12:13:40 +02:00
|
|
|
context = api.context(self.app, self.user, status.original.id)
|
2019-09-04 16:16:16 +02:00
|
|
|
ancestors = [self.make_status(s) for s in context["ancestors"]]
|
|
|
|
descendants = [self.make_status(s) for s in context["descendants"]]
|
2019-08-28 17:00:46 +02:00
|
|
|
statuses = ancestors + [status] + descendants
|
2019-08-28 15:32:57 +02:00
|
|
|
focus = len(ancestors)
|
|
|
|
|
2019-08-28 17:00:46 +02:00
|
|
|
timeline = Timeline("thread", statuses, focus, is_thread=True)
|
|
|
|
|
|
|
|
self.connect_default_timeline_signals(timeline)
|
2019-08-28 15:32:57 +02:00
|
|
|
urwid.connect_signal(timeline, "close", _close)
|
|
|
|
|
|
|
|
self.body = timeline
|
|
|
|
self.refresh_footer(timeline)
|
|
|
|
|
2020-01-26 21:29:08 +01:00
|
|
|
def async_load_timeline(self, is_initial, timeline_name=None, local=None):
|
2019-08-26 13:28:37 +02:00
|
|
|
"""Asynchronously load a list of statuses."""
|
|
|
|
|
|
|
|
def _load_statuses():
|
|
|
|
self.footer.set_message("Loading statuses...")
|
|
|
|
try:
|
|
|
|
data = next(self.timeline_generator)
|
|
|
|
except StopIteration:
|
|
|
|
return []
|
|
|
|
finally:
|
|
|
|
self.footer.clear_message()
|
|
|
|
|
2019-09-04 16:16:16 +02:00
|
|
|
return [self.make_status(s) for s in data]
|
2019-08-26 13:28:37 +02:00
|
|
|
|
|
|
|
def _done_initial(statuses):
|
|
|
|
"""Process initial batch of statuses, construct a Timeline."""
|
2020-01-26 21:29:08 +01:00
|
|
|
self.timeline = self.build_timeline(timeline_name, statuses, local)
|
2019-08-28 15:32:57 +02:00
|
|
|
self.timeline.refresh_status_details() # Draw first status
|
|
|
|
self.refresh_footer(self.timeline)
|
2019-08-26 13:28:37 +02:00
|
|
|
self.body = self.timeline
|
|
|
|
|
|
|
|
def _done_next(statuses):
|
|
|
|
"""Process sequential batch of statuses, adds statuses to the
|
|
|
|
existing timeline."""
|
2019-08-27 12:57:18 +02:00
|
|
|
self.timeline.append_statuses(statuses)
|
2019-08-26 13:28:37 +02:00
|
|
|
|
2019-08-30 14:02:00 +02:00
|
|
|
return self.run_in_thread(_load_statuses,
|
2019-08-26 13:28:37 +02:00
|
|
|
done_callback=_done_initial if is_initial else _done_next)
|
2019-08-24 13:13:22 +02:00
|
|
|
|
2020-01-03 09:15:11 +01:00
|
|
|
def async_load_instance(self):
|
|
|
|
"""
|
|
|
|
Attempt to update max_toot_chars from instance data.
|
|
|
|
Does not work on vanilla Mastodon, works on Pleroma.
|
|
|
|
See: https://github.com/tootsuite/mastodon/issues/4915
|
|
|
|
"""
|
|
|
|
def _load_instance():
|
|
|
|
return api.get_instance(self.app.instance)
|
|
|
|
|
|
|
|
def _done(instance):
|
|
|
|
if "max_toot_chars" in instance:
|
2020-09-01 16:09:29 +02:00
|
|
|
self.max_toot_chars = instance["max_toot_chars"]
|
2020-01-03 09:15:11 +01:00
|
|
|
|
|
|
|
return self.run_in_thread(_load_instance, done_callback=_done)
|
|
|
|
|
2019-08-28 15:32:57 +02:00
|
|
|
def refresh_footer(self, timeline):
|
|
|
|
"""Show status details in footer."""
|
|
|
|
status, index, count = timeline.get_focused_status_with_counts()
|
2019-08-24 12:53:55 +02:00
|
|
|
self.footer.set_status([
|
2019-08-28 15:32:57 +02:00
|
|
|
("footer_status_bold", "[{}] ".format(timeline.name)),
|
2020-05-20 00:50:21 +02:00
|
|
|
] + ([status.id, " - status ", str(index + 1), " of ", str(count)]
|
|
|
|
if status else ["no focused status"]))
|
2019-08-24 11:20:31 +02:00
|
|
|
|
2019-08-25 14:30:57 +02:00
|
|
|
def show_status_source(self, status):
|
|
|
|
self.open_overlay(
|
|
|
|
widget=StatusSource(status),
|
|
|
|
title="Status source",
|
|
|
|
)
|
|
|
|
|
2020-01-26 11:13:45 +01:00
|
|
|
def show_links(self, status):
|
2020-05-20 00:50:21 +02:00
|
|
|
links = parse_content_links(status.data["content"]) if status else []
|
2020-04-15 14:12:30 +02:00
|
|
|
if links:
|
|
|
|
self.open_overlay(
|
|
|
|
widget=StatusLinks(links),
|
|
|
|
title="Status links",
|
|
|
|
options={"height": len(links) + 2},
|
|
|
|
)
|
2020-01-26 11:13:45 +01:00
|
|
|
|
2019-08-26 14:08:41 +02:00
|
|
|
def show_exception(self, exception):
|
|
|
|
self.open_overlay(
|
|
|
|
widget=ExceptionStackTrace(exception),
|
|
|
|
title="Unhandled Exception",
|
|
|
|
)
|
|
|
|
|
2019-08-29 11:01:49 +02:00
|
|
|
def show_compose(self, in_reply_to=None):
|
2019-08-28 17:00:46 +02:00
|
|
|
def _close(*args):
|
|
|
|
self.close_overlay()
|
|
|
|
|
2019-08-29 11:01:49 +02:00
|
|
|
def _post(timeline, *args):
|
|
|
|
self.post_status(*args)
|
2019-08-28 17:00:46 +02:00
|
|
|
|
2020-01-03 09:15:11 +01:00
|
|
|
composer = StatusComposer(self.max_toot_chars, in_reply_to)
|
2019-08-28 17:00:46 +02:00
|
|
|
urwid.connect_signal(composer, "close", _close)
|
|
|
|
urwid.connect_signal(composer, "post", _post)
|
|
|
|
self.open_overlay(composer, title="Compose status")
|
2019-08-27 10:02:13 +02:00
|
|
|
|
2019-08-30 14:02:00 +02:00
|
|
|
def show_goto_menu(self):
|
2020-01-26 15:58:40 +01:00
|
|
|
user_timelines = self.config.get("timelines", {})
|
|
|
|
menu = GotoMenu(user_timelines)
|
2019-08-30 14:02:00 +02:00
|
|
|
urwid.connect_signal(menu, "home_timeline",
|
|
|
|
lambda x: self.goto_home_timeline())
|
2019-08-30 15:31:57 +02:00
|
|
|
urwid.connect_signal(menu, "public_timeline",
|
|
|
|
lambda x, local: self.goto_public_timeline(local))
|
|
|
|
urwid.connect_signal(menu, "hashtag_timeline",
|
|
|
|
lambda x, tag, local: self.goto_tag_timeline(tag, local=local))
|
2019-08-30 14:02:00 +02:00
|
|
|
|
|
|
|
self.open_overlay(menu, title="Go to", options=dict(
|
|
|
|
align="center", width=("relative", 60),
|
2020-01-26 15:58:40 +01:00
|
|
|
valign="middle", height=9 + len(user_timelines),
|
2019-08-30 14:02:00 +02:00
|
|
|
))
|
|
|
|
|
2019-08-31 11:17:46 +02:00
|
|
|
def show_help(self):
|
|
|
|
self.open_overlay(Help(), title="Help")
|
|
|
|
|
2019-08-30 14:02:00 +02:00
|
|
|
def goto_home_timeline(self):
|
|
|
|
self.timeline_generator = api.home_timeline_generator(
|
|
|
|
self.app, self.user, limit=40)
|
|
|
|
promise = self.async_load_timeline(is_initial=True, timeline_name="home")
|
|
|
|
promise.add_done_callback(lambda *args: self.close_overlay())
|
|
|
|
|
|
|
|
def goto_public_timeline(self, local):
|
|
|
|
self.timeline_generator = api.public_timeline_generator(
|
2020-05-11 12:54:35 +02:00
|
|
|
self.app, self.user, local=local, limit=40)
|
2019-08-30 14:02:00 +02:00
|
|
|
promise = self.async_load_timeline(is_initial=True, timeline_name="public")
|
|
|
|
promise.add_done_callback(lambda *args: self.close_overlay())
|
|
|
|
|
2019-08-30 15:31:57 +02:00
|
|
|
def goto_tag_timeline(self, tag, local):
|
|
|
|
self.timeline_generator = api.tag_timeline_generator(
|
2020-05-11 12:54:35 +02:00
|
|
|
self.app, self.user, tag, local=local, limit=40)
|
2020-01-26 21:29:08 +01:00
|
|
|
promise = self.async_load_timeline(
|
|
|
|
is_initial=True, timeline_name="#{}".format(tag), local=local,
|
|
|
|
)
|
2019-08-30 15:31:57 +02:00
|
|
|
promise.add_done_callback(lambda *args: self.close_overlay())
|
|
|
|
|
2019-08-29 11:47:44 +02:00
|
|
|
def show_media(self, status):
|
2019-09-22 12:13:40 +02:00
|
|
|
urls = [m["url"] for m in status.original.data["media_attachments"]]
|
2019-08-29 11:47:44 +02:00
|
|
|
if urls:
|
|
|
|
show_media(urls)
|
|
|
|
|
2019-08-30 12:28:03 +02:00
|
|
|
def show_context_menu(self, status):
|
|
|
|
# TODO: show context menu
|
|
|
|
pass
|
|
|
|
|
2019-09-04 16:16:16 +02:00
|
|
|
def show_delete_confirmation(self, status):
|
|
|
|
def _delete(widget):
|
|
|
|
promise = self.async_delete_status(self.timeline, status)
|
|
|
|
promise.add_done_callback(lambda *args: self.close_overlay())
|
|
|
|
|
|
|
|
def _close(widget):
|
|
|
|
self.close_overlay()
|
|
|
|
|
|
|
|
widget = StatusDeleteConfirmation(status)
|
|
|
|
urwid.connect_signal(widget, "close", _close)
|
|
|
|
urwid.connect_signal(widget, "delete", _delete)
|
|
|
|
self.open_overlay(widget, title="Delete status?", options=dict(
|
|
|
|
align="center", width=("relative", 60),
|
|
|
|
valign="middle", height=5,
|
|
|
|
))
|
|
|
|
|
2019-08-29 11:01:49 +02:00
|
|
|
def post_status(self, content, warning, visibility, in_reply_to_id):
|
2019-08-27 17:22:52 +02:00
|
|
|
data = api.post_status(self.app, self.user, content,
|
2019-08-29 11:01:49 +02:00
|
|
|
spoiler_text=warning,
|
|
|
|
visibility=visibility,
|
|
|
|
in_reply_to_id=in_reply_to_id)
|
2019-09-04 16:16:16 +02:00
|
|
|
status = self.make_status(data)
|
2019-08-28 17:04:45 +02:00
|
|
|
|
|
|
|
# TODO: instead of this, fetch new items from the timeline?
|
2019-08-27 10:02:13 +02:00
|
|
|
self.timeline.prepend_status(status)
|
2019-08-28 17:04:45 +02:00
|
|
|
self.timeline.focus_status(status)
|
|
|
|
|
2019-08-27 10:02:13 +02:00
|
|
|
self.footer.set_message("Status posted {} \\o/".format(status.id))
|
|
|
|
self.close_overlay()
|
|
|
|
|
2019-08-28 17:00:46 +02:00
|
|
|
def async_toggle_favourite(self, timeline, status):
|
2019-08-26 13:28:37 +02:00
|
|
|
def _favourite():
|
|
|
|
logger.info("Favouriting {}".format(status))
|
|
|
|
api.favourite(self.app, self.user, status.id)
|
|
|
|
|
|
|
|
def _unfavourite():
|
|
|
|
logger.info("Unfavouriting {}".format(status))
|
|
|
|
api.unfavourite(self.app, self.user, status.id)
|
|
|
|
|
|
|
|
def _done(loop):
|
|
|
|
# Create a new Status with flipped favourited flag
|
|
|
|
new_data = status.data
|
|
|
|
new_data["favourited"] = not status.favourited
|
2019-09-04 16:16:16 +02:00
|
|
|
new_status = self.make_status(new_data)
|
|
|
|
timeline.update_status(new_status)
|
2019-08-26 13:28:37 +02:00
|
|
|
|
|
|
|
self.run_in_thread(
|
|
|
|
_unfavourite if status.favourited else _favourite,
|
|
|
|
done_callback=_done
|
|
|
|
)
|
|
|
|
|
2019-08-28 17:00:46 +02:00
|
|
|
def async_toggle_reblog(self, timeline, status):
|
2019-08-26 13:28:37 +02:00
|
|
|
def _reblog():
|
|
|
|
logger.info("Reblogging {}".format(status))
|
|
|
|
api.reblog(self.app, self.user, status.id)
|
|
|
|
|
|
|
|
def _unreblog():
|
|
|
|
logger.info("Unreblogging {}".format(status))
|
|
|
|
api.unreblog(self.app, self.user, status.id)
|
|
|
|
|
|
|
|
def _done(loop):
|
|
|
|
# Create a new Status with flipped reblogged flag
|
|
|
|
new_data = status.data
|
|
|
|
new_data["reblogged"] = not status.reblogged
|
2019-09-04 16:16:16 +02:00
|
|
|
new_status = self.make_status(new_data)
|
|
|
|
timeline.update_status(new_status)
|
2019-08-26 13:28:37 +02:00
|
|
|
|
|
|
|
self.run_in_thread(
|
|
|
|
_unreblog if status.reblogged else _reblog,
|
|
|
|
done_callback=_done
|
|
|
|
)
|
|
|
|
|
2019-09-04 16:16:16 +02:00
|
|
|
def async_delete_status(self, timeline, status):
|
|
|
|
def _delete():
|
|
|
|
api.delete_status(self.app, self.user, status.id)
|
|
|
|
|
|
|
|
def _done(loop):
|
|
|
|
timeline.remove_status(status)
|
|
|
|
|
|
|
|
return self.run_in_thread(_delete, done_callback=_done)
|
|
|
|
|
2019-08-25 14:30:57 +02:00
|
|
|
# --- Overlay handling -----------------------------------------------------
|
|
|
|
|
2019-08-28 17:00:46 +02:00
|
|
|
default_overlay_options = dict(
|
|
|
|
align="center", width=("relative", 80),
|
|
|
|
valign="middle", height=("relative", 80),
|
|
|
|
)
|
|
|
|
|
2019-08-25 14:30:57 +02:00
|
|
|
def open_overlay(self, widget, options={}, title=""):
|
|
|
|
top_widget = urwid.LineBox(widget, title=title)
|
|
|
|
bottom_widget = self.body
|
|
|
|
|
2019-08-30 14:02:00 +02:00
|
|
|
_options = self.default_overlay_options.copy()
|
2019-08-28 17:00:46 +02:00
|
|
|
_options.update(options)
|
|
|
|
|
2019-08-25 14:30:57 +02:00
|
|
|
self.overlay = urwid.Overlay(
|
|
|
|
top_widget,
|
|
|
|
bottom_widget,
|
2019-08-28 17:00:46 +02:00
|
|
|
**_options
|
2019-08-25 14:30:57 +02:00
|
|
|
)
|
|
|
|
self.body = self.overlay
|
|
|
|
|
|
|
|
def close_overlay(self):
|
|
|
|
self.body = self.overlay.bottom_w
|
|
|
|
self.overlay = None
|
|
|
|
|
|
|
|
# --- Keys -----------------------------------------------------------------
|
|
|
|
|
2019-08-24 11:20:31 +02:00
|
|
|
def unhandled_input(self, key):
|
2019-08-27 10:02:13 +02:00
|
|
|
# TODO: this should not be in unhandled input
|
2019-08-26 14:08:41 +02:00
|
|
|
if key in ('e', 'E'):
|
|
|
|
if self.exception:
|
|
|
|
self.show_exception(self.exception)
|
|
|
|
|
2019-08-30 14:02:00 +02:00
|
|
|
elif key in ('g', 'G'):
|
|
|
|
if not self.overlay:
|
|
|
|
self.show_goto_menu()
|
2019-08-31 11:17:46 +02:00
|
|
|
|
|
|
|
elif key in ('h', 'H'):
|
|
|
|
if not self.overlay:
|
|
|
|
self.show_help()
|
2019-08-30 14:02:00 +02:00
|
|
|
|
2019-08-27 10:02:13 +02:00
|
|
|
elif key == 'esc':
|
|
|
|
if self.overlay:
|
|
|
|
self.close_overlay()
|
2020-01-26 22:20:03 +01:00
|
|
|
elif self.timeline.name != "home":
|
|
|
|
# similar to goto_home_timeline() but without handling overlay (absent here)
|
|
|
|
self.timeline_generator = api.home_timeline_generator(
|
|
|
|
self.app, self.user, limit=40)
|
|
|
|
self.async_load_timeline(is_initial=True, timeline_name="home")
|
2019-08-27 10:02:13 +02:00
|
|
|
|
|
|
|
elif key in ('q', 'Q'):
|
2019-08-25 14:30:57 +02:00
|
|
|
if self.overlay:
|
|
|
|
self.close_overlay()
|
|
|
|
else:
|
|
|
|
raise urwid.ExitMainLoop()
|