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

88 lines
2.7 KiB
Go
Raw Normal View History

2023-07-04 11:15:20 +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 rpcclient
import (
"context"
"github.com/openimsdk/protocol/relation"
2024-04-19 22:23:08 +08:00
sdkws "github.com/openimsdk/protocol/sdkws"
"github.com/openimsdk/tools/discovery"
"github.com/openimsdk/tools/system/program"
2024-03-05 10:51:55 +08:00
"google.golang.org/grpc"
2023-06-30 09:45:02 +08:00
)
type Friend struct {
conn grpc.ClientConnInterface
Client relation.FriendClient
2024-04-19 22:23:08 +08:00
discov discovery.SvcDiscoveryRegistry
2023-06-30 09:45:02 +08:00
}
2024-04-19 22:23:08 +08:00
func NewFriend(discov discovery.SvcDiscoveryRegistry, rpcRegisterName string) *Friend {
conn, err := discov.GetConn(context.Background(), rpcRegisterName)
2023-06-30 09:45:02 +08:00
if err != nil {
2024-04-19 22:23:08 +08:00
program.ExitWithError(err)
2023-06-30 09:45:02 +08:00
}
client := relation.NewFriendClient(conn)
2024-04-19 22:23:08 +08:00
return &Friend{discov: discov, conn: conn, Client: client}
2023-06-30 09:45:02 +08:00
}
type FriendRpcClient Friend
2024-04-19 22:23:08 +08:00
func NewFriendRpcClient(discov discovery.SvcDiscoveryRegistry, rpcRegisterName string) FriendRpcClient {
return FriendRpcClient(*NewFriend(discov, rpcRegisterName))
2023-06-30 09:45:02 +08:00
}
2023-07-03 16:29:22 +08:00
func (f *FriendRpcClient) GetFriendsInfo(
ctx context.Context,
ownerUserID, relationUserID string,
2023-07-03 16:29:22 +08:00
) (resp *sdkws.FriendInfo, err error) {
r, err := f.Client.GetDesignatedFriends(
ctx,
&relation.GetDesignatedFriendsReq{OwnerUserID: ownerUserID, FriendUserIDs: []string{relationUserID}},
2023-07-03 16:29:22 +08:00
)
2023-06-30 09:45:02 +08:00
if err != nil {
return nil, err
}
resp = r.FriendsInfo[0]
return
}
// possibleFriendUserID Is PossibleFriendUserId's relations.
2023-06-30 09:45:02 +08:00
func (f *FriendRpcClient) IsFriend(ctx context.Context, possibleFriendUserID, userID string) (bool, error) {
resp, err := f.Client.IsFriend(ctx, &relation.IsFriendReq{UserID1: userID, UserID2: possibleFriendUserID})
2023-06-30 09:45:02 +08:00
if err != nil {
return false, err
}
return resp.InUser1Friends, nil
}
func (f *FriendRpcClient) GetFriendIDs(ctx context.Context, ownerUserID string) (relationIDs []string, err error) {
req := relation.GetFriendIDsReq{UserID: ownerUserID}
2023-06-30 09:45:02 +08:00
resp, err := f.Client.GetFriendIDs(ctx, &req)
if err != nil {
return nil, err
}
return resp.FriendIDs, nil
}
2024-08-09 10:05:12 +08:00
func (f *FriendRpcClient) IsBlack(ctx context.Context, possibleBlackUserID, userID string) (bool, error) {
r, err := f.Client.IsBlack(ctx, &relation.IsBlackReq{UserID1: possibleBlackUserID, UserID2: userID})
2023-06-30 09:45:02 +08:00
if err != nil {
return false, err
}
return r.InUser2Blacks, nil
}