chiark / gitweb /
Ability to retrieve the thread of a post.
authorSimon Tatham <anakin@pobox.com>
Fri, 8 Dec 2023 12:48:49 +0000 (12:48 +0000)
committerSimon Tatham <anakin@pobox.com>
Fri, 8 Dec 2023 12:48:49 +0000 (12:48 +0000)
client.py
cursesclient.py

index 155ec48edbe08c0aaf31918cd24744bf9944c2c2..2a58bdab7328be6e1089123062f2a02d2b908c50 100644 (file)
--- 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", {
index e238f653faa8bbf194b2fa7ac32852f70b218a8a..946b618c3ca2a917d66ff7ce8c44f38326bc4461 100644 (file)
@@ -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