Documentation
¶
Overview ¶
Package activity provides default persistence helpers for the go-users ActivitySink. The Repository implements both the sink (writes) and the ActivityRepository read-side contract so transports can log lifecycle events and later query them for dashboards. The ActivitySink interface lives in pkg/types and is intentionally minimal (`Log(ctx, ActivityRecord) error`) so hosts can swap sinks without breaking changes.
Index ¶
- func BuildRecordFromActor(actor *auth.ActorContext, verb, objectType, objectID string, ...) (types.ActivityRecord, error)
- func BuildRecordFromUUID(actorID uuid.UUID, verb, objectType, objectID string, metadata map[string]any, ...) (types.ActivityRecord, error)
- func ToActivityRecord(entry *LogEntry) types.ActivityRecord
- type LogEntry
- type RecordOption
- type Repository
- func (r *Repository) ActivityStats(ctx context.Context, filter types.ActivityStatsFilter) (types.ActivityStats, error)
- func (r *Repository) ListActivity(ctx context.Context, filter types.ActivityFilter) (types.ActivityPage, error)
- func (r *Repository) Log(ctx context.Context, record types.ActivityRecord) error
- type RepositoryConfig
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BuildRecordFromActor ¶
func BuildRecordFromActor(actor *auth.ActorContext, verb, objectType, objectID string, metadata map[string]any, opts ...RecordOption) (types.ActivityRecord, error)
BuildRecordFromActor constructs an ActivityRecord using the actor metadata supplied by go-auth middleware plus verb/object details and optional metadata. It normalizes actor, tenant, and org identifiers into UUIDs and defensively copies metadata to avoid caller mutation.
func BuildRecordFromUUID ¶ added in v0.3.0
func BuildRecordFromUUID(actorID uuid.UUID, verb, objectType, objectID string, metadata map[string]any, opts ...RecordOption) (types.ActivityRecord, error)
BuildRecordFromUUID constructs an ActivityRecord when only the actor UUID is available. It trims verb/object fields, validates required values, copies metadata defensively, and applies RecordOptions.
func ToActivityRecord ¶
func ToActivityRecord(entry *LogEntry) types.ActivityRecord
ToActivityRecord converts the Bun model into the domain activity record.
Types ¶
type LogEntry ¶
type LogEntry struct {
bun.BaseModel `bun:"table:user_activity"`
ID uuid.UUID `bun:",pk,type:uuid"`
UserID uuid.UUID `bun:"user_id,type:uuid"`
ActorID uuid.UUID `bun:"actor_id,type:uuid"`
TenantID uuid.UUID `bun:"tenant_id,type:uuid"`
OrgID uuid.UUID `bun:"org_id,type:uuid"`
Verb string `bun:"verb"`
ObjectType string `bun:"object_type"`
ObjectID string `bun:"object_id"`
Channel string `bun:"channel"`
IP string `bun:"ip"`
Data map[string]any `bun:"data,type:jsonb"`
CreatedAt time.Time `bun:"created_at"`
}
LogEntry models the persisted row in user_activity.
func FromActivityRecord ¶
func FromActivityRecord(record types.ActivityRecord) *LogEntry
FromActivityRecord converts a domain activity record into the Bun model so it can be reused by transports without duplicating conversion logic.
type RecordOption ¶
type RecordOption func(*types.ActivityRecord)
RecordOption mutates the ActivityRecord produced by BuildRecordFromActor.
func WithChannel ¶
func WithChannel(channel string) RecordOption
WithChannel sets the channel/module field used for downstream filtering.
func WithOccurredAt ¶ added in v0.3.0
func WithOccurredAt(occurredAt time.Time) RecordOption
WithOccurredAt overrides the default occurrence timestamp.
func WithOrg ¶ added in v0.3.0
func WithOrg(orgID uuid.UUID) RecordOption
WithOrg sets the organization identifier for the record.
func WithTenant ¶ added in v0.3.0
func WithTenant(tenantID uuid.UUID) RecordOption
WithTenant sets the tenant identifier for the record.
type Repository ¶
type Repository struct {
// contains filtered or unexported fields
}
Repository persists activity logs and exposes query helpers.
func NewRepository ¶
func NewRepository(cfg RepositoryConfig) (*Repository, error)
NewRepository constructs a repository that implements both ActivitySink and ActivityRepository interfaces.
func (*Repository) ActivityStats ¶
func (r *Repository) ActivityStats(ctx context.Context, filter types.ActivityStatsFilter) (types.ActivityStats, error)
ActivityStats aggregates counts grouped by verb.
func (*Repository) ListActivity ¶
func (r *Repository) ListActivity(ctx context.Context, filter types.ActivityFilter) (types.ActivityPage, error)
ListActivity returns a paginated feed filtered by the supplied criteria.
func (*Repository) Log ¶
func (r *Repository) Log(ctx context.Context, record types.ActivityRecord) error
Log persists an activity record into the database.
type RepositoryConfig ¶
type RepositoryConfig struct {
DB *bun.DB
Repository repository.Repository[*LogEntry]
Clock types.Clock
IDGen types.IDGenerator
}
RepositoryConfig wires the Bun-backed activity repository.