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/relation/black.go

115 lines
4.0 KiB

2 years ago
package relation
import (
2 years ago
"Open_IM/pkg/common/trace_log"
3 years ago
"Open_IM/pkg/utils"
2 years ago
"context"
2 years ago
"gorm.io/gorm"
"time"
)
2 years ago
type Black struct {
OwnerUserID string `gorm:"column:owner_user_id;primary_key;size:64"`
BlockUserID string `gorm:"column:block_user_id;primary_key;size:64"`
CreateTime time.Time `gorm:"column:create_time"`
AddSource int32 `gorm:"column:add_source"`
OperatorUserID string `gorm:"column:operator_user_id;size:64"`
Ex string `gorm:"column:ex;size:1024"`
2 years ago
DB *gorm.DB `gorm:"-"`
2 years ago
}
func NewBlack(db *gorm.DB) *Black {
2 years ago
var black Black
black.DB = initModel(db, &black)
return &black
2 years ago
}
2 years ago
func (b *Black) Create(ctx context.Context, blacks []*Black) (err error) {
2 years ago
defer func() {
trace_log.SetCtxDebug(ctx, utils.GetFuncName(1), err, "blacks", blacks)
}()
2 years ago
return utils.Wrap(b.DB.Create(&blacks).Error, "")
2 years ago
}
2 years ago
func (b *Black) Delete(ctx context.Context, blacks []*Black) (err error) {
2 years ago
defer func() {
trace_log.SetCtxDebug(ctx, utils.GetFuncName(1), err, "blacks", blacks)
}()
return utils.Wrap(GroupMemberDB.Delete(blacks).Error, "")
}
2 years ago
func (b *Black) UpdateByMap(ctx context.Context, ownerUserID, blockUserID string, args map[string]interface{}) (err error) {
2 years ago
defer func() {
trace_log.SetCtxDebug(ctx, utils.GetFuncName(1), err, "ownerUserID", ownerUserID, "blockUserID", blockUserID, "args", args)
}()
2 years ago
return utils.Wrap(b.DB.Where("block_user_id = ? and block_user_id = ?", ownerUserID, blockUserID).Updates(args).Error, "")
2 years ago
}
2 years ago
func (b *Black) Update(ctx context.Context, blacks []*Black) (err error) {
2 years ago
defer func() {
trace_log.SetCtxDebug(ctx, utils.GetFuncName(1), err, "blacks", blacks)
}()
2 years ago
return utils.Wrap(b.DB.Updates(&blacks).Error, "")
2 years ago
}
2 years ago
func (b *Black) Find(ctx context.Context, blacks []*Black) (blackList []*Black, err error) {
2 years ago
defer func() {
trace_log.SetCtxDebug(ctx, utils.GetFuncName(1), err, "blacks", blacks, "blackList", blackList)
}()
var where [][]interface{}
for _, black := range blacks {
where = append(where, []interface{}{black.OwnerUserID, black.BlockUserID})
}
return blackList, utils.Wrap(GroupMemberDB.Where("(owner_user_id, block_user_id) in ?", where).Find(&blackList).Error, "")
}
2 years ago
func (b *Black) Take(ctx context.Context, ownerUserID, blockUserID string) (black *Black, err error) {
2 years ago
black = &Black{}
defer func() {
2 years ago
trace_log.SetCtxDebug(ctx, utils.GetFuncName(1), err, "ownerUserID", ownerUserID, "blockUserID", blockUserID, "black", *black)
2 years ago
}()
2 years ago
return black, utils.Wrap(b.DB.Where("owner_user_id = ? and block_user_id = ?", ownerUserID, blockUserID).Take(black).Error, "")
2 years ago
}
2 years ago
func (b *Black) FindByOwnerUserID(ctx context.Context, ownerUserID string) (blackList []*Black, err error) {
2 years ago
defer func() {
trace_log.SetCtxDebug(ctx, utils.GetFuncName(1), err, "ownerUserID", ownerUserID, "blackList", blackList)
}()
return blackList, utils.Wrap(GroupMemberDB.Where("owner_user_id = ?", ownerUserID).Find(&blackList).Error, "")
}
2 years ago
func InsertInToUserBlackList(ctx context.Context, black Black) (err error) {
2 years ago
defer trace_log.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "black", black)
3 years ago
black.CreateTime = time.Now()
2 years ago
err = BlackDB.Create(black).Error
2 years ago
return err
}
3 years ago
func CheckBlack(ownerUserID, blockUserID string) error {
2 years ago
var black Black
return BlackDB.Where("owner_user_id=? and block_user_id=?", ownerUserID, blockUserID).Find(&black).Error
}
3 years ago
func RemoveBlackList(ownerUserID, blockUserID string) error {
2 years ago
err := BlackDB.Where("owner_user_id=? and block_user_id=?", ownerUserID, blockUserID).Delete(Black{}).Error
3 years ago
return utils.Wrap(err, "RemoveBlackList failed")
}
2 years ago
func GetBlackListByUserID(ownerUserID string) ([]Black, error) {
var blackListUsersInfo []Black
err := BlackDB.Where("owner_user_id=?", ownerUserID).Find(&blackListUsersInfo).Error
if err != nil {
return nil, err
}
return blackListUsersInfo, nil
}
func GetBlackIDListByUserID(ownerUserID string) ([]string, error) {
var blackIDList []string
2 years ago
err := b.db.Where("owner_user_id=?", ownerUserID).Pluck("block_user_id", &blackIDList).Error
if err != nil {
return nil, err
}
return blackIDList, nil
}