Files
open-im-server/pkg/rpcclient/conversation.go
T

101 lines
4.0 KiB
Go
Raw Normal View History

2023-04-23 19:50:42 +08:00
package rpcclient
import (
"context"
2023-06-02 16:00:38 +08:00
"fmt"
2023-04-23 19:50:42 +08:00
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
2023-06-20 22:12:01 +08:00
"github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry"
2023-06-02 16:00:38 +08:00
"github.com/OpenIMSDK/Open-IM-Server/pkg/errs"
2023-06-20 22:12:01 +08:00
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/conversation"
2023-04-23 19:50:42 +08:00
pbConversation "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/conversation"
2023-06-20 22:12:01 +08:00
"google.golang.org/grpc"
2023-04-23 19:50:42 +08:00
)
2023-06-20 22:12:01 +08:00
type Conversation struct {
Client conversation.ConversationClient
conn *grpc.ClientConn
discov discoveryregistry.SvcDiscoveryRegistry
2023-04-23 19:50:42 +08:00
}
2023-06-20 22:12:01 +08:00
func NewConversation(discov discoveryregistry.SvcDiscoveryRegistry) *Conversation {
conn, err := discov.GetConn(context.Background(), config.Config.RpcRegisterName.OpenImConversationName)
2023-06-01 21:28:16 +08:00
if err != nil {
2023-06-20 22:12:01 +08:00
panic(err)
2023-06-01 21:28:16 +08:00
}
2023-06-20 22:12:01 +08:00
client := conversation.NewConversationClient(conn)
return &Conversation{discov: discov, conn: conn, Client: client}
}
type ConversationRpcClient Conversation
func NewConversationRpcClient(discov discoveryregistry.SvcDiscoveryRegistry) ConversationRpcClient {
return ConversationRpcClient(*NewConversation(discov))
}
func (c *ConversationRpcClient) ModifyConversationField(ctx context.Context, req *pbConversation.ModifyConversationFieldReq) error {
_, err := c.Client.ModifyConversationField(ctx, req)
2023-04-23 19:50:42 +08:00
return err
}
2023-06-20 22:12:01 +08:00
func (c *ConversationRpcClient) GetSingleConversationRecvMsgOpt(ctx context.Context, userID, conversationID string) (int32, error) {
2023-06-02 16:00:38 +08:00
var req pbConversation.GetConversationReq
2023-04-23 19:50:42 +08:00
req.OwnerUserID = userID
req.ConversationID = conversationID
2023-06-20 22:12:01 +08:00
conversation, err := c.Client.GetConversation(ctx, &req)
2023-04-23 19:50:42 +08:00
if err != nil {
return 0, err
}
return conversation.GetConversation().RecvMsgOpt, err
}
2023-04-28 18:33:33 +08:00
2023-06-20 22:12:01 +08:00
func (c *ConversationRpcClient) SingleChatFirstCreateConversation(ctx context.Context, recvID, sendID string) error {
_, err := c.Client.CreateSingleChatConversations(ctx, &pbConversation.CreateSingleChatConversationsReq{RecvID: recvID, SendID: sendID})
2023-05-04 20:07:20 +08:00
return err
2023-05-04 17:27:29 +08:00
}
2023-06-20 22:12:01 +08:00
func (c *ConversationRpcClient) GroupChatFirstCreateConversation(ctx context.Context, groupID string, userIDs []string) error {
_, err := c.Client.CreateGroupChatConversations(ctx, &pbConversation.CreateGroupChatConversationsReq{UserIDs: userIDs, GroupID: groupID})
2023-05-04 20:07:20 +08:00
return err
2023-05-04 17:27:29 +08:00
}
2023-06-20 22:12:01 +08:00
func (c *ConversationRpcClient) SetConversationMaxSeq(ctx context.Context, ownerUserIDs []string, conversationID string, maxSeq int64) error {
_, err := c.Client.SetConversationMaxSeq(ctx, &pbConversation.SetConversationMaxSeqReq{OwnerUserID: ownerUserIDs, ConversationID: conversationID, MaxSeq: maxSeq})
2023-04-28 18:33:33 +08:00
return err
}
2023-05-05 21:30:32 +08:00
2023-06-20 22:12:01 +08:00
func (c *ConversationRpcClient) GetConversationIDs(ctx context.Context, ownerUserID string) ([]string, error) {
resp, err := c.Client.GetConversationIDs(ctx, &pbConversation.GetConversationIDsReq{UserID: ownerUserID})
2023-06-02 16:00:38 +08:00
if err != nil {
return nil, err
}
return resp.ConversationIDs, nil
2023-05-05 21:30:32 +08:00
}
2023-05-29 20:18:47 +08:00
2023-06-20 22:12:01 +08:00
func (c *ConversationRpcClient) GetConversation(ctx context.Context, ownerUserID, conversationID string) (*pbConversation.Conversation, error) {
resp, err := c.Client.GetConversation(ctx, &pbConversation.GetConversationReq{OwnerUserID: ownerUserID, ConversationID: conversationID})
2023-06-02 16:00:38 +08:00
if err != nil {
return nil, err
}
return resp.Conversation, nil
2023-05-30 11:18:07 +08:00
}
2023-06-20 22:12:01 +08:00
func (c *ConversationRpcClient) GetConversationsByConversationID(ctx context.Context, conversationIDs []string) ([]*pbConversation.Conversation, error) {
resp, err := c.Client.GetConversationsByConversationID(ctx, &pbConversation.GetConversationsByConversationIDReq{ConversationIDs: conversationIDs})
2023-06-02 16:00:38 +08:00
if err != nil {
return nil, err
}
if len(resp.Conversations) == 0 {
return nil, errs.ErrRecordNotFound.Wrap(fmt.Sprintf("conversationIDs: %v not found", conversationIDs))
}
return resp.Conversations, nil
2023-05-29 20:18:47 +08:00
}
2023-06-19 10:19:01 +08:00
2023-06-20 22:12:01 +08:00
func (c *ConversationRpcClient) GetConversations(ctx context.Context, ownerUserID string, conversationIDs []string) ([]*pbConversation.Conversation, error) {
resp, err := c.Client.GetConversations(ctx, &pbConversation.GetConversationsReq{OwnerUserID: ownerUserID, ConversationIDs: conversationIDs})
2023-06-19 10:19:01 +08:00
if err != nil {
return nil, err
}
return resp.Conversations, nil
}