|
|
@ -152,42 +152,55 @@ func (f *friendDatabase) BecomeFriends(ctx context.Context, ownerUserID string,
|
|
|
|
return f.tx.Transaction(ctx, func(ctx context.Context) error {
|
|
|
|
return f.tx.Transaction(ctx, func(ctx context.Context) error {
|
|
|
|
cache := f.cache.CloneFriendCache()
|
|
|
|
cache := f.cache.CloneFriendCache()
|
|
|
|
// user find friends
|
|
|
|
// user find friends
|
|
|
|
fs1, err := f.friend.FindFriends(ctx, ownerUserID, friendUserIDs)
|
|
|
|
myFriends, err := f.friend.FindFriends(ctx, ownerUserID, friendUserIDs)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
opUserID := mcontext.GetOperationID(ctx)
|
|
|
|
addOwners, err := f.friend.FindReversalFriends(ctx, ownerUserID, friendUserIDs)
|
|
|
|
for _, v := range friendUserIDs {
|
|
|
|
|
|
|
|
fs1 = append(fs1, &model.Friend{OwnerUserID: ownerUserID, FriendUserID: v, AddSource: addSource, OperatorUserID: opUserID})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
fs11 := datautil.DistinctAny(fs1, func(e *model.Friend) string {
|
|
|
|
|
|
|
|
return e.FriendUserID
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err = f.friend.Create(ctx, fs11)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fs2, err := f.friend.FindReversalFriends(ctx, ownerUserID, friendUserIDs)
|
|
|
|
opUserID := mcontext.GetOperationID(ctx)
|
|
|
|
if err != nil {
|
|
|
|
friends := make([]*model.Friend, 0, len(friendUserIDs)*2)
|
|
|
|
return err
|
|
|
|
myFriendsSet := datautil.SliceSetAny(myFriends, func(friend *model.Friend) string {
|
|
|
|
|
|
|
|
return friend.FriendUserID
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
addOwnersSet := datautil.SliceSetAny(addOwners, func(friend *model.Friend) string {
|
|
|
|
|
|
|
|
return friend.OwnerUserID
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
newMyFriendIDs := make([]string, 0, len(friendUserIDs))
|
|
|
|
|
|
|
|
newMyOwnerIDs := make([]string, 0, len(friendUserIDs))
|
|
|
|
|
|
|
|
for _, userID := range friendUserIDs {
|
|
|
|
|
|
|
|
if ownerUserID == userID {
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var newFriendIDs []string
|
|
|
|
if _, ok := myFriendsSet[userID]; !ok {
|
|
|
|
for _, v := range friendUserIDs {
|
|
|
|
myFriendsSet[userID] = struct{}{}
|
|
|
|
fs2 = append(fs2, &model.Friend{OwnerUserID: v, FriendUserID: ownerUserID, AddSource: addSource, OperatorUserID: opUserID})
|
|
|
|
newMyFriendIDs = append(newMyFriendIDs, userID)
|
|
|
|
newFriendIDs = append(newFriendIDs, v)
|
|
|
|
friends = append(friends, &model.Friend{OwnerUserID: ownerUserID, FriendUserID: userID, AddSource: addSource, OperatorUserID: opUserID})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fs22 := datautil.DistinctAny(fs2, func(e *model.Friend) string {
|
|
|
|
if _, ok := addOwnersSet[userID]; !ok {
|
|
|
|
return e.OwnerUserID
|
|
|
|
addOwnersSet[userID] = struct{}{}
|
|
|
|
})
|
|
|
|
newMyOwnerIDs = append(newMyOwnerIDs, userID)
|
|
|
|
err = f.friend.Create(ctx, fs22)
|
|
|
|
friends = append(friends, &model.Friend{OwnerUserID: userID, FriendUserID: ownerUserID, AddSource: addSource, OperatorUserID: opUserID})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(friends) == 0 {
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
err = f.friend.Create(ctx, friends)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
newFriendIDs = append(newFriendIDs, ownerUserID)
|
|
|
|
if len(newMyFriendIDs) > 0 {
|
|
|
|
cache = cache.DelFriendIDs(newFriendIDs...).DelMaxFriendVersion(newFriendIDs...)
|
|
|
|
cache = cache.DelFriendIDs(newMyFriendIDs...)
|
|
|
|
|
|
|
|
cache = cache.DelFriends(ownerUserID, newMyFriendIDs).DelMaxFriendVersion(newMyFriendIDs...)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(newMyOwnerIDs) > 0 {
|
|
|
|
|
|
|
|
cache = cache.DelFriendIDs(newMyOwnerIDs...)
|
|
|
|
|
|
|
|
cache = cache.DelOwner(ownerUserID, newMyOwnerIDs).DelMaxFriendVersion(newMyOwnerIDs...)
|
|
|
|
|
|
|
|
}
|
|
|
|
return cache.ChainExecDel(ctx)
|
|
|
|
return cache.ChainExecDel(ctx)
|
|
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|