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.
349 lines
15 KiB
349 lines
15 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.
|
||
|
|
||
1 year ago
|
package controller
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
9 months ago
|
"fmt"
|
||
6 months ago
|
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/database"
|
||
|
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/database/mgo"
|
||
|
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/model"
|
||
1 year ago
|
"time"
|
||
|
|
||
6 months ago
|
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/cache"
|
||
8 months ago
|
"github.com/openimsdk/protocol/constant"
|
||
|
"github.com/openimsdk/tools/db/pagination"
|
||
|
"github.com/openimsdk/tools/db/tx"
|
||
|
"github.com/openimsdk/tools/errs"
|
||
|
"github.com/openimsdk/tools/log"
|
||
|
"github.com/openimsdk/tools/mcontext"
|
||
|
"github.com/openimsdk/tools/utils/datautil"
|
||
1 year ago
|
)
|
||
|
|
||
|
type FriendDatabase interface {
|
||
11 months ago
|
// CheckIn checks if user2 is in user1's friend list (inUser1Friends==true) and if user1 is in user2's friend list (inUser2Friends==true)
|
||
1 year ago
|
CheckIn(ctx context.Context, user1, user2 string) (inUser1Friends bool, inUser2Friends bool, err error)
|
||
11 months ago
|
|
||
|
// AddFriendRequest adds or updates a friend request
|
||
1 year ago
|
AddFriendRequest(ctx context.Context, fromUserID, toUserID string, reqMsg string, ex string) (err error)
|
||
11 months ago
|
|
||
6 months ago
|
// BecomeFriends first checks if the users are already in the friends model; if not, it inserts them as friends
|
||
1 year ago
|
BecomeFriends(ctx context.Context, ownerUserID string, friendUserIDs []string, addSource int32) (err error)
|
||
11 months ago
|
|
||
|
// RefuseFriendRequest refuses a friend request
|
||
6 months ago
|
RefuseFriendRequest(ctx context.Context, friendRequest *model.FriendRequest) (err error)
|
||
11 months ago
|
|
||
|
// AgreeFriendRequest accepts a friend request
|
||
6 months ago
|
AgreeFriendRequest(ctx context.Context, friendRequest *model.FriendRequest) (err error)
|
||
11 months ago
|
|
||
|
// Delete removes a friend or friends from the owner's friend list
|
||
1 year ago
|
Delete(ctx context.Context, ownerUserID string, friendUserIDs []string) (err error)
|
||
11 months ago
|
|
||
|
// UpdateRemark updates the remark for a friend
|
||
1 year ago
|
UpdateRemark(ctx context.Context, ownerUserID, friendUserID, remark string) (err error)
|
||
11 months ago
|
|
||
|
// PageOwnerFriends retrieves the friend list of ownerUserID with pagination
|
||
6 months ago
|
PageOwnerFriends(ctx context.Context, ownerUserID string, pagination pagination.Pagination) (total int64, friends []*model.Friend, err error)
|
||
11 months ago
|
|
||
|
// PageInWhoseFriends finds the users who have friendUserID in their friend list with pagination
|
||
6 months ago
|
PageInWhoseFriends(ctx context.Context, friendUserID string, pagination pagination.Pagination) (total int64, friends []*model.Friend, err error)
|
||
11 months ago
|
|
||
|
// PageFriendRequestFromMe retrieves the friend requests sent by the user with pagination
|
||
6 months ago
|
PageFriendRequestFromMe(ctx context.Context, userID string, pagination pagination.Pagination) (total int64, friends []*model.FriendRequest, err error)
|
||
11 months ago
|
|
||
|
// PageFriendRequestToMe retrieves the friend requests received by the user with pagination
|
||
6 months ago
|
PageFriendRequestToMe(ctx context.Context, userID string, pagination pagination.Pagination) (total int64, friends []*model.FriendRequest, err error)
|
||
11 months ago
|
|
||
|
// FindFriendsWithError fetches specified friends of a user and returns an error if any do not exist
|
||
6 months ago
|
FindFriendsWithError(ctx context.Context, ownerUserID string, friendUserIDs []string) (friends []*model.Friend, err error)
|
||
11 months ago
|
|
||
|
// FindFriendUserIDs retrieves the friend IDs of a user
|
||
1 year ago
|
FindFriendUserIDs(ctx context.Context, ownerUserID string) (friendUserIDs []string, err error)
|
||
11 months ago
|
|
||
|
// FindBothFriendRequests finds friend requests sent and received
|
||
6 months ago
|
FindBothFriendRequests(ctx context.Context, fromUserID, toUserID string) (friends []*model.FriendRequest, err error)
|
||
11 months ago
|
|
||
11 months ago
|
// UpdateFriends updates fields for friends
|
||
|
UpdateFriends(ctx context.Context, ownerUserID string, friendUserIDs []string, val map[string]any) (err error)
|
||
1 year ago
|
}
|
||
|
|
||
|
type friendDatabase struct {
|
||
6 months ago
|
friend database.Friend
|
||
|
friendRequest database.FriendRequest
|
||
8 months ago
|
tx tx.Tx
|
||
1 year ago
|
cache cache.FriendCache
|
||
|
}
|
||
|
|
||
6 months ago
|
func NewFriendDatabase(friend database.Friend, friendRequest database.FriendRequest, cache cache.FriendCache, tx tx.Tx) FriendDatabase {
|
||
1 year ago
|
return &friendDatabase{friend: friend, friendRequest: friendRequest, cache: cache, tx: tx}
|
||
|
}
|
||
|
|
||
9 months ago
|
// CheckIn verifies if user2 is in user1's friend list (inUser1Friends returns true) and
|
||
|
// if user1 is in user2's friend list (inUser2Friends returns true).
|
||
1 year ago
|
func (f *friendDatabase) CheckIn(ctx context.Context, userID1, userID2 string) (inUser1Friends bool, inUser2Friends bool, err error) {
|
||
9 months ago
|
// Retrieve friend IDs of userID1 from the cache
|
||
1 year ago
|
userID1FriendIDs, err := f.cache.GetFriendIDs(ctx, userID1)
|
||
|
if err != nil {
|
||
9 months ago
|
err = fmt.Errorf("error retrieving friend IDs for user %s: %w", userID1, err)
|
||
1 year ago
|
return
|
||
|
}
|
||
9 months ago
|
|
||
|
// Retrieve friend IDs of userID2 from the cache
|
||
1 year ago
|
userID2FriendIDs, err := f.cache.GetFriendIDs(ctx, userID2)
|
||
|
if err != nil {
|
||
9 months ago
|
err = fmt.Errorf("error retrieving friend IDs for user %s: %w", userID2, err)
|
||
1 year ago
|
return
|
||
|
}
|
||
9 months ago
|
|
||
|
// Check if userID2 is in userID1's friend list and vice versa
|
||
8 months ago
|
inUser1Friends = datautil.Contain(userID2, userID1FriendIDs...)
|
||
|
inUser2Friends = datautil.Contain(userID1, userID2FriendIDs...)
|
||
9 months ago
|
return inUser1Friends, inUser2Friends, nil
|
||
1 year ago
|
}
|
||
|
|
||
9 months ago
|
// AddFriendRequest adds or updates a friend request.
|
||
1 year ago
|
func (f *friendDatabase) AddFriendRequest(ctx context.Context, fromUserID, toUserID string, reqMsg string, ex string) (err error) {
|
||
|
return f.tx.Transaction(ctx, func(ctx context.Context) error {
|
||
|
_, err := f.friendRequest.Take(ctx, fromUserID, toUserID)
|
||
|
switch {
|
||
|
case err == nil:
|
||
|
m := make(map[string]any, 1)
|
||
1 year ago
|
m["handle_result"] = 0
|
||
|
m["handle_msg"] = ""
|
||
|
m["req_msg"] = reqMsg
|
||
|
m["ex"] = ex
|
||
|
m["create_time"] = time.Now()
|
||
1 year ago
|
return f.friendRequest.UpdateByMap(ctx, fromUserID, toUserID, m)
|
||
6 months ago
|
case mgo.IsNotFound(err):
|
||
1 year ago
|
return f.friendRequest.Create(
|
||
|
ctx,
|
||
6 months ago
|
[]*model.FriendRequest{{FromUserID: fromUserID, ToUserID: toUserID, ReqMsg: reqMsg, Ex: ex, CreateTime: time.Now(), HandleTime: time.Unix(0, 0)}},
|
||
1 year ago
|
)
|
||
|
default:
|
||
1 year ago
|
return err
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
|
||
9 months ago
|
// (1) First determine whether it is in the friends list (in or out does not return an error) (2) for not in the friends list can be inserted.
|
||
1 year ago
|
func (f *friendDatabase) BecomeFriends(ctx context.Context, ownerUserID string, friendUserIDs []string, addSource int32) (err error) {
|
||
|
return f.tx.Transaction(ctx, func(ctx context.Context) error {
|
||
6 months ago
|
cache := f.cache.CloneFriendCache()
|
||
8 months ago
|
// user find friends
|
||
1 year ago
|
fs1, err := f.friend.FindFriends(ctx, ownerUserID, friendUserIDs)
|
||
1 year ago
|
if err != nil {
|
||
|
return err
|
||
1 year ago
|
}
|
||
|
opUserID := mcontext.GetOperationID(ctx)
|
||
|
for _, v := range friendUserIDs {
|
||
6 months ago
|
fs1 = append(fs1, &model.Friend{OwnerUserID: ownerUserID, FriendUserID: v, AddSource: addSource, OperatorUserID: opUserID})
|
||
1 year ago
|
}
|
||
6 months ago
|
fs11 := datautil.DistinctAny(fs1, func(e *model.Friend) string {
|
||
1 year ago
|
return e.FriendUserID
|
||
|
})
|
||
|
|
||
1 year ago
|
err = f.friend.Create(ctx, fs11)
|
||
1 year ago
|
if err != nil {
|
||
|
return err
|
||
1 year ago
|
}
|
||
1 year ago
|
fs2, err := f.friend.FindReversalFriends(ctx, ownerUserID, friendUserIDs)
|
||
1 year ago
|
if err != nil {
|
||
|
return err
|
||
1 year ago
|
}
|
||
|
var newFriendIDs []string
|
||
|
for _, v := range friendUserIDs {
|
||
6 months ago
|
fs2 = append(fs2, &model.Friend{OwnerUserID: v, FriendUserID: ownerUserID, AddSource: addSource, OperatorUserID: opUserID})
|
||
1 year ago
|
newFriendIDs = append(newFriendIDs, v)
|
||
|
}
|
||
6 months ago
|
fs22 := datautil.DistinctAny(fs2, func(e *model.Friend) string {
|
||
1 year ago
|
return e.OwnerUserID
|
||
|
})
|
||
1 year ago
|
err = f.friend.Create(ctx, fs22)
|
||
1 year ago
|
if err != nil {
|
||
|
return err
|
||
1 year ago
|
}
|
||
|
newFriendIDs = append(newFriendIDs, ownerUserID)
|
||
|
cache = cache.DelFriendIDs(newFriendIDs...)
|
||
6 months ago
|
return cache.ChainExecDel(ctx)
|
||
1 year ago
|
|
||
|
})
|
||
1 year ago
|
}
|
||
|
|
||
9 months ago
|
// RefuseFriendRequest rejects a friend request. It first checks for an existing, unprocessed request.
|
||
|
// If no such request exists, it returns an error. Otherwise, it marks the request as refused.
|
||
6 months ago
|
func (f *friendDatabase) RefuseFriendRequest(ctx context.Context, friendRequest *model.FriendRequest) error {
|
||
9 months ago
|
// Attempt to retrieve the friend request from the database.
|
||
1 year ago
|
fr, err := f.friendRequest.Take(ctx, friendRequest.FromUserID, friendRequest.ToUserID)
|
||
|
if err != nil {
|
||
9 months ago
|
return fmt.Errorf("failed to retrieve friend request from %s to %s: %w", friendRequest.FromUserID, friendRequest.ToUserID, err)
|
||
1 year ago
|
}
|
||
9 months ago
|
|
||
|
// Check if the friend request has already been handled.
|
||
1 year ago
|
if fr.HandleResult != 0 {
|
||
9 months ago
|
return fmt.Errorf("friend request from %s to %s has already been processed", friendRequest.FromUserID, friendRequest.ToUserID)
|
||
1 year ago
|
}
|
||
9 months ago
|
|
||
|
// Log the action of refusing the friend request for debugging and auditing purposes.
|
||
|
log.ZDebug(ctx, "Refusing friend request", map[string]interface{}{
|
||
|
"DB_FriendRequest": fr,
|
||
|
"Arg_FriendRequest": friendRequest,
|
||
|
})
|
||
|
|
||
|
// Mark the friend request as refused and update the handle time.
|
||
1 year ago
|
friendRequest.HandleResult = constant.FriendResponseRefuse
|
||
|
friendRequest.HandleTime = time.Now()
|
||
9 months ago
|
if err := f.friendRequest.Update(ctx, friendRequest); err != nil {
|
||
|
return fmt.Errorf("failed to update friend request from %s to %s as refused: %w", friendRequest.FromUserID, friendRequest.ToUserID, err)
|
||
1 year ago
|
}
|
||
9 months ago
|
|
||
1 year ago
|
return nil
|
||
|
}
|
||
|
|
||
9 months ago
|
// AgreeFriendRequest accepts a friend request. It first checks for an existing, unprocessed request.
|
||
6 months ago
|
func (f *friendDatabase) AgreeFriendRequest(ctx context.Context, friendRequest *model.FriendRequest) (err error) {
|
||
1 year ago
|
return f.tx.Transaction(ctx, func(ctx context.Context) error {
|
||
1 year ago
|
now := time.Now()
|
||
1 year ago
|
fr, err := f.friendRequest.Take(ctx, friendRequest.FromUserID, friendRequest.ToUserID)
|
||
1 year ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
if fr.HandleResult != 0 {
|
||
8 months ago
|
return errs.ErrArgs.WrapMsg("the friend request has been processed")
|
||
1 year ago
|
}
|
||
|
friendRequest.HandlerUserID = mcontext.GetOpUserID(ctx)
|
||
|
friendRequest.HandleResult = constant.FriendResponseAgree
|
||
1 year ago
|
friendRequest.HandleTime = now
|
||
1 year ago
|
err = f.friendRequest.Update(ctx, friendRequest)
|
||
1 year ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
1 year ago
|
|
||
1 year ago
|
fr2, err := f.friendRequest.Take(ctx, friendRequest.ToUserID, friendRequest.FromUserID)
|
||
1 year ago
|
if err == nil && fr2.HandleResult == constant.FriendResponseNotHandle {
|
||
|
fr2.HandlerUserID = mcontext.GetOpUserID(ctx)
|
||
|
fr2.HandleResult = constant.FriendResponseAgree
|
||
|
fr2.HandleTime = now
|
||
1 year ago
|
err = f.friendRequest.Update(ctx, fr2)
|
||
1 year ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
6 months ago
|
} else if err != nil && (!mgo.IsNotFound(err)) {
|
||
1 year ago
|
return err
|
||
|
}
|
||
|
|
||
1 year ago
|
exists, err := f.friend.FindUserState(ctx, friendRequest.FromUserID, friendRequest.ToUserID)
|
||
1 year ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
6 months ago
|
existsMap := datautil.SliceSet(datautil.Slice(exists, func(friend *model.Friend) [2]string {
|
||
9 months ago
|
return [...]string{friend.OwnerUserID, friend.FriendUserID} // My - Friend
|
||
1 year ago
|
}))
|
||
6 months ago
|
var adds []*model.Friend
|
||
9 months ago
|
if _, ok := existsMap[[...]string{friendRequest.ToUserID, friendRequest.FromUserID}]; !ok { // My - Friend
|
||
1 year ago
|
adds = append(
|
||
|
adds,
|
||
6 months ago
|
&model.Friend{
|
||
1 year ago
|
OwnerUserID: friendRequest.ToUserID,
|
||
|
FriendUserID: friendRequest.FromUserID,
|
||
|
AddSource: int32(constant.BecomeFriendByApply),
|
||
|
OperatorUserID: friendRequest.FromUserID,
|
||
|
},
|
||
|
)
|
||
1 year ago
|
}
|
||
9 months ago
|
if _, ok := existsMap[[...]string{friendRequest.FromUserID, friendRequest.ToUserID}]; !ok { // My - Friend
|
||
1 year ago
|
adds = append(
|
||
|
adds,
|
||
6 months ago
|
&model.Friend{
|
||
1 year ago
|
OwnerUserID: friendRequest.FromUserID,
|
||
|
FriendUserID: friendRequest.ToUserID,
|
||
|
AddSource: int32(constant.BecomeFriendByApply),
|
||
|
OperatorUserID: friendRequest.FromUserID,
|
||
|
},
|
||
|
)
|
||
1 year ago
|
}
|
||
|
if len(adds) > 0 {
|
||
1 year ago
|
if err := f.friend.Create(ctx, adds); err != nil {
|
||
1 year ago
|
return err
|
||
|
}
|
||
|
}
|
||
6 months ago
|
return f.cache.DelFriendIDs(friendRequest.ToUserID, friendRequest.FromUserID).ChainExecDel(ctx)
|
||
1 year ago
|
})
|
||
|
}
|
||
|
|
||
9 months ago
|
// Delete removes a friend relationship. It is assumed that the external caller has verified the friendship status.
|
||
1 year ago
|
func (f *friendDatabase) Delete(ctx context.Context, ownerUserID string, friendUserIDs []string) (err error) {
|
||
|
if err := f.friend.Delete(ctx, ownerUserID, friendUserIDs); err != nil {
|
||
|
return err
|
||
|
}
|
||
6 months ago
|
return f.cache.DelFriendIDs(append(friendUserIDs, ownerUserID)...).ChainExecDel(ctx)
|
||
1 year ago
|
}
|
||
|
|
||
9 months ago
|
// UpdateRemark updates the remark for a friend. Zero value for remark is also supported.
|
||
1 year ago
|
func (f *friendDatabase) UpdateRemark(ctx context.Context, ownerUserID, friendUserID, remark string) (err error) {
|
||
|
if err := f.friend.UpdateRemark(ctx, ownerUserID, friendUserID, remark); err != nil {
|
||
|
return err
|
||
|
}
|
||
6 months ago
|
return f.cache.DelFriend(ownerUserID, friendUserID).ChainExecDel(ctx)
|
||
1 year ago
|
}
|
||
|
|
||
9 months ago
|
// PageOwnerFriends retrieves the list of friends for the ownerUserID. It does not return an error if the result is empty.
|
||
6 months ago
|
func (f *friendDatabase) PageOwnerFriends(ctx context.Context, ownerUserID string, pagination pagination.Pagination) (total int64, friends []*model.Friend, err error) {
|
||
1 year ago
|
return f.friend.FindOwnerFriends(ctx, ownerUserID, pagination)
|
||
1 year ago
|
}
|
||
|
|
||
9 months ago
|
// PageInWhoseFriends identifies in whose friend lists the friendUserID appears.
|
||
6 months ago
|
func (f *friendDatabase) PageInWhoseFriends(ctx context.Context, friendUserID string, pagination pagination.Pagination) (total int64, friends []*model.Friend, err error) {
|
||
1 year ago
|
return f.friend.FindInWhoseFriends(ctx, friendUserID, pagination)
|
||
1 year ago
|
}
|
||
|
|
||
9 months ago
|
// PageFriendRequestFromMe retrieves friend requests sent by me. It does not return an error if the result is empty.
|
||
6 months ago
|
func (f *friendDatabase) PageFriendRequestFromMe(ctx context.Context, userID string, pagination pagination.Pagination) (total int64, friends []*model.FriendRequest, err error) {
|
||
1 year ago
|
return f.friendRequest.FindFromUserID(ctx, userID, pagination)
|
||
1 year ago
|
}
|
||
|
|
||
9 months ago
|
// PageFriendRequestToMe retrieves friend requests received by me. It does not return an error if the result is empty.
|
||
6 months ago
|
func (f *friendDatabase) PageFriendRequestToMe(ctx context.Context, userID string, pagination pagination.Pagination) (total int64, friends []*model.FriendRequest, err error) {
|
||
1 year ago
|
return f.friendRequest.FindToUserID(ctx, userID, pagination)
|
||
1 year ago
|
}
|
||
|
|
||
9 months ago
|
// FindFriendsWithError retrieves specified friends' information for ownerUserID. Returns an error if any friend does not exist.
|
||
6 months ago
|
func (f *friendDatabase) FindFriendsWithError(ctx context.Context, ownerUserID string, friendUserIDs []string) (friends []*model.Friend, err error) {
|
||
1 year ago
|
friends, err = f.friend.FindFriends(ctx, ownerUserID, friendUserIDs)
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
if len(friends) != len(friendUserIDs) {
|
||
|
err = errs.ErrRecordNotFound.Wrap()
|
||
|
}
|
||
|
return
|
||
|
}
|
||
|
|
||
1 year ago
|
func (f *friendDatabase) FindFriendUserIDs(ctx context.Context, ownerUserID string) (friendUserIDs []string, err error) {
|
||
1 year ago
|
return f.cache.GetFriendIDs(ctx, ownerUserID)
|
||
|
}
|
||
1 year ago
|
|
||
6 months ago
|
func (f *friendDatabase) FindBothFriendRequests(ctx context.Context, fromUserID, toUserID string) (friends []*model.FriendRequest, err error) {
|
||
1 year ago
|
return f.friendRequest.FindBothFriendRequests(ctx, fromUserID, toUserID)
|
||
|
}
|
||
11 months ago
|
func (f *friendDatabase) UpdateFriends(ctx context.Context, ownerUserID string, friendUserIDs []string, val map[string]any) (err error) {
|
||
|
if len(val) == 0 {
|
||
|
return nil
|
||
12 months ago
|
}
|
||
11 months ago
|
if err := f.friend.UpdateFriends(ctx, ownerUserID, friendUserIDs, val); err != nil {
|
||
11 months ago
|
return err
|
||
|
}
|
||
6 months ago
|
return f.cache.DelFriends(ownerUserID, friendUserIDs).ChainExecDel(ctx)
|
||
11 months ago
|
}
|