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

52 lines
1.6 KiB
Go
Raw Normal View History

2023-04-21 20:29:08 +08:00
package rpcclient
import (
"context"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
discoveryRegistry "github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry"
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/friend"
sdkws "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/sdkws"
2023-05-30 16:15:11 +08:00
"google.golang.org/grpc"
2023-04-21 20:29:08 +08:00
)
type FriendClient struct {
2023-05-30 16:15:11 +08:00
conn *grpc.ClientConn
2023-04-21 20:29:08 +08:00
}
2023-05-30 16:15:11 +08:00
func NewFriendClient(discov discoveryRegistry.SvcDiscoveryRegistry) *FriendClient {
conn, err := discov.GetConn(context.Background(), config.Config.RpcRegisterName.OpenImFriendName)
if err != nil {
panic(err)
}
return &FriendClient{conn: conn}
2023-04-21 20:29:08 +08:00
}
func (f *FriendClient) GetFriendsInfo(ctx context.Context, ownerUserID, friendUserID string) (resp *sdkws.FriendInfo, err error) {
2023-05-30 16:15:11 +08:00
r, err := friend.NewFriendClient(f.conn).GetDesignatedFriends(ctx, &friend.GetDesignatedFriendsReq{OwnerUserID: ownerUserID, FriendUserIDs: []string{friendUserID}})
2023-04-21 20:29:08 +08:00
if err != nil {
return nil, err
}
resp = r.FriendsInfo[0]
return
}
// possibleFriendUserID是否在userID的好友中
func (f *FriendClient) IsFriend(ctx context.Context, possibleFriendUserID, userID string) (bool, error) {
2023-05-30 16:15:11 +08:00
resp, err := friend.NewFriendClient(f.conn).IsFriend(ctx, &friend.IsFriendReq{UserID1: userID, UserID2: possibleFriendUserID})
2023-04-21 20:29:08 +08:00
if err != nil {
return false, err
}
return resp.InUser1Friends, nil
}
func (f *FriendClient) GetFriendIDs(ctx context.Context, ownerUserID string) (friendIDs []string, err error) {
req := friend.GetFriendIDsReq{UserID: ownerUserID}
2023-05-30 16:15:11 +08:00
resp, err := friend.NewFriendClient(f.conn).GetFriendIDs(ctx, &req)
2023-04-21 20:29:08 +08:00
if err != nil {
return nil, err
}
return resp.FriendIDs, err
}