fix: wrong single message read state. (#1443)

* fix: wrong single message read state.

* Update as_read.go

* Update as_read.go

* Update as_read.go

---------

Co-authored-by: Xinwei Xiong <3293172751@qq.com>
pull/1478/head
Gordon 1 year ago committed by GitHub
parent 3cecbbc69a
commit f935d36715
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -17,9 +17,12 @@ package msg
import ( import (
"context" "context"
utils2 "github.com/OpenIMSDK/tools/utils"
"github.com/redis/go-redis/v9" "github.com/redis/go-redis/v9"
"github.com/OpenIMSDK/protocol/constant" "github.com/OpenIMSDK/protocol/constant"
"github.com/OpenIMSDK/protocol/conversation"
"github.com/OpenIMSDK/protocol/msg" "github.com/OpenIMSDK/protocol/msg"
"github.com/OpenIMSDK/protocol/sdkws" "github.com/OpenIMSDK/protocol/sdkws"
"github.com/OpenIMSDK/tools/errs" "github.com/OpenIMSDK/tools/errs"
@ -88,10 +91,7 @@ func (m *msgServer) SetConversationHasReadSeq(
return &msg.SetConversationHasReadSeqResp{}, nil return &msg.SetConversationHasReadSeqResp{}, nil
} }
func (m *msgServer) MarkMsgsAsRead( func (m *msgServer) MarkMsgsAsRead(ctx context.Context, req *msg.MarkMsgsAsReadReq) (resp *msg.MarkMsgsAsReadResp, err error) {
ctx context.Context,
req *msg.MarkMsgsAsReadReq,
) (resp *msg.MarkMsgsAsReadResp, err error) {
if len(req.Seqs) < 1 { if len(req.Seqs) < 1 {
return nil, errs.ErrArgs.Wrap("seqs must not be empty") return nil, errs.ErrArgs.Wrap("seqs must not be empty")
} }
@ -127,10 +127,7 @@ func (m *msgServer) MarkMsgsAsRead(
return &msg.MarkMsgsAsReadResp{}, nil return &msg.MarkMsgsAsReadResp{}, nil
} }
func (m *msgServer) MarkConversationAsRead( func (m *msgServer) MarkConversationAsRead(ctx context.Context, req *msg.MarkConversationAsReadReq) (resp *msg.MarkConversationAsReadResp, err error) {
ctx context.Context,
req *msg.MarkConversationAsReadReq,
) (resp *msg.MarkConversationAsReadResp, err error) {
conversation, err := m.Conversation.GetConversation(ctx, req.UserID, req.ConversationID) conversation, err := m.Conversation.GetConversation(ctx, req.UserID, req.ConversationID)
if err != nil { if err != nil {
return nil, err return nil, err
@ -139,49 +136,47 @@ func (m *msgServer) MarkConversationAsRead(
if err != nil && errs.Unwrap(err) != redis.Nil { if err != nil && errs.Unwrap(err) != redis.Nil {
return nil, err return nil, err
} }
var seqs []int64 seqs := generateSeqs(hasReadSeq, req)
log.ZDebug(ctx, "MarkConversationAsRead", "hasReadSeq", hasReadSeq, if len(seqs) > 0 || req.HasReadSeq > hasReadSeq {
"req.HasReadSeq", req.HasReadSeq) err = m.updateReadStatus(ctx, req, conversation, seqs, hasReadSeq)
if conversation.ConversationType == constant.SingleChatType { if err != nil {
for i := hasReadSeq + 1; i <= req.HasReadSeq; i++ { return nil, err
seqs = append(seqs, i)
} }
}
return &msg.MarkConversationAsReadResp{}, nil
}
if len(seqs) > 0 { func generateSeqs(hasReadSeq int64, req *msg.MarkConversationAsReadReq) []int64 {
log.ZDebug(ctx, "MarkConversationAsRead", "seqs", seqs, "conversationID", req.ConversationID) var seqs []int64
if err = m.MsgDatabase.MarkSingleChatMsgsAsRead(ctx, req.UserID, req.ConversationID, seqs); err != nil { for _, val := range req.Seqs {
return nil, err if val > hasReadSeq && !utils2.Contain(val, seqs...) {
} seqs = append(seqs, val)
}
if req.HasReadSeq > hasReadSeq {
err = m.MsgDatabase.SetHasReadSeq(ctx, req.UserID, req.ConversationID, req.HasReadSeq)
if err != nil {
return nil, err
}
hasReadSeq = req.HasReadSeq
}
if err = m.sendMarkAsReadNotification(ctx, req.ConversationID, conversation.ConversationType, req.UserID,
m.conversationAndGetRecvID(conversation, req.UserID), seqs, hasReadSeq); err != nil {
return nil, err
} }
} else if conversation.ConversationType == constant.SuperGroupChatType || }
conversation.ConversationType == constant.NotificationChatType { return seqs
if req.HasReadSeq > hasReadSeq { }
err = m.MsgDatabase.SetHasReadSeq(ctx, req.UserID, req.ConversationID, req.HasReadSeq)
if err != nil { func (m *msgServer) updateReadStatus(ctx context.Context, req *msg.MarkConversationAsReadReq, conversation *conversation.Conversation, seqs []int64, hasReadSeq int64) error {
return nil, err if conversation.ConversationType == constant.SingleChatType && len(seqs) > 0 {
} log.ZDebug(ctx, "MarkConversationAsRead", "seqs", seqs, "conversationID", req.ConversationID)
hasReadSeq = req.HasReadSeq if err := m.MsgDatabase.MarkSingleChatMsgsAsRead(ctx, req.UserID, req.ConversationID, seqs); err != nil {
return err
} }
if err = m.sendMarkAsReadNotification(ctx, req.ConversationID, constant.SingleChatType, req.UserID, }
req.UserID, seqs, hasReadSeq); err != nil {
return nil, err if req.HasReadSeq > hasReadSeq {
if err := m.MsgDatabase.SetHasReadSeq(ctx, req.UserID, req.ConversationID, req.HasReadSeq); err != nil {
return err
} }
}
recvID := m.conversationAndGetRecvID(conversation, req.UserID)
if conversation.ConversationType == constant.SuperGroupChatType || conversation.ConversationType == constant.NotificationChatType {
recvID = req.UserID
} }
return &msg.MarkConversationAsReadResp{}, nil return m.sendMarkAsReadNotification(ctx, req.ConversationID, conversation.ConversationType, req.UserID, recvID, seqs, req.HasReadSeq)
} }
func (m *msgServer) sendMarkAsReadNotification( func (m *msgServer) sendMarkAsReadNotification(

Loading…
Cancel
Save