Documentation
¶
Overview ¶
Package favorite 提供收藏:已登录用户把一篇内容标记下来,日后在「我的收藏」里找回。
分工与 search 相同——本模块只回答「谁收了哪几篇」,出的是内容 ID; 收藏页上那一列文章由主题自己的查询补齐(见 internal/theme 的 Favoriter 接口)。 这样收藏模块不必知道前台怎么展示一篇文章,主题也不必知道收藏是怎么存的。
依赖 content 模块的 posts 表与核心的 users 表:modules.go 中 content 必须先于本模块注册。 本模块又必须先于 theme 注册——主题在装配期就要取到它。
Index ¶
- Constants
- Variables
- type Favorite
- type Handler
- type Module
- type Store
- func (s *Store) Add(ctx context.Context, userID, postID int64) (bool, error)
- func (s *Store) CountFavorites(ctx context.Context, postID int64) (int, error)
- func (s *Store) FavoritePostIDs(ctx context.Context, userID int64, limit, offset int) (ids []int64, total int, err error)
- func (s *Store) HasFavorite(ctx context.Context, userID, postID int64) (bool, error)
- func (s *Store) Remove(ctx context.Context, userID, postID int64) (bool, error)
- func (s *Store) VisiblePost(ctx context.Context, postID int64) error
Constants ¶
const Name = "favorite"
Name 是模块名,也是迁移版本表的后缀(goose_db_version_favorite)。
Variables ¶
var ( // ErrPostNotFound 表示要收藏的内容不存在,或已不对访客公开。 // // 两种情况合成一个错误是有意的:收藏接口对「草稿」与「不存在」 // 必须给出同一个回答,否则它就成了一个探测未发布内容是否存在的接口。 ErrPostNotFound = errors.New("收藏的内容不存在") )
错误哨兵。处理器据此映射为 404。
Functions ¶
This section is empty.
Types ¶
type Favorite ¶
type Favorite struct {
bun.BaseModel `bun:"table:favorites,alias:fv"`
ID int64 `bun:"id,pk,autoincrement"`
UserID int64 `bun:"user_id,notnull"`
PostID int64 `bun:"post_id,notnull"`
CreatedAt time.Time `bun:"created_at,nullzero"`
}
Favorite 是一条收藏记录。
没有 json 标签:收藏不经 Console 平面,也没有任何接口回传整行—— 对外只回答「收没收」与「多少人收了」两件事(见 handler.go)。
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler 提供收藏的 Public 接口。
没有 Console 接口:后台管不了别人收藏了什么,那是访客的私人清单, 站长要看的「这篇被收了多少次」在文章页上就有。
type Module ¶
type Module struct {
// contains filtered or unexported fields
}
Module 是收藏模块。 没有 logger 字段:本模块没有任何需要在运行期告警的分支—— 收藏失败经接口返回给调用者,不是站长要处理的事。
func (*Module) Permissions ¶
func (m *Module) Permissions() []app.Permission
Permissions 实现 app.PermissionProvider:收藏不需要任何权限。
明写一个返回 nil 的实现,而不是干脆不实现这个接口:收藏是登录用户人人可做的动作 (与发评论同级),不该出现在权限清单里让站长以为自己需要给成员授权。
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store 提供收藏的持久化操作。
直接用 bun 读 posts 表而不经 content 模块:两者共用同一张表, 而模块之间只允许经 App 交互,不值得为一次可见性判定引入跨模块依赖 (与 comment 的 Store 同一条理由)。
func From ¶
From 取回收藏存储;模块未装配或没有数据库时返回 nil。
返回 Store 而不是 Module:调用方(theme)要的就是那几个查询, 拿到整个模块只会让它够得着不该碰的东西。
func (*Store) Add ¶
Add 收藏一篇内容,返回本次是否真的新增了记录。
幂等:已收藏时什么都不做并返回 false,而不是报「已存在」。 判重交给唯一约束(ON CONFLICT DO NOTHING)而不是先查后插—— 用户连点两下发出的两个请求之间没有空隙可钻,而先查后插有。
func (*Store) CountFavorites ¶
CountFavorites 返回一篇内容被收藏的次数。
func (*Store) FavoritePostIDs ¶
func (s *Store) FavoritePostIDs(ctx context.Context, userID int64, limit, offset int) ( ids []int64, total int, err error, )
FavoritePostIDs 返回某人收藏的、当前仍对访客可见的内容 ID,按收藏时间倒序, 另返回符合条件的总数。
只出 ID 不出内容:作者、分类、标签与列表视图由主题自己的查询补齐, 与 search.Service.SearchPostIDs 同一种分工——收藏模块不必知道前台怎么展示一篇文章。
可见性在这里再过一遍(而不是只在写入时过一遍):一篇文章被收藏之后 完全可能被撤回或转为私密,那时它必须从收藏页上消失, 否则收藏页就成了一扇能看见已下线内容标题的窗。 总数与列表用同一个 WHERE,故「显示 3 条却说共 5 篇」不会发生。
func (*Store) HasFavorite ¶
HasFavorite 报告某人是否收藏过某篇内容。
func (*Store) Remove ¶
Remove 取消收藏,返回本次是否真的删掉了记录。
同样幂等:没收藏过也算成功。前端的按钮状态可能与库里不一致 (换了设备、在另一个标签页里点过),把这种情况当错误报出来毫无意义。
func (*Store) VisiblePost ¶
VisiblePost 校验内容存在且对访客可见,不可见时返回 ErrPostNotFound。
判据与主题前台的 publicFilter、评论接口的 PubliclyVisible 完全一致:已发布且公开。 作者本人的私密内容也不允许收藏——收藏列表是一张长期存在的清单, 而内容的可见性随时会变,列表页每次都要再过一遍可见性(见 FavoritePostIDs), 那里没有「作者可见自己的私密内容」这一路,写入时也就不该放行。