feat: 书库导入导出/图片变体/书籍搜索/小组件运行时等
新增: - 书库导入导出(library_import/library_export)及测试 - 图片变体生成(image_variants)与响应式图片(responsiveImage) - 书籍搜索(bookSearch)+ BookSearch/LibrarySearchGrid 组件 - 小组件运行时(widgetRuntime)与静态检查(widgetLint) - 上传缓存中间件(upload_cache)与字体 CSS 提取脚本 其它: - 后端 handlers/services 全量调整 - 前端页面、组件、库函数与配置更新
This commit is contained in:
@@ -44,7 +44,7 @@ func IsNecroReply(lastReplyAt time.Time, afterHours int, now time.Time) bool {
|
||||
|
||||
// CommentService 评论服务
|
||||
type CommentService struct {
|
||||
db *gorm.DB
|
||||
db *gorm.DB
|
||||
setting *SettingService
|
||||
}
|
||||
|
||||
@@ -297,6 +297,80 @@ func (s *CommentService) FloorNumber(postID, commentID uint) int {
|
||||
return FloorNumber(s.db, postID, commentID)
|
||||
}
|
||||
|
||||
// CommentAnchor 评论在所属帖子内的相对锚点:
|
||||
// Floor=楼层号(含软删/待审占位,口径同 FloorNumber);IsRoot=true 为主楼,
|
||||
// false 为楼中楼(锚点需附带评论 ID 才能精确定位具体回复)。
|
||||
type CommentAnchor struct {
|
||||
Floor uint `json:"floor"`
|
||||
IsRoot bool `json:"is_root"`
|
||||
}
|
||||
|
||||
// CommentAnchors 批量查询评论锚点(两条 SQL,避免逐行 N+1);
|
||||
// 评论或其主楼已不存在(如整串被彻底删除)时不出现在结果中。
|
||||
func CommentAnchors(db *gorm.DB, ids []uint) map[uint]CommentAnchor {
|
||||
out := make(map[uint]CommentAnchor, len(ids))
|
||||
if db == nil || len(ids) == 0 {
|
||||
return out
|
||||
}
|
||||
// Unscoped:软删/待审评论仍占楼层,与 FloorNumber 同口径
|
||||
var targets []model.Comment
|
||||
if err := db.Unscoped().
|
||||
Select("id", "post_id", "parent_id", "root_id", "created_at").
|
||||
Where("id IN ?", ids).Find(&targets).Error; err != nil {
|
||||
return out
|
||||
}
|
||||
rootIDSet := make(map[uint]struct{}, len(targets))
|
||||
for _, c := range targets {
|
||||
rid := c.ID
|
||||
if c.ParentID != nil && c.RootID != nil {
|
||||
rid = *c.RootID
|
||||
}
|
||||
rootIDSet[rid] = struct{}{}
|
||||
}
|
||||
rootIDs := make([]uint, 0, len(rootIDSet))
|
||||
for rid := range rootIDSet {
|
||||
rootIDs = append(rootIDs, rid)
|
||||
}
|
||||
// 每个主楼之前(按 created_at/id 升序)的主楼数即楼层号 - 1
|
||||
type aheadRow struct {
|
||||
RootID uint
|
||||
Ahead int64
|
||||
}
|
||||
var aheadRows []aheadRow
|
||||
if err := db.Unscoped().Table("comments AS f").
|
||||
Select(`f.id AS root_id,
|
||||
(SELECT COUNT(*) FROM comments c
|
||||
WHERE c.post_id = f.post_id AND c.parent_id IS NULL
|
||||
AND (c.created_at < f.created_at
|
||||
OR (c.created_at = f.created_at AND c.id < f.id))) AS ahead`).
|
||||
Where("f.id IN ? AND f.parent_id IS NULL", rootIDs).
|
||||
Scan(&aheadRows).Error; err != nil {
|
||||
return out
|
||||
}
|
||||
floorByRoot := make(map[uint]uint, len(aheadRows))
|
||||
for _, r := range aheadRows {
|
||||
floorByRoot[r.RootID] = uint(r.Ahead) + 1
|
||||
}
|
||||
for _, c := range targets {
|
||||
isRoot := c.ParentID == nil
|
||||
rid := c.ID
|
||||
if !isRoot && c.RootID != nil {
|
||||
rid = *c.RootID
|
||||
}
|
||||
floor := floorByRoot[rid]
|
||||
if floor == 0 {
|
||||
continue
|
||||
}
|
||||
out[c.ID] = CommentAnchor{Floor: floor, IsRoot: isRoot}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// AnchorsByCommentIDs 见包级 CommentAnchors
|
||||
func (s *CommentService) AnchorsByCommentIDs(ids []uint) map[uint]CommentAnchor {
|
||||
return CommentAnchors(s.db, ids)
|
||||
}
|
||||
|
||||
// applyCommentListVisibility 评论流可见性:published,或 pending 且(作者 / 该板可审);
|
||||
// 软删行:已发布的对所有人占位;待审软删仅作者/可审者可见占位。
|
||||
func applyCommentListVisibility(db *gorm.DB, boardID, viewerID uint, actor *Actor) *gorm.DB {
|
||||
@@ -442,6 +516,8 @@ type UserCommentItem struct {
|
||||
PostID uint `json:"post_id"`
|
||||
PostTitle string `json:"post_title"`
|
||||
Content string `json:"content"`
|
||||
Floor uint `json:"floor"` // 所属楼层相对序号(主楼为自身楼层)
|
||||
IsRoot bool `json:"is_root"` // 主楼/楼中楼,供前端拼 #comment-{floor}[-r{id}]
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
@@ -470,6 +546,17 @@ func (s *CommentService) ListByUser(userID uint, page, size int) ([]UserCommentI
|
||||
if items == nil {
|
||||
items = []UserCommentItem{}
|
||||
}
|
||||
ids := make([]uint, 0, len(items))
|
||||
for _, it := range items {
|
||||
ids = append(ids, it.ID)
|
||||
}
|
||||
anchors := s.AnchorsByCommentIDs(ids)
|
||||
for i := range items {
|
||||
if a, ok := anchors[items[i].ID]; ok {
|
||||
items[i].Floor = a.Floor
|
||||
items[i].IsRoot = a.IsRoot
|
||||
}
|
||||
}
|
||||
return items, total, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user