Files
open-im-server/internal/rpc/conversation/conversaion.go
T

337 lines
15 KiB
Go
Raw Normal View History

2022-04-24 11:23:54 +08:00
package conversation
import (
"context"
2023-03-17 19:41:44 +08:00
2023-03-16 10:46:06 +08:00
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
2023-04-26 11:19:49 +08:00
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/convert"
2023-03-16 10:46:06 +08:00
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/cache"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/controller"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/relation"
tableRelation "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/tx"
"github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry"
2023-03-23 19:02:20 +08:00
"github.com/OpenIMSDK/Open-IM-Server/pkg/errs"
2023-03-16 10:46:06 +08:00
pbConversation "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/conversation"
2023-05-12 10:50:59 +08:00
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/sdkws"
2023-04-26 10:10:26 +08:00
"github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient"
2023-04-26 11:19:49 +08:00
"github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient/notification"
2023-03-16 10:46:06 +08:00
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
2022-04-24 11:23:54 +08:00
"google.golang.org/grpc"
)
2023-02-03 16:22:59 +08:00
type conversationServer struct {
2023-05-12 10:50:59 +08:00
groupRpcClient *rpcclient.GroupClient
2023-04-26 14:10:12 +08:00
conversationDatabase controller.ConversationDatabase
2023-04-23 19:50:42 +08:00
conversationNotificationSender *notification.ConversationNotificationSender
2023-05-19 10:44:37 +08:00
msgRpcClient *rpcclient.MsgClient
2022-04-24 11:23:54 +08:00
}
2023-03-03 17:42:26 +08:00
func Start(client discoveryregistry.SvcDiscoveryRegistry, server *grpc.Server) error {
2023-02-20 17:13:15 +08:00
db, err := relation.NewGormDB()
2023-02-03 16:22:59 +08:00
if err != nil {
2023-02-20 17:13:15 +08:00
return err
2022-04-24 11:23:54 +08:00
}
2023-02-20 17:13:15 +08:00
if err := db.AutoMigrate(&tableRelation.ConversationModel{}); err != nil {
return err
2023-02-03 16:22:59 +08:00
}
2023-03-03 17:42:26 +08:00
rdb, err := cache.NewRedis()
2023-02-22 14:31:30 +08:00
if err != nil {
return err
}
2023-03-23 19:02:20 +08:00
conversationDB := relation.NewConversationGorm(db)
2023-02-20 17:13:15 +08:00
pbConversation.RegisterConversationServer(server, &conversationServer{
2023-04-23 19:50:42 +08:00
conversationNotificationSender: notification.NewConversationNotificationSender(client),
2023-05-12 10:50:59 +08:00
groupRpcClient: rpcclient.NewGroupClient(client),
2023-05-19 10:44:37 +08:00
msgRpcClient: rpcclient.NewMsgClient(client),
2023-04-26 14:10:12 +08:00
conversationDatabase: controller.NewConversationDatabase(conversationDB, cache.NewConversationRedis(rdb, cache.GetDefaultOpt(), conversationDB), tx.NewGorm(db)),
2023-02-03 16:22:59 +08:00
})
2023-02-20 17:13:15 +08:00
return nil
2022-04-24 11:23:54 +08:00
}
2023-02-03 16:22:59 +08:00
func (c *conversationServer) GetConversation(ctx context.Context, req *pbConversation.GetConversationReq) (*pbConversation.GetConversationResp, error) {
2023-04-26 14:10:12 +08:00
conversations, err := c.conversationDatabase.FindConversations(ctx, req.OwnerUserID, []string{req.ConversationID})
2023-02-03 16:22:59 +08:00
if err != nil {
return nil, err
}
2023-05-12 16:45:50 +08:00
if len(conversations) < 1 {
return nil, errs.ErrRecordNotFound.Wrap("conversation not found")
2023-02-03 16:22:59 +08:00
}
2023-05-19 16:12:00 +08:00
resp := &pbConversation.GetConversationResp{Conversation: &pbConversation.Conversation{}}
2023-05-12 16:45:50 +08:00
resp.Conversation = convert.ConversationDB2Pb(conversations[0])
return resp, nil
2023-02-03 16:22:59 +08:00
}
func (c *conversationServer) GetAllConversations(ctx context.Context, req *pbConversation.GetAllConversationsReq) (*pbConversation.GetAllConversationsResp, error) {
2023-04-26 14:10:12 +08:00
conversations, err := c.conversationDatabase.GetUserAllConversation(ctx, req.OwnerUserID)
2023-02-03 16:22:59 +08:00
if err != nil {
return nil, err
}
2023-05-19 16:12:00 +08:00
resp := &pbConversation.GetAllConversationsResp{Conversations: []*pbConversation.Conversation{}}
2023-04-26 11:19:49 +08:00
resp.Conversations = convert.ConversationsDB2Pb(conversations)
2023-02-03 16:22:59 +08:00
return resp, nil
}
func (c *conversationServer) GetConversations(ctx context.Context, req *pbConversation.GetConversationsReq) (*pbConversation.GetConversationsResp, error) {
2023-04-26 14:10:12 +08:00
conversations, err := c.conversationDatabase.FindConversations(ctx, req.OwnerUserID, req.ConversationIDs)
2023-02-03 16:22:59 +08:00
if err != nil {
return nil, err
}
2023-03-23 19:02:20 +08:00
resp := &pbConversation.GetConversationsResp{Conversations: []*pbConversation.Conversation{}}
2023-04-26 11:19:49 +08:00
resp.Conversations = convert.ConversationsDB2Pb(conversations)
2023-02-03 16:22:59 +08:00
return resp, nil
}
func (c *conversationServer) BatchSetConversations(ctx context.Context, req *pbConversation.BatchSetConversationsReq) (*pbConversation.BatchSetConversationsResp, error) {
2023-04-26 11:19:49 +08:00
conversations := convert.ConversationsPb2DB(req.Conversations)
2023-04-26 14:10:12 +08:00
err := c.conversationDatabase.SetUserConversations(ctx, req.OwnerUserID, conversations)
2023-02-03 16:22:59 +08:00
if err != nil {
return nil, err
}
2023-04-28 11:01:48 +08:00
_ = c.conversationNotificationSender.ConversationChangeNotification(ctx, req.OwnerUserID)
2023-04-26 11:19:49 +08:00
return &pbConversation.BatchSetConversationsResp{}, nil
2023-02-03 16:22:59 +08:00
}
func (c *conversationServer) SetConversation(ctx context.Context, req *pbConversation.SetConversationReq) (*pbConversation.SetConversationResp, error) {
2023-03-23 19:02:20 +08:00
var conversation tableRelation.ConversationModel
if err := utils.CopyStructFields(&conversation, req.Conversation); err != nil {
return nil, err
}
2023-04-26 14:10:12 +08:00
err := c.conversationDatabase.SetUserConversations(ctx, req.Conversation.OwnerUserID, []*tableRelation.ConversationModel{&conversation})
2023-03-23 19:02:20 +08:00
if err != nil {
return nil, err
}
2023-04-28 11:01:48 +08:00
_ = c.conversationNotificationSender.ConversationChangeNotification(ctx, req.Conversation.OwnerUserID)
2023-03-23 19:02:20 +08:00
resp := &pbConversation.SetConversationResp{}
return resp, nil
2023-02-03 16:22:59 +08:00
}
func (c *conversationServer) SetRecvMsgOpt(ctx context.Context, req *pbConversation.SetRecvMsgOptReq) (*pbConversation.SetRecvMsgOptResp, error) {
2023-04-26 14:10:12 +08:00
if err := c.conversationDatabase.SetUsersConversationFiledTx(ctx, []string{req.OwnerUserID}, &tableRelation.ConversationModel{OwnerUserID: req.OwnerUserID, ConversationID: req.ConversationID, RecvMsgOpt: req.RecvMsgOpt}, map[string]interface{}{"recv_msg_opt": req.RecvMsgOpt}); err != nil {
2023-03-23 19:02:20 +08:00
return nil, err
}
2023-04-28 11:01:48 +08:00
_ = c.conversationNotificationSender.ConversationChangeNotification(ctx, req.OwnerUserID)
2023-03-23 19:02:20 +08:00
return &pbConversation.SetRecvMsgOptResp{}, nil
2023-02-03 16:22:59 +08:00
}
2023-05-12 16:46:45 +08:00
// deprecated
2023-02-03 16:22:59 +08:00
func (c *conversationServer) ModifyConversationField(ctx context.Context, req *pbConversation.ModifyConversationFieldReq) (*pbConversation.ModifyConversationFieldResp, error) {
resp := &pbConversation.ModifyConversationFieldResp{}
var err error
isSyncConversation := true
if req.Conversation.ConversationType == constant.GroupChatType {
2023-05-12 10:50:59 +08:00
groupInfo, err := c.groupRpcClient.GetGroupInfo(ctx, req.Conversation.GroupID)
2023-02-03 16:22:59 +08:00
if err != nil {
return nil, err
}
if groupInfo.Status == constant.GroupStatusDismissed && req.FieldType != constant.FieldUnread {
return nil, err
}
}
2023-05-12 16:45:50 +08:00
conversation := convert.ConversationPb2DB(req.Conversation)
2023-02-03 16:22:59 +08:00
if req.FieldType == constant.FieldIsPrivateChat {
2023-05-12 16:45:50 +08:00
err := c.conversationDatabase.SyncPeerUserPrivateConversationTx(ctx, []*tableRelation.ConversationModel{conversation})
2023-02-03 16:22:59 +08:00
if err != nil {
return nil, err
}
2023-04-23 19:50:42 +08:00
c.conversationNotificationSender.ConversationSetPrivateNotification(ctx, req.Conversation.OwnerUserID, req.Conversation.UserID, req.Conversation.IsPrivateChat)
2023-02-03 16:22:59 +08:00
return resp, nil
}
filedMap := make(map[string]interface{})
switch req.FieldType {
case constant.FieldRecvMsgOpt:
filedMap["recv_msg_opt"] = req.Conversation.RecvMsgOpt
case constant.FieldGroupAtType:
filedMap["group_at_type"] = req.Conversation.GroupAtType
case constant.FieldIsPinned:
filedMap["is_pinned"] = req.Conversation.IsPinned
case constant.FieldEx:
filedMap["ex"] = req.Conversation.Ex
case constant.FieldAttachedInfo:
filedMap["attached_info"] = req.Conversation.AttachedInfo
case constant.FieldUnread:
isSyncConversation = false
filedMap["update_unread_count_time"] = req.Conversation.UpdateUnreadCountTime
2023-05-12 16:45:50 +08:00
filedMap["has_read_seq"] = req.Conversation.HasReadSeq
2023-02-03 16:22:59 +08:00
case constant.FieldBurnDuration:
filedMap["burn_duration"] = req.Conversation.BurnDuration
}
2023-05-12 16:45:50 +08:00
err = c.conversationDatabase.SetUsersConversationFiledTx(ctx, req.UserIDList, conversation, filedMap)
2023-02-03 16:22:59 +08:00
if err != nil {
return nil, err
}
2023-02-10 20:57:45 +08:00
2023-02-03 16:22:59 +08:00
if isSyncConversation {
for _, v := range req.UserIDList {
2023-04-23 19:50:42 +08:00
c.conversationNotificationSender.ConversationChangeNotification(ctx, v)
2023-02-03 16:22:59 +08:00
}
} else {
for _, v := range req.UserIDList {
2023-05-12 16:45:50 +08:00
c.conversationNotificationSender.ConversationUnreadChangeNotification(ctx, v, req.Conversation.ConversationID, req.Conversation.UpdateUnreadCountTime, req.Conversation.HasReadSeq)
2023-02-03 16:22:59 +08:00
}
}
return resp, nil
}
2023-02-23 10:45:13 +08:00
2023-05-12 16:45:50 +08:00
func (c *conversationServer) SetConversations(ctx context.Context, req *pbConversation.SetConversationsReq) (*pbConversation.SetConversationsResp, error) {
isSyncConversation := true
if req.Conversation.ConversationType == constant.GroupChatType {
groupInfo, err := c.groupRpcClient.GetGroupInfo(ctx, req.Conversation.GroupID)
if err != nil {
return nil, err
}
if groupInfo.Status == constant.GroupStatusDismissed {
return nil, err
}
}
var conversation tableRelation.ConversationModel
conversation.ConversationID = req.Conversation.ConversationID
conversation.ConversationType = req.Conversation.ConversationType
conversation.UserID = req.Conversation.UserID
conversation.GroupID = req.Conversation.GroupID
m := make(map[string]interface{})
if req.Conversation.RecvMsgOpt != nil {
m["recv_msg_opt"] = req.Conversation.RecvMsgOpt.Value
}
if req.Conversation.DraftTextTime != nil {
m["draft_text_time"] = req.Conversation.DraftTextTime.Value
}
if req.Conversation.AttachedInfo != nil {
m["attached_info"] = req.Conversation.AttachedInfo.Value
}
if req.Conversation.Ex != nil {
m["ex"] = req.Conversation.Ex.Value
}
if req.Conversation.IsPinned != nil {
m["is_pinned"] = req.Conversation.IsPinned.Value
}
if req.Conversation.IsPrivateChat != nil {
var conversations []*tableRelation.ConversationModel
for _, ownerUserID := range req.UserIDs {
conversation2 := conversation
conversation.OwnerUserID = ownerUserID
conversation.IsPrivateChat = req.Conversation.IsPrivateChat.Value
conversations = append(conversations, &conversation2)
}
if err := c.conversationDatabase.SyncPeerUserPrivateConversationTx(ctx, conversations); err != nil {
return nil, err
}
for _, ownerUserID := range req.UserIDs {
c.conversationNotificationSender.ConversationSetPrivateNotification(ctx, ownerUserID, req.Conversation.UserID, req.Conversation.IsPrivateChat.Value)
}
}
if req.Conversation.BurnDuration != nil {
m["burn_duration"] = req.Conversation.BurnDuration.Value
}
if req.Conversation.HasReadSeq != nil && req.Conversation.UpdateUnreadCountTime != nil {
isSyncConversation = false
m["has_read_seq"] = req.Conversation.HasReadSeq.Value
m["update_unread_count_time"] = req.Conversation.UpdateUnreadCountTime.Value
}
err := c.conversationDatabase.SetUsersConversationFiledTx(ctx, req.UserIDs, &conversation, m)
if err != nil {
return nil, err
}
if isSyncConversation {
for _, v := range req.UserIDs {
c.conversationNotificationSender.ConversationChangeNotification(ctx, v)
}
} else {
for _, v := range req.UserIDs {
c.conversationNotificationSender.ConversationUnreadChangeNotification(ctx, v, req.Conversation.ConversationID, req.Conversation.UpdateUnreadCountTime.Value, req.Conversation.HasReadSeq.Value)
}
}
return &pbConversation.SetConversationsResp{}, nil
}
2023-02-23 10:45:13 +08:00
// 获取超级大群开启免打扰的用户ID
func (c *conversationServer) GetRecvMsgNotNotifyUserIDs(ctx context.Context, req *pbConversation.GetRecvMsgNotNotifyUserIDsReq) (*pbConversation.GetRecvMsgNotNotifyUserIDsResp, error) {
2023-04-26 14:10:12 +08:00
userIDs, err := c.conversationDatabase.FindRecvMsgNotNotifyUserIDs(ctx, req.GroupID)
if err != nil {
return nil, err
}
2023-04-26 11:19:49 +08:00
return &pbConversation.GetRecvMsgNotNotifyUserIDsResp{UserIDs: userIDs}, nil
2023-02-23 10:45:13 +08:00
}
2023-04-28 18:33:33 +08:00
// create conversation without notification for msg redis transfer
2023-05-04 20:07:20 +08:00
func (c *conversationServer) CreateSingleChatConversations(ctx context.Context, req *pbConversation.CreateSingleChatConversationsReq) (*pbConversation.CreateSingleChatConversationsResp, error) {
var conversation tableRelation.ConversationModel
conversation.ConversationID = utils.GetConversationIDBySessionType(constant.SingleChatType, req.RecvID, req.SendID)
conversation.ConversationType = constant.SingleChatType
conversation2 := conversation
conversation2.OwnerUserID = req.RecvID
conversation2.UserID = req.SendID
conversation.OwnerUserID = req.SendID
conversation.UserID = req.RecvID
err := c.conversationDatabase.CreateConversation(ctx, []*tableRelation.ConversationModel{&conversation, &conversation2})
2023-04-28 18:33:33 +08:00
if err != nil {
return nil, err
}
2023-05-04 20:07:20 +08:00
return &pbConversation.CreateSingleChatConversationsResp{}, nil
}
func (c *conversationServer) CreateGroupChatConversations(ctx context.Context, req *pbConversation.CreateGroupChatConversationsReq) (*pbConversation.CreateGroupChatConversationsResp, error) {
2023-05-31 11:48:33 +08:00
maxSeq, err := c.msgRpcClient.GetConversationMaxSeq(ctx, utils.GenGroupConversationID(req.GroupID))
if err != nil {
return nil, err
}
err = c.conversationDatabase.CreateGroupChatConversation(ctx, req.GroupID, req.UserIDs, maxSeq)
2023-05-04 20:07:20 +08:00
if err != nil {
return nil, err
}
return &pbConversation.CreateGroupChatConversationsResp{}, nil
}
func (c *conversationServer) DelGroupChatConversations(ctx context.Context, req *pbConversation.DelGroupChatConversationsReq) (*pbConversation.DelGroupChatConversationsResp, error) {
if err := c.conversationDatabase.UpdateUsersConversationFiled(ctx, req.OwnerUserID,
2023-05-17 17:09:26 +08:00
utils.GetConversationIDBySessionType(constant.SuperGroupChatType, req.GroupID),
map[string]interface{}{"max_seq": req.MaxSeq}); err != nil {
2023-05-04 20:07:20 +08:00
return nil, err
}
return &pbConversation.DelGroupChatConversationsResp{}, nil
2023-04-28 18:33:33 +08:00
}
2023-05-05 21:30:32 +08:00
func (c *conversationServer) GetConversationIDs(ctx context.Context, req *pbConversation.GetConversationIDsReq) (*pbConversation.GetConversationIDsResp, error) {
conversationIDs, err := c.conversationDatabase.GetConversationIDs(ctx, req.UserID)
if err != nil {
return nil, err
}
return &pbConversation.GetConversationIDsResp{ConversationIDs: conversationIDs}, nil
}
2023-05-12 10:50:59 +08:00
func (c *conversationServer) GetConversationsHasReadAndMaxSeq(ctx context.Context, req *pbConversation.GetConversationsHasReadAndMaxSeqReq) (*pbConversation.GetConversationsHasReadAndMaxSeqResp, error) {
2023-05-19 16:12:00 +08:00
conversations, err := c.conversationDatabase.GetUserAllHasReadSeqs(ctx, req.UserID)
2023-05-12 10:50:59 +08:00
if err != nil {
return nil, err
}
maxSeqs, err := c.msgRpcClient.GetMaxSeq(ctx, &sdkws.GetMaxSeqReq{UserID: req.UserID})
if err != nil {
return nil, err
}
resp := &pbConversation.GetConversationsHasReadAndMaxSeqResp{Seqs: make(map[string]*pbConversation.Seqs)}
2023-05-19 16:12:00 +08:00
for conversationID, seq := range conversations {
resp.Seqs[conversationID] = &pbConversation.Seqs{
HasReadSeq: seq,
MaxSeq: maxSeqs.MaxSeqs[conversationID],
2023-05-12 10:50:59 +08:00
}
}
return resp, nil
}
2023-05-17 17:09:26 +08:00
func (c *conversationServer) GetUserConversationIDsHash(ctx context.Context, req *pbConversation.GetUserConversationIDsHashReq) (*pbConversation.GetUserConversationIDsHashResp, error) {
hash, err := c.conversationDatabase.GetUserConversationIDsHash(ctx, req.OwnerUserID)
if err != nil {
return nil, err
}
return &pbConversation.GetUserConversationIDsHashResp{Hash: hash}, nil
}
2023-05-30 11:18:07 +08:00
2023-06-02 16:00:38 +08:00
func (c *conversationServer) GetConversationsByConversationID(ctx context.Context, req *pbConversation.GetConversationsByConversationIDReq) (*pbConversation.GetConversationsByConversationIDResp, error) {
conversations, err := c.conversationDatabase.GetConversationsByConversationID(ctx, req.ConversationIDs)
2023-05-30 11:18:07 +08:00
if err != nil {
return nil, err
}
2023-06-02 16:00:38 +08:00
return &pbConversation.GetConversationsByConversationIDResp{Conversations: convert.ConversationsDB2Pb(conversations)}, nil
2023-05-30 11:18:07 +08:00
}