@@ -26,6 +26,8 @@ import (
2626 "github.com/openimsdk/tools/discovery"
2727
2828 "github.com/go-redis/redis"
29+ "google.golang.org/protobuf/proto"
30+
2931 "github.com/openimsdk/open-im-server/v3/pkg/common/prommetrics"
3032 "github.com/openimsdk/open-im-server/v3/pkg/common/storage/controller"
3133 "github.com/openimsdk/open-im-server/v3/pkg/msgprocessor"
@@ -37,7 +39,6 @@ import (
3739 "github.com/openimsdk/tools/log"
3840 "github.com/openimsdk/tools/mcontext"
3941 "github.com/openimsdk/tools/utils/stringutil"
40- "google.golang.org/protobuf/proto"
4142)
4243
4344const (
@@ -134,53 +135,48 @@ func (och *OnlineHistoryRedisConsumerHandler) do(ctx context.Context, channelID
134135
135136func (och * OnlineHistoryRedisConsumerHandler ) doSetReadSeq (ctx context.Context , msgs []* ContextMsg ) {
136137
137- var conversationID string
138- var userSeqMap map [string ]int64
138+ // Outer map: conversationID -> (userID -> maxHasReadSeq)
139+ conversationUserSeq := make (map [string ]map [string ]int64 )
140+
139141 for _ , msg := range msgs {
140142 if msg .message .ContentType != constant .HasReadReceipt {
141143 continue
142144 }
143145 var elem sdkws.NotificationElem
144146 if err := json .Unmarshal (msg .message .Content , & elem ); err != nil {
145- log .ZWarn (ctx , "handlerConversationRead Unmarshal NotificationElem msg err " , err , "msg" , msg )
147+ log .ZWarn (ctx , "Unmarshal NotificationElem error " , err , "msg" , msg )
146148 continue
147149 }
148150 var tips sdkws.MarkAsReadTips
149151 if err := json .Unmarshal ([]byte (elem .Detail ), & tips ); err != nil {
150- log .ZWarn (ctx , "handlerConversationRead Unmarshal MarkAsReadTips msg err " , err , "msg" , msg )
152+ log .ZWarn (ctx , "Unmarshal MarkAsReadTips error " , err , "msg" , msg )
151153 continue
152154 }
153- //The conversation ID for each batch of messages processed by the batcher is the same.
154- conversationID = tips .ConversationID
155- if len (tips .Seqs ) > 0 {
156- for _ , seq := range tips .Seqs {
157- if tips .HasReadSeq < seq {
158- tips .HasReadSeq = seq
159- }
160- }
161- clear (tips .Seqs )
162- tips .Seqs = nil
163- }
164- if tips .HasReadSeq < 0 {
155+ if len (tips .ConversationID ) == 0 || tips .HasReadSeq < 0 {
165156 continue
166157 }
167- if userSeqMap == nil {
168- userSeqMap = make (map [string ]int64 )
158+
159+ // Calculate the max seq from tips.Seqs
160+ for _ , seq := range tips .Seqs {
161+ if tips .HasReadSeq < seq {
162+ tips .HasReadSeq = seq
163+ }
169164 }
170165
171- if userSeqMap [tips .MarkAsReadUserID ] > tips .HasReadSeq {
172- continue
166+ if _ , ok := conversationUserSeq [tips .ConversationID ]; ! ok {
167+ conversationUserSeq [tips .ConversationID ] = make (map [string ]int64 )
168+ }
169+ if conversationUserSeq [tips.ConversationID ][tips.MarkAsReadUserID ] < tips .HasReadSeq {
170+ conversationUserSeq [tips.ConversationID ][tips.MarkAsReadUserID ] = tips .HasReadSeq
173171 }
174- userSeqMap [tips .MarkAsReadUserID ] = tips .HasReadSeq
175- }
176- if userSeqMap == nil {
177- return
178- }
179- if len (conversationID ) == 0 {
180- log .ZWarn (ctx , "conversation err" , nil , "conversationID" , conversationID )
181172 }
182- if err := och .msgTransferDatabase .SetHasReadSeqToDB (ctx , conversationID , userSeqMap ); err != nil {
183- log .ZWarn (ctx , "set read seq to db error" , err , "conversationID" , conversationID , "userSeqMap" , userSeqMap )
173+ log .ZInfo (ctx , "doSetReadSeq" , "conversationUserSeq" , conversationUserSeq )
174+
175+ // persist to db
176+ for convID , userSeqMap := range conversationUserSeq {
177+ if err := och .msgTransferDatabase .SetHasReadSeqToDB (ctx , convID , userSeqMap ); err != nil {
178+ log .ZWarn (ctx , "SetHasReadSeqToDB error" , err , "conversationID" , convID , "userSeqMap" , userSeqMap )
179+ }
184180 }
185181
186182}
0 commit comments