feat(posts): add post reference/internal linking functionality
All checks were successful
Build Backend / build (push) Successful in 2m19s
Build Backend / build-docker (push) Successful in 1m18s

Add support for referencing other posts within messages through a new post_ref segment type. Includes new PostReference model to track relationships, PostRefService for processing references, and new endpoints for post suggestions, related posts, and reference click tracking.
This commit is contained in:
lafay
2026-04-26 00:37:20 +08:00
parent 898c0e6d9c
commit 23d7f1151e
11 changed files with 592 additions and 5 deletions

View File

@@ -176,6 +176,9 @@ func autoMigrate(db *gorm.DB) error {
// 用户资料审核相关
&UserProfileAudit{},
// 帖子内链引用关系
&PostReference{},
)
if err != nil {
return err

View File

@@ -0,0 +1,40 @@
package model
import (
"time"
"github.com/google/uuid"
"gorm.io/gorm"
)
type PostReferenceStatus string
const (
PostRefActive PostReferenceStatus = "active"
PostRefDead PostReferenceStatus = "dead"
PostRefHidden PostReferenceStatus = "hidden"
)
type PostReference struct {
ID string `json:"id" gorm:"type:varchar(36);primaryKey"`
SourcePostID string `json:"source_post_id" gorm:"type:varchar(36);not null;uniqueIndex:uq_source_target;index:idx_source_post"`
TargetPostID string `json:"target_post_id" gorm:"type:varchar(36);not null;uniqueIndex:uq_source_target;index:idx_target_post;index:idx_target_status"`
Status PostReferenceStatus `json:"status" gorm:"type:varchar(20);default:active;index:idx_target_status"`
ClickCount int `json:"click_count" gorm:"default:0"`
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime"`
UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime:false"`
SourcePost *Post `json:"source_post,omitempty" gorm:"foreignKey:SourcePostID"`
TargetPost *Post `json:"target_post,omitempty" gorm:"foreignKey:TargetPostID"`
}
func (pr *PostReference) BeforeCreate(tx *gorm.DB) error {
if pr.ID == "" {
pr.ID = uuid.New().String()
}
return nil
}
func (PostReference) TableName() string {
return "post_references"
}