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