2023-01-28 13:19:36 +08:00
|
|
|
package controller
|
2023-01-17 17:31:22 +08:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2023-05-16 16:31:35 +08:00
|
|
|
|
2023-03-23 19:02:20 +08:00
|
|
|
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/cache"
|
2023-03-16 10:46:06 +08:00
|
|
|
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation"
|
2023-05-16 16:31:35 +08:00
|
|
|
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
|
2023-01-17 17:31:22 +08:00
|
|
|
)
|
|
|
|
|
|
2023-02-22 12:17:59 +08:00
|
|
|
type BlackDatabase interface {
|
2023-01-31 13:37:35 +08:00
|
|
|
// Create 增加黑名单
|
2023-02-03 12:16:48 +08:00
|
|
|
Create(ctx context.Context, blacks []*relation.BlackModel) (err error)
|
2023-01-31 13:37:35 +08:00
|
|
|
// Delete 删除黑名单
|
2023-02-03 12:16:48 +08:00
|
|
|
Delete(ctx context.Context, blacks []*relation.BlackModel) (err error)
|
2023-01-31 13:37:35 +08:00
|
|
|
// FindOwnerBlacks 获取黑名单列表
|
2023-02-03 12:16:48 +08:00
|
|
|
FindOwnerBlacks(ctx context.Context, ownerUserID string, pageNumber, showNumber int32) (blacks []*relation.BlackModel, total int64, err error)
|
2023-03-23 19:02:20 +08:00
|
|
|
FindBlackIDs(ctx context.Context, ownerUserID string) (blackIDs []string, err error)
|
2023-01-31 11:34:30 +08:00
|
|
|
// CheckIn 检查user2是否在user1的黑名单列表中(inUser1Blacks==true) 检查user1是否在user2的黑名单列表中(inUser2Blacks==true)
|
2023-01-31 15:45:24 +08:00
|
|
|
CheckIn(ctx context.Context, userID1, userID2 string) (inUser1Blacks bool, inUser2Blacks bool, err error)
|
2023-01-17 17:31:22 +08:00
|
|
|
}
|
|
|
|
|
|
2023-02-22 12:17:59 +08:00
|
|
|
type blackDatabase struct {
|
2023-02-21 14:38:01 +08:00
|
|
|
black relation.BlackModelInterface
|
2023-03-23 19:02:20 +08:00
|
|
|
cache cache.BlackCache
|
2023-01-17 17:31:22 +08:00
|
|
|
}
|
2023-01-17 17:43:30 +08:00
|
|
|
|
2023-03-23 19:02:20 +08:00
|
|
|
func NewBlackDatabase(black relation.BlackModelInterface, cache cache.BlackCache) BlackDatabase {
|
|
|
|
|
return &blackDatabase{black, cache}
|
2023-01-30 15:26:39 +08:00
|
|
|
}
|
|
|
|
|
|
2023-01-31 13:37:35 +08:00
|
|
|
// Create 增加黑名单
|
2023-02-22 12:17:59 +08:00
|
|
|
func (b *blackDatabase) Create(ctx context.Context, blacks []*relation.BlackModel) (err error) {
|
2023-05-16 16:31:35 +08:00
|
|
|
if err := b.black.Create(ctx, blacks); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return b.deleteBlackIDsCache(ctx, blacks)
|
2023-01-30 15:26:39 +08:00
|
|
|
}
|
2023-01-31 13:37:35 +08:00
|
|
|
|
|
|
|
|
// Delete 删除黑名单
|
2023-02-22 12:17:59 +08:00
|
|
|
func (b *blackDatabase) Delete(ctx context.Context, blacks []*relation.BlackModel) (err error) {
|
2023-05-16 16:31:35 +08:00
|
|
|
if err := b.black.Delete(ctx, blacks); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return b.deleteBlackIDsCache(ctx, blacks)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (b *blackDatabase) deleteBlackIDsCache(ctx context.Context, blacks []*relation.BlackModel) (err error) {
|
|
|
|
|
cache := b.cache.NewCache()
|
|
|
|
|
for _, black := range blacks {
|
|
|
|
|
cache = cache.DelBlackIDs(ctx, black.OwnerUserID)
|
|
|
|
|
}
|
|
|
|
|
return cache.ExecDel(ctx)
|
2023-01-30 15:26:39 +08:00
|
|
|
}
|
2023-01-31 13:37:35 +08:00
|
|
|
|
|
|
|
|
// FindOwnerBlacks 获取黑名单列表
|
2023-02-22 12:17:59 +08:00
|
|
|
func (b *blackDatabase) FindOwnerBlacks(ctx context.Context, ownerUserID string, pageNumber, showNumber int32) (blacks []*relation.BlackModel, total int64, err error) {
|
2023-02-21 14:38:01 +08:00
|
|
|
return b.black.FindOwnerBlacks(ctx, ownerUserID, pageNumber, showNumber)
|
2023-01-30 15:26:39 +08:00
|
|
|
}
|
2023-01-31 13:37:35 +08:00
|
|
|
|
|
|
|
|
// CheckIn 检查user2是否在user1的黑名单列表中(inUser1Blacks==true) 检查user1是否在user2的黑名单列表中(inUser2Blacks==true)
|
2023-02-22 12:17:59 +08:00
|
|
|
func (b *blackDatabase) CheckIn(ctx context.Context, userID1, userID2 string) (inUser1Blacks bool, inUser2Blacks bool, err error) {
|
2023-05-16 16:31:35 +08:00
|
|
|
userID1BlackIDs, err := b.cache.GetBlackIDs(ctx, userID1)
|
2023-01-31 15:45:24 +08:00
|
|
|
if err != nil {
|
2023-05-16 16:31:35 +08:00
|
|
|
return
|
2023-01-31 15:45:24 +08:00
|
|
|
}
|
2023-05-16 16:31:35 +08:00
|
|
|
userID2BlackIDs, err := b.cache.GetBlackIDs(ctx, userID2)
|
2023-01-31 15:45:24 +08:00
|
|
|
if err != nil {
|
2023-05-16 16:31:35 +08:00
|
|
|
return
|
2023-01-31 15:45:24 +08:00
|
|
|
}
|
2023-05-16 16:31:35 +08:00
|
|
|
return utils.IsContain(userID2, userID1BlackIDs), utils.IsContain(userID1, userID2BlackIDs), nil
|
2023-01-17 17:43:30 +08:00
|
|
|
}
|
2023-03-23 19:02:20 +08:00
|
|
|
|
|
|
|
|
func (b *blackDatabase) FindBlackIDs(ctx context.Context, ownerUserID string) (blackIDs []string, err error) {
|
|
|
|
|
return b.cache.GetBlackIDs(ctx, ownerUserID)
|
|
|
|
|
}
|