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.
101 lines
3.9 KiB
101 lines
3.9 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/cache"
|
||
|
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/database"
|
||
|
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/model"
|
||
7 months ago
|
"github.com/openimsdk/tools/db/pagination"
|
||
|
"github.com/openimsdk/tools/log"
|
||
|
"github.com/openimsdk/tools/utils/datautil"
|
||
1 year ago
|
)
|
||
|
|
||
|
type BlackDatabase interface {
|
||
9 months ago
|
// Create add BlackList
|
||
6 months ago
|
Create(ctx context.Context, blacks []*model.Black) (err error)
|
||
9 months ago
|
// Delete delete BlackList
|
||
6 months ago
|
Delete(ctx context.Context, blacks []*model.Black) (err error)
|
||
9 months ago
|
// FindOwnerBlacks get BlackList list
|
||
6 months ago
|
FindOwnerBlacks(ctx context.Context, ownerUserID string, pagination pagination.Pagination) (total int64, blacks []*model.Black, err error)
|
||
|
FindBlackInfos(ctx context.Context, ownerUserID string, userIDs []string) (blacks []*model.Black, err error)
|
||
9 months ago
|
// CheckIn Check whether user2 is in the black list of user1 (inUser1Blacks==true) Check whether user1 is in the black list of user2 (inUser2Blacks==true)
|
||
1 year ago
|
CheckIn(ctx context.Context, userID1, userID2 string) (inUser1Blacks bool, inUser2Blacks bool, err error)
|
||
|
}
|
||
|
|
||
|
type blackDatabase struct {
|
||
6 months ago
|
black database.Black
|
||
1 year ago
|
cache cache.BlackCache
|
||
|
}
|
||
|
|
||
6 months ago
|
func NewBlackDatabase(black database.Black, cache cache.BlackCache) BlackDatabase {
|
||
1 year ago
|
return &blackDatabase{black, cache}
|
||
|
}
|
||
|
|
||
9 months ago
|
// Create Add Blacklist.
|
||
6 months ago
|
func (b *blackDatabase) Create(ctx context.Context, blacks []*model.Black) (err error) {
|
||
1 year ago
|
if err := b.black.Create(ctx, blacks); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
return b.deleteBlackIDsCache(ctx, blacks)
|
||
|
}
|
||
|
|
||
9 months ago
|
// Delete Delete Blacklist.
|
||
6 months ago
|
func (b *blackDatabase) Delete(ctx context.Context, blacks []*model.Black) (err error) {
|
||
1 year ago
|
if err := b.black.Delete(ctx, blacks); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
return b.deleteBlackIDsCache(ctx, blacks)
|
||
|
}
|
||
|
|
||
9 months ago
|
// FindOwnerBlacks Get Blacklist List.
|
||
6 months ago
|
func (b *blackDatabase) deleteBlackIDsCache(ctx context.Context, blacks []*model.Black) (err error) {
|
||
|
cache := b.cache.CloneBlackCache()
|
||
1 year ago
|
for _, black := range blacks {
|
||
|
cache = cache.DelBlackIDs(ctx, black.OwnerUserID)
|
||
|
}
|
||
6 months ago
|
return cache.ChainExecDel(ctx)
|
||
1 year ago
|
}
|
||
|
|
||
9 months ago
|
// FindOwnerBlacks Get Blacklist List.
|
||
6 months ago
|
func (b *blackDatabase) FindOwnerBlacks(ctx context.Context, ownerUserID string, pagination pagination.Pagination) (total int64, blacks []*model.Black, err error) {
|
||
12 months ago
|
return b.black.FindOwnerBlacks(ctx, ownerUserID, pagination)
|
||
1 year ago
|
}
|
||
|
|
||
9 months ago
|
// FindOwnerBlacks Get Blacklist List.
|
||
|
func (b *blackDatabase) CheckIn(ctx context.Context, userID1, userID2 string) (inUser1Blacks bool, inUser2Blacks bool, err error) {
|
||
1 year ago
|
userID1BlackIDs, err := b.cache.GetBlackIDs(ctx, userID1)
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
userID2BlackIDs, err := b.cache.GetBlackIDs(ctx, userID2)
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
log.ZDebug(ctx, "blackIDs", "user1BlackIDs", userID1BlackIDs, "user2BlackIDs", userID2BlackIDs)
|
||
7 months ago
|
return datautil.Contain(userID2, userID1BlackIDs...), datautil.Contain(userID1, userID2BlackIDs...), nil
|
||
1 year ago
|
}
|
||
|
|
||
9 months ago
|
// FindBlackIDs Get Blacklist List.
|
||
1 year ago
|
func (b *blackDatabase) FindBlackIDs(ctx context.Context, ownerUserID string) (blackIDs []string, err error) {
|
||
|
return b.cache.GetBlackIDs(ctx, ownerUserID)
|
||
|
}
|
||
1 year ago
|
|
||
9 months ago
|
// FindBlackInfos Get Blacklist List.
|
||
6 months ago
|
func (b *blackDatabase) FindBlackInfos(ctx context.Context, ownerUserID string, userIDs []string) (blacks []*model.Black, err error) {
|
||
1 year ago
|
return b.black.FindOwnerBlackInfos(ctx, ownerUserID, userIDs)
|
||
|
}
|