comments

package
v1.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 8, 2026 License: MIT Imports: 9 Imported by: 3 modules, 2 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
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

func ValidateCommentText(comment string) error

ValidateCommentText validates the comment text field

Types

type AllowCommentsChecker

type AllowCommentsChecker func(ctx context.Context, contentID int) (bool, error)

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

func (s *Service) ApproveComment(ctx context.Context, commentID int) error

ApproveComment moves a comment to the approved status.

func (*Service) CountComments

func (s *Service) CountComments(ctx context.Context, userID int) (int, error)

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

func (s *Service) DeleteComment(ctx context.Context, commentID int) error

DeleteComment removes a comment by ID (admin path).

func (*Service) DeleteCommentsByUserID

func (s *Service) DeleteCommentsByUserID(ctx context.Context, userID int) error

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

func (s *Service) GetComment(ctx context.Context, commentID int) (*Comment, error)

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

func (s *Service) GetCommentsByUserID(ctx context.Context, userID int) ([]*Comment, error)

GetCommentsByUserID returns the given user's comments in newest-first order.

func (*Service) GetCommentsForContent

func (s *Service) GetCommentsForContent(ctx context.Context, contentID int) ([]*Comment, error)

GetCommentsForContent returns the approved comments visible on a content item.

func (*Service) GetCommentsForModeration

func (s *Service) GetCommentsForModeration(ctx context.Context, contentID int) ([]*Comment, error)

GetCommentsForModeration returns every comment on a content item in any status for the moderation queue.

func (*Service) MarkAsSpam

func (s *Service) MarkAsSpam(ctx context.Context, commentID int) error

MarkAsSpam moves a comment to the spam status.

func (*Service) RejectComment

func (s *Service) RejectComment(ctx context.Context, commentID int) error

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL