Update as_read.go

pull/1443/head
Xinwei Xiong 2 years ago committed by GitHub
parent d91572b887
commit 1334487279
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,12 +1,26 @@
// Package msg provides message handling functionalities for the messaging server. // Copyright © 2023 OpenIM. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package msg package msg
import ( import (
"context" "context"
utils2 "github.com/OpenIMSDK/tools/utils"
"github.com/redis/go-redis/v9" "github.com/redis/go-redis/v9"
// Importing necessary components from OpenIMSDK.
"github.com/OpenIMSDK/protocol/constant" "github.com/OpenIMSDK/protocol/constant"
"github.com/OpenIMSDK/protocol/conversation" "github.com/OpenIMSDK/protocol/conversation"
"github.com/OpenIMSDK/protocol/msg" "github.com/OpenIMSDK/protocol/msg"
@ -15,11 +29,8 @@ import (
"github.com/OpenIMSDK/tools/log" "github.com/OpenIMSDK/tools/log"
) )
// GetConversationsHasReadAndMaxSeq retrieves the read status and maximum sequence number of specified conversations.
func (m *msgServer) GetConversationsHasReadAndMaxSeq(ctx context.Context, req *msg.GetConversationsHasReadAndMaxSeqReq) (resp *msg.GetConversationsHasReadAndMaxSeqResp, err error) { func (m *msgServer) GetConversationsHasReadAndMaxSeq(ctx context.Context, req *msg.GetConversationsHasReadAndMaxSeqReq) (resp *msg.GetConversationsHasReadAndMaxSeqResp, err error) {
var conversationIDs []string var conversationIDs []string
// If no conversation IDs are provided, retrieve them from the local cache.
if len(req.ConversationIDs) == 0 { if len(req.ConversationIDs) == 0 {
conversationIDs, err = m.ConversationLocalCache.GetConversationIDs(ctx, req.UserID) conversationIDs, err = m.ConversationLocalCache.GetConversationIDs(ctx, req.UserID)
if err != nil { if err != nil {
@ -28,71 +39,51 @@ func (m *msgServer) GetConversationsHasReadAndMaxSeq(ctx context.Context, req *m
} else { } else {
conversationIDs = req.ConversationIDs conversationIDs = req.ConversationIDs
} }
// Retrieve the read sequence numbers for the conversations.
hasReadSeqs, err := m.MsgDatabase.GetHasReadSeqs(ctx, req.UserID, conversationIDs) hasReadSeqs, err := m.MsgDatabase.GetHasReadSeqs(ctx, req.UserID, conversationIDs)
if err != nil { if err != nil {
return nil, err return nil, err
} }
// Retrieve conversation details.
conversations, err := m.Conversation.GetConversations(ctx, req.UserID, conversationIDs) conversations, err := m.Conversation.GetConversations(ctx, req.UserID, conversationIDs)
if err != nil { if err != nil {
return nil, err return nil, err
} }
// Prepare a map to store the maximum sequence numbers.
conversationMaxSeqMap := make(map[string]int64) conversationMaxSeqMap := make(map[string]int64)
for _, conversation := range conversations { for _, conversation := range conversations {
if conversation.MaxSeq != 0 { if conversation.MaxSeq != 0 {
conversationMaxSeqMap[conversation.ConversationID] = conversation.MaxSeq conversationMaxSeqMap[conversation.ConversationID] = conversation.MaxSeq
} }
} }
// Retrieve the maximum sequence numbers for the conversations.
maxSeqs, err := m.MsgDatabase.GetMaxSeqs(ctx, conversationIDs) maxSeqs, err := m.MsgDatabase.GetMaxSeqs(ctx, conversationIDs)
if err != nil { if err != nil {
return nil, err return nil, err
} }
// Prepare the response with the sequence information.
resp = &msg.GetConversationsHasReadAndMaxSeqResp{Seqs: make(map[string]*msg.Seqs)} resp = &msg.GetConversationsHasReadAndMaxSeqResp{Seqs: make(map[string]*msg.Seqs)}
for conversationID, maxSeq := range maxSeqs { for conversarionID, maxSeq := range maxSeqs {
resp.Seqs[conversationID] = &msg.Seqs{ resp.Seqs[conversarionID] = &msg.Seqs{
HasReadSeq: hasReadSeqs[conversationID], HasReadSeq: hasReadSeqs[conversarionID],
MaxSeq: maxSeq, MaxSeq: maxSeq,
} }
if v, ok := conversationMaxSeqMap[conversarionID]; ok {
// Override the maximum sequence number if available in the map. resp.Seqs[conversarionID].MaxSeq = v
if v, ok := conversationMaxSeqMap[conversationID]; ok {
resp.Seqs[conversationID].MaxSeq = v
} }
} }
return resp, nil return resp, nil
} }
// SetConversationHasReadSeq updates the read sequence number for a specific conversation.
func (m *msgServer) SetConversationHasReadSeq( func (m *msgServer) SetConversationHasReadSeq(
ctx context.Context, ctx context.Context,
req *msg.SetConversationHasReadSeqReq, req *msg.SetConversationHasReadSeqReq,
) (resp *msg.SetConversationHasReadSeqResp, err error) { ) (resp *msg.SetConversationHasReadSeqResp, err error) {
// Retrieve the maximum sequence number for the conversation.
maxSeq, err := m.MsgDatabase.GetMaxSeq(ctx, req.ConversationID) maxSeq, err := m.MsgDatabase.GetMaxSeq(ctx, req.ConversationID)
if err != nil { if err != nil {
return return
} }
// Validate the provided read sequence number.
if req.HasReadSeq > maxSeq { if req.HasReadSeq > maxSeq {
return nil, errs.ErrArgs.Wrap("hasReadSeq must not be bigger than maxSeq") return nil, errs.ErrArgs.Wrap("hasReadSeq must not be bigger than maxSeq")
} }
// Update the read sequence number in the database.
if err := m.MsgDatabase.SetHasReadSeq(ctx, req.UserID, req.ConversationID, req.HasReadSeq); err != nil { if err := m.MsgDatabase.SetHasReadSeq(ctx, req.UserID, req.ConversationID, req.HasReadSeq); err != nil {
return nil, err return nil, err
} }
// Send a notification for the read status update.
if err = m.sendMarkAsReadNotification(ctx, req.ConversationID, constant.SingleChatType, req.UserID, if err = m.sendMarkAsReadNotification(ctx, req.ConversationID, constant.SingleChatType, req.UserID,
req.UserID, nil, req.HasReadSeq); err != nil { req.UserID, nil, req.HasReadSeq); err != nil {
return return
@ -100,52 +91,35 @@ func (m *msgServer) SetConversationHasReadSeq(
return &msg.SetConversationHasReadSeqResp{}, nil return &msg.SetConversationHasReadSeqResp{}, nil
} }
// MarkMsgsAsRead marks specific messages in a conversation as read.
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) {
// Ensure that the sequence numbers are provided.
// Ensure that the sequence numbers are provided.
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")
} }
// Retrieve the maximum sequence number for the conversation.
maxSeq, err := m.MsgDatabase.GetMaxSeq(ctx, req.ConversationID) maxSeq, err := m.MsgDatabase.GetMaxSeq(ctx, req.ConversationID)
if err != nil { if err != nil {
return return
} }
// Determine the highest sequence number from the request.
hasReadSeq := req.Seqs[len(req.Seqs)-1] hasReadSeq := req.Seqs[len(req.Seqs)-1]
if hasReadSeq > maxSeq { if hasReadSeq > maxSeq {
return nil, errs.ErrArgs.Wrap("hasReadSeq must not be bigger than maxSeq") return nil, errs.ErrArgs.Wrap("hasReadSeq must not be bigger than maxSeq")
} }
// Retrieve conversation details.
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 return
} }
// Mark the specified messages as read in the database.
if err = m.MsgDatabase.MarkSingleChatMsgsAsRead(ctx, req.UserID, req.ConversationID, req.Seqs); err != nil { if err = m.MsgDatabase.MarkSingleChatMsgsAsRead(ctx, req.UserID, req.ConversationID, req.Seqs); err != nil {
return return
} }
// Get the current read sequence number.
currentHasReadSeq, err := m.MsgDatabase.GetHasReadSeq(ctx, req.UserID, req.ConversationID) currentHasReadSeq, err := m.MsgDatabase.GetHasReadSeq(ctx, req.UserID, req.ConversationID)
if err != nil && errs.Unwrap(err) != redis.Nil { if err != nil && errs.Unwrap(err) != redis.Nil {
return return
} }
// Update the read sequence number if the new value is greater.
if hasReadSeq > currentHasReadSeq { if hasReadSeq > currentHasReadSeq {
err = m.MsgDatabase.SetHasReadSeq(ctx, req.UserID, req.ConversationID, hasReadSeq) err = m.MsgDatabase.SetHasReadSeq(ctx, req.UserID, req.ConversationID, hasReadSeq)
if err != nil { if err != nil {
return return
} }
} }
// Send a notification to indicate that messages have been marked as read.
if err = m.sendMarkAsReadNotification(ctx, req.ConversationID, conversation.ConversationType, req.UserID, if err = m.sendMarkAsReadNotification(ctx, req.ConversationID, conversation.ConversationType, req.UserID,
m.conversationAndGetRecvID(conversation, req.UserID), req.Seqs, hasReadSeq); err != nil { m.conversationAndGetRecvID(conversation, req.UserID), req.Seqs, hasReadSeq); err != nil {
return return
@ -153,24 +127,17 @@ func (m *msgServer) MarkMsgsAsRead(ctx context.Context, req *msg.MarkMsgsAsReadR
return &msg.MarkMsgsAsReadResp{}, nil return &msg.MarkMsgsAsReadResp{}, nil
} }
// MarkConversationAsRead marks an entire conversation as read.
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) {
// Retrieve conversation details.
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
} }
// Get the current read sequence number.
hasReadSeq, err := m.MsgDatabase.GetHasReadSeq(ctx, req.UserID, req.ConversationID) hasReadSeq, err := m.MsgDatabase.GetHasReadSeq(ctx, req.UserID, req.ConversationID)
if err != nil && errs.Unwrap(err) != redis.Nil { if err != nil && errs.Unwrap(err) != redis.Nil {
return nil, err return nil, err
} }
// Generate the sequence numbers to be marked as read.
seqs := generateSeqs(hasReadSeq, req) seqs := generateSeqs(hasReadSeq, req)
// Update the read status if there are new sequences to mark or if the hasReadSeq is greater.
if len(seqs) > 0 || req.HasReadSeq > hasReadSeq { if len(seqs) > 0 || req.HasReadSeq > hasReadSeq {
err = m.updateReadStatus(ctx, req, conversation, seqs, hasReadSeq) err = m.updateReadStatus(ctx, req, conversation, seqs, hasReadSeq)
if err != nil { if err != nil {
@ -180,7 +147,6 @@ func (m *msgServer) MarkConversationAsRead(ctx context.Context, req *msg.MarkCon
return &msg.MarkConversationAsReadResp{}, nil return &msg.MarkConversationAsReadResp{}, nil
} }
// generateSeqs creates a slice of sequence numbers that are greater than the provided hasReadSeq.
func generateSeqs(hasReadSeq int64, req *msg.MarkConversationAsReadReq) []int64 { func generateSeqs(hasReadSeq int64, req *msg.MarkConversationAsReadReq) []int64 {
var seqs []int64 var seqs []int64
for _, val := range req.Seqs { for _, val := range req.Seqs {
@ -191,9 +157,7 @@ func generateSeqs(hasReadSeq int64, req *msg.MarkConversationAsReadReq) []int64
return seqs return seqs
} }
// updateReadStatus updates the read status for messages in a conversation.
func (m *msgServer) updateReadStatus(ctx context.Context, req *msg.MarkConversationAsReadReq, conversation *conversation.Conversation, seqs []int64, hasReadSeq int64) error { func (m *msgServer) updateReadStatus(ctx context.Context, req *msg.MarkConversationAsReadReq, conversation *conversation.Conversation, seqs []int64, hasReadSeq int64) error {
// Special handling for single chat type conversations.
if conversation.ConversationType == constant.SingleChatType && len(seqs) > 0 { if conversation.ConversationType == constant.SingleChatType && len(seqs) > 0 {
log.ZDebug(ctx, "MarkConversationAsRead", "seqs", seqs, "conversationID", req.ConversationID) log.ZDebug(ctx, "MarkConversationAsRead", "seqs", seqs, "conversationID", req.ConversationID)
if err := m.MsgDatabase.MarkSingleChatMsgsAsRead(ctx, req.UserID, req.ConversationID, seqs); err != nil { if err := m.MsgDatabase.MarkSingleChatMsgsAsRead(ctx, req.UserID, req.ConversationID, seqs); err != nil {
@ -201,25 +165,20 @@ func (m *msgServer) updateReadStatus(ctx context.Context, req *msg.MarkConversat
} }
} }
// Update the hasReadSeq if the new value is greater.
if req.HasReadSeq > hasReadSeq { if req.HasReadSeq > hasReadSeq {
if err := m.MsgDatabase.SetHasReadSeq(ctx, req.UserID, req.ConversationID, req.HasReadSeq); err != nil { if err := m.MsgDatabase.SetHasReadSeq(ctx, req.UserID, req.ConversationID, req.HasReadSeq); err != nil {
return err return err
} }
} }
// Determine the receiver ID for the read receipt.
recvID := m.conversationAndGetRecvID(conversation, req.UserID) recvID := m.conversationAndGetRecvID(conversation, req.UserID)
// Adjust the receiver ID for specific conversation types.
if conversation.ConversationType == constant.SuperGroupChatType || conversation.ConversationType == constant.NotificationChatType { if conversation.ConversationType == constant.SuperGroupChatType || conversation.ConversationType == constant.NotificationChatType {
recvID = req.UserID recvID = req.UserID
} }
// Send a notification to indicate the read status update.
return m.sendMarkAsReadNotification(ctx, req.ConversationID, conversation.ConversationType, req.UserID, recvID, seqs, req.HasReadSeq) return m.sendMarkAsReadNotification(ctx, req.ConversationID, conversation.ConversationType, req.UserID, recvID, seqs, req.HasReadSeq)
} }
// sendMarkAsReadNotification sends a notification about the read status update.
func (m *msgServer) sendMarkAsReadNotification( func (m *msgServer) sendMarkAsReadNotification(
ctx context.Context, ctx context.Context,
conversationID string, conversationID string,
@ -228,14 +187,12 @@ func (m *msgServer) sendMarkAsReadNotification(
seqs []int64, seqs []int64,
hasReadSeq int64, hasReadSeq int64,
) error { ) error {
// Construct the read receipt notification.
tips := &sdkws.MarkAsReadTips{ tips := &sdkws.MarkAsReadTips{
MarkAsReadUserID: sendID, MarkAsReadUserID: sendID,
ConversationID: conversationID, ConversationID: conversationID,
Seqs: seqs, Seqs: seqs,
HasReadSeq: hasReadSeq, HasReadSeq: hasReadSeq,
} }
// Send the notification with session type information.
err := m.notificationSender.NotificationWithSesstionType(ctx, sendID, recvID, constant.HasReadReceipt, sessionType, tips) err := m.notificationSender.NotificationWithSesstionType(ctx, sendID, recvID, constant.HasReadReceipt, sessionType, tips)
if err != nil { if err != nil {
log.ZWarn(ctx, "send has read Receipt err", err) log.ZWarn(ctx, "send has read Receipt err", err)

Loading…
Cancel
Save