contribution

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2026 License: MIT Imports: 17 Imported by: 0 modules, 0 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 (
	ErrNotFound       = errors.New("contribution not found")
	ErrWorkNotFound   = errors.New("work not found")
	ErrEmptyPatch     = errors.New("patch is empty")
	ErrAlreadyDecided = errors.New("contribution already decided")
	// ErrInvalidPatch is wrapped with a user-facing reason by validatePatch;
	// the handler returns the full message as a 400.
	ErrInvalidPatch = errors.New("invalid patch")
)

Functions

This section is empty.

Types

type Contribution

type Contribution struct {
	ID            uuid.UUID      `gorm:"type:uuid;primaryKey" json:"id"`
	WorkID        uuid.UUID      `gorm:"type:uuid;not null;index" json:"work_id"`
	ContributorID uint           `gorm:"not null;index" json:"contributor_id"`
	Status        Status         `gorm:"not null;index;default:pending" json:"status"`
	Patch         datatypes.JSON `json:"patch"`
	ReviewerID    *uint          `gorm:"index" json:"reviewer_id,omitempty"`
	DecidedAt     *time.Time     `json:"decided_at,omitempty"`
	CreatedAt     time.Time      `json:"created_at"`
	UpdatedAt     time.Time      `json:"updated_at"`

	// ContributorName is populated by the service layer via a users-table
	// lookup (gorm:"-" so AutoMigrate ignores it). It's what the moderator
	// queue UI displays under each card — empty for users that no longer
	// exist, which the UI falls back to "user #ID" on.
	ContributorName string `gorm:"-" json:"contributor_name,omitempty"`

	// Current holds the target Work's present value for each field named in
	// Patch — i.e. the "old" side of the moderator-queue diff. Populated by
	// the service via a batch Works lookup (gorm:"-"); keys mirror Patch so
	// the UI can pair old↔new. Resolved at read time, so it reflects the
	// Work as it stands now (another approval may have moved it since
	// submission). Omitted from the wire when empty.
	Current map[string]any `gorm:"-" json:"current,omitempty"`
}

Contribution is a proposed edit to a Work. The Patch column stores a flat partial-object of the form {"title": "...", "authors": "..."}; only whitelisted keys (see service.allowedPatchFields) survive the Approve apply step. datatypes.JSON picks JSONB on Postgres and TEXT on sqlite so smoke tests stay portable.

type Handler

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

func NewHandler

func NewHandler(s *Service) *Handler

func (*Handler) Approve

func (h *Handler) Approve(w http.ResponseWriter, r *http.Request)

func (*Handler) Get

func (h *Handler) Get(w http.ResponseWriter, r *http.Request)

func (*Handler) List

func (h *Handler) List(w http.ResponseWriter, r *http.Request)

func (*Handler) ListMine

func (h *Handler) ListMine(w http.ResponseWriter, r *http.Request)

ListMine returns the calling user's own contributions. Separate route (/me/contributions) rather than a ?contributor=me pseudo-value on /contributions, so the public List handler stays trivially cacheable and auth is enforced by the router rather than re-parsed here.

func (*Handler) Reject

func (h *Handler) Reject(w http.ResponseWriter, r *http.Request)

func (*Handler) Submit

func (h *Handler) Submit(w http.ResponseWriter, r *http.Request)

type ListFilter

type ListFilter struct {
	Status        Status
	ContributorID *uint
	Limit         int
	Offset        int
}

ListFilter narrows List results. Zero-valued fields are ignored.

type Service

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

func NewService

func NewService(db *gorm.DB) *Service

func (*Service) Approve

func (s *Service) Approve(ctx context.Context, id uuid.UUID, reviewerID uint) (*Contribution, error)

Approve applies the contribution's whitelisted patch fields to the target Work and marks the contribution approved. Returns ErrAlreadyDecided if the contribution isn't pending — including when another moderator decides it concurrently (see transition).

func (*Service) Get

func (s *Service) Get(ctx context.Context, id uuid.UUID) (*Contribution, error)

func (*Service) List

func (s *Service) List(ctx context.Context, f ListFilter) ([]Contribution, error)

func (*Service) Reject

func (s *Service) Reject(ctx context.Context, id uuid.UUID, reviewerID uint) (*Contribution, error)

Reject marks the contribution rejected without touching the Work.

func (*Service) Submit

func (s *Service) Submit(ctx context.Context, workID uuid.UUID, contributorID uint, patch map[string]any) (*Contribution, error)

Submit validates and records a new pending contribution. A valid patch is stored verbatim so the original proposal stays auditable.

type Status

type Status string
const (
	StatusPending  Status = "pending"
	StatusApproved Status = "approved"
	StatusRejected Status = "rejected"
)

Jump to

Keyboard shortcuts

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