From 458ba514453fcdb5c3ff05200f66fe7147ec4071 Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Wed, 20 Sep 2023 21:41:26 +0200 Subject: [PATCH] Added better conversation list sorting --- sbapp/sideband/core.py | 53 ++++++++++++++++++++++++++++++++------- sbapp/ui/conversations.py | 7 +++++- 2 files changed, 50 insertions(+), 10 deletions(-) diff --git a/sbapp/sideband/core.py b/sbapp/sideband/core.py index d8db76d..8d6bec4 100644 --- a/sbapp/sideband/core.py +++ b/sbapp/sideband/core.py @@ -605,8 +605,11 @@ class SidebandCore(): def read_conversation(self, context_dest): self._db_conversation_set_unread(context_dest, False) - def unread_conversation(self, context_dest): - self._db_conversation_set_unread(context_dest, True) + def unread_conversation(self, context_dest, tx=False): + self._db_conversation_set_unread(context_dest, True, tx=tx) + + def txtime_conversation(self, context_dest): + self._db_conversation_update_txtime(context_dest) def trusted_conversation(self, context_dest): self._db_conversation_set_trusted(context_dest, True) @@ -883,12 +886,32 @@ class SidebandCore(): RNS.log("An error occurred during persistent setstate database operation: "+str(e), RNS.LOG_ERROR) self.db = None - def _db_conversation_set_unread(self, context_dest, unread): + def _db_conversation_update_txtime(self, context_dest): + db = self.__db_connect() + dbc = db.cursor() + + query = "UPDATE conv set last_tx = ? where dest_context = ?" + data = (time.time(), context_dest) + + dbc.execute(query, data) + result = dbc.fetchall() + db.commit() + + def _db_conversation_set_unread(self, context_dest, unread, tx = False): db = self.__db_connect() dbc = db.cursor() - query = "UPDATE conv set unread = ? where dest_context = ?" - data = (unread, context_dest) + if unread: + if tx: + query = "UPDATE conv set unread = ?, last_tx = ? where dest_context = ?" + data = (unread, time.time(), context_dest) + else: + query = "UPDATE conv set unread = ?, last_rx = ? where dest_context = ?" + data = (unread, time.time(), context_dest) + else: + query = "UPDATE conv set unread = ? where dest_context = ?" + data = (unread, context_dest) + dbc.execute(query, data) result = dbc.fetchall() db.commit() @@ -924,13 +947,20 @@ class SidebandCore(): else: convs = [] for entry in result: + last_rx = entry[1] + last_tx = entry[2] + last_activity = max(last_rx, last_tx) + conv = { "dest": entry[0], "unread": entry[3], + "last_rx": last_rx, + "last_tx": last_tx, + "last_activity": last_activity, } convs.append(conv) - return convs + return sorted(convs, key=lambda c: c["last_activity"], reverse=True) def _db_announces(self): db = self.__db_connect() @@ -982,6 +1012,7 @@ class SidebandCore(): conv["trust"] = c[5] conv["name"] = c[6].decode("utf-8") conv["data"] = msgpack.unpackb(c[7]) + conv["last_activity"] = max(c[1], c[2]) return conv def _db_clear_conversation(self, context_dest): @@ -1019,7 +1050,7 @@ class SidebandCore(): def_name = "".encode("utf-8") query = "INSERT INTO conv (dest_context, last_tx, last_rx, unread, type, trust, name, data) values (?, ?, ?, ?, ?, ?, ?, ?)" - data = (context_dest, 0, 0, 0, SidebandCore.CONV_P2P, 0, def_name, msgpack.packb(None)) + data = (context_dest, 0, time.time(), 0, SidebandCore.CONV_P2P, 0, def_name, msgpack.packb(None)) dbc.execute(query, data) db.commit() @@ -2000,9 +2031,11 @@ class SidebandCore(): def lxm_ingest(self, message, originator = False): should_notify = False is_trusted = False + unread_reason_tx = False if originator: context_dest = message.destination_hash + unread_reason_tx = True else: context_dest = message.source_hash is_trusted = self.is_trusted(context_dest) @@ -2023,14 +2056,16 @@ class SidebandCore(): if self.gui_display() == "messages_screen": if self.gui_conversation() != context_dest: - self.unread_conversation(context_dest) + self.unread_conversation(context_dest, tx=unread_reason_tx) self.setstate("app.flags.unread_conversations", True) else: + self.txtime_conversation(context_dest) + self.setstate("wants.viewupdate.conversations", True) if self.gui_foreground(): RNS.log("Squelching notification since GUI is in foreground", RNS.LOG_DEBUG) should_notify = False else: - self.unread_conversation(context_dest) + self.unread_conversation(context_dest, tx=unread_reason_tx) self.setstate("app.flags.unread_conversations", True) if RNS.vendor.platformutils.is_android(): diff --git a/sbapp/ui/conversations.py b/sbapp/ui/conversations.py index 039c498..8779176 100644 --- a/sbapp/ui/conversations.py +++ b/sbapp/ui/conversations.py @@ -97,11 +97,13 @@ class Conversations(): for conv in self.context_dests: context_dest = conv["dest"] unread = conv["unread"] + last_activity = conv["last_activity"] if not context_dest in self.added_item_dests: iconl = IconLeftWidget(icon=self.trust_icon(context_dest, unread), on_release=self.app.conversation_action) item = OneLineAvatarIconListItem(text=self.app.sideband.peer_display_name(context_dest), on_release=self.app.conversation_action) item.add_widget(iconl) + item.last_activity = last_activity item.iconl = iconl item.sb_uid = context_dest item.sb_unread = unread @@ -112,7 +114,7 @@ class Conversations(): t_s = time.time() dest = self.conversation_dropdown.context_dest try: - disp_name = self.app.sideband.raw_display_name(dest) + disp_name = self.app.sideband.raw_display_name(dest)+" "+str(conv["last_activity"]) is_trusted = self.app.sideband.is_trusted(dest) yes_button = MDRectangleFlatButton(text="Save",font_size=dp(18), theme_text_color="Custom", line_color=self.app.color_accept, text_color=self.app.color_accept) @@ -282,12 +284,15 @@ class Conversations(): if w.sb_uid == context_dest: disp_name = self.app.sideband.peer_display_name(context_dest) trust_icon = self.trust_icon(context_dest, unread) + w.last_activity = last_activity if w.iconl.icon != trust_icon: w.iconl.icon = trust_icon w.sb_unread = unread if w.text != disp_name: w.text = disp_name + self.list.children.sort(key=lambda w: w.last_activity) + RNS.log("Updated conversation list widgets in "+RNS.prettytime(time.time()-us), RNS.LOG_DEBUG) def get_widget(self):