Files
ONE/backend/internal/model/model.go
T
Sakurasan 1b3eb870da 文件删除前查引用:被文章/项目/站主头像用着就先挡住
- store.FileReferences(key) 反查谁在用这个文件:文章(封面 + content_md +
  content_html)、项目封面、settings.owner_avatar_key。匹配的键是 files.key 本身
  而不是完整 URL——本地 /uploads/{key}、R2 {publicBase}/{key}、缩略图
  /uploads/thumb/{key} 三种形态都以 key 结尾,换存储端后正文里的老链接照样查得到。
  草稿也算:现在没发布,删了将来发出来就是裂的。
- 按需 LIKE 现扫,不维护计数表:写入口有编辑器、外链转存、短文、项目、头像好几处,
  计数一旦漂移就再也信不过;删文件是低频操作,扫全表几十毫秒换一个永远正确的答案。
- 新端点 GET /api/admin/files/{id}/refs;DELETE 默认对在用文件返回 409(消息带引用数
  和前三个位置名),明确带 force=1 才真删。守卫放在动 blob 之前——存储端一删就没法回头。
- 后台删除弹层先把引用清单摊出来(文章《标题》(草稿)、站主头像),仍然要删才带 force。
  引用查询失败就按无引用走:真被引用时后端会 409,不会静默删掉在用文件。

顺手把另一个 agent 工具的本地草稿目录(.zcode/、.zcodeignore)加进 .gitignore。
2026-09-30 02:03:14 +08:00

314 lines
12 KiB
Go
Raw 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
// Post kinds. "long" is a normal article; "short" is a Twitter-like note
// with no title shown in the timeline.
const (
KindLong = "long"
KindShort = "short"
)
const (
StatusDraft = "draft"
StatusPublished = "published"
)
type Post struct {
ID int64 `json:"id"`
Kind string `json:"kind"`
Title string `json:"title"`
Slug string `json:"slug"`
Summary string `json:"summary"`
CoverURL string `json:"cover_url"`
ContentMd string `json:"content_md,omitempty"`
ContentHTML string `json:"content_html"`
Status string `json:"status"`
PublishedAt string `json:"published_at"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
ReadingMinutes int `json:"reading_minutes"`
// ContentLen 是正文字符数:列表接口不返回全文,但后台列表要显示字数。
ContentLen int64 `json:"content_len"`
Tags []string `json:"tags"`
// Images 是短文配图(快发盒上传,≤4 张,Twitter 式网格展示),
// 以 JSON 数组存在 posts.images 列里,正文 content_md 不含它们。
Images []string `json:"images"`
// LinkCard 是正文里第一个外链的预览卡片(发布时抓一次,存 posts.link_card)。
// nil = 没有链接或抓取失败,前台不渲染卡片。
LinkCard *LinkCard `json:"link_card,omitempty"`
}
// LinkCard 是链接预览卡片:由 internal/linkmeta 抓取,以 JSON 存在 posts 表里。
type LinkCard struct {
URL string `json:"url"`
Title string `json:"title,omitempty"`
Desc string `json:"desc,omitempty"`
Site string `json:"site,omitempty"`
Image string `json:"image,omitempty"`
}
// Empty 报告卡片有没有可展示的字段(只有 URL 不算)。
func (c *LinkCard) Empty() bool {
return c == nil || (c.Title == "" && c.Desc == "" && c.Image == "")
}
type PostInput struct {
Kind string `json:"kind"`
Title string `json:"title"`
Slug string `json:"slug"`
Summary string `json:"summary"`
CoverURL string `json:"cover_url"`
ContentMd string `json:"content_md"`
Status string `json:"status"`
PublishedAt string `json:"published_at"`
Tags []string `json:"tags"`
ReadingMinutes *int `json:"reading_minutes"`
// Images 是短文配图 URL(快发盒上传),nil = 不修改、空数组 = 清空
Images []string `json:"images"`
// LinkCard 由服务端在保存时按正文内容重算(站主不必发这个字段)
LinkCard *LinkCard `json:"link_card,omitempty"`
}
type Tag struct {
ID int64 `json:"id"`
Name string `json:"name"`
Slug string `json:"slug"`
Color string `json:"color"`
Count int `json:"count"`
}
// Project is a showcase entry rendered on the public /projects page. It links
// out to an external homepage and (optionally) a source repository.
type Project struct {
ID int64 `json:"id"`
Title string `json:"title"`
Slug string `json:"slug"`
Summary string `json:"summary"`
CoverURL string `json:"cover_url"`
URL string `json:"url"`
RepoURL string `json:"repo_url"`
Status string `json:"status"`
Position int `json:"position"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
// ProjectInput carries the editable fields for a project. A blank Slug or
// Status is filled in by the store (slug from Title, status defaults to
// published) so the admin UI can omit them.
type ProjectInput struct {
Title string `json:"title"`
Slug string `json:"slug"`
Summary string `json:"summary"`
CoverURL string `json:"cover_url"`
URL string `json:"url"`
RepoURL string `json:"repo_url"`
Status string `json:"status"`
Position int `json:"position"`
}
type ArchiveMonth struct {
Month string `json:"month"`
Posts []Post `json:"posts"`
}
type ArchiveYear struct {
Year string `json:"year"`
Months []ArchiveMonth `json:"months"`
Count int `json:"count"`
}
type Page struct {
Items []Post `json:"items"`
Total int `json:"total"`
Page int `json:"page"`
Size int `json:"size"`
}
// File 是一条上传文件的索引行。内容本体在对象存储里(store 字段记来源:
// r2 | local),key 是存储端的对象名,URL 由 API 层按「来源 + PublicBase」
// 在响应时解析——切存储端不破坏存量链接。
type File struct {
ID int64 `json:"id"`
Key string `json:"key"`
Name string `json:"name"`
Mime string `json:"mime"`
Size int64 `json:"size"`
SHA256 string `json:"sha256"`
Store string `json:"store"`
URL string `json:"url"`
CreatedAt string `json:"created_at"`
}
// FilePage 是文件管理的分页容器(Items 用 File,与文章的 Page 区分开)。
type FilePage struct {
Items []File `json:"items"`
Total int `json:"total"`
Page int `json:"page"`
Size int `json:"size"`
}
// FileRef 是「谁在用这个文件」的一处记录,供删除前的引用检查。
// Kind 取 post / project / avatar;avatar 没有可跳转的行,ID 为 0。
type FileRef struct {
Kind string `json:"kind"`
ID int64 `json:"id"`
Title string `json:"title"`
Slug string `json:"slug"`
Status string `json:"status"`
}
// CommentPage 是后台评论管理的分页容器(含文章标题与发表者)。
type CommentPage struct {
Items []Comment `json:"items"`
Total int `json:"total"`
Page int `json:"page"`
Size int `json:"size"`
}
// Dashboard is the snapshot rendered on /admin (homepage).
type Dashboard struct {
TotalPosts int `json:"total_posts"`
PublishedPosts int `json:"published_posts"`
DraftPosts int `json:"draft_posts"`
ShortPosts int `json:"short_posts"`
LongPosts int `json:"long_posts"`
TotalTags int `json:"total_tags"`
TotalWords int `json:"total_words"`
RecentPosts []Post `json:"recent_posts"`
RecentDrafts []Post `json:"recent_drafts"`
TopTags []Tag `json:"top_tags"`
PublishedByMonth []MonthBucket `json:"published_by_month"`
}
type MonthBucket struct {
Month string `json:"month"` // "YYYY-MM"
Count int `json:"count"`
}
// SocialLink 是站主在后台填写的社交 / 源码入口(label + url),
// 前台渲染成图标或文字链接。icon 不入库 —— 由前端按 url 域名推导。
type SocialLink struct {
Label string `json:"label"`
URL string `json:"url"`
}
type Settings struct {
SiteTitle string `json:"site_title"`
SiteDesc string `json:"site_desc"`
AuthorName string `json:"author_name"`
AuthorBio string `json:"author_bio"`
FooterNote string `json:"footer_note"`
ICPLicense string `json:"icp"`
PostsPerPage int `json:"posts_per_page"`
// SocialLinks 以 JSON 数组形式存在 settings KV 里(key: social_links),
// 解析失败/为空时前台拿到空数组,区块自动隐藏。
SocialLinks []SocialLink `json:"social_links"`
// 评论开关(comments_enabled):关着时前台整个评论区不渲染。
CommentsEnabled bool `json:"comments_enabled"`
// 审核开关(comments_review):开着时新评论先进「待审」,站主通过后才公开。
CommentsReview bool `json:"comments_review"`
// LightSkinID is the front-end skin used when the client (or system)
// prefers light. Valid values: paper / sage / rose.
// Dark side is fixed to ink for now — kept implicit so we can add
// dark variants later without breaking clients.
LightSkinID string `json:"light_skin_id"`
// ThemeID is kept for backward compatibility with older clients that
// only know about a single skin. settingsFromMap falls back to it
// when LightSkinID is empty.
ThemeID string `json:"theme_id,omitempty"`
// UIID selects which front-end UI the whole site renders. Valid values
// are "classic" (the original minimalist layout) and "vivid" (the
// livelier one). The admin UI is unaffected by this — it always uses the
// --admin-* tokens.
UIID string `json:"ui_id"`
// CustomCSS holds owner-authored stylesheets keyed by page section
// ("global", "home", "post", "archive", "tags", "projects", "about").
// Only injected for the vivid UI, and never on /admin. Always non-nil so
// the JSON response is {} rather than null.
CustomCSS map[string]string `json:"custom_css"`
// CustomJS holds owner-authored JavaScript (analytics snippets like
// Google Analytics / Plausible / Umami). Injected on every public page
// of either UI, never on /admin. Stored raw — it's the owner's own code,
// sanitizing it would only break the snippet.
CustomJS string `json:"custom_js"`
// AuthorAvatarKey 是站主头像在 files 表里的 key(不是 URL):换存储端不破坏
// 存量链接,和 images/link_card 一样属于「存标识、读时解析」。
// 故意不并进 UpdateSettings 的全量替换 —— 后台「站点设置」PUT 不该顺手清掉
// 账户页设置的头像,两者写入路径分开。
AuthorAvatarKey string `json:"author_avatar_key,omitempty"`
// AuthorAvatarURL 由 API 层按 AuthorAvatarKey + 存储端解析出来,
// 不落库;key 指向的文件已删除时为空串。
AuthorAvatarURL string `json:"author_avatar_url"`
}
// 账号角色。owner 全库唯一(站主),reader 是评论区登录进来的访客。
const (
RoleOwner = "owner"
RoleReader = "reader"
)
// Reader 是一条评论区身份:既包括站主(role=owner,provider=admin),
// 也包括通过 GitHub / Google / Telegram 登录进来的访客(role=reader)。
// Banned = 禁言中。
type Reader struct {
ID int64 `json:"id"`
Provider string `json:"provider"`
Handle string `json:"handle"`
Name string `json:"name"`
AvatarURL string `json:"avatar_url"`
URL string `json:"url"`
Banned bool `json:"banned"`
// Role 区分站主与访客:绑定到 owner 的第三方身份登录时会话升级为管理员。
Role string `json:"role"`
// CommentCount 是累计评论数(后台用户列表展示用)
CommentCount int64 `json:"comment_count"`
CreatedAt string `json:"created_at"`
}
// UserIdentity 是「这个账号绑定了哪个第三方身份」。
// ExternUID 用各平台的稳定主键(GitHub 数字 id / Google sub / Telegram id),
// 不用可改的用户名;Display 只是列表里给人看的标签。
type UserIdentity struct {
ID int64 `json:"id"`
UserID int64 `json:"user_id"`
Provider string `json:"provider"`
ExternUID string `json:"extern_uid"`
Display string `json:"display"`
CreatedAt string `json:"created_at"`
}
// Passkey 是一把已注册的 WebAuthn 凭据。PublicKey 以 base64 存
// (model 包保持零依赖,字节解码在 auth 层做);SignCount 用于检测
// 凭据被克隆(计数回退即异常),LastUsedAt 给管理页显示「上次使用」。
type Passkey struct {
ID int64 `json:"id"`
UserID int64 `json:"user_id"`
CredentialID string `json:"credential_id"`
PublicKey string `json:"public_key"`
SignCount uint32 `json:"sign_count"`
Name string `json:"name"`
CreatedAt string `json:"created_at"`
LastUsedAt string `json:"last_used_at"`
}
// Comment 是一条评论。回复扁平存储(parent_id/root_id),渲染时挂到 replies。
// User 是发表者快照;is_deleted = 软删(留壳显示「已删除」,保住楼层上下文)。
type Comment struct {
ID int64 `json:"id"`
PostID int64 `json:"post_id"`
ParentID int64 `json:"parent_id"`
RootID int64 `json:"root_id"`
User *Reader `json:"user"`
UserID int64 `json:"user_id"`
BodyMd string `json:"body_md"`
BodyHTML string `json:"body_html"`
Status string `json:"status"`
IsDeleted bool `json:"is_deleted"`
CreatedAt string `json:"created_at"`
EditedAt string `json:"edited_at"`
Replies []Comment `json:"replies"`
ReplyCount int `json:"reply_count"`
// PostTitle 是文章标题(后台评论列表联表带出,仅管理接口填充)
PostTitle string `json:"post_title"`
}