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.
275 lines
12 KiB
275 lines
12 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"
|
||
6 months ago
|
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/database"
|
||
|
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/model"
|
||
8 months ago
|
"github.com/openimsdk/tools/db/pagination"
|
||
|
"github.com/openimsdk/tools/db/tx"
|
||
|
"github.com/openimsdk/tools/utils/datautil"
|
||
1 year ago
|
"time"
|
||
1 year ago
|
|
||
8 months ago
|
"github.com/openimsdk/protocol/user"
|
||
|
"github.com/openimsdk/tools/errs"
|
||
|
|
||
6 months ago
|
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/cache"
|
||
1 year ago
|
)
|
||
|
|
||
|
type UserDatabase interface {
|
||
1 year ago
|
// FindWithError Get the information of the specified user. If the userID is not found, it will also return an error
|
||
6 months ago
|
FindWithError(ctx context.Context, userIDs []string) (users []*model.User, err error)
|
||
1 year ago
|
// Find Get the information of the specified user If the userID is not found, no error will be returned
|
||
6 months ago
|
Find(ctx context.Context, userIDs []string) (users []*model.User, err error)
|
||
11 months ago
|
// Find userInfo By Nickname
|
||
6 months ago
|
FindByNickname(ctx context.Context, nickname string) (users []*model.User, err error)
|
||
11 months ago
|
// Find notificationAccounts
|
||
6 months ago
|
FindNotification(ctx context.Context, level int64) (users []*model.User, err error)
|
||
|
// Create Insert multiple external guarantees that the userID is not repeated and does not exist in the storage
|
||
|
Create(ctx context.Context, users []*model.User) (err error)
|
||
1 year ago
|
// UpdateByMap update (zero value) external guarantee userID exists
|
||
1 year ago
|
UpdateByMap(ctx context.Context, userID string, args map[string]any) (err error)
|
||
11 months ago
|
// FindUser
|
||
6 months ago
|
PageFindUser(ctx context.Context, level1 int64, level2 int64, pagination pagination.Pagination) (count int64, users []*model.User, err error)
|
||
8 months ago
|
// FindUser with keyword
|
||
6 months ago
|
PageFindUserWithKeyword(ctx context.Context, level1 int64, level2 int64, userID string, nickName string, pagination pagination.Pagination) (count int64, users []*model.User, err error)
|
||
1 year ago
|
// Page If not found, no error is returned
|
||
6 months ago
|
Page(ctx context.Context, pagination pagination.Pagination) (count int64, users []*model.User, err error)
|
||
1 year ago
|
// IsExist true as long as one exists
|
||
1 year ago
|
IsExist(ctx context.Context, userIDs []string) (exist bool, err error)
|
||
1 year ago
|
// GetAllUserID Get all user IDs
|
||
1 year ago
|
GetAllUserID(ctx context.Context, pagination pagination.Pagination) (int64, []string, error)
|
||
11 months ago
|
// Get user by userID
|
||
6 months ago
|
GetUserByID(ctx context.Context, userID string) (user *model.User, err error)
|
||
|
// InitOnce Inside the function, first query whether it exists in the storage, if it exists, do nothing; if it does not exist, insert it
|
||
|
InitOnce(ctx context.Context, users []*model.User) (err error)
|
||
1 year ago
|
// CountTotal Get the total number of users
|
||
1 year ago
|
CountTotal(ctx context.Context, before *time.Time) (int64, error)
|
||
1 year ago
|
// CountRangeEverydayTotal Get the user increment in the range
|
||
1 year ago
|
CountRangeEverydayTotal(ctx context.Context, start time.Time, end time.Time) (map[string]int64, error)
|
||
1 year ago
|
// SubscribeUsersStatus Subscribe a user's presence status
|
||
1 year ago
|
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
|
||
1 year ago
|
// GetAllSubscribeList Get a list of all subscriptions
|
||
|
GetAllSubscribeList(ctx context.Context, userID string) ([]string, error)
|
||
|
// GetSubscribedList Get all subscribed lists
|
||
|
GetSubscribedList(ctx context.Context, userID string) ([]string, error)
|
||
1 year ago
|
// GetUserStatus Get the online status of the user
|
||
|
GetUserStatus(ctx context.Context, userIDs []string) ([]*user.OnlineStatus, error)
|
||
|
// SetUserStatus Set the user status and store the user status in redis
|
||
1 year ago
|
SetUserStatus(ctx context.Context, userID string, status, platformID int32) error
|
||
12 months ago
|
|
||
8 months ago
|
// CRUD user command
|
||
11 months ago
|
AddUserCommand(ctx context.Context, userID string, Type int32, UUID string, value string, ex string) error
|
||
12 months ago
|
DeleteUserCommand(ctx context.Context, userID string, Type int32, UUID string) error
|
||
11 months ago
|
UpdateUserCommand(ctx context.Context, userID string, Type int32, UUID string, val map[string]any) error
|
||
12 months ago
|
GetUserCommands(ctx context.Context, userID string, Type int32) ([]*user.CommandInfoResp, error)
|
||
11 months ago
|
GetAllUserCommands(ctx context.Context, userID string) ([]*user.AllCommandInfoResp, error)
|
||
1 year ago
|
}
|
||
|
|
||
|
type userDatabase struct {
|
||
8 months ago
|
tx tx.Tx
|
||
6 months ago
|
userDB database.User
|
||
1 year ago
|
cache cache.UserCache
|
||
6 months ago
|
mongoDB database.SubscribeUser
|
||
1 year ago
|
}
|
||
|
|
||
6 months ago
|
func NewUserDatabase(userDB database.User, cache cache.UserCache, tx tx.Tx, mongoDB database.SubscribeUser) UserDatabase {
|
||
1 year ago
|
return &userDatabase{userDB: userDB, cache: cache, tx: tx, mongoDB: mongoDB}
|
||
1 year ago
|
}
|
||
|
|
||
6 months ago
|
func (u *userDatabase) InitOnce(ctx context.Context, users []*model.User) error {
|
||
1 year ago
|
// Extract user IDs from the given user models.
|
||
6 months ago
|
userIDs := datautil.Slice(users, func(e *model.User) string {
|
||
1 year ago
|
return e.UserID
|
||
|
})
|
||
1 year ago
|
|
||
|
// Find existing users in the database.
|
||
|
existingUsers, err := u.userDB.Find(ctx, userIDs)
|
||
1 year ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
1 year ago
|
|
||
|
// Determine which users are missing from the database.
|
||
6 months ago
|
missingUsers := datautil.SliceAnySub(users, existingUsers, func(e *model.User) string {
|
||
1 year ago
|
return e.UserID
|
||
|
})
|
||
|
|
||
|
// Create records for missing users.
|
||
|
if len(missingUsers) > 0 {
|
||
|
if err := u.userDB.Create(ctx, missingUsers); err != nil {
|
||
|
return err
|
||
|
}
|
||
1 year ago
|
}
|
||
1 year ago
|
|
||
1 year ago
|
return nil
|
||
|
}
|
||
|
|
||
1 year ago
|
// FindWithError Get the information of the specified user and return an error if the userID is not found.
|
||
6 months ago
|
func (u *userDatabase) FindWithError(ctx context.Context, userIDs []string) (users []*model.User, err error) {
|
||
1 year ago
|
users, err = u.cache.GetUsersInfo(ctx, userIDs)
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
if len(users) != len(userIDs) {
|
||
8 months ago
|
err = errs.ErrRecordNotFound.WrapMsg("userID not found")
|
||
1 year ago
|
}
|
||
|
return
|
||
|
}
|
||
|
|
||
1 year ago
|
// Find Get the information of the specified user. If the userID is not found, no error will be returned.
|
||
6 months ago
|
func (u *userDatabase) Find(ctx context.Context, userIDs []string) (users []*model.User, err error) {
|
||
1 year ago
|
return u.cache.GetUsersInfo(ctx, userIDs)
|
||
1 year ago
|
}
|
||
|
|
||
6 months ago
|
func (u *userDatabase) FindByNickname(ctx context.Context, nickname string) (users []*model.User, err error) {
|
||
11 months ago
|
return u.userDB.TakeByNickname(ctx, nickname)
|
||
|
}
|
||
|
|
||
6 months ago
|
func (u *userDatabase) FindNotification(ctx context.Context, level int64) (users []*model.User, err error) {
|
||
11 months ago
|
return u.userDB.TakeNotification(ctx, level)
|
||
|
}
|
||
|
|
||
6 months ago
|
// Create Insert multiple external guarantees that the userID is not repeated and does not exist in the storage.
|
||
|
func (u *userDatabase) Create(ctx context.Context, users []*model.User) (err error) {
|
||
1 year ago
|
return u.tx.Transaction(ctx, func(ctx context.Context) error {
|
||
|
if err = u.userDB.Create(ctx, users); err != nil {
|
||
1 year ago
|
return err
|
||
|
}
|
||
6 months ago
|
return u.cache.DelUsersInfo(datautil.Slice(users, func(e *model.User) string {
|
||
1 year ago
|
return e.UserID
|
||
6 months ago
|
})...).ChainExecDel(ctx)
|
||
1 year ago
|
})
|
||
1 year ago
|
}
|
||
|
|
||
1 year ago
|
// UpdateByMap update (zero value) externally guarantees that userID exists.
|
||
1 year ago
|
func (u *userDatabase) UpdateByMap(ctx context.Context, userID string, args map[string]any) (err error) {
|
||
|
return u.tx.Transaction(ctx, func(ctx context.Context) error {
|
||
|
if err := u.userDB.UpdateByMap(ctx, userID, args); err != nil {
|
||
|
return err
|
||
|
}
|
||
6 months ago
|
return u.cache.DelUsersInfo(userID).ChainExecDel(ctx)
|
||
1 year ago
|
})
|
||
1 year ago
|
}
|
||
|
|
||
1 year ago
|
// Page Gets, returns no error if not found.
|
||
6 months ago
|
func (u *userDatabase) Page(ctx context.Context, pagination pagination.Pagination) (count int64, users []*model.User, err error) {
|
||
1 year ago
|
return u.userDB.Page(ctx, pagination)
|
||
1 year ago
|
}
|
||
|
|
||
6 months ago
|
func (u *userDatabase) PageFindUser(ctx context.Context, level1 int64, level2 int64, pagination pagination.Pagination) (count int64, users []*model.User, err error) {
|
||
11 months ago
|
return u.userDB.PageFindUser(ctx, level1, level2, pagination)
|
||
|
}
|
||
10 months ago
|
|
||
6 months ago
|
func (u *userDatabase) PageFindUserWithKeyword(ctx context.Context, level1 int64, level2 int64, userID, nickName string, pagination pagination.Pagination) (count int64, users []*model.User, err error) {
|
||
11 months ago
|
return u.userDB.PageFindUserWithKeyword(ctx, level1, level2, userID, nickName, pagination)
|
||
11 months ago
|
}
|
||
|
|
||
1 year ago
|
// IsExist Does userIDs exist? As long as there is one, it will be true.
|
||
1 year ago
|
func (u *userDatabase) IsExist(ctx context.Context, userIDs []string) (exist bool, err error) {
|
||
|
users, err := u.userDB.Find(ctx, userIDs)
|
||
|
if err != nil {
|
||
|
return false, err
|
||
|
}
|
||
|
if len(users) > 0 {
|
||
|
return true, nil
|
||
|
}
|
||
|
return false, nil
|
||
|
}
|
||
|
|
||
1 year ago
|
// GetAllUserID Get all user IDs.
|
||
1 year ago
|
func (u *userDatabase) GetAllUserID(ctx context.Context, pagination pagination.Pagination) (total int64, userIDs []string, err error) {
|
||
|
return u.userDB.GetAllUserID(ctx, pagination)
|
||
1 year ago
|
}
|
||
|
|
||
6 months ago
|
func (u *userDatabase) GetUserByID(ctx context.Context, userID string) (user *model.User, err error) {
|
||
11 months ago
|
return u.userDB.Take(ctx, userID)
|
||
|
}
|
||
|
|
||
1 year ago
|
// CountTotal Get the total number of users.
|
||
1 year ago
|
func (u *userDatabase) CountTotal(ctx context.Context, before *time.Time) (count int64, err error) {
|
||
|
return u.userDB.CountTotal(ctx, before)
|
||
1 year ago
|
}
|
||
|
|
||
1 year ago
|
// CountRangeEverydayTotal Get the user increment in the range.
|
||
1 year ago
|
func (u *userDatabase) CountRangeEverydayTotal(ctx context.Context, start time.Time, end time.Time) (map[string]int64, error) {
|
||
1 year ago
|
return u.userDB.CountRangeEverydayTotal(ctx, start, end)
|
||
|
}
|
||
1 year ago
|
|
||
1 year ago
|
// SubscribeUsersStatus Subscribe or unsubscribe a user's presence status.
|
||
1 year ago
|
func (u *userDatabase) SubscribeUsersStatus(ctx context.Context, userID string, userIDs []string) error {
|
||
|
err := u.mongoDB.AddSubscriptionList(ctx, userID, userIDs)
|
||
|
return err
|
||
|
}
|
||
|
|
||
1 year ago
|
// UnsubscribeUsersStatus unsubscribe a user's presence status.
|
||
1 year ago
|
func (u *userDatabase) UnsubscribeUsersStatus(ctx context.Context, userID string, userIDs []string) error {
|
||
|
err := u.mongoDB.UnsubscriptionList(ctx, userID, userIDs)
|
||
1 year ago
|
return err
|
||
|
}
|
||
|
|
||
|
// GetAllSubscribeList Get a list of all subscriptions.
|
||
|
func (u *userDatabase) GetAllSubscribeList(ctx context.Context, userID string) ([]string, error) {
|
||
1 year ago
|
list, err := u.mongoDB.GetAllSubscribeList(ctx, userID)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return list, nil
|
||
1 year ago
|
}
|
||
|
|
||
1 year ago
|
// GetSubscribedList Get all subscribed lists.
|
||
1 year ago
|
func (u *userDatabase) GetSubscribedList(ctx context.Context, userID string) ([]string, error) {
|
||
1 year ago
|
list, err := u.mongoDB.GetSubscribedList(ctx, userID)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return list, nil
|
||
1 year ago
|
}
|
||
1 year ago
|
|
||
1 year ago
|
// GetUserStatus get user status.
|
||
1 year ago
|
func (u *userDatabase) GetUserStatus(ctx context.Context, userIDs []string) ([]*user.OnlineStatus, error) {
|
||
|
onlineStatusList, err := u.cache.GetUserStatus(ctx, userIDs)
|
||
|
return onlineStatusList, err
|
||
|
}
|
||
|
|
||
1 year ago
|
// SetUserStatus Set the user status and save it in redis.
|
||
1 year ago
|
func (u *userDatabase) SetUserStatus(ctx context.Context, userID string, status, platformID int32) error {
|
||
|
return u.cache.SetUserStatus(ctx, userID, status, platformID)
|
||
1 year ago
|
}
|
||
8 months ago
|
|
||
11 months ago
|
func (u *userDatabase) AddUserCommand(ctx context.Context, userID string, Type int32, UUID string, value string, ex string) error {
|
||
|
return u.userDB.AddUserCommand(ctx, userID, Type, UUID, value, ex)
|
||
12 months ago
|
}
|
||
8 months ago
|
|
||
12 months ago
|
func (u *userDatabase) DeleteUserCommand(ctx context.Context, userID string, Type int32, UUID string) error {
|
||
|
return u.userDB.DeleteUserCommand(ctx, userID, Type, UUID)
|
||
|
}
|
||
8 months ago
|
|
||
11 months ago
|
func (u *userDatabase) UpdateUserCommand(ctx context.Context, userID string, Type int32, UUID string, val map[string]any) error {
|
||
|
return u.userDB.UpdateUserCommand(ctx, userID, Type, UUID, val)
|
||
12 months ago
|
}
|
||
8 months ago
|
|
||
12 months ago
|
func (u *userDatabase) GetUserCommands(ctx context.Context, userID string, Type int32) ([]*user.CommandInfoResp, error) {
|
||
|
commands, err := u.userDB.GetUserCommand(ctx, userID, Type)
|
||
|
return commands, err
|
||
|
}
|
||
8 months ago
|
|
||
11 months ago
|
func (u *userDatabase) GetAllUserCommands(ctx context.Context, userID string) ([]*user.AllCommandInfoResp, error) {
|
||
|
commands, err := u.userDB.GetAllUserCommand(ctx, userID)
|
||
|
return commands, err
|
||
|
}
|