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.
256 lines
9.0 KiB
256 lines
9.0 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
|
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/database"
|
||
|
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/versionctx"
|
||
5 months ago
|
|
||
6 months ago
|
relationtb "github.com/openimsdk/open-im-server/v3/pkg/common/storage/model"
|
||
1 year ago
|
|
||
9 months ago
|
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
|
||
1 year ago
|
"github.com/openimsdk/open-im-server/v3/pkg/common/convert"
|
||
6 months ago
|
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/controller"
|
||
1 year ago
|
"github.com/openimsdk/open-im-server/v3/pkg/rpcclient"
|
||
8 months ago
|
"github.com/openimsdk/open-im-server/v3/pkg/rpcclient/notification"
|
||
|
"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/mcontext"
|
||
1 year ago
|
)
|
||
|
|
||
|
type FriendNotificationSender struct {
|
||
|
*rpcclient.NotificationSender
|
||
9 months ago
|
// Target not found err
|
||
8 months ago
|
getUsersInfo func(ctx context.Context, userIDs []string) ([]notification.CommonUser, error)
|
||
1 year ago
|
// db controller
|
||
|
db controller.FriendDatabase
|
||
|
}
|
||
|
|
||
|
type friendNotificationSenderOptions func(*FriendNotificationSender)
|
||
|
|
||
|
func WithFriendDB(db controller.FriendDatabase) friendNotificationSenderOptions {
|
||
|
return func(s *FriendNotificationSender) {
|
||
|
s.db = db
|
||
|
}
|
||
|
}
|
||
|
|
||
1 year ago
|
func WithDBFunc(
|
||
6 months ago
|
fn func(ctx context.Context, userIDs []string) (users []*relationtb.User, err error),
|
||
1 year ago
|
) friendNotificationSenderOptions {
|
||
1 year ago
|
return func(s *FriendNotificationSender) {
|
||
8 months ago
|
f := func(ctx context.Context, userIDs []string) (result []notification.CommonUser, err error) {
|
||
1 year ago
|
users, err := fn(ctx, userIDs)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
for _, user := range users {
|
||
|
result = append(result, user)
|
||
|
}
|
||
|
return result, nil
|
||
|
}
|
||
|
s.getUsersInfo = f
|
||
|
}
|
||
|
}
|
||
|
|
||
1 year ago
|
func WithRpcFunc(
|
||
|
fn func(ctx context.Context, userIDs []string) ([]*sdkws.UserInfo, error),
|
||
|
) friendNotificationSenderOptions {
|
||
1 year ago
|
return func(s *FriendNotificationSender) {
|
||
8 months ago
|
f := func(ctx context.Context, userIDs []string) (result []notification.CommonUser, err error) {
|
||
1 year ago
|
users, err := fn(ctx, userIDs)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
for _, user := range users {
|
||
|
result = append(result, user)
|
||
|
}
|
||
|
return result, err
|
||
|
}
|
||
|
s.getUsersInfo = f
|
||
|
}
|
||
|
}
|
||
|
|
||
1 year ago
|
func NewFriendNotificationSender(
|
||
8 months ago
|
conf *config.Notification,
|
||
1 year ago
|
msgRpcClient *rpcclient.MessageRpcClient,
|
||
|
opts ...friendNotificationSenderOptions,
|
||
|
) *FriendNotificationSender {
|
||
1 year ago
|
f := &FriendNotificationSender{
|
||
8 months ago
|
NotificationSender: rpcclient.NewNotificationSender(conf, rpcclient.WithRpcClient(msgRpcClient)),
|
||
1 year ago
|
}
|
||
|
for _, opt := range opts {
|
||
|
opt(f)
|
||
|
}
|
||
|
return f
|
||
|
}
|
||
|
|
||
1 year ago
|
func (f *FriendNotificationSender) getUsersInfoMap(
|
||
|
ctx context.Context,
|
||
|
userIDs []string,
|
||
|
) (map[string]*sdkws.UserInfo, error) {
|
||
1 year ago
|
users, err := f.getUsersInfo(ctx, userIDs)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
result := make(map[string]*sdkws.UserInfo)
|
||
|
for _, user := range users {
|
||
|
result[user.GetUserID()] = user.(*sdkws.UserInfo)
|
||
|
}
|
||
1 year ago
|
return result, nil
|
||
1 year ago
|
}
|
||
|
|
||
9 months ago
|
//nolint:unused
|
||
1 year ago
|
func (f *FriendNotificationSender) getFromToUserNickname(
|
||
|
ctx context.Context,
|
||
|
fromUserID, toUserID string,
|
||
|
) (string, string, error) {
|
||
|
users, err := f.getUsersInfoMap(ctx, []string{fromUserID, toUserID})
|
||
|
if err != nil {
|
||
|
return "", "", nil
|
||
|
}
|
||
|
return users[fromUserID].Nickname, users[toUserID].Nickname, nil
|
||
|
}
|
||
|
|
||
8 months ago
|
func (f *FriendNotificationSender) UserInfoUpdatedNotification(ctx context.Context, changedUserID string) {
|
||
1 year ago
|
tips := sdkws.UserInfoUpdatedTips{UserID: changedUserID}
|
||
8 months ago
|
f.Notification(ctx, mcontext.GetOpUserID(ctx), changedUserID, constant.UserInfoUpdatedNotification, &tips)
|
||
1 year ago
|
}
|
||
|
|
||
5 months ago
|
func (f *FriendNotificationSender) FriendApplicationAddNotification(ctx context.Context, req *relation.ApplyToAddFriendReq) {
|
||
1 year ago
|
tips := sdkws.FriendApplicationTips{FromToUserID: &sdkws.FromToUserID{
|
||
|
FromUserID: req.FromUserID,
|
||
|
ToUserID: req.ToUserID,
|
||
|
}}
|
||
8 months ago
|
f.Notification(ctx, req.FromUserID, req.ToUserID, constant.FriendApplicationNotification, &tips)
|
||
1 year ago
|
}
|
||
|
|
||
1 year ago
|
func (f *FriendNotificationSender) FriendApplicationAgreedNotification(
|
||
1 year ago
|
ctx context.Context,
|
||
5 months ago
|
req *relation.RespondFriendApplyReq,
|
||
8 months ago
|
) {
|
||
1 year ago
|
tips := sdkws.FriendApplicationApprovedTips{FromToUserID: &sdkws.FromToUserID{
|
||
|
FromUserID: req.FromUserID,
|
||
|
ToUserID: req.ToUserID,
|
||
|
}, HandleMsg: req.HandleMsg}
|
||
8 months ago
|
f.Notification(ctx, req.ToUserID, req.FromUserID, constant.FriendApplicationApprovedNotification, &tips)
|
||
1 year ago
|
}
|
||
|
|
||
1 year ago
|
func (f *FriendNotificationSender) FriendApplicationRefusedNotification(
|
||
1 year ago
|
ctx context.Context,
|
||
5 months ago
|
req *relation.RespondFriendApplyReq,
|
||
8 months ago
|
) {
|
||
1 year ago
|
tips := sdkws.FriendApplicationApprovedTips{FromToUserID: &sdkws.FromToUserID{
|
||
|
FromUserID: req.FromUserID,
|
||
|
ToUserID: req.ToUserID,
|
||
|
}, HandleMsg: req.HandleMsg}
|
||
8 months ago
|
f.Notification(ctx, req.ToUserID, req.FromUserID, constant.FriendApplicationRejectedNotification, &tips)
|
||
1 year ago
|
}
|
||
|
|
||
1 year ago
|
func (f *FriendNotificationSender) FriendAddedNotification(
|
||
1 year ago
|
ctx context.Context,
|
||
|
operationID, opUserID, fromUserID, toUserID string,
|
||
|
) error {
|
||
1 year ago
|
tips := sdkws.FriendAddedTips{Friend: &sdkws.FriendInfo{}, OpUser: &sdkws.PublicUserInfo{}}
|
||
1 year ago
|
user, err := f.getUsersInfo(ctx, []string{opUserID})
|
||
1 year ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
tips.OpUser.UserID = user[0].GetUserID()
|
||
|
tips.OpUser.Ex = user[0].GetEx()
|
||
|
tips.OpUser.Nickname = user[0].GetNickname()
|
||
|
tips.OpUser.FaceURL = user[0].GetFaceURL()
|
||
1 year ago
|
friends, err := f.db.FindFriendsWithError(ctx, fromUserID, []string{toUserID})
|
||
1 year ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
1 year ago
|
tips.Friend, err = convert.FriendDB2Pb(ctx, friends[0], f.getUsersInfoMap)
|
||
1 year ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
8 months ago
|
f.Notification(ctx, fromUserID, toUserID, constant.FriendAddedNotification, &tips)
|
||
|
return nil
|
||
1 year ago
|
}
|
||
|
|
||
5 months ago
|
func (f *FriendNotificationSender) FriendDeletedNotification(ctx context.Context, req *relation.DeleteFriendReq) {
|
||
1 year ago
|
tips := sdkws.FriendDeletedTips{FromToUserID: &sdkws.FromToUserID{
|
||
|
FromUserID: req.OwnerUserID,
|
||
|
ToUserID: req.FriendUserID,
|
||
|
}}
|
||
8 months ago
|
f.Notification(ctx, req.OwnerUserID, req.FriendUserID, constant.FriendDeletedNotification, &tips)
|
||
1 year ago
|
}
|
||
|
|
||
4 months ago
|
func (f *FriendNotificationSender) setVersion(ctx context.Context, version *uint64, versionID *string, collName string, id string) {
|
||
|
versions := versionctx.GetVersionLog(ctx).Get()
|
||
|
for _, coll := range versions {
|
||
|
if coll.Name == collName && coll.Doc.DID == id {
|
||
|
*version = uint64(coll.Doc.Version)
|
||
|
*versionID = coll.Doc.ID.Hex()
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (f *FriendNotificationSender) setSortVersion(ctx context.Context, version *uint64, versionID *string, collName string, id string, sortVersion *uint64) {
|
||
|
versions := versionctx.GetVersionLog(ctx).Get()
|
||
|
for _, coll := range versions {
|
||
|
if coll.Name == collName && coll.Doc.DID == id {
|
||
|
*version = uint64(coll.Doc.Version)
|
||
|
*versionID = coll.Doc.ID.Hex()
|
||
|
for _, elem := range coll.Doc.Logs {
|
||
|
if elem.EID == relationtb.VersionSortChangeID {
|
||
|
*sortVersion = uint64(elem.Version)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
8 months ago
|
func (f *FriendNotificationSender) FriendRemarkSetNotification(ctx context.Context, fromUserID, toUserID string) {
|
||
1 year ago
|
tips := sdkws.FriendInfoChangedTips{FromToUserID: &sdkws.FromToUserID{}}
|
||
|
tips.FromToUserID.FromUserID = fromUserID
|
||
|
tips.FromToUserID.ToUserID = toUserID
|
||
4 months ago
|
f.setSortVersion(ctx, &tips.FriendVersion, &tips.FriendVersionID, database.FriendVersionName, toUserID, &tips.FriendSortVersion)
|
||
8 months ago
|
f.Notification(ctx, fromUserID, toUserID, constant.FriendRemarkSetNotification, &tips)
|
||
1 year ago
|
}
|
||
8 months ago
|
|
||
|
func (f *FriendNotificationSender) FriendsInfoUpdateNotification(ctx context.Context, toUserID string, friendIDs []string) {
|
||
11 months ago
|
tips := sdkws.FriendsInfoUpdateTips{FromToUserID: &sdkws.FromToUserID{}}
|
||
11 months ago
|
tips.FromToUserID.ToUserID = toUserID
|
||
|
tips.FriendIDs = friendIDs
|
||
8 months ago
|
f.Notification(ctx, toUserID, toUserID, constant.FriendsInfoUpdateNotification, &tips)
|
||
11 months ago
|
}
|
||
8 months ago
|
|
||
5 months ago
|
func (f *FriendNotificationSender) BlackAddedNotification(ctx context.Context, req *relation.AddBlackReq) {
|
||
1 year ago
|
tips := sdkws.BlackAddedTips{FromToUserID: &sdkws.FromToUserID{}}
|
||
|
tips.FromToUserID.FromUserID = req.OwnerUserID
|
||
|
tips.FromToUserID.ToUserID = req.BlackUserID
|
||
8 months ago
|
f.Notification(ctx, req.OwnerUserID, req.BlackUserID, constant.BlackAddedNotification, &tips)
|
||
1 year ago
|
}
|
||
|
|
||
5 months ago
|
func (f *FriendNotificationSender) BlackDeletedNotification(ctx context.Context, req *relation.RemoveBlackReq) {
|
||
1 year ago
|
blackDeletedTips := sdkws.BlackDeletedTips{FromToUserID: &sdkws.FromToUserID{
|
||
|
FromUserID: req.OwnerUserID,
|
||
|
ToUserID: req.BlackUserID,
|
||
|
}}
|
||
8 months ago
|
f.Notification(ctx, req.OwnerUserID, req.BlackUserID, constant.BlackDeletedNotification, &blackDeletedTips)
|
||
1 year ago
|
}
|
||
|
|
||
8 months ago
|
func (f *FriendNotificationSender) FriendInfoUpdatedNotification(ctx context.Context, changedUserID string, needNotifiedUserID string) {
|
||
1 year ago
|
tips := sdkws.UserInfoUpdatedTips{UserID: changedUserID}
|
||
8 months ago
|
f.Notification(ctx, mcontext.GetOpUserID(ctx), needNotifiedUserID, constant.FriendInfoUpdatedNotification, &tips)
|
||
1 year ago
|
}
|