Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrContentNotFound = errors.New("comments: content not found")
ErrContentNotFound is returned by the ContentLookup when no content matches the ID. Core translates the content lookup failure to it so this package never imports the content domain.
Functions ¶
This section is empty.
Types ¶
type CommentHandler ¶
type CommentHandler struct {
// contains filtered or unexported fields
}
CommentHandler exposes the Bearer-authenticated agent comment endpoints. It reuses the comments module service (which already validates comment text and enforces the AllowComments gate) — it never duplicates that logic. The comment surface is nested under the agent's content-keyed namespace (/api/v1/content/{id}/comments) so it is collision-free vs the browser realm's /api/v1/content_items/.../comments and /api/v1/comments routes, and consistent with the agent surface that keys everything by content id.
func NewCommentHandler ¶
func NewCommentHandler( commentService CommentService, content ContentLookup, logger kernel.Logger, opts ...CommentHandlerOption, ) *CommentHandler
NewCommentHandler constructs a CommentHandler backed by the given comment service and content lookup. A nil logger degrades to a discard sink so the handler is safe to construct in any context.
func (*CommentHandler) Create ¶
func (h *CommentHandler) Create(w http.ResponseWriter, r *http.Request)
Create handles POST /api/v1/content/{id}/comments. It resolves the content by id (404 NOT_FOUND when missing, and 404 — never 403 — when the caller is neither owner nor Admin and the content is a draft, so existence is not disclosed), rejects commenting when the content has comments disabled (403 FORBIDDEN), then delegates to SubmitComment which validates the text (1–2000 chars, no HTML → ErrInvalidCommentText → 400 VALIDATION_ERROR) and persists the comment in the "pending" moderation status. Like the rest of the agent surface, success is 200 (the browser realm returns 201; the agent realm standardizes on 200 via httpx.Success).
func (*CommentHandler) Delete ¶
func (h *CommentHandler) Delete(w http.ResponseWriter, r *http.Request)
Delete handles DELETE /api/v1/content/{id}/comments/{commentId}. The path content id is bound to the comment (a mismatch — the comment belongs to other content — is rejected with a clean 404, no disclosure), then an Admin may delete any comment (DeleteComment) while any other caller may delete only their own (DeleteOwnComment, which returns ErrCommentNotFound — a 404 with no ownership disclosure — when the comment is missing or belongs to someone else). Success is 204 No Content.
func (*CommentHandler) List ¶
func (h *CommentHandler) List(w http.ResponseWriter, r *http.Request)
List handles GET /api/v1/content/{id}/comments. An Admin sees the moderation view (every comment, any status — mirroring the browser realm's admin-gated moderation queue); any other caller sees only approved comments, mirroring the public GET /api/v1/public/content_items/{slug}/comments. Both are scoped by the same visibility model as Create (published, or owned by the caller, or Admin; else 404). Comments-disabled content yields an empty list for non-admins (admins still moderate); the data array is always present, even when empty.
func (*CommentHandler) UpdateStatus ¶
func (h *CommentHandler) UpdateStatus(w http.ResponseWriter, r *http.Request)
UpdateStatus handles PUT /api/v1/content/{id}/comments/{commentId}/status. It is admin-only (a non-admin API key gets 403 FORBIDDEN). The path content id is bound to the comment (a mismatch yields a clean 404), then it delegates to UpdateCommentStatus, which validates the status (a valid commentdomain.CommentStatus — pending/approved/rejected/spam; an unknown value returns ErrInvalidCommentStatus → 400 VALIDATION_ERROR). The updated comment is returned so the caller sees the new status.
type CommentHandlerOption ¶
type CommentHandlerOption func(*CommentHandler)
CommentHandlerOption configures a CommentHandler. A role service makes the comment capability config-driven; without one every authenticated user may comment (the legacy behavior).
func WithCommentRoleService ¶
func WithCommentRoleService(rs RoleCapabilities) CommentHandlerOption
WithCommentRoleService attaches the config-driven role registry so posting comments is gated by the caller's CanComment capability.
type CommentProjection ¶
type CommentProjection struct {
ID int `json:"id"`
Comment string `json:"comment"`
Author string `json:"author,omitempty"`
Username string `json:"username,omitempty"`
Role string `json:"role,omitempty"`
Status string `json:"status,omitempty"`
CreatedAt time.Time `json:"createdAt"`
}
CommentProjection is the public, whitelisted view of a comment returned by the agent comment endpoints. It mirrors the browser comment handler's response shape (id, comment, author, username, role, status, createdAt) so the wire contract is consistent across the two realms, and excludes the numeric owner / content ids the raw commentdomain.Comment carries.
type CommentResponse ¶
type CommentResponse struct {
Comment CommentProjection `json:"comment"`
}
CommentResponse wraps a projected comment for the agent comment responses. Envelope body: {"data":{"comment":{...}}}.
func NewCommentResponse ¶
func NewCommentResponse(c *commentdomain.Comment) CommentResponse
NewCommentResponse builds a CommentResponse projecting the given comment entity to its public fields. A nil entity yields an empty projection (defensive — the service contract returns a non-nil comment on success).
type CommentService ¶
type CommentService interface {
SubmitComment(ctx context.Context, contentID int, principal kernel.Principal, req commentdomain.CreateCommentRequest) (*commentdomain.Comment, error)
GetCommentsForContent(ctx context.Context, contentID int) ([]*commentdomain.Comment, error)
GetCommentsForModeration(ctx context.Context, contentID int) ([]*commentdomain.Comment, error)
GetComment(ctx context.Context, commentID int) (*commentdomain.Comment, error)
UpdateCommentStatus(ctx context.Context, commentID int, status commentdomain.CommentStatus) error
DeleteComment(ctx context.Context, commentID int) error
DeleteOwnComment(ctx context.Context, commentID int, principal kernel.Principal) error
}
CommentService is the narrow slice of comment operations the agent comment handlers depend on. *commentdomain.Service satisfies it. It is exported so mockery can generate an exported constructor callable from the package's *_test files, which cannot reach an unexported mock.
type ContentDetail ¶
ContentDetail is the comment-relevant projection of a content item: its ID and owner for visibility checks, its published flag, and its AllowComments flag for gating.
type ContentLookup ¶
type ContentLookup func(ctx context.Context, id int) (ContentDetail, error)
ContentLookup resolves a content ID to its comment-relevant facts. Core satisfies it with a closure over the content service (which cannot be imported here); tests stub it with a func literal.
type RoleCapabilities ¶
RoleCapabilities is the narrow role-registry seam this handler depends on. Core satisfies it with an adapter over *role.Service (which cannot be imported here); the users module implements it natively when it lands.