Skip to content

Commit

Permalink
Added better conversation list sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
markqvist committed Sep 20, 2023
1 parent ce9f9f4 commit 458ba51
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 10 deletions.
53 changes: 44 additions & 9 deletions sbapp/sideband/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,8 +605,11 @@ def delete_message(self, message_hash):
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)
Expand Down Expand Up @@ -883,12 +886,32 @@ def _db_setpersistent(self, prop, val):
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()
Expand Down Expand Up @@ -924,13 +947,20 @@ def _db_conversations(self):
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()
Expand Down Expand Up @@ -982,6 +1012,7 @@ def _db_conversation(self, context_dest):
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):
Expand Down Expand Up @@ -1019,7 +1050,7 @@ def _db_create_conversation(self, context_dest, name = None, trust = False):

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()
Expand Down Expand Up @@ -2000,9 +2031,11 @@ def lxm_ingest_uri(self, uri):
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)
Expand All @@ -2023,14 +2056,16 @@ def lxm_ingest(self, message, originator = False):

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():
Expand Down
7 changes: 6 additions & 1 deletion sbapp/ui/conversations.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,13 @@ def update_widget(self):
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
Expand All @@ -112,7 +114,7 @@ def x():
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)
Expand Down Expand Up @@ -282,12 +284,15 @@ def x(sender):
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):
Expand Down

0 comments on commit 458ba51

Please sign in to comment.