Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement marking chat as read/unread #691

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading