diff --git a/constants/attributes.go b/constants/attributes.go index 2fbe174..b20836f 100644 --- a/constants/attributes.go +++ b/constants/attributes.go @@ -8,6 +8,7 @@ const Picture = "picture" const DefaultProfilePicture = "defaultPicture" const ShowBlocked = "show-blocked" const Archived = "archived" +const LastSeenTime = "lastMessageSeenTime" const ProfileTypeV1DefaultPassword = "v1-defaultPassword" const ProfileTypeV1Password = "v1-userPassword" diff --git a/constants/globals.go b/constants/globals.go index 31cf2be..ed300fd 100644 --- a/constants/globals.go +++ b/constants/globals.go @@ -31,3 +31,5 @@ const ( // ConversationNotificationPolicyNever enum for conversation indicating to opt in to never do notifications ConversationNotificationPolicyNever = "ConversationNotificationPolicy.Never" ) + +const DartIso8601 = "2006-01-02T15:04:05.999Z" diff --git a/utils/eventHandler.go b/utils/eventHandler.go index 7fe6fb8..3d3f371 100644 --- a/utils/eventHandler.go +++ b/utils/eventHandler.go @@ -201,6 +201,28 @@ func (eh *EventHandler) handleAppBusEvent(e *event.Event) string { isArchived = event.False } + unread := 0 + lastSeenTimeStr, set := conversationInfo.GetAttribute(attr.LocalScope, attr.ProfileZone, constants2.LastSeenTime) + if set { + lastSeenTime, err := time.Parse(constants2.DartIso8601, lastSeenTimeStr) + if err == nil { + // get last 100 messages and count how many are after the lastSeenTime (100 cus hte ui just shows 99+ after) + messages, err := profile.GetMostRecentMessages(conversationInfo.ID, 0, 0, 100) + if err == nil { + for _, message := range messages { + msgTime, err := time.Parse(time.RFC3339Nano, message.Attr[constants.AttrSentTimestamp]) + if err == nil { + if msgTime.UTC().After(lastSeenTime.UTC()) { + unread++ + } else { + break + } + } + } + } + } + } + groupServer, _ := conversationInfo.GetAttribute(attr.LocalScope, attr.LegacyGroupZone, constants.GroupServer) stateHandle := conversationInfo.Handle @@ -241,7 +263,7 @@ func (eh *EventHandler) handleAppBusEvent(e *event.Event) string { Blocked: blocked, SaveHistory: saveHistory, Messages: count, - Unread: 0, + Unread: unread, LastMessage: strconv.Itoa(getLastMessageTime(lastMessage)), IsGroup: conversationInfo.IsGroup(), GroupServer: groupServer,