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
- Variables
- func RegisterRoutes(r modulekit.Mux, c *Controller, submitRL *ratelimit.RateLimiter, ...)
- func Wire(d modulekit.Deps) modulekit.Parts
- type Controller
- func (c *Controller) Decide(w http.ResponseWriter, r *http.Request)
- func (c *Controller) NodePage(ctx context.Context, node modulekit.Node) modulekit.NodePageParts
- func (c *Controller) PageData(r *http.Request, data map[string]any)
- func (c *Controller) Queue(w http.ResponseWriter, r *http.Request)
- func (c *Controller) Settings(w http.ResponseWriter, r *http.Request)
- func (c *Controller) Submit(w http.ResponseWriter, r *http.Request)
- type ControllerDeps
- type Note
- type ReasonCount
- type Repo
- func (r *Repo) Add(ctx context.Context, t Target, userID int, reason, note string, now time.Time) (target Target, added, fresh bool, err error)
- func (r *Repo) CountNew(ctx context.Context) (int, error)
- func (r *Repo) CountSince(ctx context.Context, targetID int, since time.Time) (int, error)
- func (r *Repo) DeleteByNode(ctx context.Context, nodeID int) error
- func (r *Repo) List(ctx context.Context, status string, limit, offset int) ([]Target, int, error)
- func (r *Repo) Resolve(ctx context.Context, id int, resolution string, by int, at time.Time) error
- func (r *Repo) SaveSettings(ctx context.Context, s Settings) error
- func (r *Repo) SetHidden(ctx context.Context, id int, hidden bool) error
- func (r *Repo) Settings(ctx context.Context) (Settings, error)
- func (r *Repo) TargetByID(ctx context.Context, id int) (*Target, error)
- type Service
- type ServiceDeps
- type Settings
- type Target
Constants ¶
const ( KindNode = "node" KindComment = "comment" )
What a complaint is about (Target.Kind).
const ( StatusNew = "new" StatusResolved = "resolved" )
A target's state in the queue (Target.Status).
const ( ResolutionKept = "kept" ResolutionSpam = "spam" ResolutionDeleted = "deleted" ResolutionUnpublish = "unpublished" )
What a moderator did (Target.Resolution, Decide's action).
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 ¶
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).
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.
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.
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).
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 ¶
func (c *Controller) NodePage(ctx context.Context, node modulekit.Node) modulekit.NodePageParts
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 ReasonCount ¶
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 (*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) CountSince ¶
CountSince - the complaints about targetID since t (zero - all).
func (*Repo) DeleteByNode ¶
DeleteByNode removes the targets of a content item and of its comments (the item deleted), their complaints with them.
func (*Repo) List ¶
List - the targets of status (StatusNew - the queue, newest complaint first), a page of them, and how many there are.
func (*Repo) SaveSettings ¶
SaveSettings stores the module's settings (the row made on the first save).
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 (*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) 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 ¶
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 ¶
NodeTypeAllowed - whether typ's items may be reported.
func (Settings) ReasonList ¶
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.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
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). |