Add HTTP /v1 MVC, TCP send path, groups with server-side member-avatar collage, object-storage upload, and friends/favorites around the architecture-v3 layout. Co-authored-by: Cursor <cursoragent@cursor.com>
78 lines
2.2 KiB
Go
78 lines
2.2 KiB
Go
package model
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"spark-server/internal/module/friend/entity"
|
|
"spark-server/internal/pkg/errs"
|
|
)
|
|
|
|
// UserBlock maps table user_blocks.
|
|
type UserBlock struct {
|
|
UserID int64 `gorm:"column:user_id;primaryKey;comment:拉黑人 user_id"`
|
|
BlockedID int64 `gorm:"column:blocked_id;primaryKey;comment:被拉黑人 user_id"`
|
|
CreatedAt time.Time `gorm:"column:created_at;comment:拉黑时间"`
|
|
}
|
|
|
|
func (UserBlock) TableName() string { return "user_blocks" }
|
|
|
|
func (m *Repo) IsBlocked(ctx context.Context, userID, targetID int64) (bool, error) {
|
|
db, err := m.session(ctx)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
var n int64
|
|
if err := db.Model(&UserBlock{}).Where("user_id = ? AND blocked_id = ?", userID, targetID).Count(&n).Error; err != nil {
|
|
return false, mapDBErr(err)
|
|
}
|
|
return n > 0, nil
|
|
}
|
|
|
|
func (m *Repo) BlockedEither(ctx context.Context, a, b int64) (bool, error) {
|
|
me, err := m.IsBlocked(ctx, a, b)
|
|
if err != nil || me {
|
|
return me, err
|
|
}
|
|
return m.IsBlocked(ctx, b, a)
|
|
}
|
|
|
|
func (m *Repo) CreateBlock(ctx context.Context, b *entity.Block) error {
|
|
if b == nil || b.UserID <= 0 || b.BlockedID <= 0 {
|
|
return errs.ErrInvalidArgument
|
|
}
|
|
db, err := m.session(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
now := b.CreatedAt
|
|
if now.IsZero() {
|
|
now = time.Now()
|
|
}
|
|
return db.Transaction(func(tx *gorm.DB) error {
|
|
row := UserBlock{UserID: b.UserID, BlockedID: b.BlockedID, CreatedAt: now}
|
|
if err := tx.Where("user_id = ? AND blocked_id = ?", b.UserID, b.BlockedID).
|
|
Attrs(row).FirstOrCreate(&row).Error; err != nil {
|
|
return mapDBErr(err)
|
|
}
|
|
small, big := entity.OrderUsers(b.UserID, b.BlockedID)
|
|
if err := tx.Where("user_small = ? AND user_big = ?", small, big).Delete(&FriendRelation{}).Error; err != nil {
|
|
return mapDBErr(err)
|
|
}
|
|
if err := deleteRemarksBetween(tx, b.UserID, b.BlockedID); err != nil {
|
|
return err
|
|
}
|
|
return rejectPairRequests(tx, b.UserID, b.BlockedID, now)
|
|
})
|
|
}
|
|
|
|
func (m *Repo) DeleteBlock(ctx context.Context, userID, blockedID int64) error {
|
|
db, err := m.session(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return mapDBErr(db.Where("user_id = ? AND blocked_id = ?", userID, blockedID).Delete(&UserBlock{}).Error)
|
|
}
|