Add feature 829 (#853)

* add user notification

* add user notification

* add UserStatusChangeNotification constant

* add get_subscribe_users_status interface

* update note

* log debug

* log debug

* add SingleChatType

* add SingleChatType and ContentTypeConf

* delete debug

* auto go.work.sum
This commit is contained in:
pluto
2023-08-15 19:56:41 +08:00
committed by GitHub
parent 57ce0bd29d
commit 5353e5fa66
13 changed files with 330 additions and 38 deletions
+2 -1
View File
@@ -284,7 +284,8 @@ type notification struct {
GroupInfoSetAnnouncement NotificationConf `yaml:"groupInfoSetAnnouncement"`
GroupInfoSetName NotificationConf `yaml:"groupInfoSetName"`
////////////////////////user///////////////////////
UserInfoUpdated NotificationConf `yaml:"userInfoUpdated"`
UserInfoUpdated NotificationConf `yaml:"userInfoUpdated"`
UserStatusChanged NotificationConf `yaml:"userStatusChanged"`
//////////////////////friend///////////////////////
FriendApplicationAdded NotificationConf `yaml:"friendApplicationAdded"`
FriendApplicationApproved NotificationConf `yaml:"friendApplicationApproved"`
+1 -1
View File
@@ -53,7 +53,7 @@ type UserDatabase interface {
CountTotal(ctx context.Context, before *time.Time) (int64, error)
// CountRangeEverydayTotal Get the user increment in the range
CountRangeEverydayTotal(ctx context.Context, start time.Time, end time.Time) (map[string]int64, error)
//SubscribeUsersStatus Subscribe a user's presence status
// SubscribeUsersStatus Subscribe a user's presence status
SubscribeUsersStatus(ctx context.Context, userID string, userIDs []string) error
// UnsubscribeUsersStatus unsubscribe a user's presence status
UnsubscribeUsersStatus(ctx context.Context, userID string, userIDs []string) error
+2 -2
View File
@@ -155,7 +155,7 @@ func (u *UserMongoDriver) RemoveSubscribedListFromUser(ctx context.Context, user
return errs.Wrap(err)
}
// GetAllSubscribeList Get all users subscribed by this user
// GetAllSubscribeList Get all users subscribed by this user.
func (u *UserMongoDriver) GetAllSubscribeList(ctx context.Context, userID string) (userIDList []string, err error) {
var user unrelation.UserModel
cursor := u.userCollection.FindOne(
@@ -168,7 +168,7 @@ func (u *UserMongoDriver) GetAllSubscribeList(ctx context.Context, userID string
return user.UserIDList, nil
}
// GetSubscribedList Get the user subscribed by those users
// GetSubscribedList Get the user subscribed by those users.
func (u *UserMongoDriver) GetSubscribedList(ctx context.Context, userID string) (userIDList []string, err error) {
var user unrelation.UserModel
cursor := u.userCollection.FindOne(
+4 -2
View File
@@ -56,7 +56,8 @@ func newContentTypeConf() map[int32]config.NotificationConf {
constant.GroupInfoSetAnnouncementNotification: config.Config.Notification.GroupInfoSetAnnouncement,
constant.GroupInfoSetNameNotification: config.Config.Notification.GroupInfoSetName,
// user
constant.UserInfoUpdatedNotification: config.Config.Notification.UserInfoUpdated,
constant.UserInfoUpdatedNotification: config.Config.Notification.UserInfoUpdated,
constant.UserStatusChangeNotification: config.Config.Notification.UserStatusChanged,
// friend
constant.FriendApplicationNotification: config.Config.Notification.FriendApplicationAdded,
constant.FriendApplicationApprovedNotification: config.Config.Notification.FriendApplicationApproved,
@@ -102,7 +103,8 @@ func newSessionTypeConf() map[int32]int32 {
constant.GroupInfoSetAnnouncementNotification: constant.SuperGroupChatType,
constant.GroupInfoSetNameNotification: constant.SuperGroupChatType,
// user
constant.UserInfoUpdatedNotification: constant.SingleChatType,
constant.UserInfoUpdatedNotification: constant.SingleChatType,
constant.UserStatusChangeNotification: constant.SingleChatType,
// friend
constant.FriendApplicationNotification: constant.SingleChatType,
constant.FriendApplicationApprovedNotification: constant.SingleChatType,
+19 -19
View File
@@ -136,7 +136,7 @@ func (f *FriendNotificationSender) FriendApplicationAddNotification(
return f.Notification(ctx, req.FromUserID, req.ToUserID, constant.FriendApplicationNotification, &tips)
}
func (c *FriendNotificationSender) FriendApplicationAgreedNotification(
func (f *FriendNotificationSender) FriendApplicationAgreedNotification(
ctx context.Context,
req *pbFriend.RespondFriendApplyReq,
) error {
@@ -144,10 +144,10 @@ func (c *FriendNotificationSender) FriendApplicationAgreedNotification(
FromUserID: req.FromUserID,
ToUserID: req.ToUserID,
}, HandleMsg: req.HandleMsg}
return c.Notification(ctx, req.ToUserID, req.FromUserID, constant.FriendApplicationApprovedNotification, &tips)
return f.Notification(ctx, req.ToUserID, req.FromUserID, constant.FriendApplicationApprovedNotification, &tips)
}
func (c *FriendNotificationSender) FriendApplicationRefusedNotification(
func (f *FriendNotificationSender) FriendApplicationRefusedNotification(
ctx context.Context,
req *pbFriend.RespondFriendApplyReq,
) error {
@@ -155,15 +155,15 @@ func (c *FriendNotificationSender) FriendApplicationRefusedNotification(
FromUserID: req.FromUserID,
ToUserID: req.ToUserID,
}, HandleMsg: req.HandleMsg}
return c.Notification(ctx, req.ToUserID, req.FromUserID, constant.FriendApplicationRejectedNotification, &tips)
return f.Notification(ctx, req.ToUserID, req.FromUserID, constant.FriendApplicationRejectedNotification, &tips)
}
func (c *FriendNotificationSender) FriendAddedNotification(
func (f *FriendNotificationSender) FriendAddedNotification(
ctx context.Context,
operationID, opUserID, fromUserID, toUserID string,
) error {
tips := sdkws.FriendAddedTips{Friend: &sdkws.FriendInfo{}, OpUser: &sdkws.PublicUserInfo{}}
user, err := c.getUsersInfo(ctx, []string{opUserID})
user, err := f.getUsersInfo(ctx, []string{opUserID})
if err != nil {
return err
}
@@ -171,52 +171,52 @@ func (c *FriendNotificationSender) FriendAddedNotification(
tips.OpUser.Ex = user[0].GetEx()
tips.OpUser.Nickname = user[0].GetNickname()
tips.OpUser.FaceURL = user[0].GetFaceURL()
friends, err := c.db.FindFriendsWithError(ctx, fromUserID, []string{toUserID})
friends, err := f.db.FindFriendsWithError(ctx, fromUserID, []string{toUserID})
if err != nil {
return err
}
tips.Friend, err = convert.FriendDB2Pb(ctx, friends[0], c.getUsersInfoMap)
tips.Friend, err = convert.FriendDB2Pb(ctx, friends[0], f.getUsersInfoMap)
if err != nil {
return err
}
return c.Notification(ctx, fromUserID, toUserID, constant.FriendAddedNotification, &tips)
return f.Notification(ctx, fromUserID, toUserID, constant.FriendAddedNotification, &tips)
}
func (c *FriendNotificationSender) FriendDeletedNotification(ctx context.Context, req *pbFriend.DeleteFriendReq) error {
func (f *FriendNotificationSender) FriendDeletedNotification(ctx context.Context, req *pbFriend.DeleteFriendReq) error {
tips := sdkws.FriendDeletedTips{FromToUserID: &sdkws.FromToUserID{
FromUserID: req.OwnerUserID,
ToUserID: req.FriendUserID,
}}
return c.Notification(ctx, req.OwnerUserID, req.FriendUserID, constant.FriendDeletedNotification, &tips)
return f.Notification(ctx, req.OwnerUserID, req.FriendUserID, constant.FriendDeletedNotification, &tips)
}
func (c *FriendNotificationSender) FriendRemarkSetNotification(ctx context.Context, fromUserID, toUserID string) error {
func (f *FriendNotificationSender) FriendRemarkSetNotification(ctx context.Context, fromUserID, toUserID string) error {
tips := sdkws.FriendInfoChangedTips{FromToUserID: &sdkws.FromToUserID{}}
tips.FromToUserID.FromUserID = fromUserID
tips.FromToUserID.ToUserID = toUserID
return c.Notification(ctx, fromUserID, toUserID, constant.FriendRemarkSetNotification, &tips)
return f.Notification(ctx, fromUserID, toUserID, constant.FriendRemarkSetNotification, &tips)
}
func (c *FriendNotificationSender) BlackAddedNotification(ctx context.Context, req *pbFriend.AddBlackReq) error {
func (f *FriendNotificationSender) BlackAddedNotification(ctx context.Context, req *pbFriend.AddBlackReq) error {
tips := sdkws.BlackAddedTips{FromToUserID: &sdkws.FromToUserID{}}
tips.FromToUserID.FromUserID = req.OwnerUserID
tips.FromToUserID.ToUserID = req.BlackUserID
return c.Notification(ctx, req.OwnerUserID, req.BlackUserID, constant.BlackAddedNotification, &tips)
return f.Notification(ctx, req.OwnerUserID, req.BlackUserID, constant.BlackAddedNotification, &tips)
}
func (c *FriendNotificationSender) BlackDeletedNotification(ctx context.Context, req *pbFriend.RemoveBlackReq) {
func (f *FriendNotificationSender) BlackDeletedNotification(ctx context.Context, req *pbFriend.RemoveBlackReq) {
blackDeletedTips := sdkws.BlackDeletedTips{FromToUserID: &sdkws.FromToUserID{
FromUserID: req.OwnerUserID,
ToUserID: req.BlackUserID,
}}
c.Notification(ctx, req.OwnerUserID, req.BlackUserID, constant.BlackDeletedNotification, &blackDeletedTips)
f.Notification(ctx, req.OwnerUserID, req.BlackUserID, constant.BlackDeletedNotification, &blackDeletedTips)
}
func (c *FriendNotificationSender) FriendInfoUpdatedNotification(
func (f *FriendNotificationSender) FriendInfoUpdatedNotification(
ctx context.Context,
changedUserID string,
needNotifiedUserID string,
) {
tips := sdkws.UserInfoUpdatedTips{UserID: changedUserID}
c.Notification(ctx, mcontext.GetOpUserID(ctx), needNotifiedUserID, constant.FriendInfoUpdatedNotification, &tips)
f.Notification(ctx, mcontext.GetOpUserID(ctx), needNotifiedUserID, constant.FriendInfoUpdatedNotification, &tips)
}
+103
View File
@@ -0,0 +1,103 @@
// 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 notification
import (
"context"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/controller"
relationTb "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation"
"github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient"
"github.com/OpenIMSDK/protocol/constant"
"github.com/OpenIMSDK/protocol/sdkws"
)
type UserNotificationSender struct {
*rpcclient.NotificationSender
getUsersInfo func(ctx context.Context, userIDs []string) ([]CommonUser, error)
// db controller
db controller.UserDatabase
}
type userNotificationSenderOptions func(*UserNotificationSender)
func WithUserDB(db controller.UserDatabase) userNotificationSenderOptions {
return func(u *UserNotificationSender) {
u.db = db
}
}
func WithUserFunc(
fn func(ctx context.Context, userIDs []string) (users []*relationTb.UserModel, err error),
) userNotificationSenderOptions {
return func(u *UserNotificationSender) {
f := func(ctx context.Context, userIDs []string) (result []CommonUser, err error) {
users, err := fn(ctx, userIDs)
if err != nil {
return nil, err
}
for _, user := range users {
result = append(result, user)
}
return result, nil
}
u.getUsersInfo = f
}
}
func NewUserNotificationSender(
msgRpcClient *rpcclient.MessageRpcClient,
opts ...userNotificationSenderOptions,
) *UserNotificationSender {
f := &UserNotificationSender{
NotificationSender: rpcclient.NewNotificationSender(rpcclient.WithRpcClient(msgRpcClient)),
}
for _, opt := range opts {
opt(f)
}
return f
}
func (u *UserNotificationSender) getUsersInfoMap(
ctx context.Context,
userIDs []string,
) (map[string]*sdkws.UserInfo, error) {
users, err := u.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)
}
return result, nil
}
func (u *UserNotificationSender) getFromToUserNickname(
ctx context.Context,
fromUserID, toUserID string,
) (string, string, error) {
users, err := u.getUsersInfoMap(ctx, []string{fromUserID, toUserID})
if err != nil {
return "", "", nil
}
return users[fromUserID].Nickname, users[toUserID].Nickname, nil
}
func (u *UserNotificationSender) UserStatusChangeNotification(
ctx context.Context,
tips *sdkws.UserStatusChangeTips,
) error {
return u.Notification(ctx, tips.FromUserID, tips.ToUserID, constant.UserStatusChangeNotification, tips)
}