show unread conversation indicators

This commit is contained in:
liamcottle 2024-06-01 19:45:19 +12:00
parent 0795664d87
commit 45eeaa5ac8
2 changed files with 46 additions and 6 deletions

View file

@ -89,6 +89,7 @@
</svg>
</span>
<span class="my-auto">Messages</span>
<span v-if="unreadConversationsCount > 0" class="my-auto ml-auto mr-2">{{ unreadConversationsCount }}</span>
</button>
</li>
@ -257,10 +258,13 @@
</svg>
</div>
</div>
<div>
<div class="text-gray-900">{{ conversation.name }}</div>
<div class="mr-auto">
<div class="text-gray-900" :class="{ 'font-semibold': conversation.is_unread }">{{ conversation.name }}</div>
<div class="text-gray-500 text-sm">{{ formatTimeAgo(conversation.updated_at) }}</div>
</div>
<div v-if="conversation.is_unread" class="my-auto ml-2 mr-2">
<div class="bg-blue-500 rounded-full p-1"></div>
</div>
</div>
</div>
<div v-else class="mx-auto my-auto text-center leading-5">
@ -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

28
web.py
View file

@ -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: