feat: 类型帖互动、积分经济与发帖体验

补齐问答采纳/重开、投票匿名与有票禁编、悬赏过期退回、抽奖回帖开奖,并接入积分账本与发帖附件可见性。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-09-16 03:17:07 +08:00
parent 1ef3d8e299
commit b9ba8cb8c9
58 changed files with 6913 additions and 506 deletions

View File

@@ -39,10 +39,23 @@ func InitDB(dsn string) error {
&User{}, &Board{}, &Post{}, &Comment{}, &RefreshToken{}, &Like{}, &Notification{}, &Checkin{},
&Announcement{}, &SiteSetting{}, &Attachment{}, &UserBoard{}, &LoginLog{},
&ChatRoom{}, &ChatRoomMember{}, &ChatMessage{},
&PointLedger{}, &PostContentUnlock{}, &PostAttachment{}, &PostAttachmentUnlock{},
&PostPollVote{}, &PostLotteryEntry{},
); err != nil {
return fmt.Errorf("自动迁移失败: %w", err)
}
// 旧 post_type=normal → discussion
if err := db.Exec(`UPDATE posts SET post_type = ? WHERE post_type = ? OR post_type = '' OR post_type IS NULL`,
PostTypeDiscussion, PostTypeNormal).Error; err != nil {
return fmt.Errorf("帖子类型归一失败: %w", err)
}
// 一次性:用签到累计回填 User.Points(仅余额仍为 0 且有签到积分的用户)
if err := backfillPointsFromCheckin(db); err != nil {
return fmt.Errorf("积分余额回填失败: %w", err)
}
// RBAC:把初始管理员(id 最小的 admin,通常即首个注册账号)升级为站长;
// 已存在 owner 时不动数据,保证幂等
if err := ensureOwnerRole(db); err != nil {
@@ -213,6 +226,46 @@ func ensureOwnerRole(db *gorm.DB) error {
return nil
}
// backfillPointsFromCheckin 把历史签到积分写入 User.Points(幂等:仅 points=0 且有签到)
func backfillPointsFromCheckin(db *gorm.DB) error {
type row struct {
UserID uint
Total int
}
var rows []row
if err := db.Raw(`
SELECT c.user_id, COALESCE(SUM(c.points), 0)::int AS total
FROM checkins c
INNER JOIN users u ON u.id = c.user_id AND u.deleted_at IS NULL AND u.points = 0
GROUP BY c.user_id
HAVING COALESCE(SUM(c.points), 0) > 0
`).Scan(&rows).Error; err != nil {
return err
}
for _, r := range rows {
err := db.Transaction(func(tx *gorm.DB) error {
if err := tx.Model(&User{}).Where("id = ? AND points = 0", r.UserID).
Update("points", r.Total).Error; err != nil {
return err
}
return tx.Create(&PointLedger{
UserID: r.UserID,
Delta: r.Total,
Balance: r.Total,
Reason: PointReasonMigrateCheckin,
Note: "历史签到积分回填",
}).Error
})
if err != nil {
return err
}
}
if len(rows) > 0 {
log.Printf("[model] 已回填 %d 名用户的签到积分余额", len(rows))
}
return nil
}
// seedDefaultBoards 写入默认板块
func seedDefaultBoards(db *gorm.DB) {
defaults := []Board{

View File

@@ -54,12 +54,64 @@ const (
ContentStatusRejected = "rejected"
)
// 帖子类型
// 帖子类型(discussion 为默认;兼容旧值 normal → discussion)
const (
PostTypeNormal = "normal"
PostTypeQuestion = "question"
PostTypeDiscussion = "discussion" // 讨论(默认)
PostTypeQuestion = "question" // 问答
PostTypePoll = "poll" // 投票
PostTypeBounty = "bounty" // 悬赏
PostTypeLottery = "lottery" // 抽奖
PostTypeNormal = "normal" // 旧值,读取时归一为 discussion
)
// ValidPostType 发帖类型白名单
func ValidPostType(t string) bool {
switch t {
case PostTypeDiscussion, PostTypeQuestion, PostTypePoll, PostTypeBounty, PostTypeLottery:
return true
}
return false
}
// NormalizePostType 归一化类型(空/旧 normal → discussion)
func NormalizePostType(t string) string {
if t == "" || t == PostTypeNormal {
return PostTypeDiscussion
}
if ValidPostType(t) {
return t
}
return PostTypeDiscussion
}
// 正文可见性
const (
ContentAccessPublic = "public" // 公开
ContentAccessLogin = "login" // 登录可见
ContentAccessReply = "reply" // 回复可见
ContentAccessPoints = "points" // 积分购买可见
)
// ValidContentAccess 可见性白名单
func ValidContentAccess(a string) bool {
switch a {
case ContentAccessPublic, ContentAccessLogin, ContentAccessReply, ContentAccessPoints:
return true
}
return false
}
// NormalizeContentAccess 归一化可见性
func NormalizeContentAccess(a string) string {
if a == "" {
return ContentAccessPublic
}
if ValidContentAccess(a) {
return a
}
return ContentAccessPublic
}
// User 用户表
type User struct {
ID uint `gorm:"primaryKey" json:"id"`
@@ -71,6 +123,7 @@ type User struct {
Signature string `gorm:"size:255;default:''" json:"signature"` // 个性签名
Role Role `gorm:"size:16;default:user" json:"role"`
Banned bool `gorm:"default:false" json:"banned"`
Points int `gorm:"not null;default:0" json:"points"` // 可用积分余额
TokenVersion int `gorm:"default:0" json:"-"` // token 版本号,改密码/封禁时递增使旧 JWT 失效
LastSeenAt *time.Time `gorm:"index" json:"-"` // 最近活跃时间(在线统计,限频更新)
CreatedAt time.Time `json:"created_at"`
@@ -111,28 +164,105 @@ type Board struct {
// Post 帖子
type Post struct {
ID uint `gorm:"primaryKey" json:"id"`
BoardID uint `gorm:"index;not null" json:"board_id"`
UserID uint `gorm:"index;not null" json:"user_id"`
Title string `gorm:"size:256;not null" json:"title"`
Content string `gorm:"type:text;not null" json:"content"`
Tags string `gorm:"size:256" json:"tags"`
PostType string `gorm:"size:16;default:normal;index" json:"post_type"`
Pinned int `gorm:"default:0" json:"pinned"`
Recommended bool `gorm:"default:false;index" json:"recommended"`
Status string `gorm:"size:16;default:published;index" json:"status"`
LikeCount int `gorm:"default:0" json:"like_count"`
ViewCount int `gorm:"default:0" json:"view_count"`
CommentCount int `gorm:"default:0" json:"comment_count"`
Liked bool `gorm:"-" json:"liked"` // 当前用户是否已点赞(展示字段,不入库)
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
ID uint `gorm:"primaryKey" json:"id"`
BoardID uint `gorm:"index;not null" json:"board_id"`
UserID uint `gorm:"index;not null" json:"user_id"`
Title string `gorm:"size:256;not null" json:"title"`
Content string `gorm:"type:text;not null" json:"content"`
Tags string `gorm:"size:256" json:"tags"`
PostType string `gorm:"size:16;default:discussion;index" json:"post_type"`
ContentAccess string `gorm:"size:16;default:public;index" json:"content_access"` // public|login|reply|points
AccessPoints int `gorm:"not null;default:0" json:"access_points"` // points 可见时所需积分
TypeMeta string `gorm:"type:text;default:''" json:"type_meta"` // 类型扩展 JSON(投票/悬赏/抽奖壳)
Pinned int `gorm:"default:0" json:"pinned"`
Recommended bool `gorm:"default:false;index" json:"recommended"`
Status string `gorm:"size:16;default:published;index" json:"status"`
LikeCount int `gorm:"default:0" json:"like_count"`
ViewCount int `gorm:"default:0" json:"view_count"`
CommentCount int `gorm:"default:0" json:"comment_count"`
Liked bool `gorm:"-" json:"liked"` // 当前用户是否已点赞(展示字段,不入库)
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
Board Board `gorm:"foreignKey:BoardID" json:"board,omitempty"`
User User `gorm:"foreignKey:UserID" json:"user,omitempty"`
}
// PointLedger 积分流水(余额以 User.Points 为准,本表可审计)
type PointLedger struct {
ID uint `gorm:"primaryKey" json:"id"`
UserID uint `gorm:"index;not null" json:"user_id"`
Delta int `gorm:"not null" json:"delta"` // 正入负出
Balance int `gorm:"not null" json:"balance"` // 变动后余额
Reason string `gorm:"size:32;not null;index" json:"reason"`
RefType string `gorm:"size:32;not null;default:''" json:"ref_type"`
RefID uint `gorm:"not null;default:0" json:"ref_id"`
Note string `gorm:"size:256;not null;default:''" json:"note"`
CreatedAt time.Time `gorm:"index" json:"created_at"`
}
// 积分流水原因
const (
PointReasonCheckin = "checkin"
PointReasonUnlockPost = "unlock_post"
PointReasonDownloadFile = "download_file"
PointReasonMigrateCheckin = "migrate_checkin"
PointReasonBountyEscrow = "bounty_escrow"
PointReasonBountyRefund = "bounty_refund"
PointReasonBountyAward = "bounty_award"
)
// PostPollVote 投票记录(多选时同一用户多行)
type PostPollVote struct {
ID uint `gorm:"primaryKey" json:"id"`
PostID uint `gorm:"uniqueIndex:idx_poll_vote;not null" json:"post_id"`
UserID uint `gorm:"uniqueIndex:idx_poll_vote;not null" json:"user_id"`
OptionIndex int `gorm:"uniqueIndex:idx_poll_vote;not null" json:"option_index"`
CreatedAt time.Time `json:"created_at"`
}
// PostLotteryEntry 抽奖报名
type PostLotteryEntry struct {
ID uint `gorm:"primaryKey" json:"id"`
PostID uint `gorm:"uniqueIndex:idx_lottery_entry;not null" json:"post_id"`
UserID uint `gorm:"uniqueIndex:idx_lottery_entry;not null" json:"user_id"`
CreatedAt time.Time `json:"created_at"`
}
// PostContentUnlock 积分购买正文解锁记录
type PostContentUnlock struct {
ID uint `gorm:"primaryKey" json:"id"`
PostID uint `gorm:"uniqueIndex:idx_post_unlock_user;not null" json:"post_id"`
UserID uint `gorm:"uniqueIndex:idx_post_unlock_user;not null" json:"user_id"`
Points int `gorm:"not null;default:0" json:"points"`
CreatedAt time.Time `json:"created_at"`
}
// PostAttachment 帖子文件附件(不走公开静态目录,经 API 鉴权下载)
type PostAttachment struct {
ID uint `gorm:"primaryKey" json:"id"`
PostID uint `gorm:"index;not null;default:0" json:"post_id"` // 0=草稿未绑定
UserID uint `gorm:"index;not null" json:"user_id"`
Name string `gorm:"size:256;not null" json:"name"` // 原始文件名
StoredName string `gorm:"size:64;not null" json:"-"` // 磁盘文件名
MIME string `gorm:"size:128;not null;default:application/octet-stream" json:"mime"`
Size int `gorm:"not null;default:0" json:"size"`
PricePoints int `gorm:"not null;default:0" json:"price_points"` // 0=免费
DownloadCount int `gorm:"not null;default:0" json:"download_count"`
CreatedAt time.Time `json:"created_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
}
// PostAttachmentUnlock 积分附件下载解锁(按附件计费一次)
type PostAttachmentUnlock struct {
ID uint `gorm:"primaryKey" json:"id"`
AttachmentID uint `gorm:"uniqueIndex:idx_att_unlock_user;not null" json:"attachment_id"`
UserID uint `gorm:"uniqueIndex:idx_att_unlock_user;not null" json:"user_id"`
Points int `gorm:"not null;default:0" json:"points"`
CreatedAt time.Time `json:"created_at"`
}
// Comment 评论(主评论 = 楼层,ParentID 为空;子评论挂 root_id 对应楼层下)
type Comment struct {
ID uint `gorm:"primaryKey" json:"id"`