mirror of
				https://github.com/openimsdk/open-im-server.git
				synced 2025-11-04 19:32:17 +08:00 
			
		
		
		
	* Script Refactoring * Script Refactoring * Script Refactoring * Script Refactoring * Script Refactoring * Script Refactoring * Script Refactoring * Script Refactoring * Script Refactoring * Script Refactoring * Script Refactoring * Script Refactoring * feat: add code lint * feat: add code lint * Script Refactoring * Script Refactoring * Script Refactoring * Script Refactoring * Script Refactoring * Script Refactoring * Script Refactoring * Script Refactoring * Script Refactoring * Script Refactoring * Script Refactoring * Script Refactoring * Script Refactoring * Script Refactoring * Script Refactoring * Script Refactoring * Script Refactoring * feat: code format * Script Refactoring * Script Refactoring * Script Refactoring * Adjust MinIO configuration settings * Adjust configuration settings * Adjust configuration settings * refactor: config change. * refactor: webhooks update. * Adjust configuration settings * refactor: webhooks update. * Adjust configuration settings * Adjust configuration settings * Adjust configuration settings * feat: s3 api addr * refactor: webhooks update. * Adjust configuration settings * Adjust configuration settings * Adjust configuration settings * Adjust configuration settings * Adjust configuration settings * Adjust configuration settings * Adjust configuration settings * refactor: webhooks update. * refactor: kafka update. * Simplify the Docker Compose configuration, remove unnecessary environment variables, and eliminate the gateway service. * refactor: kafka update. * refactor: kafka update. * Simplify the Docker Compose configuration, remove unnecessary environment variables, and eliminate the gateway service. * Simplify the Docker Compose configuration, remove unnecessary environment variables, and eliminate the gateway service. * Windows can compile and run. * Windows can compile and run. * refactor: kafka update. * feat: msg cache split * refactor: webhooks update * refactor: webhooks update * refactor: friends update * refactor: group update * refactor: third update * refactor: api update * refactor: crontab update * refactor: msggateway update * mage * mage * refactor: all module update. * check * refactor: all module update. * load config * load config * load config * load config * refactor: all module update. * refactor: all module update. * refactor: all module update. * refactor: all module update. * refactor: all module update. * Optimize Docker configuration and script. * refactor: all module update. * Optimize Docker configuration and script. * Optimize Docker configuration and script. * Optimize Docker configuration and script. * refactor: all module update. * Optimize Docker configuration and script. * refactor: all module update. * refactor: all module update. * Optimize Docker configuration and script. * Optimize Docker configuration and script. * Optimize Docker configuration and script. * Optimize Docker configuration and script. * Optimize Docker configuration and script. * Optimize Docker configuration and script. * update tools * update tools * Optimize Docker configuration and script. * Optimize Docker configuration and script. * Optimize Docker configuration and script. * Optimize Docker configuration and script. * Optimize Docker configuration and script. * Optimize Docker configuration and script. * Optimize Docker configuration and script. * Optimize Docker configuration and script. * Optimize Docker configuration and script. * Optimize Docker configuration and script. * Optimize Docker configuration and script. * update protocol * Optimize Docker configuration and script. * Optimize Docker configuration and script. * refactor: all module update. * Optimize Docker configuration and script. * Optimize Docker configuration and script. * Optimize Docker configuration and script. * Optimize Docker configuration and script. * refactor: api remove token auth by redis directly. * Code Refactoring * refactor: websocket auth change to call rpc of auth. * refactor: kick online user and remove token change to call auth rpc. * refactor: kick online user and remove token change to call auth rpc. * refactor: remove msggateway redis. * refactor: cmd update. * refactor: cmd update. * refactor: cmd update. * refactor: cmd update. * refactor: cmd update. * refactor: cmd update. * refactor: cmd update. * refactor: cmd update. * refactor: cmd update. * refactor: cmd update. * refactor: cmd update. * refactor: cmd update. * refactor: cmd update. * refactor: cmd update. * refactor: cmd update. * refactor: cmd update. * refactor: cmd update. * refactor: cmd update. * refactor: cmd update. * refactor: cmd update. * refactor: cmd update. * refactor: cmd update. * refactor: cmd update. * refactor: cmd update. * refactor: cmd update. * refactor: cmd update. * refactor webhook * refactor: cmd update. * refactor: cmd update. * refactor: cmd update. * refactor: cmd update. * refactor webhook * refactor: cmd update. * refactor: cmd update. * fix: runtime: goroutine stack exceeds * refactor: cmd update. * refactor notification * refactor notification * refactor * refactor: cmd update. * refactor: cmd update. * refactor * refactor * refactor * protojson * protojson * protojson * go mod * wrapperspb * refactor: cmd update. * refactor: cmd update. * refactor: cmd update. * refactor: context update. * refactor: websocket update info. * refactor: websocket update info. * refactor: websocket update info. * refactor: websocket update info. * refactor: api name change. * refactor: debug info. * refactor: debug info. * refactor: debug info. * fix: update file * refactor * refactor * refactor: debug info. * refactor: debug info. * refactor: debug info. * refactor: debug info. * refactor: debug info. * refactor: debug info. * fix: callback update. * fix: callback update. * refactor * fix: update message. * fix: msg cache timeout. * refactor * refactor * fix: push update. * fix: push update. * fix: push update. * fix: push update. * fix: push update. * fix: push update. * fix: push update. * fix: websocket handle error remove when upgrade error. --------- Co-authored-by: skiffer-git <44203734@qq.com> Co-authored-by: Xinwei Xiong (cubxxw) <3293172751nss@gmail.com> Co-authored-by: withchao <993506633@qq.com>
		
			
				
	
	
		
			203 lines
		
	
	
		
			7.2 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			203 lines
		
	
	
		
			7.2 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
// 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
 | 
						|
 | 
						|
import (
 | 
						|
	"context"
 | 
						|
 | 
						|
	cbapi "github.com/openimsdk/open-im-server/v3/pkg/callbackstruct"
 | 
						|
	"github.com/openimsdk/protocol/constant"
 | 
						|
	"github.com/openimsdk/protocol/msg"
 | 
						|
	"github.com/openimsdk/protocol/sdkws"
 | 
						|
	"github.com/openimsdk/tools/errs"
 | 
						|
	"github.com/openimsdk/tools/log"
 | 
						|
	"github.com/openimsdk/tools/utils/datautil"
 | 
						|
	"github.com/redis/go-redis/v9"
 | 
						|
)
 | 
						|
 | 
						|
func (m *msgServer) GetConversationsHasReadAndMaxSeq(ctx context.Context, req *msg.GetConversationsHasReadAndMaxSeqReq) (*msg.GetConversationsHasReadAndMaxSeqResp, error) {
 | 
						|
	var conversationIDs []string
 | 
						|
	if len(req.ConversationIDs) == 0 {
 | 
						|
		var err error
 | 
						|
		conversationIDs, err = m.ConversationLocalCache.GetConversationIDs(ctx, req.UserID)
 | 
						|
		if err != nil {
 | 
						|
			return nil, err
 | 
						|
		}
 | 
						|
	} else {
 | 
						|
		conversationIDs = req.ConversationIDs
 | 
						|
	}
 | 
						|
 | 
						|
	hasReadSeqs, err := m.MsgDatabase.GetHasReadSeqs(ctx, req.UserID, conversationIDs)
 | 
						|
	if err != nil {
 | 
						|
		return nil, err
 | 
						|
	}
 | 
						|
 | 
						|
	conversations, err := m.ConversationLocalCache.GetConversations(ctx, req.UserID, conversationIDs)
 | 
						|
	if err != nil {
 | 
						|
		return nil, err
 | 
						|
	}
 | 
						|
 | 
						|
	conversationMaxSeqMap := make(map[string]int64)
 | 
						|
	for _, conversation := range conversations {
 | 
						|
		if conversation.MaxSeq != 0 {
 | 
						|
			conversationMaxSeqMap[conversation.ConversationID] = conversation.MaxSeq
 | 
						|
		}
 | 
						|
	}
 | 
						|
	maxSeqs, err := m.MsgDatabase.GetMaxSeqs(ctx, conversationIDs)
 | 
						|
	if err != nil {
 | 
						|
		return nil, err
 | 
						|
	}
 | 
						|
	resp := &msg.GetConversationsHasReadAndMaxSeqResp{Seqs: make(map[string]*msg.Seqs)}
 | 
						|
	for conversationID, maxSeq := range maxSeqs {
 | 
						|
		resp.Seqs[conversationID] = &msg.Seqs{
 | 
						|
			HasReadSeq: hasReadSeqs[conversationID],
 | 
						|
			MaxSeq:     maxSeq,
 | 
						|
		}
 | 
						|
		if v, ok := conversationMaxSeqMap[conversationID]; ok {
 | 
						|
			resp.Seqs[conversationID].MaxSeq = v
 | 
						|
		}
 | 
						|
	}
 | 
						|
	return resp, nil
 | 
						|
}
 | 
						|
 | 
						|
func (m *msgServer) SetConversationHasReadSeq(ctx context.Context, req *msg.SetConversationHasReadSeqReq) (*msg.SetConversationHasReadSeqResp, error) {
 | 
						|
	maxSeq, err := m.MsgDatabase.GetMaxSeq(ctx, req.ConversationID)
 | 
						|
	if err != nil {
 | 
						|
		return nil, err
 | 
						|
	}
 | 
						|
	if req.HasReadSeq > maxSeq {
 | 
						|
		return nil, errs.ErrArgs.WrapMsg("hasReadSeq must not be bigger than maxSeq")
 | 
						|
	}
 | 
						|
	if err := m.MsgDatabase.SetHasReadSeq(ctx, req.UserID, req.ConversationID, req.HasReadSeq); err != nil {
 | 
						|
		return nil, err
 | 
						|
	}
 | 
						|
	m.sendMarkAsReadNotification(ctx, req.ConversationID, constant.SingleChatType, req.UserID, req.UserID, nil, req.HasReadSeq)
 | 
						|
	return &msg.SetConversationHasReadSeqResp{}, nil
 | 
						|
}
 | 
						|
 | 
						|
func (m *msgServer) MarkMsgsAsRead(ctx context.Context, req *msg.MarkMsgsAsReadReq) (*msg.MarkMsgsAsReadResp, error) {
 | 
						|
	if len(req.Seqs) < 1 {
 | 
						|
		return nil, errs.ErrArgs.WrapMsg("seqs must not be empty")
 | 
						|
	}
 | 
						|
	maxSeq, err := m.MsgDatabase.GetMaxSeq(ctx, req.ConversationID)
 | 
						|
	if err != nil {
 | 
						|
		return nil, err
 | 
						|
	}
 | 
						|
	hasReadSeq := req.Seqs[len(req.Seqs)-1]
 | 
						|
	if hasReadSeq > maxSeq {
 | 
						|
		return nil, errs.ErrArgs.WrapMsg("hasReadSeq must not be bigger than maxSeq")
 | 
						|
	}
 | 
						|
	conversation, err := m.ConversationLocalCache.GetConversation(ctx, req.UserID, req.ConversationID)
 | 
						|
	if err != nil {
 | 
						|
		return nil, err
 | 
						|
	}
 | 
						|
	if err := m.MsgDatabase.MarkSingleChatMsgsAsRead(ctx, req.UserID, req.ConversationID, req.Seqs); err != nil {
 | 
						|
		return nil, err
 | 
						|
	}
 | 
						|
	currentHasReadSeq, err := m.MsgDatabase.GetHasReadSeq(ctx, req.UserID, req.ConversationID)
 | 
						|
	if err != nil && errs.Unwrap(err) != redis.Nil {
 | 
						|
		return nil, err
 | 
						|
	}
 | 
						|
	if hasReadSeq > currentHasReadSeq {
 | 
						|
		err = m.MsgDatabase.SetHasReadSeq(ctx, req.UserID, req.ConversationID, hasReadSeq)
 | 
						|
		if err != nil {
 | 
						|
			return nil, err
 | 
						|
		}
 | 
						|
	}
 | 
						|
 | 
						|
	reqCallback := &cbapi.CallbackSingleMsgReadReq{
 | 
						|
		ConversationID: conversation.ConversationID,
 | 
						|
		UserID:         req.UserID,
 | 
						|
		Seqs:           req.Seqs,
 | 
						|
		ContentType:    conversation.ConversationType,
 | 
						|
	}
 | 
						|
	m.webhookAfterSingleMsgRead(ctx, &m.config.WebhooksConfig.AfterSingleMsgRead, reqCallback)
 | 
						|
	m.sendMarkAsReadNotification(ctx, req.ConversationID, conversation.ConversationType, req.UserID,
 | 
						|
		m.conversationAndGetRecvID(conversation, req.UserID), req.Seqs, hasReadSeq)
 | 
						|
	return &msg.MarkMsgsAsReadResp{}, nil
 | 
						|
}
 | 
						|
 | 
						|
func (m *msgServer) MarkConversationAsRead(ctx context.Context, req *msg.MarkConversationAsReadReq) (*msg.MarkConversationAsReadResp, error) {
 | 
						|
	conversation, err := m.ConversationLocalCache.GetConversation(ctx, req.UserID, req.ConversationID)
 | 
						|
	if err != nil {
 | 
						|
		return nil, err
 | 
						|
	}
 | 
						|
	hasReadSeq, err := m.MsgDatabase.GetHasReadSeq(ctx, req.UserID, req.ConversationID)
 | 
						|
	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)
 | 
						|
		}
 | 
						|
		// avoid client missed call MarkConversationMessageAsRead by order
 | 
						|
		for _, val := range req.Seqs {
 | 
						|
			if !datautil.Contain(val, seqs...) {
 | 
						|
				seqs = append(seqs, val)
 | 
						|
			}
 | 
						|
		}
 | 
						|
		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 {
 | 
						|
				return nil, err
 | 
						|
			}
 | 
						|
		}
 | 
						|
		if req.HasReadSeq > hasReadSeq {
 | 
						|
			err = m.MsgDatabase.SetHasReadSeq(ctx, req.UserID, req.ConversationID, req.HasReadSeq)
 | 
						|
			if err != nil {
 | 
						|
				return nil, err
 | 
						|
			}
 | 
						|
			hasReadSeq = req.HasReadSeq
 | 
						|
		}
 | 
						|
		m.sendMarkAsReadNotification(ctx, req.ConversationID, conversation.ConversationType, req.UserID,
 | 
						|
			m.conversationAndGetRecvID(conversation, req.UserID), seqs, hasReadSeq)
 | 
						|
	} else if conversation.ConversationType == constant.ReadGroupChatType ||
 | 
						|
		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
 | 
						|
		}
 | 
						|
		m.sendMarkAsReadNotification(ctx, req.ConversationID, constant.SingleChatType, req.UserID,
 | 
						|
			req.UserID, seqs, hasReadSeq)
 | 
						|
	}
 | 
						|
 | 
						|
	reqCall := &cbapi.CallbackGroupMsgReadReq{
 | 
						|
		SendID:       conversation.OwnerUserID,
 | 
						|
		ReceiveID:    req.UserID,
 | 
						|
		UnreadMsgNum: req.HasReadSeq,
 | 
						|
		ContentType:  int64(conversation.ConversationType),
 | 
						|
	}
 | 
						|
 | 
						|
	m.webhookAfterGroupMsgRead(ctx, &m.config.WebhooksConfig.AfterGroupMsgRead, reqCall)
 | 
						|
	return &msg.MarkConversationAsReadResp{}, nil
 | 
						|
}
 | 
						|
 | 
						|
func (m *msgServer) sendMarkAsReadNotification(ctx context.Context, conversationID string, sessionType int32, sendID, recvID string, seqs []int64, hasReadSeq int64) {
 | 
						|
	tips := &sdkws.MarkAsReadTips{
 | 
						|
		MarkAsReadUserID: sendID,
 | 
						|
		ConversationID:   conversationID,
 | 
						|
		Seqs:             seqs,
 | 
						|
		HasReadSeq:       hasReadSeq,
 | 
						|
	}
 | 
						|
	m.notificationSender.NotificationWithSessionType(ctx, sendID, recvID, constant.HasReadReceipt, sessionType, tips)
 | 
						|
 | 
						|
}
 |