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.
499 lines
17 KiB
499 lines
17 KiB
1 year ago
|
// 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.
|
||
|
|
||
4 months ago
|
package relation
|
||
1 year ago
|
|
||
|
import (
|
||
|
"context"
|
||
4 months ago
|
|
||
4 months ago
|
"github.com/openimsdk/tools/mq/memamq"
|
||
5 months ago
|
|
||
8 months ago
|
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
|
||
6 months ago
|
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/cache/redis"
|
||
|
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/database/mgo"
|
||
|
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/model"
|
||
8 months ago
|
"github.com/openimsdk/open-im-server/v3/pkg/common/webhook"
|
||
6 months ago
|
"github.com/openimsdk/open-im-server/v3/pkg/localcache"
|
||
8 months ago
|
"github.com/openimsdk/tools/db/redisutil"
|
||
12 months ago
|
|
||
9 months ago
|
"github.com/openimsdk/open-im-server/v3/pkg/authverify"
|
||
|
"github.com/openimsdk/open-im-server/v3/pkg/common/convert"
|
||
8 months ago
|
"github.com/openimsdk/open-im-server/v3/pkg/common/servererrs"
|
||
6 months ago
|
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/controller"
|
||
9 months ago
|
"github.com/openimsdk/open-im-server/v3/pkg/rpcclient"
|
||
8 months ago
|
"github.com/openimsdk/protocol/constant"
|
||
5 months ago
|
"github.com/openimsdk/protocol/relation"
|
||
8 months ago
|
"github.com/openimsdk/protocol/sdkws"
|
||
|
"github.com/openimsdk/tools/db/mongoutil"
|
||
|
"github.com/openimsdk/tools/discovery"
|
||
|
"github.com/openimsdk/tools/errs"
|
||
|
"github.com/openimsdk/tools/utils/datautil"
|
||
9 months ago
|
"google.golang.org/grpc"
|
||
1 year ago
|
)
|
||
|
|
||
|
type friendServer struct {
|
||
5 months ago
|
db controller.FriendDatabase
|
||
1 year ago
|
blackDatabase controller.BlackDatabase
|
||
|
userRpcClient *rpcclient.UserRpcClient
|
||
8 months ago
|
notificationSender *FriendNotificationSender
|
||
1 year ago
|
conversationRpcClient rpcclient.ConversationRpcClient
|
||
8 months ago
|
RegisterCenter discovery.SvcDiscoveryRegistry
|
||
|
config *Config
|
||
|
webhookClient *webhook.Client
|
||
4 months ago
|
queue *memamq.MemoryQueue
|
||
8 months ago
|
}
|
||
|
|
||
|
type Config struct {
|
||
7 months ago
|
RpcConfig config.Friend
|
||
|
RedisConfig config.Redis
|
||
|
MongodbConfig config.Mongo
|
||
5 months ago
|
// ZookeeperConfig config.ZooKeeper
|
||
8 months ago
|
NotificationConfig config.Notification
|
||
|
Share config.Share
|
||
|
WebhooksConfig config.Webhooks
|
||
|
LocalCacheConfig config.LocalCache
|
||
7 months ago
|
Discovery config.Discovery
|
||
1 year ago
|
}
|
||
|
|
||
8 months ago
|
func Start(ctx context.Context, config *Config, client discovery.SvcDiscoveryRegistry, server *grpc.Server) error {
|
||
|
mgocli, err := mongoutil.NewMongoDB(ctx, config.MongodbConfig.Build())
|
||
1 year ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
8 months ago
|
rdb, err := redisutil.NewRedisClient(ctx, config.RedisConfig.Build())
|
||
1 year ago
|
if err != nil {
|
||
1 year ago
|
return err
|
||
|
}
|
||
1 year ago
|
|
||
8 months ago
|
friendMongoDB, err := mgo.NewFriendMongo(mgocli.GetDB())
|
||
1 year ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
1 year ago
|
|
||
8 months ago
|
friendRequestMongoDB, err := mgo.NewFriendRequestMongo(mgocli.GetDB())
|
||
1 year ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
8 months ago
|
blackMongoDB, err := mgo.NewBlackMongo(mgocli.GetDB())
|
||
1 year ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
// Initialize RPC clients
|
||
8 months ago
|
userRpcClient := rpcclient.NewUserRpcClient(client, config.Share.RpcRegisterName.User, config.Share.IMAdminUserID)
|
||
|
msgRpcClient := rpcclient.NewMessageRpcClient(client, config.Share.RpcRegisterName.Msg)
|
||
1 year ago
|
|
||
|
// Initialize notification sender
|
||
8 months ago
|
notificationSender := NewFriendNotificationSender(
|
||
|
&config.NotificationConfig,
|
||
1 year ago
|
&msgRpcClient,
|
||
8 months ago
|
WithRpcFunc(userRpcClient.GetUsersInfo),
|
||
1 year ago
|
)
|
||
6 months ago
|
localcache.InitLocalCache(&config.LocalCacheConfig)
|
||
8 months ago
|
|
||
1 year ago
|
// Register Friend server with refactored MongoDB and Redis integrations
|
||
5 months ago
|
relation.RegisterFriendServer(server, &friendServer{
|
||
|
db: controller.NewFriendDatabase(
|
||
1 year ago
|
friendMongoDB,
|
||
|
friendRequestMongoDB,
|
||
6 months ago
|
redis.NewFriendCacheRedis(rdb, &config.LocalCacheConfig, friendMongoDB, redis.GetRocksCacheOptions()),
|
||
8 months ago
|
mgocli.GetTx(),
|
||
1 year ago
|
),
|
||
|
blackDatabase: controller.NewBlackDatabase(
|
||
1 year ago
|
blackMongoDB,
|
||
6 months ago
|
redis.NewBlackCacheRedis(rdb, &config.LocalCacheConfig, blackMongoDB, redis.GetRocksCacheOptions()),
|
||
1 year ago
|
),
|
||
1 year ago
|
userRpcClient: &userRpcClient,
|
||
|
notificationSender: notificationSender,
|
||
|
RegisterCenter: client,
|
||
8 months ago
|
conversationRpcClient: rpcclient.NewConversationRpcClient(client, config.Share.RpcRegisterName.Conversation),
|
||
9 months ago
|
config: config,
|
||
8 months ago
|
webhookClient: webhook.NewWebhookClient(config.WebhooksConfig.URL),
|
||
4 months ago
|
queue: memamq.NewMemoryQueue(128, 1024*8),
|
||
1 year ago
|
})
|
||
|
return nil
|
||
|
}
|
||
|
|
||
1 year ago
|
// ok.
|
||
5 months ago
|
func (s *friendServer) ApplyToAddFriend(ctx context.Context, req *relation.ApplyToAddFriendReq) (resp *relation.ApplyToAddFriendResp, err error) {
|
||
|
resp = &relation.ApplyToAddFriendResp{}
|
||
8 months ago
|
if err := authverify.CheckAccessV3(ctx, req.FromUserID, s.config.Share.IMAdminUserID); err != nil {
|
||
1 year ago
|
return nil, err
|
||
|
}
|
||
|
if req.ToUserID == req.FromUserID {
|
||
8 months ago
|
return nil, servererrs.ErrCanNotAddYourself.WrapMsg("req.ToUserID", req.ToUserID)
|
||
1 year ago
|
}
|
||
8 months ago
|
if err = s.webhookBeforeAddFriend(ctx, &s.config.WebhooksConfig.BeforeAddFriend, req); err != nil && err != servererrs.ErrCallbackContinue {
|
||
1 year ago
|
return nil, err
|
||
|
}
|
||
9 months ago
|
if _, err := s.userRpcClient.GetUsersInfoMap(ctx, []string{req.ToUserID, req.FromUserID}); err != nil {
|
||
1 year ago
|
return nil, err
|
||
|
}
|
||
9 months ago
|
|
||
5 months ago
|
in1, in2, err := s.db.CheckIn(ctx, req.FromUserID, req.ToUserID)
|
||
1 year ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
if in1 && in2 {
|
||
8 months ago
|
return nil, servererrs.ErrRelationshipAlready.WrapMsg("already friends has f")
|
||
1 year ago
|
}
|
||
5 months ago
|
if err = s.db.AddFriendRequest(ctx, req.FromUserID, req.ToUserID, req.ReqMsg, req.Ex); err != nil {
|
||
1 year ago
|
return nil, err
|
||
|
}
|
||
8 months ago
|
s.notificationSender.FriendApplicationAddNotification(ctx, req)
|
||
|
s.webhookAfterAddFriend(ctx, &s.config.WebhooksConfig.AfterAddFriend, req)
|
||
1 year ago
|
return resp, nil
|
||
|
}
|
||
|
|
||
1 year ago
|
// ok.
|
||
5 months ago
|
func (s *friendServer) ImportFriends(ctx context.Context, req *relation.ImportFriendReq) (resp *relation.ImportFriendResp, err error) {
|
||
8 months ago
|
if err := authverify.CheckAdmin(ctx, s.config.Share.IMAdminUserID); err != nil {
|
||
1 year ago
|
return nil, err
|
||
|
}
|
||
|
if _, err := s.userRpcClient.GetUsersInfo(ctx, append([]string{req.OwnerUserID}, req.FriendUserIDs...)); err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
8 months ago
|
if datautil.Contain(req.OwnerUserID, req.FriendUserIDs...) {
|
||
|
return nil, servererrs.ErrCanNotAddYourself.WrapMsg("can not add yourself")
|
||
1 year ago
|
}
|
||
8 months ago
|
if datautil.Duplicate(req.FriendUserIDs) {
|
||
|
return nil, errs.ErrArgs.WrapMsg("friend userID repeated")
|
||
1 year ago
|
}
|
||
8 months ago
|
|
||
|
if err := s.webhookBeforeImportFriends(ctx, &s.config.WebhooksConfig.BeforeImportFriends, req); err != nil && err != servererrs.ErrCallbackContinue {
|
||
1 year ago
|
return nil, err
|
||
|
}
|
||
|
|
||
5 months ago
|
if err := s.db.BecomeFriends(ctx, req.OwnerUserID, req.FriendUserIDs, constant.BecomeFriendByImport); err != nil {
|
||
1 year ago
|
return nil, err
|
||
|
}
|
||
1 year ago
|
for _, userID := range req.FriendUserIDs {
|
||
5 months ago
|
s.notificationSender.FriendApplicationAgreedNotification(ctx, &relation.RespondFriendApplyReq{
|
||
1 year ago
|
FromUserID: req.OwnerUserID,
|
||
|
ToUserID: userID,
|
||
|
HandleResult: constant.FriendResponseAgree,
|
||
|
})
|
||
|
}
|
||
8 months ago
|
|
||
|
s.webhookAfterImportFriends(ctx, &s.config.WebhooksConfig.AfterImportFriends, req)
|
||
5 months ago
|
return &relation.ImportFriendResp{}, nil
|
||
1 year ago
|
}
|
||
|
|
||
1 year ago
|
// ok.
|
||
5 months ago
|
func (s *friendServer) RespondFriendApply(ctx context.Context, req *relation.RespondFriendApplyReq) (resp *relation.RespondFriendApplyResp, err error) {
|
||
|
resp = &relation.RespondFriendApplyResp{}
|
||
8 months ago
|
if err := authverify.CheckAccessV3(ctx, req.ToUserID, s.config.Share.IMAdminUserID); err != nil {
|
||
1 year ago
|
return nil, err
|
||
|
}
|
||
|
|
||
6 months ago
|
friendRequest := model.FriendRequest{
|
||
1 year ago
|
FromUserID: req.FromUserID,
|
||
|
ToUserID: req.ToUserID,
|
||
|
HandleMsg: req.HandleMsg,
|
||
|
HandleResult: req.HandleResult,
|
||
|
}
|
||
1 year ago
|
if req.HandleResult == constant.FriendResponseAgree {
|
||
8 months ago
|
if err := s.webhookBeforeAddFriendAgree(ctx, &s.config.WebhooksConfig.BeforeAddFriendAgree, req); err != nil && err != servererrs.ErrCallbackContinue {
|
||
1 year ago
|
return nil, err
|
||
|
}
|
||
5 months ago
|
err := s.db.AgreeFriendRequest(ctx, &friendRequest)
|
||
1 year ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
8 months ago
|
s.notificationSender.FriendApplicationAgreedNotification(ctx, req)
|
||
1 year ago
|
return resp, nil
|
||
|
}
|
||
|
if req.HandleResult == constant.FriendResponseRefuse {
|
||
5 months ago
|
err := s.db.RefuseFriendRequest(ctx, &friendRequest)
|
||
1 year ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
s.notificationSender.FriendApplicationRefusedNotification(ctx, req)
|
||
|
return resp, nil
|
||
|
}
|
||
8 months ago
|
return nil, errs.ErrArgs.WrapMsg("req.HandleResult != -1/1")
|
||
1 year ago
|
}
|
||
|
|
||
1 year ago
|
// ok.
|
||
5 months ago
|
func (s *friendServer) DeleteFriend(ctx context.Context, req *relation.DeleteFriendReq) (resp *relation.DeleteFriendResp, err error) {
|
||
|
resp = &relation.DeleteFriendResp{}
|
||
9 months ago
|
if err := s.userRpcClient.Access(ctx, req.OwnerUserID); err != nil {
|
||
1 year ago
|
return nil, err
|
||
|
}
|
||
5 months ago
|
_, err = s.db.FindFriendsWithError(ctx, req.OwnerUserID, []string{req.FriendUserID})
|
||
1 year ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
5 months ago
|
if err := s.db.Delete(ctx, req.OwnerUserID, []string{req.FriendUserID}); err != nil {
|
||
1 year ago
|
return nil, err
|
||
|
}
|
||
|
s.notificationSender.FriendDeletedNotification(ctx, req)
|
||
8 months ago
|
s.webhookAfterDeleteFriend(ctx, &s.config.WebhooksConfig.AfterDeleteFriend, req)
|
||
1 year ago
|
return resp, nil
|
||
|
}
|
||
|
|
||
1 year ago
|
// ok.
|
||
5 months ago
|
func (s *friendServer) SetFriendRemark(ctx context.Context, req *relation.SetFriendRemarkReq) (resp *relation.SetFriendRemarkResp, err error) {
|
||
8 months ago
|
if err = s.webhookBeforeSetFriendRemark(ctx, &s.config.WebhooksConfig.BeforeSetFriendRemark, req); err != nil && err != servererrs.ErrCallbackContinue {
|
||
1 year ago
|
return nil, err
|
||
|
}
|
||
5 months ago
|
resp = &relation.SetFriendRemarkResp{}
|
||
9 months ago
|
if err := s.userRpcClient.Access(ctx, req.OwnerUserID); err != nil {
|
||
1 year ago
|
return nil, err
|
||
|
}
|
||
5 months ago
|
_, err = s.db.FindFriendsWithError(ctx, req.OwnerUserID, []string{req.FriendUserID})
|
||
1 year ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
5 months ago
|
if err := s.db.UpdateRemark(ctx, req.OwnerUserID, req.FriendUserID, req.Remark); err != nil {
|
||
1 year ago
|
return nil, err
|
||
|
}
|
||
8 months ago
|
s.webhookAfterSetFriendRemark(ctx, &s.config.WebhooksConfig.AfterSetFriendRemark, req)
|
||
1 year ago
|
s.notificationSender.FriendRemarkSetNotification(ctx, req.OwnerUserID, req.FriendUserID)
|
||
|
return resp, nil
|
||
|
}
|
||
|
|
||
1 year ago
|
// ok.
|
||
5 months ago
|
func (s *friendServer) GetDesignatedFriends(ctx context.Context, req *relation.GetDesignatedFriendsReq) (resp *relation.GetDesignatedFriendsResp, err error) {
|
||
|
resp = &relation.GetDesignatedFriendsResp{}
|
||
8 months ago
|
if datautil.Duplicate(req.FriendUserIDs) {
|
||
|
return nil, errs.ErrArgs.WrapMsg("friend userID repeated")
|
||
1 year ago
|
}
|
||
5 months ago
|
friends, err := s.getFriend(ctx, req.OwnerUserID, req.FriendUserIDs)
|
||
1 year ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
5 months ago
|
return &relation.GetDesignatedFriendsResp{
|
||
|
FriendsInfo: friends,
|
||
|
}, nil
|
||
|
}
|
||
|
|
||
|
func (s *friendServer) getFriend(ctx context.Context, ownerUserID string, friendUserIDs []string) ([]*sdkws.FriendInfo, error) {
|
||
|
if len(friendUserIDs) == 0 {
|
||
|
return nil, nil
|
||
|
}
|
||
|
friends, err := s.db.FindFriendsWithError(ctx, ownerUserID, friendUserIDs)
|
||
|
if err != nil {
|
||
1 year ago
|
return nil, err
|
||
|
}
|
||
5 months ago
|
return convert.FriendsDB2Pb(ctx, friends, s.userRpcClient.GetUsersInfoMap)
|
||
1 year ago
|
}
|
||
|
|
||
9 months ago
|
// Get the list of friend requests sent out proactively.
|
||
1 year ago
|
func (s *friendServer) GetDesignatedFriendsApply(ctx context.Context,
|
||
5 months ago
|
req *relation.GetDesignatedFriendsApplyReq,
|
||
|
) (resp *relation.GetDesignatedFriendsApplyResp, err error) {
|
||
|
friendRequests, err := s.db.FindBothFriendRequests(ctx, req.FromUserID, req.ToUserID)
|
||
1 year ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
5 months ago
|
resp = &relation.GetDesignatedFriendsApplyResp{}
|
||
1 year ago
|
resp.FriendRequests, err = convert.FriendRequestDB2Pb(ctx, friendRequests, s.userRpcClient.GetUsersInfoMap)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return resp, nil
|
||
|
}
|
||
|
|
||
9 months ago
|
// Get received friend requests (i.e., those initiated by others).
|
||
5 months ago
|
func (s *friendServer) GetPaginationFriendsApplyTo(ctx context.Context, req *relation.GetPaginationFriendsApplyToReq) (resp *relation.GetPaginationFriendsApplyToResp, err error) {
|
||
9 months ago
|
if err := s.userRpcClient.Access(ctx, req.UserID); err != nil {
|
||
1 year ago
|
return nil, err
|
||
|
}
|
||
5 months ago
|
total, friendRequests, err := s.db.PageFriendRequestToMe(ctx, req.UserID, req.Pagination)
|
||
1 year ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
5 months ago
|
resp = &relation.GetPaginationFriendsApplyToResp{}
|
||
1 year ago
|
resp.FriendRequests, err = convert.FriendRequestDB2Pb(ctx, friendRequests, s.userRpcClient.GetUsersInfoMap)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
resp.Total = int32(total)
|
||
|
return resp, nil
|
||
|
}
|
||
|
|
||
5 months ago
|
func (s *friendServer) GetPaginationFriendsApplyFrom(ctx context.Context, req *relation.GetPaginationFriendsApplyFromReq) (resp *relation.GetPaginationFriendsApplyFromResp, err error) {
|
||
|
resp = &relation.GetPaginationFriendsApplyFromResp{}
|
||
9 months ago
|
if err := s.userRpcClient.Access(ctx, req.UserID); err != nil {
|
||
1 year ago
|
return nil, err
|
||
|
}
|
||
5 months ago
|
total, friendRequests, err := s.db.PageFriendRequestFromMe(ctx, req.UserID, req.Pagination)
|
||
1 year ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
resp.FriendRequests, err = convert.FriendRequestDB2Pb(ctx, friendRequests, s.userRpcClient.GetUsersInfoMap)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
resp.Total = int32(total)
|
||
|
return resp, nil
|
||
|
}
|
||
|
|
||
1 year ago
|
// ok.
|
||
5 months ago
|
func (s *friendServer) IsFriend(ctx context.Context, req *relation.IsFriendReq) (resp *relation.IsFriendResp, err error) {
|
||
|
resp = &relation.IsFriendResp{}
|
||
|
resp.InUser1Friends, resp.InUser2Friends, err = s.db.CheckIn(ctx, req.UserID1, req.UserID2)
|
||
1 year ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return resp, nil
|
||
|
}
|
||
|
|
||
5 months ago
|
func (s *friendServer) GetPaginationFriends(ctx context.Context, req *relation.GetPaginationFriendsReq) (resp *relation.GetPaginationFriendsResp, err error) {
|
||
9 months ago
|
if err := s.userRpcClient.Access(ctx, req.UserID); err != nil {
|
||
1 year ago
|
return nil, err
|
||
|
}
|
||
5 months ago
|
total, friends, err := s.db.PageOwnerFriends(ctx, req.UserID, req.Pagination)
|
||
1 year ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
5 months ago
|
resp = &relation.GetPaginationFriendsResp{}
|
||
1 year ago
|
resp.FriendsInfo, err = convert.FriendsDB2Pb(ctx, friends, s.userRpcClient.GetUsersInfoMap)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
resp.Total = int32(total)
|
||
|
return resp, nil
|
||
|
}
|
||
|
|
||
5 months ago
|
func (s *friendServer) GetFriendIDs(ctx context.Context, req *relation.GetFriendIDsReq) (resp *relation.GetFriendIDsResp, err error) {
|
||
9 months ago
|
if err := s.userRpcClient.Access(ctx, req.UserID); err != nil {
|
||
1 year ago
|
return nil, err
|
||
|
}
|
||
5 months ago
|
resp = &relation.GetFriendIDsResp{}
|
||
|
resp.FriendIDs, err = s.db.FindFriendUserIDs(ctx, req.UserID)
|
||
1 year ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return resp, nil
|
||
|
}
|
||
1 year ago
|
|
||
5 months ago
|
func (s *friendServer) GetSpecifiedFriendsInfo(ctx context.Context, req *relation.GetSpecifiedFriendsInfoReq) (*relation.GetSpecifiedFriendsInfoResp, error) {
|
||
1 year ago
|
if len(req.UserIDList) == 0 {
|
||
8 months ago
|
return nil, errs.ErrArgs.WrapMsg("userIDList is empty")
|
||
1 year ago
|
}
|
||
8 months ago
|
if datautil.Duplicate(req.UserIDList) {
|
||
|
return nil, errs.ErrArgs.WrapMsg("userIDList repeated")
|
||
1 year ago
|
}
|
||
|
userMap, err := s.userRpcClient.GetUsersInfoMap(ctx, req.UserIDList)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
5 months ago
|
friends, err := s.db.FindFriendsWithError(ctx, req.OwnerUserID, req.UserIDList)
|
||
1 year ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
blacks, err := s.blackDatabase.FindBlackInfos(ctx, req.OwnerUserID, req.UserIDList)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
6 months ago
|
friendMap := datautil.SliceToMap(friends, func(e *model.Friend) string {
|
||
1 year ago
|
return e.FriendUserID
|
||
|
})
|
||
6 months ago
|
blackMap := datautil.SliceToMap(blacks, func(e *model.Black) string {
|
||
1 year ago
|
return e.BlockUserID
|
||
|
})
|
||
5 months ago
|
resp := &relation.GetSpecifiedFriendsInfoResp{
|
||
|
Infos: make([]*relation.GetSpecifiedFriendsInfoInfo, 0, len(req.UserIDList)),
|
||
1 year ago
|
}
|
||
|
for _, userID := range req.UserIDList {
|
||
|
user := userMap[userID]
|
||
|
if user == nil {
|
||
|
continue
|
||
|
}
|
||
|
var friendInfo *sdkws.FriendInfo
|
||
|
if friend := friendMap[userID]; friend != nil {
|
||
|
friendInfo = &sdkws.FriendInfo{
|
||
|
OwnerUserID: friend.OwnerUserID,
|
||
|
Remark: friend.Remark,
|
||
|
CreateTime: friend.CreateTime.UnixMilli(),
|
||
|
AddSource: friend.AddSource,
|
||
|
OperatorUserID: friend.OperatorUserID,
|
||
|
Ex: friend.Ex,
|
||
12 months ago
|
IsPinned: friend.IsPinned,
|
||
1 year ago
|
}
|
||
|
}
|
||
|
var blackInfo *sdkws.BlackInfo
|
||
|
if black := blackMap[userID]; black != nil {
|
||
|
blackInfo = &sdkws.BlackInfo{
|
||
|
OwnerUserID: black.OwnerUserID,
|
||
|
CreateTime: black.CreateTime.UnixMilli(),
|
||
|
AddSource: black.AddSource,
|
||
|
OperatorUserID: black.OperatorUserID,
|
||
|
Ex: black.Ex,
|
||
|
}
|
||
|
}
|
||
5 months ago
|
resp.Infos = append(resp.Infos, &relation.GetSpecifiedFriendsInfoInfo{
|
||
1 year ago
|
UserInfo: user,
|
||
|
FriendInfo: friendInfo,
|
||
|
BlackInfo: blackInfo,
|
||
|
})
|
||
|
}
|
||
|
return resp, nil
|
||
|
}
|
||
5 months ago
|
|
||
11 months ago
|
func (s *friendServer) UpdateFriends(
|
||
12 months ago
|
ctx context.Context,
|
||
5 months ago
|
req *relation.UpdateFriendsReq,
|
||
|
) (*relation.UpdateFriendsResp, error) {
|
||
12 months ago
|
if len(req.FriendUserIDs) == 0 {
|
||
8 months ago
|
return nil, errs.ErrArgs.WrapMsg("friendIDList is empty")
|
||
12 months ago
|
}
|
||
8 months ago
|
if datautil.Duplicate(req.FriendUserIDs) {
|
||
|
return nil, errs.ErrArgs.WrapMsg("friendIDList repeated")
|
||
12 months ago
|
}
|
||
11 months ago
|
|
||
5 months ago
|
_, err := s.db.FindFriendsWithError(ctx, req.OwnerUserID, req.FriendUserIDs)
|
||
12 months ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
11 months ago
|
val := make(map[string]any)
|
||
|
|
||
|
if req.IsPinned != nil {
|
||
|
val["is_pinned"] = req.IsPinned.Value
|
||
|
}
|
||
|
if req.Remark != nil {
|
||
|
val["remark"] = req.Remark.Value
|
||
|
}
|
||
|
if req.Ex != nil {
|
||
|
val["ex"] = req.Ex.Value
|
||
|
}
|
||
5 months ago
|
if err = s.db.UpdateFriends(ctx, req.OwnerUserID, req.FriendUserIDs, val); err != nil {
|
||
11 months ago
|
return nil, err
|
||
12 months ago
|
}
|
||
|
|
||
5 months ago
|
resp := &relation.UpdateFriendsResp{}
|
||
11 months ago
|
|
||
8 months ago
|
s.notificationSender.FriendsInfoUpdateNotification(ctx, req.OwnerUserID, req.FriendUserIDs)
|
||
12 months ago
|
return resp, nil
|
||
|
}
|
||
5 months ago
|
|
||
|
func (s *friendServer) GetIncrementalFriendsApplyTo(ctx context.Context, req *relation.GetIncrementalFriendsApplyToReq) (*relation.GetIncrementalFriendsApplyToResp, error) {
|
||
|
// TODO implement me
|
||
|
return nil, nil
|
||
|
}
|
||
|
|
||
|
func (s *friendServer) GetIncrementalFriendsApplyFrom(ctx context.Context, req *relation.GetIncrementalFriendsApplyFromReq) (*relation.GetIncrementalFriendsApplyFromResp, error) {
|
||
|
// TODO implement me
|
||
|
return nil, nil
|
||
|
}
|
||
|
|
||
|
func (s *friendServer) GetIncrementalBlacks(ctx context.Context, req *relation.GetIncrementalBlacksReq) (*relation.GetIncrementalBlacksResp, error) {
|
||
|
// TODO implement me
|
||
|
return nil, nil
|
||
|
}
|