Documentation
¶
Index ¶
- Variables
- func ValidateCommentStatus(status CommentStatus) error
- func ValidateCommentText(comment string) error
- type AllowCommentsChecker
- type Comment
- type CommentRepository
- type CommentStatus
- type CreateCommentRequest
- type Option
- type Service
- func (s *Service) ApproveComment(ctx context.Context, commentID int) error
- func (s *Service) CountComments(ctx context.Context, userID int) (int, error)
- func (s *Service) DeleteComment(ctx context.Context, commentID int) error
- func (s *Service) DeleteCommentsByUserID(ctx context.Context, userID int) error
- func (s *Service) DeleteOwnComment(ctx context.Context, commentID int, principal kernel.Principal) error
- func (s *Service) GetComment(ctx context.Context, commentID int) (*Comment, error)
- func (s *Service) GetCommentsByStatus(ctx context.Context, status CommentStatus) ([]*Comment, error)
- func (s *Service) GetCommentsByUserID(ctx context.Context, userID int) ([]*Comment, error)
- func (s *Service) GetCommentsForContent(ctx context.Context, contentID int) ([]*Comment, error)
- func (s *Service) GetCommentsForModeration(ctx context.Context, contentID int) ([]*Comment, error)
- func (s *Service) MarkAsSpam(ctx context.Context, commentID int) error
- func (s *Service) RejectComment(ctx context.Context, commentID int) error
- func (s *Service) SubmitComment(ctx context.Context, contentID int, principal kernel.Principal, ...) (*Comment, error)
- func (s *Service) UpdateCommentStatus(ctx context.Context, commentID int, status CommentStatus) error
- type UpdateCommentStatusRequest
Constants ¶
This section is empty.
Variables ¶
var ( // ErrCommentNotFound is returned when comment cannot be found ErrCommentNotFound = errors.New("comment not found") // ErrContentNotFound is returned when the target content item cannot be // found. Core translates the content lookup failure to it so the module // never imports the content domain. ErrContentNotFound = errors.New("comment target content not found") // ErrInvalidCommentText is returned when comment text validation fails ErrInvalidCommentText = errors.New("comment text is required and must be between 1 and 2000 characters") // ErrInvalidCommentStatus is returned when comment status is not valid ErrInvalidCommentStatus = errors.New("comment status must be one of: pending, approved, rejected, spam") // ErrHTMLInPlainText is returned when plain text contains HTML tags ErrHTMLInPlainText = errors.New("field must not contain HTML tags") // ErrCommentsDisabled is returned when the target content does not // accept comments. It replaces the content package's ErrUnauthorized on // this path so the failure names the feature, not the content. ErrCommentsDisabled = errors.New("comments are not allowed on this content") )
Functions ¶
func ValidateCommentStatus ¶
func ValidateCommentStatus(status CommentStatus) error
ValidateCommentStatus validates the comment status
func ValidateCommentText ¶
ValidateCommentText validates the comment text field
Types ¶
type AllowCommentsChecker ¶
AllowCommentsChecker reports whether a content item accepts comments. Core satisfies it with a closure over the content service (which the module cannot import); the error chain preserves the content lookup failure so callers map a missing item exactly as before.
type Comment ¶
type Comment struct {
ID int `json:"id"`
ContentID int `json:"contentId"`
UserID int `json:"userId"`
Comment string `json:"comment"`
Status CommentStatus `json:"status"`
Author string `json:"author,omitempty"`
Username string `json:"username,omitempty"`
Role string `json:"role,omitempty"`
ContentTitle string `json:"contentTitle,omitempty"`
ContentSlug string `json:"contentSlug,omitempty"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
Comment represents a comment on a content item
type CommentRepository ¶
type CommentRepository interface {
Create(ctx context.Context, comment *Comment) error
GetByContentID(ctx context.Context, contentID int) ([]*Comment, error)
GetByContentIDForModeration(ctx context.Context, contentID int) ([]*Comment, error)
GetByID(ctx context.Context, id int) (*Comment, error)
GetByUserID(ctx context.Context, userID int) ([]*Comment, error)
GetByStatus(ctx context.Context, status CommentStatus) ([]*Comment, error)
// Count returns the total number of comments. userID <= 0 counts ALL comments
// (admin global scope); otherwise it counts only the given user's comments,
// mirroring GetByUserID's WHERE clause so a list's total matches its rows.
Count(ctx context.Context, userID int) (int, error)
UpdateStatus(ctx context.Context, id int, status CommentStatus) error
Delete(ctx context.Context, id int) error
DeleteByUserID(ctx context.Context, userID int) error
}
CommentRepository defines the interface for comment repository operations
type CommentStatus ¶
type CommentStatus string
CommentStatus represents the moderation status of a comment
const ( // CommentStatusPending represents a comment awaiting moderation CommentStatusPending CommentStatus = "pending" // CommentStatusApproved represents an approved comment CommentStatusApproved CommentStatus = "approved" // CommentStatusRejected represents a comment rejected by moderation CommentStatusRejected CommentStatus = "rejected" // CommentStatusSpam represents a comment marked as spam CommentStatusSpam CommentStatus = "spam" )
func (CommentStatus) IsApproved ¶
func (s CommentStatus) IsApproved() bool
IsApproved returns true if the comment status is approved
func (CommentStatus) IsPending ¶
func (s CommentStatus) IsPending() bool
IsPending returns true if the comment status is pending
func (CommentStatus) IsRejected ¶
func (s CommentStatus) IsRejected() bool
IsRejected returns true if the comment status is rejected
func (CommentStatus) IsSpam ¶
func (s CommentStatus) IsSpam() bool
IsSpam returns true if the comment status is spam
func (CommentStatus) IsValid ¶
func (s CommentStatus) IsValid() bool
IsValid checks if the comment status is valid
func (CommentStatus) String ¶
func (s CommentStatus) String() string
String returns the string representation of the comment status
type CreateCommentRequest ¶
type CreateCommentRequest struct {
Comment string `json:"comment"`
}
CreateCommentRequest represents a request to create a comment
type Option ¶
type Option func(*Service)
Option configures a Service at construction time.
func WithAllowCommentsChecker ¶
func WithAllowCommentsChecker(checker AllowCommentsChecker) Option
WithAllowCommentsChecker attaches the content gate for SubmitComment. Without it submissions fail closed — the gate is required, never optional.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service handles comment business logic
func NewService ¶
func NewService( repo CommentRepository, opts ...Option, ) *Service
NewService creates a comment Service on the given repository. The AllowComments gate defaults to closed; the composition root attaches the content-backed checker via WithAllowCommentsChecker.
func (*Service) ApproveComment ¶
ApproveComment moves a comment to the approved status.
func (*Service) CountComments ¶
CountComments returns the total number of comments. userID <= 0 counts ALL comments (admin global scope); otherwise it counts only the given user's comments, mirroring GetCommentsByUserID's scope so a list's total matches its rows.
func (*Service) DeleteComment ¶
DeleteComment removes a comment by ID (admin path).
func (*Service) DeleteCommentsByUserID ¶
DeleteCommentsByUserID removes every comment owned by the given user.
func (*Service) DeleteOwnComment ¶
func (s *Service) DeleteOwnComment(ctx context.Context, commentID int, principal kernel.Principal) error
DeleteOwnComment removes a comment owned by the caller's user ID. A missing comment or one owned by someone else both yield ErrCommentNotFound so ownership is never disclosed.
func (*Service) GetComment ¶
GetComment returns a single comment by ID.
func (*Service) GetCommentsByStatus ¶
func (s *Service) GetCommentsByStatus(ctx context.Context, status CommentStatus) ([]*Comment, error)
GetCommentsByStatus returns comments in the given moderation status with their content context for the pending queue.
func (*Service) GetCommentsByUserID ¶
GetCommentsByUserID returns the given user's comments in newest-first order.
func (*Service) GetCommentsForContent ¶
GetCommentsForContent returns the approved comments visible on a content item.
func (*Service) GetCommentsForModeration ¶
GetCommentsForModeration returns every comment on a content item in any status for the moderation queue.
func (*Service) MarkAsSpam ¶
MarkAsSpam moves a comment to the spam status.
func (*Service) RejectComment ¶
RejectComment moves a comment to the rejected status.
func (*Service) SubmitComment ¶
func (s *Service) SubmitComment(ctx context.Context, contentID int, principal kernel.Principal, req CreateCommentRequest) (*Comment, error)
SubmitComment validates the text, enforces the target's AllowComments gate, and persists the comment in the pending moderation status.
func (*Service) UpdateCommentStatus ¶
func (s *Service) UpdateCommentStatus(ctx context.Context, commentID int, status CommentStatus) error
UpdateCommentStatus moves a comment to a validated moderation status.
type UpdateCommentStatusRequest ¶
type UpdateCommentStatusRequest struct {
Status CommentStatus `json:"status"`
}
UpdateCommentStatusRequest represents a request to update comment status