mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-07-04 18:11:16 +08:00
804 lines
27 KiB
Go
804 lines
27 KiB
Go
// 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.
|
||
|
||
package relation
|
||
|
||
import (
|
||
"context"
|
||
"time"
|
||
|
||
"github.com/openimsdk/open-im-server/v3/pkg/notification/common_user"
|
||
"github.com/openimsdk/open-im-server/v3/pkg/rpcli"
|
||
|
||
"github.com/openimsdk/tools/mq/memamq"
|
||
|
||
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
|
||
"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"
|
||
"github.com/openimsdk/open-im-server/v3/pkg/common/webhook"
|
||
"github.com/openimsdk/open-im-server/v3/pkg/localcache"
|
||
"github.com/openimsdk/tools/db/redisutil"
|
||
|
||
"github.com/openimsdk/open-im-server/v3/pkg/authverify"
|
||
"github.com/openimsdk/open-im-server/v3/pkg/common/convert"
|
||
"github.com/openimsdk/open-im-server/v3/pkg/common/servererrs"
|
||
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/controller"
|
||
"github.com/openimsdk/open-im-server/v3/pkg/util/conversationutil"
|
||
"github.com/openimsdk/protocol/constant"
|
||
conversationpb "github.com/openimsdk/protocol/conversation"
|
||
"github.com/openimsdk/protocol/relation"
|
||
"github.com/openimsdk/protocol/sdkws"
|
||
"github.com/openimsdk/protocol/wrapperspb"
|
||
"github.com/openimsdk/tools/db/mongoutil"
|
||
"github.com/openimsdk/tools/discovery"
|
||
"github.com/openimsdk/tools/errs"
|
||
"github.com/openimsdk/tools/log"
|
||
"github.com/openimsdk/tools/utils/datautil"
|
||
"google.golang.org/grpc"
|
||
)
|
||
|
||
// keep stable add_source value for one-way friendship even if protocol constants lag behind.
|
||
const becomeFriendByOneway int32 = 3
|
||
|
||
type friendServer struct {
|
||
relation.UnimplementedFriendServer
|
||
db controller.FriendDatabase
|
||
blackDatabase controller.BlackDatabase
|
||
globalBlackDB controller.UserGlobalBlackDatabase
|
||
userMuteDB controller.UserMuteDatabase
|
||
notificationSender *FriendNotificationSender
|
||
RegisterCenter discovery.SvcDiscoveryRegistry
|
||
config *Config
|
||
webhookClient *webhook.Client
|
||
queue *memamq.MemoryQueue
|
||
userClient *rpcli.UserClient
|
||
conversationClient *rpcli.ConversationClient
|
||
}
|
||
|
||
type Config struct {
|
||
RpcConfig config.Friend
|
||
RedisConfig config.Redis
|
||
MongodbConfig config.Mongo
|
||
// ZookeeperConfig config.ZooKeeper
|
||
NotificationConfig config.Notification
|
||
Share config.Share
|
||
WebhooksConfig config.Webhooks
|
||
LocalCacheConfig config.LocalCache
|
||
Discovery config.Discovery
|
||
}
|
||
|
||
func Start(ctx context.Context, config *Config, client discovery.SvcDiscoveryRegistry, server *grpc.Server) error {
|
||
mgocli, err := mongoutil.NewMongoDB(ctx, config.MongodbConfig.Build())
|
||
if err != nil {
|
||
return err
|
||
}
|
||
rdb, err := redisutil.NewRedisClient(ctx, config.RedisConfig.Build())
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
friendMongoDB, err := mgo.NewFriendMongo(mgocli.GetDB())
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
friendRequestMongoDB, err := mgo.NewFriendRequestMongo(mgocli.GetDB())
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
blackMongoDB, err := mgo.NewBlackMongo(mgocli.GetDB())
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
globalBlackMongoDB, err := mgo.NewUserGlobalBlackMongo(mgocli.GetDB())
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
userMuteMongoDB, err := mgo.NewUserMuteMongo(mgocli.GetDB())
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
userConn, err := client.GetConn(ctx, config.Share.RpcRegisterName.User)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
msgConn, err := client.GetConn(ctx, config.Share.RpcRegisterName.Msg)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
conversationConn, err := client.GetConn(ctx, config.Share.RpcRegisterName.Conversation)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
userClient := rpcli.NewUserClient(userConn)
|
||
|
||
database := controller.NewFriendDatabase(
|
||
friendMongoDB,
|
||
friendRequestMongoDB,
|
||
redis.NewFriendCacheRedis(rdb, &config.LocalCacheConfig, friendMongoDB, redis.GetRocksCacheOptions()),
|
||
mgocli.GetTx(),
|
||
)
|
||
// Initialize notification sender
|
||
notificationSender := NewFriendNotificationSender(
|
||
&config.NotificationConfig,
|
||
rpcli.NewMsgClient(msgConn),
|
||
WithRpcFunc(userClient.GetUsersInfo),
|
||
WithFriendDB(database),
|
||
)
|
||
localcache.InitLocalCache(&config.LocalCacheConfig)
|
||
|
||
// Register Friend server with refactored MongoDB and Redis integrations
|
||
relation.RegisterFriendServer(server, &friendServer{
|
||
db: database,
|
||
blackDatabase: controller.NewBlackDatabase(
|
||
blackMongoDB,
|
||
redis.NewBlackCacheRedis(rdb, &config.LocalCacheConfig, blackMongoDB, redis.GetRocksCacheOptions()),
|
||
),
|
||
globalBlackDB: controller.NewUserGlobalBlackDatabase(globalBlackMongoDB),
|
||
userMuteDB: controller.NewUserMuteDatabase(userMuteMongoDB),
|
||
notificationSender: notificationSender,
|
||
RegisterCenter: client,
|
||
config: config,
|
||
webhookClient: webhook.NewWebhookClient(config.WebhooksConfig.URL),
|
||
queue: memamq.NewMemoryQueue(16, 1024*1024),
|
||
userClient: userClient,
|
||
conversationClient: rpcli.NewConversationClient(conversationConn),
|
||
})
|
||
return nil
|
||
}
|
||
|
||
// ok.
|
||
func (s *friendServer) ApplyToAddFriend(ctx context.Context, req *relation.ApplyToAddFriendReq) (resp *relation.ApplyToAddFriendResp, err error) {
|
||
resp = &relation.ApplyToAddFriendResp{}
|
||
if err := authverify.CheckAccessV3(ctx, req.FromUserID, s.config.Share.IMAdminUserID); err != nil {
|
||
return nil, err
|
||
}
|
||
if req.ToUserID == req.FromUserID {
|
||
return nil, servererrs.ErrCanNotAddYourself.WrapMsg("req.ToUserID", req.ToUserID)
|
||
}
|
||
if err = s.webhookBeforeAddFriend(ctx, &s.config.WebhooksConfig.BeforeAddFriend, req); err != nil && err != servererrs.ErrCallbackContinue {
|
||
return nil, err
|
||
}
|
||
if err := s.userClient.CheckUser(ctx, []string{req.ToUserID, req.FromUserID}); err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
in1, in2, err := s.db.CheckIn(ctx, req.FromUserID, req.ToUserID)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
if in1 && in2 {
|
||
return nil, servererrs.ErrRelationshipAlready.WrapMsg("already friends has f")
|
||
}
|
||
if err = s.db.AddFriendRequest(ctx, req.FromUserID, req.ToUserID, req.ReqMsg, req.Ex); err != nil {
|
||
return nil, err
|
||
}
|
||
s.notificationSender.FriendApplicationAddNotification(ctx, req)
|
||
s.webhookAfterAddFriend(ctx, &s.config.WebhooksConfig.AfterAddFriend, req)
|
||
return resp, nil
|
||
}
|
||
|
||
// ok.
|
||
func (s *friendServer) ImportFriends(ctx context.Context, req *relation.ImportFriendReq) (resp *relation.ImportFriendResp, err error) {
|
||
if err := authverify.CheckAdmin(ctx, s.config.Share.IMAdminUserID); err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
if err := s.userClient.CheckUser(ctx, append([]string{req.OwnerUserID}, req.FriendUserIDs...)); err != nil {
|
||
return nil, err
|
||
}
|
||
if datautil.Contain(req.OwnerUserID, req.FriendUserIDs...) {
|
||
return nil, servererrs.ErrCanNotAddYourself.WrapMsg("can not add yourself")
|
||
}
|
||
if datautil.Duplicate(req.FriendUserIDs) {
|
||
return nil, errs.ErrArgs.WrapMsg("friend userID repeated")
|
||
}
|
||
|
||
if err := s.webhookBeforeImportFriends(ctx, &s.config.WebhooksConfig.BeforeImportFriends, req); err != nil && err != servererrs.ErrCallbackContinue {
|
||
return nil, err
|
||
}
|
||
|
||
if err := s.db.BecomeFriends(ctx, req.OwnerUserID, req.FriendUserIDs, constant.BecomeFriendByImport); err != nil {
|
||
return nil, err
|
||
}
|
||
for _, userID := range req.FriendUserIDs {
|
||
s.notificationSender.FriendApplicationAgreedNotification(ctx, &relation.RespondFriendApplyReq{
|
||
FromUserID: req.OwnerUserID,
|
||
ToUserID: userID,
|
||
HandleResult: constant.FriendResponseAgree,
|
||
}, false)
|
||
}
|
||
|
||
s.webhookAfterImportFriends(ctx, &s.config.WebhooksConfig.AfterImportFriends, req)
|
||
return &relation.ImportFriendResp{}, nil
|
||
}
|
||
|
||
// ok.
|
||
func (s *friendServer) RespondFriendApply(ctx context.Context, req *relation.RespondFriendApplyReq) (resp *relation.RespondFriendApplyResp, err error) {
|
||
resp = &relation.RespondFriendApplyResp{}
|
||
if err := authverify.CheckAccessV3(ctx, req.ToUserID, s.config.Share.IMAdminUserID); err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
friendRequest := model.FriendRequest{
|
||
FromUserID: req.FromUserID,
|
||
ToUserID: req.ToUserID,
|
||
HandleMsg: req.HandleMsg,
|
||
HandleResult: req.HandleResult,
|
||
}
|
||
if req.HandleResult == constant.FriendResponseAgree {
|
||
if err := s.webhookBeforeAddFriendAgree(ctx, &s.config.WebhooksConfig.BeforeAddFriendAgree, req); err != nil && err != servererrs.ErrCallbackContinue {
|
||
return nil, err
|
||
}
|
||
err := s.db.AgreeFriendRequest(ctx, &friendRequest)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
s.webhookAfterAddFriendAgree(ctx, &s.config.WebhooksConfig.AfterAddFriendAgree, req)
|
||
s.notificationSender.FriendApplicationAgreedNotification(ctx, req, true)
|
||
return resp, nil
|
||
}
|
||
if req.HandleResult == constant.FriendResponseRefuse {
|
||
err := s.db.RefuseFriendRequest(ctx, &friendRequest)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
s.notificationSender.FriendApplicationRefusedNotification(ctx, req)
|
||
return resp, nil
|
||
}
|
||
return nil, errs.ErrArgs.WrapMsg("req.HandleResult != -1/1")
|
||
}
|
||
|
||
// ok.
|
||
func (s *friendServer) DeleteFriend(ctx context.Context, req *relation.DeleteFriendReq) (resp *relation.DeleteFriendResp, err error) {
|
||
if err := authverify.CheckAccessV3(ctx, req.OwnerUserID, s.config.Share.IMAdminUserID); err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
_, err = s.db.FindFriendsWithError(ctx, req.OwnerUserID, []string{req.FriendUserID})
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
if err := s.db.Delete(ctx, req.OwnerUserID, []string{req.FriendUserID}); err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
s.notificationSender.FriendDeletedNotification(ctx, req)
|
||
s.webhookAfterDeleteFriend(ctx, &s.config.WebhooksConfig.AfterDeleteFriend, req)
|
||
|
||
return &relation.DeleteFriendResp{}, nil
|
||
}
|
||
|
||
// DeleteFriendOneway 单向删除好友:只删除 ownerUserID 侧的 friend 文档;
|
||
// 对端 friend 文档保留;仅向 owner 下发 FriendsInfoUpdateNotification 以刷新本地好友列表,
|
||
// 不向对端发送 FriendDeletedNotification。
|
||
func (s *friendServer) DeleteFriendOneway(ctx context.Context, req *relation.DeleteFriendReq) (resp *relation.DeleteFriendResp, err error) {
|
||
if err := authverify.CheckAccessV3(ctx, req.OwnerUserID, s.config.Share.IMAdminUserID); err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
_, err = s.db.FindFriendsWithError(ctx, req.OwnerUserID, []string{req.FriendUserID})
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
if err := s.db.Delete(ctx, req.OwnerUserID, []string{req.FriendUserID}); err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
s.notificationSender.FriendDeletedOnewayNotification(ctx, req.OwnerUserID, req.FriendUserID)
|
||
s.webhookAfterDeleteFriend(ctx, &s.config.WebhooksConfig.AfterDeleteFriend, req)
|
||
|
||
return &relation.DeleteFriendResp{}, nil
|
||
}
|
||
|
||
// ok.
|
||
func (s *friendServer) SetFriendRemark(ctx context.Context, req *relation.SetFriendRemarkReq) (resp *relation.SetFriendRemarkResp, err error) {
|
||
if err = s.webhookBeforeSetFriendRemark(ctx, &s.config.WebhooksConfig.BeforeSetFriendRemark, req); err != nil && err != servererrs.ErrCallbackContinue {
|
||
return nil, err
|
||
}
|
||
|
||
if err := authverify.CheckAccessV3(ctx, req.OwnerUserID, s.config.Share.IMAdminUserID); err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
_, err = s.db.FindFriendsWithError(ctx, req.OwnerUserID, []string{req.FriendUserID})
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
if err := s.db.UpdateRemark(ctx, req.OwnerUserID, req.FriendUserID, req.Remark); err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
s.webhookAfterSetFriendRemark(ctx, &s.config.WebhooksConfig.AfterSetFriendRemark, req)
|
||
s.notificationSender.FriendRemarkSetNotification(ctx, req.OwnerUserID, req.FriendUserID)
|
||
|
||
return &relation.SetFriendRemarkResp{}, nil
|
||
}
|
||
|
||
func (s *friendServer) GetFriendInfo(ctx context.Context, req *relation.GetFriendInfoReq) (*relation.GetFriendInfoResp, error) {
|
||
if err := authverify.CheckAccessV3(ctx, req.OwnerUserID, s.config.Share.IMAdminUserID); err != nil {
|
||
return nil, err
|
||
}
|
||
if err := s.checkUsersNotGlobalBlocked(ctx, req.FriendUserIDs); err != nil {
|
||
return nil, err
|
||
}
|
||
friends, err := s.db.FindFriendsWithError(ctx, req.OwnerUserID, req.FriendUserIDs)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
users, err := s.userClient.GetUsersInfoMap(ctx, req.FriendUserIDs)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return &relation.GetFriendInfoResp{FriendInfos: convert.FriendOnlyDB2PbOnly(friends, users)}, nil
|
||
}
|
||
|
||
func (s *friendServer) GetDesignatedFriends(ctx context.Context, req *relation.GetDesignatedFriendsReq) (resp *relation.GetDesignatedFriendsResp, err error) {
|
||
resp = &relation.GetDesignatedFriendsResp{}
|
||
if datautil.Duplicate(req.FriendUserIDs) {
|
||
return nil, errs.ErrArgs.WrapMsg("friend userID repeated")
|
||
}
|
||
if err := authverify.CheckAccessV3(ctx, req.OwnerUserID, s.config.Share.IMAdminUserID); err != nil {
|
||
return nil, err
|
||
}
|
||
if err := s.checkUsersNotGlobalBlocked(ctx, req.FriendUserIDs); err != nil {
|
||
return nil, err
|
||
}
|
||
friends, err := s.getFriend(ctx, req.OwnerUserID, req.FriendUserIDs)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return &relation.GetDesignatedFriendsResp{
|
||
FriendsInfo: friends,
|
||
}, nil
|
||
}
|
||
|
||
// checkUsersNotGlobalBlocked returns ErrUserBlocked if any of the given userIDs are in the global blacklist.
|
||
func (s *friendServer) checkUsersNotGlobalBlocked(ctx context.Context, userIDs []string) error {
|
||
if len(userIDs) == 0 {
|
||
return nil
|
||
}
|
||
blocked, err := s.globalBlackDB.FindBlocked(ctx, userIDs)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if len(blocked) == 0 {
|
||
return nil
|
||
}
|
||
bannedIDs := make([]string, 0, len(blocked))
|
||
for _, b := range blocked {
|
||
bannedIDs = append(bannedIDs, b.UserID)
|
||
}
|
||
return servererrs.ErrUserBlocked.WrapMsg("user is banned", "userIDs", bannedIDs)
|
||
}
|
||
|
||
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 {
|
||
return nil, err
|
||
}
|
||
return convert.FriendsDB2Pb(ctx, friends, s.userClient.GetUsersInfoMap)
|
||
}
|
||
|
||
// Get the list of friend requests sent out proactively.
|
||
func (s *friendServer) GetDesignatedFriendsApply(ctx context.Context,
|
||
req *relation.GetDesignatedFriendsApplyReq,
|
||
) (resp *relation.GetDesignatedFriendsApplyResp, err error) {
|
||
friendRequests, err := s.db.FindBothFriendRequests(ctx, req.FromUserID, req.ToUserID)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
resp = &relation.GetDesignatedFriendsApplyResp{}
|
||
resp.FriendRequests, err = convert.FriendRequestDB2Pb(ctx, friendRequests, s.getCommonUserMap)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return resp, nil
|
||
}
|
||
|
||
// Get received friend requests (i.e., those initiated by others).
|
||
func (s *friendServer) GetPaginationFriendsApplyTo(ctx context.Context, req *relation.GetPaginationFriendsApplyToReq) (resp *relation.GetPaginationFriendsApplyToResp, err error) {
|
||
if err := authverify.CheckAccessV3(ctx, req.UserID, s.config.Share.IMAdminUserID); err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
handleResults := datautil.Slice(req.HandleResults, func(e int32) int {
|
||
return int(e)
|
||
})
|
||
total, friendRequests, err := s.db.PageFriendRequestToMe(ctx, req.UserID, handleResults, req.Pagination)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
resp = &relation.GetPaginationFriendsApplyToResp{}
|
||
resp.FriendRequests, err = convert.FriendRequestDB2Pb(ctx, friendRequests, s.getCommonUserMap)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
resp.Total = int32(total)
|
||
|
||
return resp, nil
|
||
}
|
||
|
||
func (s *friendServer) GetPaginationFriendsApplyFrom(ctx context.Context, req *relation.GetPaginationFriendsApplyFromReq) (resp *relation.GetPaginationFriendsApplyFromResp, err error) {
|
||
resp = &relation.GetPaginationFriendsApplyFromResp{}
|
||
|
||
if err := authverify.CheckAccessV3(ctx, req.UserID, s.config.Share.IMAdminUserID); err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
handleResults := datautil.Slice(req.HandleResults, func(e int32) int {
|
||
return int(e)
|
||
})
|
||
total, friendRequests, err := s.db.PageFriendRequestFromMe(ctx, req.UserID, handleResults, req.Pagination)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
resp.FriendRequests, err = convert.FriendRequestDB2Pb(ctx, friendRequests, s.getCommonUserMap)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
resp.Total = int32(total)
|
||
|
||
return resp, nil
|
||
}
|
||
|
||
// ok.
|
||
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)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return resp, nil
|
||
}
|
||
|
||
func (s *friendServer) GetPaginationFriends(ctx context.Context, req *relation.GetPaginationFriendsReq) (resp *relation.GetPaginationFriendsResp, err error) {
|
||
if err := authverify.CheckAccessV3(ctx, req.UserID, s.config.Share.IMAdminUserID); err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
total, friends, err := s.db.PageOwnerFriends(ctx, req.UserID, req.Pagination)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
resp = &relation.GetPaginationFriendsResp{}
|
||
resp.FriendsInfo, err = convert.FriendsDB2Pb(ctx, friends, s.userClient.GetUsersInfoMap)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
resp.Total = int32(total)
|
||
|
||
return resp, nil
|
||
}
|
||
|
||
func (s *friendServer) GetFriendIDs(ctx context.Context, req *relation.GetFriendIDsReq) (resp *relation.GetFriendIDsResp, err error) {
|
||
if err := authverify.CheckAccessV3(ctx, req.UserID, s.config.Share.IMAdminUserID); err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
resp = &relation.GetFriendIDsResp{}
|
||
resp.FriendIDs, err = s.db.FindFriendUserIDs(ctx, req.UserID)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
return resp, nil
|
||
}
|
||
|
||
func (s *friendServer) GetSpecifiedFriendsInfo(ctx context.Context, req *relation.GetSpecifiedFriendsInfoReq) (*relation.GetSpecifiedFriendsInfoResp, error) {
|
||
if len(req.UserIDList) == 0 {
|
||
return nil, errs.ErrArgs.WrapMsg("userIDList is empty")
|
||
}
|
||
|
||
if datautil.Duplicate(req.UserIDList) {
|
||
return nil, errs.ErrArgs.WrapMsg("userIDList repeated")
|
||
}
|
||
|
||
if err := authverify.CheckAccessV3(ctx, req.OwnerUserID, s.config.Share.IMAdminUserID); err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
userMap, err := s.userClient.GetUsersInfoMap(ctx, req.UserIDList)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
friends, err := s.db.FindFriendsWithError(ctx, req.OwnerUserID, req.UserIDList)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
blacks, err := s.blackDatabase.FindBlackInfos(ctx, req.OwnerUserID, req.UserIDList)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
friendMap := datautil.SliceToMap(friends, func(e *model.Friend) string {
|
||
return e.FriendUserID
|
||
})
|
||
|
||
blackMap := datautil.SliceToMap(blacks, func(e *model.Black) string {
|
||
return e.BlockUserID
|
||
})
|
||
|
||
resp := &relation.GetSpecifiedFriendsInfoResp{
|
||
Infos: make([]*relation.GetSpecifiedFriendsInfoInfo, 0, len(req.UserIDList)),
|
||
}
|
||
|
||
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,
|
||
IsPinned: friend.IsPinned,
|
||
IsMute: friend.IsMuted,
|
||
MuteDuration: friend.MuteDuration,
|
||
MuteEndTime: friend.MuteEndTime,
|
||
}
|
||
}
|
||
|
||
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,
|
||
}
|
||
}
|
||
|
||
resp.Infos = append(resp.Infos, &relation.GetSpecifiedFriendsInfoInfo{
|
||
UserInfo: user,
|
||
FriendInfo: friendInfo,
|
||
BlackInfo: blackInfo,
|
||
})
|
||
}
|
||
|
||
return resp, nil
|
||
}
|
||
|
||
func (s *friendServer) UpdateFriends(
|
||
ctx context.Context,
|
||
req *relation.UpdateFriendsReq,
|
||
) (*relation.UpdateFriendsResp, error) {
|
||
if len(req.FriendUserIDs) == 0 {
|
||
return nil, errs.ErrArgs.WrapMsg("friendIDList is empty")
|
||
}
|
||
if datautil.Duplicate(req.FriendUserIDs) {
|
||
return nil, errs.ErrArgs.WrapMsg("friendIDList repeated")
|
||
}
|
||
|
||
if err := authverify.CheckAccessV3(ctx, req.OwnerUserID, s.config.Share.IMAdminUserID); err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
_, err := s.db.FindFriendsWithError(ctx, req.OwnerUserID, req.FriendUserIDs)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
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
|
||
}
|
||
if req.IsMute != nil {
|
||
val["is_muted"] = req.IsMute.Value
|
||
}
|
||
if req.MuteDuration != nil {
|
||
val["mute_duration"] = req.MuteDuration.Value
|
||
}
|
||
if req.MuteEndTime != nil {
|
||
val["mute_end_time"] = req.MuteEndTime.Value
|
||
}
|
||
if err = s.db.UpdateFriends(ctx, req.OwnerUserID, req.FriendUserIDs, val); err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
if req.IsPinned != nil {
|
||
for _, friendUserID := range req.FriendUserIDs {
|
||
convID := conversationutil.GenConversationIDForSingle(req.OwnerUserID, friendUserID)
|
||
if err := s.conversationClient.SetConversations(ctx, []string{req.OwnerUserID},
|
||
&conversationpb.ConversationReq{
|
||
ConversationID: convID,
|
||
ConversationType: constant.SingleChatType,
|
||
UserID: friendUserID,
|
||
IsPinned: req.IsPinned,
|
||
}); err != nil {
|
||
log.ZWarn(ctx, "sync conversation isPinned failed", err,
|
||
"ownerUserID", req.OwnerUserID, "friendUserID", friendUserID)
|
||
}
|
||
}
|
||
}
|
||
|
||
if req.IsMute != nil {
|
||
recvMsgOpt := int32(constant.ReceiveNotNotifyMessage)
|
||
if !req.IsMute.Value {
|
||
recvMsgOpt = constant.ReceiveMessage
|
||
}
|
||
for _, friendUserID := range req.FriendUserIDs {
|
||
convID := conversationutil.GenConversationIDForSingle(req.OwnerUserID, friendUserID)
|
||
if err := s.conversationClient.SetConversations(ctx, []string{req.OwnerUserID},
|
||
&conversationpb.ConversationReq{
|
||
ConversationID: convID,
|
||
ConversationType: constant.SingleChatType,
|
||
UserID: friendUserID,
|
||
RecvMsgOpt: &wrapperspb.Int32Value{Value: recvMsgOpt},
|
||
}); err != nil {
|
||
log.ZWarn(ctx, "sync conversation recvMsgOpt failed", err,
|
||
"ownerUserID", req.OwnerUserID, "friendUserID", friendUserID)
|
||
}
|
||
}
|
||
}
|
||
|
||
resp := &relation.UpdateFriendsResp{}
|
||
|
||
s.notificationSender.FriendsInfoUpdateNotification(ctx, req.OwnerUserID, req.FriendUserIDs)
|
||
return resp, nil
|
||
}
|
||
|
||
func (s *friendServer) GetSelfUnhandledApplyCount(ctx context.Context, req *relation.GetSelfUnhandledApplyCountReq) (*relation.GetSelfUnhandledApplyCountResp, error) {
|
||
if err := authverify.CheckAccessV3(ctx, req.UserID, s.config.Share.IMAdminUserID); err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
count, err := s.db.GetUnhandledCount(ctx, req.UserID, req.Time)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
return &relation.GetSelfUnhandledApplyCountResp{
|
||
Count: count,
|
||
}, nil
|
||
}
|
||
|
||
func (s *friendServer) GetPinnedFriendIDs(ctx context.Context, req *relation.GetPinnedFriendIDsReq) (*relation.GetPinnedFriendIDsResp, error) {
|
||
if err := authverify.CheckAccessV3(ctx, req.UserID, s.config.Share.IMAdminUserID); err != nil {
|
||
return nil, err
|
||
}
|
||
ids, err := s.db.GetPinnedFriendIDs(ctx, req.UserID)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return &relation.GetPinnedFriendIDsResp{FriendUserIDs: ids}, nil
|
||
}
|
||
|
||
// AddOnewayFriend adds B to A's friend list without requiring B's consent.
|
||
// Only the A->B side of the friendship is created; B's friend list is unaffected.
|
||
func (s *friendServer) AddOnewayFriend(ctx context.Context, req *relation.ApplyToAddFriendReq) (*relation.ApplyToAddFriendResp, error) {
|
||
if err := authverify.CheckAccessV3(ctx, req.FromUserID, s.config.Share.IMAdminUserID); err != nil {
|
||
return nil, err
|
||
}
|
||
if req.ToUserID == req.FromUserID {
|
||
return nil, servererrs.ErrCanNotAddYourself.WrapMsg("req.ToUserID", req.ToUserID)
|
||
}
|
||
if err := s.userClient.CheckUser(ctx, []string{req.ToUserID, req.FromUserID}); err != nil {
|
||
return nil, err
|
||
}
|
||
in1, _, err := s.db.CheckIn(ctx, req.FromUserID, req.ToUserID)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
if in1 {
|
||
return nil, servererrs.ErrRelationshipAlready.WrapMsg("already in friend list")
|
||
}
|
||
if err := s.db.BecomeOnewayFriend(ctx, req.FromUserID, req.ToUserID, becomeFriendByOneway, req.Remark); err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
// Silently notify only A (FromUserID) to trigger an incremental friend-list sync
|
||
// so the remark is reflected in the conversation list.
|
||
// B (ToUserID) receives no notification of any kind.
|
||
s.notificationSender.FriendAddedOnewayNotification(ctx, req.FromUserID, req.ToUserID)
|
||
|
||
// Notify only A (FromUserID) so incremental friend sync is triggered
|
||
// without notifying B (ToUserID).
|
||
//tips := sdkws.FriendApplicationApprovedTips{
|
||
// FromToUserID: &sdkws.FromToUserID{
|
||
// FromUserID: req.FromUserID,
|
||
// ToUserID: req.ToUserID,
|
||
// },
|
||
//}
|
||
//s.notificationSender.Notification(ctx, req.FromUserID, req.FromUserID, constant.FriendApplicationApprovedNotification, &tips)
|
||
return &relation.ApplyToAddFriendResp{}, nil
|
||
}
|
||
|
||
// SetMute 设置用户对另一用户的静音:duration 为秒;0=取消静音;-1=永久静音;>0=从现在起持续 duration 秒后自动解除。
|
||
func (s *friendServer) SetMute(ctx context.Context, req *relation.SetMuteReq) (*relation.SetMuteResp, error) {
|
||
if err := authverify.CheckAccessV3(ctx, req.OwnerUserID, s.config.Share.IMAdminUserID); err != nil {
|
||
return nil, err
|
||
}
|
||
if req.Duration == 0 {
|
||
return &relation.SetMuteResp{}, s.userMuteDB.Delete(ctx, req.OwnerUserID, req.TargetUserID)
|
||
}
|
||
if req.Duration < 0 && req.Duration != -1 {
|
||
return nil, errs.ErrArgs.WrapMsg("duration must be 0 (unmute), -1 (permanent), or positive seconds")
|
||
}
|
||
var muteEndTime int64
|
||
if req.Duration != -1 {
|
||
muteEndTime = time.Now().Unix() + req.Duration
|
||
}
|
||
return &relation.SetMuteResp{}, s.userMuteDB.Upsert(ctx, &model.UserMute{
|
||
OwnerUserID: req.OwnerUserID,
|
||
MutedUserID: req.TargetUserID,
|
||
MuteEndTime: muteEndTime,
|
||
MuteDuration: req.Duration,
|
||
CreateTime: time.Now(),
|
||
})
|
||
}
|
||
|
||
// GetMute 查询静音状态:未静音或已过期时 muted=false、duration=0;永久静音为 duration=-1 且 muteEndTime=0。
|
||
func (s *friendServer) GetMute(ctx context.Context, req *relation.GetMuteReq) (*relation.GetMuteResp, error) {
|
||
if err := authverify.CheckAccessV3(ctx, req.OwnerUserID, s.config.Share.IMAdminUserID); err != nil {
|
||
return nil, err
|
||
}
|
||
rec, err := s.userMuteDB.Get(ctx, req.OwnerUserID, req.TargetUserID)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
if rec == nil {
|
||
return &relation.GetMuteResp{Muted: false, MuteEndTime: 0, Duration: 0}, nil
|
||
}
|
||
now := time.Now().Unix()
|
||
if rec.MuteEndTime != 0 && rec.MuteEndTime <= now {
|
||
return &relation.GetMuteResp{Muted: false, MuteEndTime: 0, Duration: rec.MuteDuration}, nil
|
||
}
|
||
return &relation.GetMuteResp{Muted: true, MuteEndTime: rec.MuteEndTime, Duration: rec.MuteDuration}, nil
|
||
}
|
||
|
||
func (s *friendServer) getCommonUserMap(ctx context.Context, userIDs []string) (map[string]common_user.CommonUser, error) {
|
||
users, err := s.userClient.GetUsersInfo(ctx, userIDs)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return datautil.SliceToMapAny(users, func(e *sdkws.UserInfo) (string, common_user.CommonUser) {
|
||
return e.UserID, e
|
||
}), nil
|
||
}
|