Files
open-im-server/internal/rpc/msg/server.go
T

171 lines
6.7 KiB
Go
Raw Normal View History

2023-07-13 17:07:42 +08:00
// 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.
2023-06-30 09:45:02 +08:00
package msg
import (
2024-04-19 22:23:08 +08:00
"context"
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/cache"
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/cache/mcache"
"github.com/openimsdk/open-im-server/v3/pkg/dbbuild"
"github.com/openimsdk/open-im-server/v3/pkg/mqbuild"
2024-12-24 10:51:38 +08:00
"github.com/openimsdk/open-im-server/v3/pkg/rpcli"
"google.golang.org/grpc"
2024-03-06 15:58:05 +08:00
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/cache/redis"
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/controller"
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/database/mgo"
2024-04-19 22:23:08 +08:00
"github.com/openimsdk/open-im-server/v3/pkg/common/webhook"
2024-12-24 10:51:38 +08:00
"github.com/openimsdk/open-im-server/v3/pkg/notification"
2024-03-10 10:24:20 +08:00
"github.com/openimsdk/open-im-server/v3/pkg/rpccache"
2024-04-19 22:23:08 +08:00
"github.com/openimsdk/protocol/constant"
"github.com/openimsdk/protocol/conversation"
"github.com/openimsdk/protocol/msg"
"github.com/openimsdk/protocol/sdkws"
2024-04-19 22:23:08 +08:00
"github.com/openimsdk/tools/discovery"
2023-06-30 09:45:02 +08:00
)
2024-04-19 22:23:08 +08:00
type MessageInterceptorFunc func(ctx context.Context, globalConfig *Config, req *msg.SendMsgReq) (*sdkws.MsgData, error)
2024-12-24 10:51:38 +08:00
// MessageInterceptorChain defines a chain of message interceptor functions.
type MessageInterceptorChain []MessageInterceptorFunc
2024-04-19 22:23:08 +08:00
2024-12-24 10:51:38 +08:00
type Config struct {
RpcConfig config.Msg
RedisConfig config.Redis
MongodbConfig config.Mongo
KafkaConfig config.Kafka
NotificationConfig config.Notification
Share config.Share
WebhooksConfig config.Webhooks
LocalCacheConfig config.LocalCache
Discovery config.Discovery
}
// MsgServer encapsulates dependencies required for message handling.
type msgServer struct {
msg.UnimplementedMsgServer
RegisterCenter discovery.Conn // Service discovery registry for service registration.
MsgDatabase controller.CommonMsgDatabase // Interface for message database operations.
2024-12-24 10:51:38 +08:00
UserLocalCache *rpccache.UserLocalCache // Local cache for user data.
FriendLocalCache *rpccache.FriendLocalCache // Local cache for friend data.
GroupLocalCache *rpccache.GroupLocalCache // Local cache for group data.
ConversationLocalCache *rpccache.ConversationLocalCache // Local cache for conversation data.
Handlers MessageInterceptorChain // Chain of handlers for processing messages.
notificationSender *notification.NotificationSender // RPC client for sending notifications.
2024-12-24 10:51:38 +08:00
msgNotificationSender *MsgNotificationSender // RPC client for sending msg notifications.
config *Config // Global configuration settings.
webhookClient *webhook.Client
conversationClient *rpcli.ConversationClient
}
2023-06-30 09:45:02 +08:00
func (m *msgServer) addInterceptorHandler(interceptorFunc ...MessageInterceptorFunc) {
m.Handlers = append(m.Handlers, interceptorFunc...)
2024-04-19 22:23:08 +08:00
}
2023-06-30 09:45:02 +08:00
2025-06-11 16:29:44 +08:00
func Start(ctx context.Context, config *Config, client discovery.SvcDiscoveryRegistry, server grpc.ServiceRegistrar) error {
builder := mqbuild.NewBuilder(&config.KafkaConfig)
redisProducer, err := builder.GetTopicProducer(ctx, config.KafkaConfig.ToRedisTopic)
2023-06-30 09:45:02 +08:00
if err != nil {
return err
}
dbb := dbbuild.NewBuilder(&config.MongodbConfig, &config.RedisConfig)
mgocli, err := dbb.Mongo(ctx)
if err != nil {
return err
}
rdb, err := dbb.Redis(ctx)
2023-06-30 09:45:02 +08:00
if err != nil {
return err
}
2024-04-19 22:23:08 +08:00
msgDocModel, err := mgo.NewMsgMongo(mgocli.GetDB())
if err != nil {
2023-06-30 09:45:02 +08:00
return err
}
var msgModel cache.MsgCache
if rdb == nil {
cm, err := mgo.NewCacheMgo(mgocli.GetDB())
if err != nil {
return err
}
msgModel = mcache.NewMsgCache(cm, msgDocModel)
} else {
msgModel = redis.NewMsgCache(rdb, msgDocModel)
}
2024-07-16 10:46:21 +08:00
seqConversation, err := mgo.NewSeqConversationMongo(mgocli.GetDB())
if err != nil {
return err
}
seqConversationCache := redis.NewSeqConversationCacheRedis(rdb, seqConversation)
seqUser, err := mgo.NewSeqUserMongo(mgocli.GetDB())
if err != nil {
return err
}
seqUserCache := redis.NewSeqUserCacheRedis(rdb, seqUser)
2024-12-24 10:51:38 +08:00
userConn, err := client.GetConn(ctx, config.Discovery.RpcService.User)
if err != nil {
return err
}
groupConn, err := client.GetConn(ctx, config.Discovery.RpcService.Group)
if err != nil {
return err
}
friendConn, err := client.GetConn(ctx, config.Discovery.RpcService.Friend)
if err != nil {
return err
}
conversationConn, err := client.GetConn(ctx, config.Discovery.RpcService.Conversation)
if err != nil {
return err
}
conversationClient := rpcli.NewConversationClient(conversationConn)
msgDatabase := controller.NewCommonMsgDatabase(msgDocModel, msgModel, seqUserCache, seqConversationCache, redisProducer)
2023-06-30 09:45:02 +08:00
s := &msgServer{
MsgDatabase: msgDatabase,
RegisterCenter: client,
2024-12-24 10:51:38 +08:00
UserLocalCache: rpccache.NewUserLocalCache(rpcli.NewUserClient(userConn), &config.LocalCacheConfig, rdb),
GroupLocalCache: rpccache.NewGroupLocalCache(rpcli.NewGroupClient(groupConn), &config.LocalCacheConfig, rdb),
ConversationLocalCache: rpccache.NewConversationLocalCache(conversationClient, &config.LocalCacheConfig, rdb),
FriendLocalCache: rpccache.NewFriendLocalCache(rpcli.NewRelationClient(friendConn), &config.LocalCacheConfig, rdb),
config: config,
2024-04-19 22:23:08 +08:00
webhookClient: webhook.NewWebhookClient(config.WebhooksConfig.URL),
2024-12-24 10:51:38 +08:00
conversationClient: conversationClient,
2023-06-30 09:45:02 +08:00
}
2024-04-19 22:23:08 +08:00
s.notificationSender = notification.NewNotificationSender(&config.NotificationConfig, notification.WithLocalSendMsg(s.SendMsg))
s.msgNotificationSender = NewMsgNotificationSender(config, notification.WithLocalSendMsg(s.SendMsg))
2023-06-30 09:45:02 +08:00
msg.RegisterMsgServer(server, s)
2023-06-30 09:45:02 +08:00
return nil
}
2024-04-19 22:23:08 +08:00
func (m *msgServer) conversationAndGetRecvID(conversation *conversation.Conversation, userID string) string {
2023-07-13 11:37:23 +08:00
if conversation.ConversationType == constant.SingleChatType ||
conversation.ConversationType == constant.NotificationChatType {
2023-06-30 09:45:02 +08:00
if userID == conversation.OwnerUserID {
2024-04-19 22:23:08 +08:00
return conversation.UserID
2023-06-30 09:45:02 +08:00
} else {
2024-04-19 22:23:08 +08:00
return conversation.OwnerUserID
2023-06-30 09:45:02 +08:00
}
2024-04-19 22:23:08 +08:00
} else if conversation.ConversationType == constant.ReadGroupChatType {
return conversation.GroupID
2023-06-30 09:45:02 +08:00
}
2024-04-19 22:23:08 +08:00
return ""
2023-06-30 09:45:02 +08:00
}