spark-server/internal/module/friend/model/user_follow.go
DESKTOP-L8OUL9F\gfl0034 ce9c7a13aa feat: 互相关注后自动成为好友
一方关注只记关注。双方互关时写入已有双向好友边;取消关注若不再互关只删好友边,不删消息。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-18 21:51:18 +08:00

99 lines
2.6 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package model
import (
"context"
"time"
"spark-server/internal/pkg/errs"
)
type followKey struct {
UserID int64
TargetID int64
}
// UserFollow maps table t_user_follows (one directed follow per row).
type UserFollow struct {
ID int64 `gorm:"column:id;primaryKey;autoIncrement;comment:行 id,表内自增"`
UserID int64 `gorm:"column:user_id;comment:关注人 user_id"`
TargetID int64 `gorm:"column:target_id;comment:被关注人 user_id"`
CreatedAt time.Time `gorm:"column:created_at;comment:关注时间"`
}
func (UserFollow) TableName() string { return "t_user_follows" }
func (m *Repo) PutFollow(ctx context.Context, userID, targetID int64) error {
if userID <= 0 || targetID <= 0 || userID == targetID {
return errs.ErrInvalidArgument
}
db, err := m.session(ctx)
if err != nil {
return err
}
row := UserFollow{UserID: userID, TargetID: targetID, CreatedAt: time.Now()}
err = db.Where("user_id = ? AND target_id = ?", userID, targetID).
Attrs(row).FirstOrCreate(&row).Error
return mapDBErr(err)
}
func (m *Repo) DeleteFollow(ctx context.Context, userID, targetID int64) error {
if userID <= 0 || targetID <= 0 {
return errs.ErrInvalidArgument
}
db, err := m.session(ctx)
if err != nil {
return err
}
err = db.Where("user_id = ? AND target_id = ?", userID, targetID).Delete(&UserFollow{}).Error
return mapDBErr(err)
}
func (m *Repo) HasFollow(ctx context.Context, userID, targetID int64) (bool, error) {
if userID <= 0 || targetID <= 0 {
return false, nil
}
db, err := m.session(ctx)
if err != nil {
return false, err
}
var n int64
err = db.Model(&UserFollow{}).Where("user_id = ? AND target_id = ?", userID, targetID).Count(&n).Error
if err != nil {
return false, mapDBErr(err)
}
return n > 0, nil
}
func (m *MemoryRepo) PutFollow(_ context.Context, userID, targetID int64) error {
if userID <= 0 || targetID <= 0 || userID == targetID {
return errs.ErrInvalidArgument
}
m.mu.Lock()
defer m.mu.Unlock()
if m.follows == nil {
m.follows = make(map[followKey]struct{})
}
m.follows[followKey{UserID: userID, TargetID: targetID}] = struct{}{}
return nil
}
func (m *MemoryRepo) DeleteFollow(_ context.Context, userID, targetID int64) error {
if userID <= 0 || targetID <= 0 {
return errs.ErrInvalidArgument
}
m.mu.Lock()
defer m.mu.Unlock()
delete(m.follows, followKey{UserID: userID, TargetID: targetID})
return nil
}
func (m *MemoryRepo) HasFollow(_ context.Context, userID, targetID int64) (bool, error) {
if userID <= 0 || targetID <= 0 {
return false, nil
}
m.mu.Lock()
defer m.mu.Unlock()
_, ok := m.follows[followKey{UserID: userID, TargetID: targetID}]
return ok, nil
}