mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-12-03 19:02:31 +08:00
Update as_read.go
This commit is contained in:
parent
e0e945e3fc
commit
d91572b887
@ -1,34 +1,25 @@
|
|||||||
// Copyright © 2023 OpenIM. All rights reserved.
|
// Package msg provides message handling functionalities for the messaging server.
|
||||||
//
|
|
||||||
// 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/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"
|
||||||
"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 {
|
||||||
@ -37,51 +28,71 @@ 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 conversarionID, maxSeq := range maxSeqs {
|
for conversationID, maxSeq := range maxSeqs {
|
||||||
resp.Seqs[conversarionID] = &msg.Seqs{
|
resp.Seqs[conversationID] = &msg.Seqs{
|
||||||
HasReadSeq: hasReadSeqs[conversarionID],
|
HasReadSeq: hasReadSeqs[conversationID],
|
||||||
MaxSeq: maxSeq,
|
MaxSeq: maxSeq,
|
||||||
}
|
}
|
||||||
if v, ok := conversationMaxSeqMap[conversarionID]; ok {
|
|
||||||
resp.Seqs[conversarionID].MaxSeq = v
|
// Override the maximum sequence number if available in the map.
|
||||||
|
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
|
||||||
@ -89,35 +100,52 @@ 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
|
||||||
@ -125,17 +153,24 @@ 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 {
|
||||||
@ -145,6 +180,7 @@ 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 {
|
||||||
@ -155,7 +191,9 @@ func generateSeqs(hasReadSeq int64, req *msg.MarkConversationAsReadReq) []int64
|
|||||||
return seqs
|
return seqs
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *msgServer) updateReadStatus(ctx context.Context, req *msg.MarkConversationAsReadReq, conversation *Conversation, seqs []int64, hasReadSeq int64) error {
|
// 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 {
|
||||||
|
// 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 {
|
||||||
@ -163,20 +201,25 @@ 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,
|
||||||
@ -185,12 +228,14 @@ 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…
x
Reference in New Issue
Block a user