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.
Open-IM-Server/pkg/common/db/controller/black.go

111 lines
4.0 KiB

2 years ago
package controller
2 years ago
import (
2 years ago
"Open_IM/pkg/common/db/table/relation"
2 years ago
"context"
"errors"
2 years ago
"gorm.io/gorm"
2 years ago
)
type BlackInterface interface {
// Create 增加黑名单
2 years ago
Create(ctx context.Context, blacks []*relation.BlackModel) (err error)
// Delete 删除黑名单
2 years ago
Delete(ctx context.Context, blacks []*relation.BlackModel) (err error)
// FindOwnerBlacks 获取黑名单列表
2 years ago
FindOwnerBlacks(ctx context.Context, ownerUserID string, pageNumber, showNumber int32) (blacks []*relation.BlackModel, total int64, err error)
// CheckIn 检查user2是否在user1的黑名单列表中(inUser1Blacks==true) 检查user1是否在user2的黑名单列表中(inUser2Blacks==true)
CheckIn(ctx context.Context, userID1, userID2 string) (inUser1Blacks bool, inUser2Blacks bool, err error)
2 years ago
}
type BlackController struct {
database BlackDatabaseInterface
2 years ago
}
func NewBlackController(db *gorm.DB) *BlackController {
return &BlackController{database: NewBlackDatabase(db)}
2 years ago
}
// Create 增加黑名单
func (b *BlackController) Create(ctx context.Context, blacks []*relation.Black) (err error) {
return b.database.Create(ctx, blacks)
}
// Delete 删除黑名单
func (b *BlackController) Delete(ctx context.Context, blacks []*relation.Black) (err error) {
return b.database.Delete(ctx, blacks)
}
// FindOwnerBlacks 获取黑名单列表
func (b *BlackController) FindOwnerBlacks(ctx context.Context, ownerUserID string, pageNumber, showNumber int32) (blackList []*relation.Black, total int64, err error) {
return b.database.FindOwnerBlacks(ctx, ownerUserID, pageNumber, showNumber)
}
// CheckIn 检查user2是否在user1的黑名单列表中(inUser1Blacks==true) 检查user1是否在user2的黑名单列表中(inUser2Blacks==true)
func (b *BlackController) CheckIn(ctx context.Context, userID1, userID2 string) (inUser1Blacks bool, inUser2Blacks bool, err error) {
return b.database.CheckIn(ctx, userID1, userID2)
2 years ago
}
type BlackDatabaseInterface interface {
// Create 增加黑名单
Create(ctx context.Context, blacks []*relation.Black) (err error)
// Delete 删除黑名单
Delete(ctx context.Context, blacks []*relation.Black) (err error)
// FindOwnerBlacks 获取黑名单列表
FindOwnerBlacks(ctx context.Context, ownerUserID string, pageNumber, showNumber int32) (blacks []*relation.Black, total int64, err error)
// CheckIn 检查user2是否在user1的黑名单列表中(inUser1Blacks==true) 检查user1是否在user2的黑名单列表中(inUser2Blacks==true)
CheckIn(ctx context.Context, userID1, userID2 string) (inUser1Blacks bool, inUser2Blacks bool, err error)
2 years ago
}
type BlackDatabase struct {
sqlDB *relation.Black
2 years ago
}
2 years ago
func NewBlackDatabase(db *gorm.DB) *BlackDatabase {
sqlDB := relation.NewBlack(db)
database := &BlackDatabase{
sqlDB: sqlDB,
2 years ago
}
return database
}
// Create 增加黑名单
func (b *BlackDatabase) Create(ctx context.Context, blacks []*relation.Black) (err error) {
return b.sqlDB.Create(ctx, blacks)
}
// Delete 删除黑名单
func (b *BlackDatabase) Delete(ctx context.Context, blacks []*relation.Black) (err error) {
return b.sqlDB.Delete(ctx, blacks)
}
// FindOwnerBlacks 获取黑名单列表
func (b *BlackDatabase) FindOwnerBlacks(ctx context.Context, ownerUserID string, pageNumber, showNumber int32) (blacks []*relation.Black, total int64, err error) {
return b.sqlDB.FindOwnerBlacks(ctx, ownerUserID, pageNumber, showNumber)
}
// CheckIn 检查user2是否在user1的黑名单列表中(inUser1Blacks==true) 检查user1是否在user2的黑名单列表中(inUser2Blacks==true)
func (b *BlackDatabase) CheckIn(ctx context.Context, userID1, userID2 string) (inUser1Blacks bool, inUser2Blacks bool, err error) {
_, err = b.sqlDB.Take(ctx, userID1, userID2)
if err != nil {
if errors.Unwrap(err) != gorm.ErrRecordNotFound {
return
}
inUser1Blacks = false
} else {
inUser1Blacks = true
}
inUser2Blacks = true
_, err = b.sqlDB.Take(ctx, userID2, userID1)
if err != nil {
if errors.Unwrap(err) != gorm.ErrRecordNotFound {
return
}
inUser2Blacks = false
} else {
inUser2Blacks = true
}
return
2 years ago
}