parent
9bb769a2eb
commit
706c6000cb
@ -0,0 +1,45 @@
|
||||
package controller
|
||||
|
||||
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/pagination"
|
||||
)
|
||||
|
||||
// UserGlobalBlackDatabase 全局黑名单业务接口
|
||||
type UserGlobalBlackDatabase interface {
|
||||
// AddBlack 将用户加入全局黑名单
|
||||
AddBlack(ctx context.Context, blacks []*model.UserGlobalBlack) error
|
||||
// RemoveBlack 按昵称将用户从全局黑名单移除
|
||||
RemoveBlack(ctx context.Context, nicknames []string) error
|
||||
// IsBlocked 检查用户是否在全局黑名单
|
||||
IsBlocked(ctx context.Context, userID string) (bool, error)
|
||||
// GetBlackList 分页获取黑名单列表
|
||||
GetBlackList(ctx context.Context, pagination pagination.Pagination) (count int64, blacks []*model.UserGlobalBlack, err error)
|
||||
}
|
||||
|
||||
type userGlobalBlackDatabase struct {
|
||||
db database.UserGlobalBlack
|
||||
}
|
||||
|
||||
func NewUserGlobalBlackDatabase(db database.UserGlobalBlack) UserGlobalBlackDatabase {
|
||||
return &userGlobalBlackDatabase{db: db}
|
||||
}
|
||||
|
||||
func (u *userGlobalBlackDatabase) AddBlack(ctx context.Context, blacks []*model.UserGlobalBlack) error {
|
||||
return u.db.Add(ctx, blacks)
|
||||
}
|
||||
|
||||
func (u *userGlobalBlackDatabase) RemoveBlack(ctx context.Context, nicknames []string) error {
|
||||
return u.db.Remove(ctx, nicknames)
|
||||
}
|
||||
|
||||
func (u *userGlobalBlackDatabase) IsBlocked(ctx context.Context, userID string) (bool, error) {
|
||||
return u.db.IsBlocked(ctx, userID)
|
||||
}
|
||||
|
||||
func (u *userGlobalBlackDatabase) GetBlackList(ctx context.Context, pagination pagination.Pagination) (int64, []*model.UserGlobalBlack, error) {
|
||||
return u.db.Page(ctx, pagination)
|
||||
}
|
||||
@ -0,0 +1,89 @@
|
||||
package mgo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"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"
|
||||
"github.com/openimsdk/tools/errs"
|
||||
"github.com/openimsdk/tools/log"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
func NewUserGlobalBlackMongo(db *mongo.Database) (database.UserGlobalBlack, error) {
|
||||
coll := db.Collection(database.UserGlobalBlackName)
|
||||
_, err := coll.Indexes().CreateOne(context.Background(), mongo.IndexModel{
|
||||
Keys: bson.D{{Key: "user_id", Value: 1}},
|
||||
Options: options.Index().SetUnique(true),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, errs.Wrap(err)
|
||||
}
|
||||
return &UserGlobalBlackMgo{coll: coll}, nil
|
||||
}
|
||||
|
||||
type UserGlobalBlackMgo struct {
|
||||
coll *mongo.Collection
|
||||
}
|
||||
|
||||
func (u *UserGlobalBlackMgo) Add(ctx context.Context, blacks []*model.UserGlobalBlack) error {
|
||||
for _, b := range blacks {
|
||||
if b.CreateTime.IsZero() {
|
||||
b.CreateTime = time.Now()
|
||||
}
|
||||
}
|
||||
// 使用 upsert 避免重复插入报错
|
||||
for _, b := range blacks {
|
||||
filter := bson.M{"user_id": b.UserID}
|
||||
update := bson.M{
|
||||
"$set": bson.M{
|
||||
"nickname": b.Nickname,
|
||||
"operator_id": b.OperatorID,
|
||||
"reason": b.Reason,
|
||||
},
|
||||
"$setOnInsert": bson.M{
|
||||
"user_id": b.UserID,
|
||||
"create_time": b.CreateTime,
|
||||
},
|
||||
}
|
||||
opts := options.Update().SetUpsert(true)
|
||||
if _, err := u.coll.UpdateOne(ctx, filter, update, opts); err != nil {
|
||||
return errs.Wrap(err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *UserGlobalBlackMgo) Remove(ctx context.Context, users []string) error {
|
||||
if len(users) == 0 {
|
||||
return nil
|
||||
}
|
||||
_, err := u.coll.DeleteMany(ctx, bson.M{"user_id": bson.M{"$in": users}})
|
||||
return errs.Wrap(err)
|
||||
}
|
||||
|
||||
func (u *UserGlobalBlackMgo) Find(ctx context.Context, userIDs []string) ([]*model.UserGlobalBlack, error) {
|
||||
if len(userIDs) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
return mongoutil.Find[*model.UserGlobalBlack](ctx, u.coll, bson.M{"user_id": bson.M{"$in": userIDs}})
|
||||
}
|
||||
|
||||
func (u *UserGlobalBlackMgo) IsBlocked(ctx context.Context, userID string) (bool, error) {
|
||||
count, err := u.coll.CountDocuments(ctx, bson.M{"user_id": userID})
|
||||
if err != nil {
|
||||
log.ZWarn(ctx, "IsBlocked failed", err, "collection", database.UserGlobalBlackName, "userID", userID, "count", count)
|
||||
return false, nil
|
||||
}
|
||||
|
||||
return count > 0, nil
|
||||
}
|
||||
|
||||
func (u *UserGlobalBlackMgo) Page(ctx context.Context, pagination pagination.Pagination) (int64, []*model.UserGlobalBlack, error) {
|
||||
return mongoutil.FindPage[*model.UserGlobalBlack](ctx, u.coll, bson.M{}, pagination)
|
||||
}
|
||||
@ -0,0 +1,22 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/model"
|
||||
"github.com/openimsdk/tools/db/pagination"
|
||||
)
|
||||
|
||||
// UserGlobalBlack 全局黑名单持久化接口
|
||||
type UserGlobalBlack interface {
|
||||
// Add 批量添加用户到全局黑名单
|
||||
Add(ctx context.Context, blacks []*model.UserGlobalBlack) error
|
||||
// Remove 按昵称从全局黑名单移除用户
|
||||
Remove(ctx context.Context, nicknames []string) error
|
||||
// Find 查询指定用户是否在黑名单(返回在黑名单中的记录)
|
||||
Find(ctx context.Context, userIDs []string) ([]*model.UserGlobalBlack, error)
|
||||
// IsBlocked 检查单个用户是否在黑名单
|
||||
IsBlocked(ctx context.Context, userID string) (bool, error)
|
||||
// Page 分页查询黑名单列表
|
||||
Page(ctx context.Context, pagination pagination.Pagination) (count int64, blacks []*model.UserGlobalBlack, err error)
|
||||
}
|
||||
@ -0,0 +1,12 @@
|
||||
package model
|
||||
|
||||
import "time"
|
||||
|
||||
// UserGlobalBlack 全局黑名单记录,被加入黑名单的用户无法登录
|
||||
type UserGlobalBlack struct {
|
||||
UserID string `bson:"user_id"`
|
||||
Nickname string `bson:"nickname"`
|
||||
OperatorID string `bson:"operator_id"`
|
||||
Reason string `bson:"reason"`
|
||||
CreateTime time.Time `bson:"create_time"`
|
||||
}
|
||||
Loading…
Reference in new issue