feat: support server-issued configuration, which can be set for individual users (#3271)
* pb * fix: Modifying other fields while setting IsPrivateChat does not take effect * fix: quote message error revoke * refactoring scheduled tasks * refactoring scheduled tasks * refactoring scheduled tasks * refactoring scheduled tasks * refactoring scheduled tasks * refactoring scheduled tasks * upgrading pkg tools * fix * fix * optimize log output * feat: support GetLastMessage * feat: support GetLastMessage * feat: s3 switch * feat: s3 switch * fix: GetUsersOnline * feat: SendBusinessNotification supported configuration parameters * feat: SendBusinessNotification supported configuration parameters * feat: SendBusinessNotification supported configuration parameters * feat: seq conversion failed without exiting * fix: DeleteDoc crash * fix: fill send time * fix: fill send time * fix: crash caused by withdrawing messages from users who have left the group * fix: user msg timestamp * seq read config * seq read config * fix: the source message of the reference is withdrawn, and the referenced message is deleted * feat: optimize the default notification.yml * fix: shouldPushOffline * fix: the sorting is wrong after canceling the administrator in group settings * feat: Sending messages supports returning fields modified by webhook * feat: Sending messages supports returning fields modified by webhook * feat: Sending messages supports returning fields modified by webhook * fix: oss specifies content-type when uploading * fix: the version number contains a line break * fix: the version number contains a line break * feat: support client config * feat: support client configpull/3286/head
parent
aca0eac955
commit
d385fdd0aa
@ -0,0 +1,71 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/authverify"
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/model"
|
||||
pbuser "github.com/openimsdk/protocol/user"
|
||||
"github.com/openimsdk/tools/utils/datautil"
|
||||
)
|
||||
|
||||
func (s *userServer) GetUserClientConfig(ctx context.Context, req *pbuser.GetUserClientConfigReq) (*pbuser.GetUserClientConfigResp, error) {
|
||||
if req.UserID != "" {
|
||||
if err := authverify.CheckAccessV3(ctx, req.UserID, s.config.Share.IMAdminUserID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if _, err := s.db.GetUserByID(ctx, req.UserID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
res, err := s.clientConfig.GetUserConfig(ctx, req.UserID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &pbuser.GetUserClientConfigResp{Configs: res}, nil
|
||||
}
|
||||
|
||||
func (s *userServer) SetUserClientConfig(ctx context.Context, req *pbuser.SetUserClientConfigReq) (*pbuser.SetUserClientConfigResp, error) {
|
||||
if err := authverify.CheckAdmin(ctx, s.config.Share.IMAdminUserID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if req.UserID != "" {
|
||||
if _, err := s.db.GetUserByID(ctx, req.UserID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if err := s.clientConfig.SetUserConfig(ctx, req.UserID, req.Configs); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &pbuser.SetUserClientConfigResp{}, nil
|
||||
}
|
||||
|
||||
func (s *userServer) DelUserClientConfig(ctx context.Context, req *pbuser.DelUserClientConfigReq) (*pbuser.DelUserClientConfigResp, error) {
|
||||
if err := authverify.CheckAdmin(ctx, s.config.Share.IMAdminUserID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.clientConfig.DelUserConfig(ctx, req.UserID, req.Keys); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &pbuser.DelUserClientConfigResp{}, nil
|
||||
}
|
||||
|
||||
func (s *userServer) PageUserClientConfig(ctx context.Context, req *pbuser.PageUserClientConfigReq) (*pbuser.PageUserClientConfigResp, error) {
|
||||
if err := authverify.CheckAdmin(ctx, s.config.Share.IMAdminUserID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
total, res, err := s.clientConfig.GetUserConfigPage(ctx, req.UserID, req.Key, req.Pagination)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &pbuser.PageUserClientConfigResp{
|
||||
Total: total,
|
||||
Configs: datautil.Slice(res, func(e *model.ClientConfig) *pbuser.ClientConfig {
|
||||
return &pbuser.ClientConfig{
|
||||
UserID: e.UserID,
|
||||
Key: e.Key,
|
||||
Value: e.Value,
|
||||
}
|
||||
}),
|
||||
}, nil
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package cachekey
|
||||
|
||||
const ClientConfig = "CLIENT_CONFIG"
|
||||
|
||||
func GetClientConfigKey(userID string) string {
|
||||
if userID == "" {
|
||||
return ClientConfig
|
||||
}
|
||||
return ClientConfig + ":" + userID
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package cache
|
||||
|
||||
import "context"
|
||||
|
||||
type ClientConfigCache interface {
|
||||
DeleteUserCache(ctx context.Context, userIDs []string) error
|
||||
GetUserConfig(ctx context.Context, userID string) (map[string]string, error)
|
||||
}
|
@ -0,0 +1,69 @@
|
||||
package redis
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/cache"
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/cache/cachekey"
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/database"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
func NewClientConfigCache(rdb redis.UniversalClient, mgo database.ClientConfig) cache.ClientConfigCache {
|
||||
rc := newRocksCacheClient(rdb)
|
||||
return &ClientConfigCache{
|
||||
mgo: mgo,
|
||||
rcClient: rc,
|
||||
delete: rc.GetBatchDeleter(),
|
||||
}
|
||||
}
|
||||
|
||||
type ClientConfigCache struct {
|
||||
mgo database.ClientConfig
|
||||
rcClient *rocksCacheClient
|
||||
delete cache.BatchDeleter
|
||||
}
|
||||
|
||||
func (x *ClientConfigCache) getExpireTime(userID string) time.Duration {
|
||||
if userID == "" {
|
||||
return time.Hour * 24
|
||||
} else {
|
||||
return time.Hour
|
||||
}
|
||||
}
|
||||
|
||||
func (x *ClientConfigCache) getClientConfigKey(userID string) string {
|
||||
return cachekey.GetClientConfigKey(userID)
|
||||
}
|
||||
|
||||
func (x *ClientConfigCache) GetConfig(ctx context.Context, userID string) (map[string]string, error) {
|
||||
return getCache(ctx, x.rcClient, x.getClientConfigKey(userID), x.getExpireTime(userID), func(ctx context.Context) (map[string]string, error) {
|
||||
return x.mgo.Get(ctx, userID)
|
||||
})
|
||||
}
|
||||
|
||||
func (x *ClientConfigCache) DeleteUserCache(ctx context.Context, userIDs []string) error {
|
||||
keys := make([]string, 0, len(userIDs))
|
||||
for _, userID := range userIDs {
|
||||
keys = append(keys, x.getClientConfigKey(userID))
|
||||
}
|
||||
return x.delete.ExecDelWithKeys(ctx, keys)
|
||||
}
|
||||
|
||||
func (x *ClientConfigCache) GetUserConfig(ctx context.Context, userID string) (map[string]string, error) {
|
||||
config, err := x.GetConfig(ctx, "")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if userID != "" {
|
||||
userConfig, err := x.GetConfig(ctx, userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for k, v := range userConfig {
|
||||
config[k] = v
|
||||
}
|
||||
}
|
||||
return config, nil
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/cache"
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/database"
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/model"
|
||||
"github.com/openimsdk/tools/db/pagination"
|
||||
"github.com/openimsdk/tools/db/tx"
|
||||
)
|
||||
|
||||
type ClientConfigDatabase interface {
|
||||
SetUserConfig(ctx context.Context, userID string, config map[string]string) error
|
||||
GetUserConfig(ctx context.Context, userID string) (map[string]string, error)
|
||||
DelUserConfig(ctx context.Context, userID string, keys []string) error
|
||||
GetUserConfigPage(ctx context.Context, userID string, key string, pagination pagination.Pagination) (int64, []*model.ClientConfig, error)
|
||||
}
|
||||
|
||||
func NewClientConfigDatabase(db database.ClientConfig, cache cache.ClientConfigCache, tx tx.Tx) ClientConfigDatabase {
|
||||
return &clientConfigDatabase{
|
||||
tx: tx,
|
||||
db: db,
|
||||
cache: cache,
|
||||
}
|
||||
}
|
||||
|
||||
type clientConfigDatabase struct {
|
||||
tx tx.Tx
|
||||
db database.ClientConfig
|
||||
cache cache.ClientConfigCache
|
||||
}
|
||||
|
||||
func (x *clientConfigDatabase) SetUserConfig(ctx context.Context, userID string, config map[string]string) error {
|
||||
return x.tx.Transaction(ctx, func(ctx context.Context) error {
|
||||
if err := x.db.Set(ctx, userID, config); err != nil {
|
||||
return err
|
||||
}
|
||||
return x.cache.DeleteUserCache(ctx, []string{userID})
|
||||
})
|
||||
}
|
||||
|
||||
func (x *clientConfigDatabase) GetUserConfig(ctx context.Context, userID string) (map[string]string, error) {
|
||||
return x.cache.GetUserConfig(ctx, userID)
|
||||
}
|
||||
|
||||
func (x *clientConfigDatabase) DelUserConfig(ctx context.Context, userID string, keys []string) error {
|
||||
return x.tx.Transaction(ctx, func(ctx context.Context) error {
|
||||
if err := x.db.Del(ctx, userID, keys); err != nil {
|
||||
return err
|
||||
}
|
||||
return x.cache.DeleteUserCache(ctx, []string{userID})
|
||||
})
|
||||
}
|
||||
|
||||
func (x *clientConfigDatabase) GetUserConfigPage(ctx context.Context, userID string, key string, pagination pagination.Pagination) (int64, []*model.ClientConfig, error) {
|
||||
return x.db.GetPage(ctx, userID, key, pagination)
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/model"
|
||||
"github.com/openimsdk/tools/db/pagination"
|
||||
)
|
||||
|
||||
type ClientConfig interface {
|
||||
Set(ctx context.Context, userID string, config map[string]string) error
|
||||
Get(ctx context.Context, userID string) (map[string]string, error)
|
||||
Del(ctx context.Context, userID string, keys []string) error
|
||||
GetPage(ctx context.Context, userID string, key string, pagination pagination.Pagination) (int64, []*model.ClientConfig, error)
|
||||
}
|
@ -0,0 +1,99 @@
|
||||
// Copyright © 2023 OpenIM open source community. 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 mgo
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/database"
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/model"
|
||||
"github.com/openimsdk/tools/db/mongoutil"
|
||||
"github.com/openimsdk/tools/db/pagination"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
|
||||
"github.com/openimsdk/tools/errs"
|
||||
)
|
||||
|
||||
func NewClientConfig(db *mongo.Database) (database.ClientConfig, error) {
|
||||
coll := db.Collection("config")
|
||||
_, err := coll.Indexes().CreateMany(context.Background(), []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: "key", Value: 1},
|
||||
{Key: "user_id", Value: 1},
|
||||
},
|
||||
Options: options.Index().SetUnique(true),
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, errs.Wrap(err)
|
||||
}
|
||||
return &ClientConfig{
|
||||
coll: coll,
|
||||
}, nil
|
||||
}
|
||||
|
||||
type ClientConfig struct {
|
||||
coll *mongo.Collection
|
||||
}
|
||||
|
||||
func (x *ClientConfig) Set(ctx context.Context, userID string, config map[string]string) error {
|
||||
if len(config) == 0 {
|
||||
return nil
|
||||
}
|
||||
for key, value := range config {
|
||||
filter := bson.M{"key": key, "user_id": userID}
|
||||
update := bson.M{
|
||||
"value": value,
|
||||
}
|
||||
err := mongoutil.UpdateOne(ctx, x.coll, filter, bson.M{"$set": update}, false, options.Update().SetUpsert(true))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (x *ClientConfig) Get(ctx context.Context, userID string) (map[string]string, error) {
|
||||
cs, err := mongoutil.Find[*model.ClientConfig](ctx, x.coll, bson.M{"user_id": userID})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cm := make(map[string]string)
|
||||
for _, config := range cs {
|
||||
cm[config.Key] = config.Value
|
||||
}
|
||||
return cm, nil
|
||||
}
|
||||
|
||||
func (x *ClientConfig) Del(ctx context.Context, userID string, keys []string) error {
|
||||
if len(keys) == 0 {
|
||||
return nil
|
||||
}
|
||||
return mongoutil.DeleteMany(ctx, x.coll, bson.M{"key": bson.M{"$in": keys}, "user_id": userID})
|
||||
}
|
||||
|
||||
func (x *ClientConfig) GetPage(ctx context.Context, userID string, key string, pagination pagination.Pagination) (int64, []*model.ClientConfig, error) {
|
||||
filter := bson.M{}
|
||||
if userID != "" {
|
||||
filter["user_id"] = userID
|
||||
}
|
||||
if key != "" {
|
||||
filter["key"] = key
|
||||
}
|
||||
return mongoutil.FindPage[*model.ClientConfig](ctx, x.coll, filter, pagination)
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package model
|
||||
|
||||
type ClientConfig struct {
|
||||
Key string `bson:"key"`
|
||||
UserID string `bson:"user_id"`
|
||||
Value string `bson:"value"`
|
||||
}
|
Loading…
Reference in new issue