You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Open-IM-Server/internal/rpc/conversation/conversaion.go

184 lines
7.9 KiB

package conversation
import (
"context"
2 years ago
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/convert"
2 years ago
"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"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/errs"
2 years ago
pbConversation "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/conversation"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient/notification"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
"google.golang.org/grpc"
)
type conversationServer struct {
2 years ago
group *rpcclient.GroupClient
2 years ago
conversationDatabase controller.ConversationDatabase
2 years ago
conversationNotificationSender *notification.ConversationNotificationSender
}
2 years ago
func Start(client discoveryregistry.SvcDiscoveryRegistry, server *grpc.Server) error {
db, err := relation.NewGormDB()
if err != nil {
return err
}
if err := db.AutoMigrate(&tableRelation.ConversationModel{}); err != nil {
return err
}
2 years ago
rdb, err := cache.NewRedis()
2 years ago
if err != nil {
return err
}
2 years ago
conversationDB := relation.NewConversationGorm(db)
pbConversation.RegisterConversationServer(server, &conversationServer{
2 years ago
conversationNotificationSender: notification.NewConversationNotificationSender(client),
2 years ago
group: rpcclient.NewGroupClient(client),
2 years ago
conversationDatabase: controller.NewConversationDatabase(conversationDB, cache.NewConversationRedis(rdb, cache.GetDefaultOpt(), conversationDB), tx.NewGorm(db)),
})
return nil
}
func (c *conversationServer) GetConversation(ctx context.Context, req *pbConversation.GetConversationReq) (*pbConversation.GetConversationResp, error) {
resp := &pbConversation.GetConversationResp{Conversation: &pbConversation.Conversation{}}
2 years ago
conversations, err := c.conversationDatabase.FindConversations(ctx, req.OwnerUserID, []string{req.ConversationID})
if err != nil {
return nil, err
}
if len(conversations) > 0 {
2 years ago
resp.Conversation = convert.ConversationDB2Pb(conversations[0])
return resp, nil
}
2 years ago
return nil, errs.ErrRecordNotFound.Wrap("conversation not found")
}
func (c *conversationServer) GetAllConversations(ctx context.Context, req *pbConversation.GetAllConversationsReq) (*pbConversation.GetAllConversationsResp, error) {
resp := &pbConversation.GetAllConversationsResp{Conversations: []*pbConversation.Conversation{}}
2 years ago
conversations, err := c.conversationDatabase.GetUserAllConversation(ctx, req.OwnerUserID)
if err != nil {
return nil, err
}
2 years ago
resp.Conversations = convert.ConversationsDB2Pb(conversations)
return resp, nil
}
func (c *conversationServer) GetConversations(ctx context.Context, req *pbConversation.GetConversationsReq) (*pbConversation.GetConversationsResp, error) {
2 years ago
conversations, err := c.conversationDatabase.FindConversations(ctx, req.OwnerUserID, req.ConversationIDs)
if err != nil {
return nil, err
}
2 years ago
resp := &pbConversation.GetConversationsResp{Conversations: []*pbConversation.Conversation{}}
2 years ago
resp.Conversations = convert.ConversationsDB2Pb(conversations)
return resp, nil
}
func (c *conversationServer) BatchSetConversations(ctx context.Context, req *pbConversation.BatchSetConversationsReq) (*pbConversation.BatchSetConversationsResp, error) {
2 years ago
conversations := convert.ConversationsPb2DB(req.Conversations)
2 years ago
err := c.conversationDatabase.SetUserConversations(ctx, req.OwnerUserID, conversations)
if err != nil {
return nil, err
}
2 years ago
_ = c.conversationNotificationSender.ConversationChangeNotification(ctx, req.OwnerUserID)
2 years ago
return &pbConversation.BatchSetConversationsResp{}, nil
}
func (c *conversationServer) SetConversation(ctx context.Context, req *pbConversation.SetConversationReq) (*pbConversation.SetConversationResp, error) {
2 years ago
var conversation tableRelation.ConversationModel
if err := utils.CopyStructFields(&conversation, req.Conversation); err != nil {
return nil, err
}
2 years ago
err := c.conversationDatabase.SetUserConversations(ctx, req.Conversation.OwnerUserID, []*tableRelation.ConversationModel{&conversation})
2 years ago
if err != nil {
return nil, err
}
2 years ago
_ = c.conversationNotificationSender.ConversationChangeNotification(ctx, req.Conversation.OwnerUserID)
2 years ago
resp := &pbConversation.SetConversationResp{}
return resp, nil
}
func (c *conversationServer) SetRecvMsgOpt(ctx context.Context, req *pbConversation.SetRecvMsgOptReq) (*pbConversation.SetRecvMsgOptResp, error) {
2 years ago
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 {
2 years ago
return nil, err
}
2 years ago
_ = c.conversationNotificationSender.ConversationChangeNotification(ctx, req.OwnerUserID)
2 years ago
return &pbConversation.SetRecvMsgOptResp{}, nil
}
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 {
2 years ago
groupInfo, err := c.group.GetGroupInfo(ctx, req.Conversation.GroupID)
if err != nil {
return nil, err
}
if groupInfo.Status == constant.GroupStatusDismissed && req.FieldType != constant.FieldUnread {
return nil, err
}
}
var conversation tableRelation.ConversationModel
if err := utils.CopyStructFields(&conversation, req.Conversation); err != nil {
return nil, err
}
if req.FieldType == constant.FieldIsPrivateChat {
2 years ago
err := c.conversationDatabase.SyncPeerUserPrivateConversationTx(ctx, &conversation)
if err != nil {
return nil, err
}
2 years ago
c.conversationNotificationSender.ConversationSetPrivateNotification(ctx, req.Conversation.OwnerUserID, req.Conversation.UserID, req.Conversation.IsPrivateChat)
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.FieldIsNotInGroup:
filedMap["is_not_in_group"] = req.Conversation.IsNotInGroup
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
case constant.FieldBurnDuration:
filedMap["burn_duration"] = req.Conversation.BurnDuration
}
2 years ago
err = c.conversationDatabase.SetUsersConversationFiledTx(ctx, req.UserIDList, &conversation, filedMap)
if err != nil {
return nil, err
}
if isSyncConversation {
for _, v := range req.UserIDList {
2 years ago
c.conversationNotificationSender.ConversationChangeNotification(ctx, v)
}
} else {
for _, v := range req.UserIDList {
2 years ago
c.conversationNotificationSender.ConversationUnreadChangeNotification(ctx, v, req.Conversation.ConversationID, req.Conversation.UpdateUnreadCountTime)
}
}
return resp, nil
}
// 获取超级大群开启免打扰的用户ID
func (c *conversationServer) GetRecvMsgNotNotifyUserIDs(ctx context.Context, req *pbConversation.GetRecvMsgNotNotifyUserIDsReq) (*pbConversation.GetRecvMsgNotNotifyUserIDsResp, error) {
2 years ago
userIDs, err := c.conversationDatabase.FindRecvMsgNotNotifyUserIDs(ctx, req.GroupID)
if err != nil {
return nil, err
}
2 years ago
return &pbConversation.GetRecvMsgNotNotifyUserIDsResp{UserIDs: userIDs}, nil
}