From 80ac5402ae542959f1d9df17a278700397901aa1 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Fri, 8 Dec 2023 12:48:49 +0000 Subject: [PATCH] Ability to retrieve the thread of a post. --- client.py | 31 +++++++++++++++++++++++++++++++ cursesclient.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/client.py b/client.py index 155ec48..2a58bda 100644 --- a/client.py +++ b/client.py @@ -166,6 +166,9 @@ class Client: def ego_feed(self): return EgoFeed(self) + def thread_feed(self, id, full): + return ThreadFeed(self, id, full) + def cache_status(self, status): self.status_cache[status['id']] = status @@ -241,6 +244,34 @@ class MentionsFeed(IncrementalServerFeed): super().__init__(client, "notifications", {"types[]":['mention']}, get=lambda item: item['status']) +class ThreadFeed(Feed): + def __init__(self, client, post_id, full): + super().__init__(client) + self.post_id = post_id + self.want_full = full + + def start(self): + status = cstatus = self.client.get_status_by_id(self.post_id) + context = self.client.get(f"statuses/{self.post_id}/context") + if self.want_full and len(context["ancestors"]) > 0: + cstatus = context["ancestors"][0] + cid = cstatus["id"] + context = self.client.get(f"statuses/{cid}/context") + self.data = context["ancestors"] + [cstatus] + context["descendants"] + self.is_full = len(context["ancestors"]) == 0 + + def min_index(self): + return 0 + def max_index(self): + return len(self.data) + def __getitem__(self, n): + return self.data[n] + + def extend_past(self): + return False + def extend_future(self): + return False + class EgoFeed(IncrementalServerFeed): def __init__(self, client): super().__init__(client, "notifications", { diff --git a/cursesclient.py b/cursesclient.py index e238f65..946b618 100644 --- a/cursesclient.py +++ b/cursesclient.py @@ -433,6 +433,8 @@ class File(Activity): self.favourite_mode() elif ch in {ctrl('b')}: self.boost_mode() + elif ch in {'t', 'T'}: + self.thread_mode() elif self.mode == 'select': if ch in {'q', 'Q'}: self.mode = 'normal' @@ -455,6 +457,12 @@ class File(Activity): elif (self.select_type == 'boost' and ch in {'d', 'D'}): self.boost_complete(-1) + elif (self.select_type == 'thread' and + ch in {' '}): + self.thread_complete(False) + elif (self.select_type == 'thread' and + ch in {'f', 'F'}): + self.thread_complete(True) def unprime(self): pass # not supported @@ -464,6 +472,8 @@ class File(Activity): pass # not supported def boost_mode(self): pass # not supported + def thread_mode(self): + pass # not supported class ObjectFile(File): def __init__(self, cc, constructor, feed, title): @@ -582,6 +592,9 @@ class ObjectFile(File): sl.keys.append(('SPACE', 'Boost')) if self.statuses[self.select_target].boosted: sl.keys.append(('D', 'Unboost')) + elif self.select_type == 'thread': + sl.keys.append(('SPACE', 'Show Thread Context')) + sl.keys.append(('F', 'Show Full Thread')) sl.keys.append(('-', None)) sl.keys.append(('+', None)) sl.keys.append(('Q', 'Quit')) @@ -615,6 +628,11 @@ class ObjectFile(File): self.mode = 'select' self.select_type = 'boost' self.select_target = self.index_by_line[self.linepos-1] + def thread_mode(self): + if self.items_are_statuses: + self.mode = 'select' + self.select_type = 'thread' + self.select_target = self.index_by_line[self.linepos-1] def prev_select_target(self): self.select_target = max(self.minpos, self.select_target-1) def next_select_target(self): @@ -669,6 +687,19 @@ class ObjectFile(File): data = self.cc.post(f"statuses/{reply_id}/{verb}") target.update_fave_boost(data) + def thread_complete(self, full): + self.mode = 'normal' + target = self.statuses[self.select_target] + reply_id = target.get_reply_id() + feed = self.cc.thread_feed(reply_id, full) + feed.start() + if feed.is_full: + title = "Full thread of post " + reply_id + else: + title = "Thread context of post " + reply_id + self.push_to(StatusFile( + self.cc, feed, text.ColouredString(title, "H"))) + def move_to(self, pos): old_linepos = self.linepos self.linepos = pos -- 2.30.2