activity

package
v0.21.1 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package activity implements the activity-hooks pattern for event emission.

This pattern follows the strategy from go-users where commands emit rich lifecycle events to a slice of hooks, enabling fan-out notification to multiple sinks (activity logs, metrics, workflows, external systems).

Pattern Overview

1. Hook Contract: ActivityHook interface with Notify(ctx, Event) 2. Event Model: Rich metadata (channel, verb, object type/ID, data) 3. Fan-out: ActivityHookSlice broadcasts to multiple hooks 4. Adapters: Bridge hooks to concrete sinks (admin.ActivitySink, go-users, etc.)

Usage in Commands

type UserActivateCommand struct {
    store         *stores.UserStore
    activityHooks activity.ActivityHookSlice
}

func (c *UserActivateCommand) WithActivityHooks(hooks ...activity.ActivityHook) *UserActivateCommand {
    c.activityHooks = append(c.activityHooks, hooks...)
    return c
}

func (c *UserActivateCommand) Execute(ctx admin.AdminContext) error {
    // ... perform activation ...

    // Emit activity event
    c.activityHooks.Notify(ctx.Context, activity.Event{
        Channel:    "users",
        Verb:       "activated",
        ObjectType: "user",
        ObjectID:   userID,
        Data: map[string]any{
            "email": user.Email,
        },
    })

    return nil
}

Wiring in Modules

func (m *usersModule) Register(ctx admin.ModuleContext) error {
    // Create activity adapter
    activityAdapter := activity.NewAdminActivityAdapter(ctx.Admin.ActivityFeed())

    // Register commands with activity hooks
    activateCmd := commands.NewUserActivateCommand(m.store).
        WithActivityHooks(activityAdapter)

    ctx.Admin.Commands().Register(activateCmd)
    return nil
}

Benefits

- Commands remain decoupled from activity sink implementation - Multiple sinks can be wired without changing command code - Activity emission is opt-in and composable - Rich event metadata supports advanced filtering and analytics - Pattern reusable across modules (users, pages, posts, media)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ActivityHook

type ActivityHook interface {
	Notify(ctx context.Context, event Event)
}

ActivityHook receives activity notifications. Implementations can log to persistence, emit metrics, trigger workflows, etc.

type ActivityHookSlice

type ActivityHookSlice []ActivityHook

ActivityHookSlice enables fan-out emission to multiple hooks. Commands hold a slice of hooks and broadcast events to all registered listeners.

func (ActivityHookSlice) Notify

func (s ActivityHookSlice) Notify(ctx context.Context, event Event)

Notify broadcasts the event to all registered hooks. Failures are silently ignored to prevent hook errors from blocking command execution.

type AdminActivityAdapter

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

AdminActivityAdapter bridges activity hooks to admin.ActivitySink. This adapter allows commands to emit activity events through the hook contract while delegating actual persistence to the admin orchestrator's activity sink.

func NewAdminActivityAdapter

func NewAdminActivityAdapter(sink admin.ActivitySink) *AdminActivityAdapter

NewAdminActivityAdapter creates an adapter that forwards events to an admin.ActivitySink.

func (*AdminActivityAdapter) Notify

func (a *AdminActivityAdapter) Notify(ctx context.Context, event Event)

Notify converts an activity Event to an ActivityEntry and records it via the sink.

type AdminActivitySinkAdapter

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

AdminActivitySinkAdapter adapts go-dashboard activity hooks to admin.ActivitySink. This allows dashboard activity hooks to receive events from admin's activity system.

func NewAdminActivitySinkAdapter

func NewAdminActivitySinkAdapter(hooks dashboardactivity.Hooks, cfg dashboardactivity.Config) *AdminActivitySinkAdapter

NewAdminActivitySinkAdapter creates an adapter that forwards admin ActivityEntry records to go-dashboard activity hooks.

func (*AdminActivitySinkAdapter) List

List implements admin.ActivitySink but is not supported by this adapter since go-dashboard hooks are write-only.

func (*AdminActivitySinkAdapter) Record

Record implements admin.ActivitySink by converting ActivityEntry to dashboard Event.

type DashboardActivityHook

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

DashboardActivityHook adapts go-admin activity events to go-dashboard activity hooks. This allows admin activity to be emitted to go-dashboard's hook system for broader integration (metrics, analytics, notifications, etc).

func NewDashboardActivityHook

func NewDashboardActivityHook(hooks dashboardactivity.Hooks, cfg dashboardactivity.Config) *DashboardActivityHook

NewDashboardActivityHook creates an adapter that forwards admin activity to go-dashboard hooks.

func (*DashboardActivityHook) Notify

func (h *DashboardActivityHook) Notify(ctx context.Context, event Event)

Notify implements the activity.ActivityHook interface by converting admin events to go-dashboard events and emitting them through the dashboard activity system.

type Event

type Event struct {
	// Channel categorizes the event source (e.g., "users", "pages", "posts")
	Channel string

	// Verb describes the action taken (e.g., "created", "activated", "published")
	Verb string

	// ObjectType identifies the resource type (e.g., "user", "page", "post")
	ObjectType string

	// ObjectID is the unique identifier of the affected resource
	ObjectID string

	// Data carries rich metadata for context (e.g., email, status, previous values)
	Data map[string]any
}

Event represents a lifecycle event for activity tracking. This follows the activity-hooks pattern from go-users where commands emit rich events to a slice of hooks for fan-out notification.

Jump to

Keyboard shortcuts

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