complaints

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2026 License: MIT Imports: 26 Imported by: 0

Documentation

Overview

Package complaints - the complaints module: signed-in visitors report a content item or a comment (a reason, a note), moderators decide in the queue (Content → Complaints); enough complaints of different visitors hide the item until they do.

Index

Constants

View Source
const (
	KindNode    = "node"
	KindComment = "comment"
)

What a complaint is about (Target.Kind).

View Source
const (
	StatusNew      = "new"
	StatusResolved = "resolved"
)

A target's state in the queue (Target.Status).

View Source
const (
	ResolutionKept      = "kept"
	ResolutionSpam      = "spam"
	ResolutionDeleted   = "deleted"
	ResolutionUnpublish = "unpublished"
)

What a moderator did (Target.Resolution, Decide's action).

View Source
const MailNewComplaint = manifest.Name + ".new_complaint"

MailNewComplaint - the e-mail of a complaint new to the queue, to the site's address (modulekit.AddMail).

Variables

View Source
var (
	// ErrNotFound - no such item, or not one everyone sees.
	ErrNotFound = i18n.Errorf("there is nothing to complain about here")
	// ErrNotAllowed - the item's type takes no complaints, or it is the
	// visitor's own.
	ErrNotAllowed = i18n.Errorf("this can't be reported")
	// ErrReason - the reason isn't one offered, or "Other" without a note.
	ErrReason = i18n.Errorf("choose a reason; for \"Other\", say what is wrong")
	// ErrAction - a moderator's action that doesn't fit the item.
	ErrAction = errors.New("no such action for this item")
)

Why a complaint isn't taken (Complain).

View Source
var Assets embed.FS

Assets - the module's templates/ and static/, built into the binary (modules.Assets, paths.SetModuleAssets): a site runs without the module's source directory.

View Source
var DefaultReasons = []string{i18n.N("Spam or advertising"), i18n.N("Insults"), i18n.N("Breaks the site's rules")}

DefaultReasons - the reasons offered while the settings name none.

View Source
var OtherReason = i18n.N("Other")

OtherReason - the reason always offered last, with a note.

Functions

func RegisterRoutes

func RegisterRoutes(r modulekit.Mux, c *Controller, submitRL *ratelimit.RateLimiter, signedIn, admin func(http.Handler) http.Handler)

RegisterRoutes registers the complaint's endpoint (behind signedIn, rate limited), the queue with its decisions and the settings (behind admin; the handlers check the module's permission).

func Wire

func Wire(d modulekit.Deps) modulekit.Parts

Wire - the module's service, its e-mail (the Mail page), the Report links of content pages and the window's reasons (page data), the admin menu's count of the queue, the complaints about deleted content removed, the controller and routes (modulekit).

Types

type Controller

type Controller struct {
	*modulekit.BaseController
	// contains filtered or unexported fields
}

Controller - the module's pages: the complaint's endpoint, the queue, the settings.

func NewController

func NewController(deps modulekit.BaseDeps, d ControllerDeps) *Controller

NewController - the module's controller.

func (*Controller) Decide

func (c *Controller) Decide(w http.ResponseWriter, r *http.Request)

Decide handles POST /admin/content/complaints/{id}/{action}: a moderator's decision (Service.Decide), then back to the queue.

func (*Controller) NodePage

NodePage - the "Report" link under a public content item of a type the settings let be reported, for a signed-in visitor who may report and isn't its author.

func (*Controller) PageData

func (c *Controller) PageData(r *http.Request, data map[string]any)

PageData - every page's data (AddPageData): the reasons of the complaint's window, data["complaintsReasons"], and whether comments may be reported, data["complaintsComments"], for a signed-in user who may report (no reasons - no window, and the comments' links go away).

func (*Controller) Queue

func (c *Controller) Queue(w http.ResponseWriter, r *http.Request)

Queue renders GET /admin/content/complaints (?tab=resolved - the decided ones): the content items and comments complained about, the newest complaint first - what it is, its author, how many complaints and why, the visitors' notes, whether they hid it - and the decisions.

func (*Controller) Settings

func (c *Controller) Settings(w http.ResponseWriter, r *http.Request)

Settings renders and saves GET/POST /admin/config/complaints: the content types whose items may be reported, the reasons, how many complaints hide an item.

func (*Controller) Submit

func (c *Controller) Submit(w http.ResponseWriter, r *http.Request)

Submit handles POST /api/complaints (signed in, CSRF, rate limited): a complaint about a content item or a comment; the answer's message is for the visitor.

type ControllerDeps

type ControllerDeps struct {
	Service      *Service
	Repo         *Repo
	Auth         modulekit.AuthService
	Users        modulekit.UserRepository
	Nodes        modulekit.NodesRepository
	Comments     modulekit.CommentsService
	ContentTypes modulekit.ContentTypeService
}

ControllerDeps - what NewController needs besides the BaseDeps.

type Note

type Note struct {
	UserID int
	Text   string
	At     time.Time
}

Note - a visitor's note with their complaint.

type ReasonCount

type ReasonCount struct {
	Reason string
	Count  int
}

ReasonCount - how many complaints gave a reason.

type Repo

type Repo struct {
	// contains filtered or unexported fields
}

Repo - the module's tables: the targets, the complaints, the settings.

func NewRepo

func NewRepo(db *sql.DB) *Repo

NewRepo - the module's tables on db.

func (*Repo) Add

func (r *Repo) Add(ctx context.Context, t Target, userID int, reason, note string, now time.Time) (target Target, added, fresh bool, err error)

Add records userID's complaint about t's object (Kind, ObjectID, NodeID, AuthorID): its target made, or a resolved one back in the queue (its Hidden kept). added false - the visitor complained already: nothing changes. fresh - the target is new to the queue (made or back).

func (*Repo) CountNew

func (r *Repo) CountNew(ctx context.Context) (int, error)

CountNew - the targets waiting in the queue (the admin menu's number).

func (*Repo) CountSince

func (r *Repo) CountSince(ctx context.Context, targetID int, since time.Time) (int, error)

CountSince - the complaints about targetID since t (zero - all).

func (*Repo) DeleteByNode

func (r *Repo) DeleteByNode(ctx context.Context, nodeID int) error

DeleteByNode removes the targets of a content item and of its comments (the item deleted), their complaints with them.

func (*Repo) List

func (r *Repo) List(ctx context.Context, status string, limit, offset int) ([]Target, int, error)

List - the targets of status (StatusNew - the queue, newest complaint first), a page of them, and how many there are.

func (*Repo) Resolve

func (r *Repo) Resolve(ctx context.Context, id int, resolution string, by int, at time.Time) error

Resolve takes the target out of the queue: what by did, when.

func (*Repo) SaveSettings

func (r *Repo) SaveSettings(ctx context.Context, s Settings) error

SaveSettings stores the module's settings (the row made on the first save).

func (*Repo) SetHidden

func (r *Repo) SetHidden(ctx context.Context, id int, hidden bool) error

SetHidden marks whether the complaints hid the target's object.

func (*Repo) Settings

func (r *Repo) Settings(ctx context.Context) (Settings, error)

Settings - the module's settings (the defaults while its row is missing).

func (*Repo) TargetByID

func (r *Repo) TargetByID(ctx context.Context, id int) (*Target, error)

TargetByID - the target id with its complaints' count, reasons and notes (a not-found error - none).

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service - the complaints: taken from visitors, an item hidden after enough of them, decided by moderators.

func NewService

func NewService(d ServiceDeps) *Service

NewService - the complaints' service.

func (*Service) Complain

func (s *Service) Complain(ctx context.Context, user *modulekit.User, kind string, id int, reason, note string) error

Complain takes user's complaint about the object (KindNode or KindComment, its id) for reason (one of the settings' ReasonList; with OtherReason a note is needed): a second one of the same visitor changes nothing. A complaint new to the queue is mailed to the site; enough of them hide the object until a moderator decides. ErrNotFound, ErrNotAllowed, ErrReason - refused.

func (*Service) CountNew

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

CountNew - the targets waiting in the queue (the admin menu's number).

func (*Service) Decide

func (s *Service) Decide(ctx context.Context, moderator *modulekit.User, targetID int, action string) error

Decide - a moderator's decision on the target: ResolutionKept (an object the complaints hid shown again), ResolutionSpam (a comment), ResolutionUnpublish (a content item unpublished, a comment back to waiting for approval), ResolutionDeleted (a comment deleted, a content item into the trash); the target leaves the queue. An object gone already is resolved as is. ErrAction - an action not for its kind.

type ServiceDeps

type ServiceDeps struct {
	Repo     *Repo
	Nodes    modulekit.NodesRepository
	Comments modulekit.CommentsService
	Deleter  modulekit.NodeDeleter
	Site     modulekit.SiteInformationRepository
	Users    modulekit.UserRepository
	Mail     mailtemplate.Mailer
	Logger   *slog.Logger
	// BaseURL - the site's address for the e-mail's links (nil -
	// modulekit.MailBaseURL).
	BaseURL func() string
}

ServiceDeps - what NewService needs.

type Settings

type Settings struct {
	Comments  bool
	NodeTypes []string
	Reasons   string
	HideAfter int
}

Settings - the module's settings: whether comments may be reported, the content types whose items may, the reasons (one per line; none - the defaults), how many complaints of different visitors hide an item (0 - none hides).

func (Settings) NodeTypeAllowed

func (s Settings) NodeTypeAllowed(typ string) bool

NodeTypeAllowed - whether typ's items may be reported.

func (Settings) ReasonList

func (s Settings) ReasonList() []string

ReasonList - the reasons offered (each cut to reasonMax characters), OtherReason last (translation sources when the defaults).

type Target

type Target struct {
	ID         int
	Kind       string
	ObjectID   int
	NodeID     int
	AuthorID   int
	Status     string
	Hidden     bool
	Resolution string
	ResolvedBy int
	ResolvedAt *time.Time
	LastAt     time.Time
	CreatedAt  time.Time
	// Count - the complaints; Reasons - how many of each reason; Notes -
	// the newest notes visitors wrote.
	Count   int
	Reasons []ReasonCount
	Notes   []Note
}

Target - a content item or a comment complained about, in the queue: what it is (Kind, ObjectID, its content item NodeID, its author), its state, the complaints' count, reasons and notes.

Directories

Path Synopsis
ent
Package ent - the complaints module's own ent schema (schema/), its generated client (generated/, go generate) and migration files (migrations/<dialect>/, scripts/migrations_gen.sh writes them), apart from the core's: the module's set of migrations (Schema).
Package ent - the complaints module's own ent schema (schema/), its generated client (generated/, go generate) and migration files (migrations/<dialect>/, scripts/migrations_gen.sh writes them), apart from the core's: the module's set of migrations (Schema).
schema
Package schema - the complaints module's own ent schema: its tables, apart from the core's (internal/infrastructure/ent/schema).
Package schema - the complaints module's own ent schema: its tables, apart from the core's (internal/infrastructure/ent/schema).
Package manifest declares the complaints module (its registry entry).
Package manifest declares the complaints module (its registry entry).

Jump to

Keyboard shortcuts

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