diff --git a/public/index.html b/public/index.html index 4a60792..d0429a1 100644 --- a/public/index.html +++ b/public/index.html @@ -89,6 +89,7 @@ Messages + {{ unreadConversationsCount }} @@ -257,10 +258,13 @@ -
-
{{ conversation.name }}
+
+
{{ conversation.name }}
{{ formatTimeAgo(conversation.updated_at) }}
+
+
+
@@ -2048,7 +2052,7 @@ this.onPeerClick(conversation); // mark conversation as read - this.markConversationAsRead(conversation.destination_hash); + this.markConversationAsRead(conversation); }, parseSeconds: function(secondsToFormat) { @@ -2198,11 +2202,14 @@ await this.loadLxmfMessages(this.selectedPeer.destination_hash); }, - async markConversationAsRead(destinationHash) { + async markConversationAsRead(conversation) { - // mark conversation as read + // manually mark conversation read in memory to avoid delay updating ui + conversation.is_unread = false; + + // mark conversation as read on server try { - await window.axios.get(`/api/v1/lxmf/conversations/${this.selectedPeer.destination_hash}/mark-as-read`); + await window.axios.get(`/api/v1/lxmf/conversations/${conversation.destination_hash}/mark-as-read`); } catch(e) { // do nothing if failed to mark as read console.log(e); @@ -2502,6 +2509,11 @@ return matchesName || matchesDestinationHash; }); }, + unreadConversationsCount() { + return this.conversations.filter((conversation) => { + return conversation.is_unread; + }).length; + }, canSendMessage() { // can't send if empty message diff --git a/web.py b/web.py index 5c8e95e..8cb48de 100644 --- a/web.py +++ b/web.py @@ -846,6 +846,7 @@ class ReticulumWebChat: conversations.append({ "name": self.get_lxmf_conversation_name(other_user_hash), "destination_hash": other_user_hash, + "is_unread": self.is_lxmf_conversation_unread(other_user_hash), "updated_at": updated_at, }) @@ -1486,6 +1487,33 @@ class ReticulumWebChat: return "Unknown" + # returns true if the conversation has messages newer than the last read at timestamp + def is_lxmf_conversation_unread(self, destination_hash): + + # get lxmf.delivery announce from database for the provided destination hash + lxmf_conversation_read_state = (database.LxmfConversationReadState.select() + .where(database.LxmfConversationReadState.destination_hash == destination_hash) + .get_or_none()) + + # user has never read this conversation, so it's unread + if lxmf_conversation_read_state is None: + return True + + # get most recent incoming message from destination hash + latest_incoming_lxmf_message = (database.LxmfMessage.select() + .where(database.LxmfMessage.source_hash == destination_hash) + .order_by(database.LxmfMessage.created_at.desc()) + .get_or_none()) + + # there's no incoming message, so it can't be unread + if latest_incoming_lxmf_message is None: + return False + + # conversation is unread if last read at is before the latest incoming message creation date + conversation_last_read_at = datetime.strptime(lxmf_conversation_read_state.last_read_at, "%Y-%m-%d %H:%M:%S.%f%z") + conversation_latest_message_at = datetime.strptime(latest_incoming_lxmf_message.created_at, "%Y-%m-%d %H:%M:%S.%f%z") + return conversation_last_read_at < conversation_latest_message_at + # class to manage config stored in database class Config: