Skip to content

Commit

Permalink
Implement marking chat as read/unread
Browse files Browse the repository at this point in the history
  • Loading branch information
masewo committed Nov 5, 2024
1 parent f98aea1 commit bc4eafb
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions appstate/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"crypto/sha256"
"encoding/json"
"fmt"
"go.mau.fi/whatsmeow/proto/waCommon"
"go.mau.fi/whatsmeow/proto/waSyncAction"
"time"

"google.golang.org/protobuf/proto"
Expand Down Expand Up @@ -123,6 +125,42 @@ func BuildArchive(target types.JID, archive bool, lastMessageTimestamp time.Time
return result
}

// BuildMarkChatAsRead builds an app state patch for marking a chat as read or unread.
//
// The last message timestamp and last message key are optional and can be set to zero values (`time.Time{}` and `nil`).
// It is recommended to set them.
//
// The last message key should contain RemoteJID, FromMe and ID. In case of a group chat also Participant.
func BuildMarkChatAsRead(target types.JID, read bool, lastMessageTimestamp time.Time, lastMessageKey *waCommon.MessageKey) PatchInfo {
if lastMessageTimestamp.IsZero() {
lastMessageTimestamp = time.Now()
}
mutationInfo := MutationInfo{
Index: []string{IndexMarkChatAsRead, target.String()},
Version: 3,
Value: &waSyncAction.SyncActionValue{
MarkChatAsReadAction: &waSyncAction.MarkChatAsReadAction{
Read: proto.Bool(read),
MessageRange: &waSyncAction.SyncActionMessageRange{
LastMessageTimestamp: proto.Int64(lastMessageTimestamp.Unix()),
},
},
},
}

if lastMessageKey != nil {
mutationInfo.Value.MarkChatAsReadAction.MessageRange.Messages = []*waSyncAction.SyncActionMessage{{
Key: lastMessageKey,
Timestamp: proto.Int64(lastMessageTimestamp.Unix()),
}}
}

return PatchInfo{
Type: WAPatchRegularLow,
Mutations: []MutationInfo{mutationInfo},
}
}

func newLabelChatMutation(target types.JID, labelID string, labeled bool) MutationInfo {
return MutationInfo{
Index: []string{IndexLabelAssociationChat, labelID, target.String()},
Expand Down

0 comments on commit bc4eafb

Please sign in to comment.