122 lines
3.8 KiB
Go
122 lines
3.8 KiB
Go
// 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 chat
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/openimsdk/tools/db/mongoutil"
|
|
"github.com/openimsdk/tools/db/pagination"
|
|
"github.com/openimsdk/tools/errs"
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
|
|
"git.imall.cloud/openim/chat/pkg/common/db/table/chat"
|
|
)
|
|
|
|
func NewSystemConfig(db *mongo.Database) (chat.SystemConfigInterface, error) {
|
|
coll := db.Collection("system_configs")
|
|
_, err := coll.Indexes().CreateMany(context.Background(), []mongo.IndexModel{
|
|
{
|
|
Keys: bson.D{
|
|
{Key: "key", Value: 1},
|
|
},
|
|
Options: options.Index().SetUnique(true),
|
|
},
|
|
{
|
|
Keys: bson.D{
|
|
{Key: "enabled", Value: 1},
|
|
},
|
|
},
|
|
})
|
|
if err != nil {
|
|
return nil, errs.Wrap(err)
|
|
}
|
|
return &SystemConfig{coll: coll}, nil
|
|
}
|
|
|
|
type SystemConfig struct {
|
|
coll *mongo.Collection
|
|
}
|
|
|
|
func (o *SystemConfig) Create(ctx context.Context, configs ...*chat.SystemConfig) error {
|
|
for _, config := range configs {
|
|
if config.CreateTime.IsZero() {
|
|
config.CreateTime = time.Now()
|
|
}
|
|
if config.UpdateTime.IsZero() {
|
|
config.UpdateTime = time.Now()
|
|
}
|
|
}
|
|
return mongoutil.InsertMany(ctx, o.coll, configs)
|
|
}
|
|
|
|
func (o *SystemConfig) Take(ctx context.Context, key string) (*chat.SystemConfig, error) {
|
|
return mongoutil.FindOne[*chat.SystemConfig](ctx, o.coll, bson.M{"key": key})
|
|
}
|
|
|
|
func (o *SystemConfig) FindByKeys(ctx context.Context, keys []string) ([]*chat.SystemConfig, error) {
|
|
if len(keys) == 0 {
|
|
return []*chat.SystemConfig{}, nil
|
|
}
|
|
return mongoutil.Find[*chat.SystemConfig](ctx, o.coll, bson.M{"key": bson.M{"$in": keys}}, options.Find().SetSort(bson.D{{Key: "key", Value: 1}}))
|
|
}
|
|
|
|
func (o *SystemConfig) FindAll(ctx context.Context, pagination pagination.Pagination) (int64, []*chat.SystemConfig, error) {
|
|
filter := bson.M{}
|
|
return mongoutil.FindPage[*chat.SystemConfig](ctx, o.coll, filter, pagination, options.Find().SetSort(bson.D{{Key: "key", Value: 1}}))
|
|
}
|
|
|
|
func (o *SystemConfig) Update(ctx context.Context, key string, data map[string]any) error {
|
|
if len(data) == 0 {
|
|
return nil
|
|
}
|
|
data["update_time"] = time.Now()
|
|
return mongoutil.UpdateOne(ctx, o.coll, bson.M{"key": key}, bson.M{"$set": data}, false)
|
|
}
|
|
|
|
func (o *SystemConfig) UpdateValue(ctx context.Context, key string, value string) error {
|
|
return o.Update(ctx, key, map[string]any{"value": value})
|
|
}
|
|
|
|
func (o *SystemConfig) UpdateEnabled(ctx context.Context, key string, enabled bool) error {
|
|
return o.Update(ctx, key, map[string]any{"enabled": enabled})
|
|
}
|
|
|
|
func (o *SystemConfig) Delete(ctx context.Context, keys []string) error {
|
|
if len(keys) == 0 {
|
|
return nil
|
|
}
|
|
_, err := o.coll.DeleteMany(ctx, bson.M{"key": bson.M{"$in": keys}})
|
|
return errs.Wrap(err)
|
|
}
|
|
|
|
func (o *SystemConfig) GetEnabledConfigs(ctx context.Context) ([]*chat.SystemConfig, error) {
|
|
filter := bson.M{
|
|
"enabled": true,
|
|
}
|
|
return mongoutil.Find[*chat.SystemConfig](ctx, o.coll, filter, options.Find().SetSort(bson.D{{Key: "key", Value: 1}}))
|
|
}
|
|
|
|
func (o *SystemConfig) GetAppConfigs(ctx context.Context) ([]*chat.SystemConfig, error) {
|
|
filter := bson.M{
|
|
"show_in_app": true,
|
|
"enabled": true, // 同时要求 enabled=true
|
|
}
|
|
return mongoutil.Find[*chat.SystemConfig](ctx, o.coll, filter, options.Find().SetSort(bson.D{{Key: "key", Value: 1}}))
|
|
}
|