inbox

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	FilterAll    = "all"
	FilterUnread = "unread"
	FilterRead   = "read"
)

Filter constants used by List to narrow messages by read state.

Variables

This section is empty.

Functions

func IsValidFilter

func IsValidFilter(filter string) bool

IsValidFilter reports whether filter is one of the supported filter constants (all, unread, read).

Types

type Channel

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

Channel wraps a Store to implement the notification.Channel interface, allowing in-app inbox notifications to be registered with a notification.Dispatcher.

func NewChannel

func NewChannel(name string, store Store) *Channel

NewChannel creates an inbox Channel backed by the given Store. The channel is enabled by default.

func (*Channel) Enabled

func (c *Channel) Enabled() bool

Enabled reports whether the channel is active.

func (*Channel) Name

func (c *Channel) Name() string

Name returns the channel name.

func (*Channel) Send

func (c *Channel) Send(ctx context.Context, msg notification.Message) error

Send delivers an inbox Message via the underlying Store. The recipient is taken from msg.UserID (falling back to msg.To), the title from msg.Title (falling back to msg.Subject), and the content from msg.Content (falling back to msg.Body).

func (*Channel) SetEnabled

func (c *Channel) SetEnabled(enabled bool)

SetEnabled toggles the channel on or off.

func (*Channel) Type

func (c *Channel) Type() notification.MessageType

Type returns notification.TypeInbox.

type GormMessage

type GormMessage struct {
	ID          uint      `gorm:"primaryKey" json:"id"`
	UserID      string    `gorm:"index" json:"user_id"`
	Title       string    `json:"title"`
	Content     string    `json:"content"`
	ActionURL   string    `json:"action_url"`
	ActionLabel string    `json:"action_label"`
	Read        bool      `gorm:"index" json:"read"`
	CreatedAt   time.Time `json:"created_at"`
	UpdatedAt   time.Time `json:"updated_at"`
}

GormMessage is the gorm-backed persistence model for inbox messages. Applications using GormStore should include it in their AutoMigrate call: db.AutoMigrate(&inbox.GormMessage{}).

func (GormMessage) TableName

func (GormMessage) TableName() string

TableName overrides the default table name.

type GormStore

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

GormStore is a gorm-backed implementation of Store.

func NewGormStore

func NewGormStore(db *gorm.DB) *GormStore

NewGormStore creates a GormStore using the provided gorm.DB. The caller is responsible for running migrations, e.g.

db.AutoMigrate(&inbox.GormMessage{})

func (*GormStore) BatchDelete

func (s *GormStore) BatchDelete(userID string, ids []string) (int64, error)

BatchDelete removes multiple messages by ID and returns the number actually removed.

func (*GormStore) CleanOldUnread

func (s *GormStore) CleanOldUnread(before time.Time) (int64, error)

CleanOldUnread deletes unread messages older than `before` across all users and returns the number removed.

func (*GormStore) Create

func (s *GormStore) Create(msg Message) error

Create persists a new message. ID and timestamps are assigned by the database; the caller-supplied ID field (string) is ignored in favour of the auto-increment primary key.

func (*GormStore) Delete

func (s *GormStore) Delete(userID, id string) error

Delete removes a single message by ID.

func (*GormStore) GetByID

func (s *GormStore) GetByID(userID, id string) (*Message, error)

GetByID retrieves a single message by userID and ID.

func (*GormStore) List

func (s *GormStore) List(userID string, page, size int, filter, titleKeyword, contentKeyword string, startTime, endTime time.Time) (PageResult, error)

List returns a paginated, filtered view of a user's messages.

func (*GormStore) MarkAllRead

func (s *GormStore) MarkAllRead(userID string) error

MarkAllRead marks every unread message for the user as read.

func (*GormStore) MarkRead

func (s *GormStore) MarkRead(userID, id string) error

MarkRead marks a single message as read.

func (*GormStore) UnreadCount

func (s *GormStore) UnreadCount(userID string) (int64, error)

UnreadCount returns the number of unread messages for the user.

type MemoryStore

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

MemoryStore is a goroutine-safe, in-memory implementation of Store. It is primarily intended for testing and small, ephemeral workloads.

func NewMemoryStore

func NewMemoryStore() *MemoryStore

NewMemoryStore creates a new empty MemoryStore.

func (*MemoryStore) BatchDelete

func (m *MemoryStore) BatchDelete(userID string, ids []string) (int64, error)

BatchDelete removes multiple messages by ID and returns the number actually removed.

func (*MemoryStore) CleanOldUnread

func (m *MemoryStore) CleanOldUnread(before time.Time) (int64, error)

CleanOldUnread deletes unread messages older than `before` across all users and returns the number removed.

func (*MemoryStore) Create

func (m *MemoryStore) Create(msg Message) error

Create persists a new message, assigning an ID and timestamps when they are zero.

func (*MemoryStore) Delete

func (m *MemoryStore) Delete(userID, id string) error

Delete removes a single message by ID.

func (*MemoryStore) GetByID

func (m *MemoryStore) GetByID(userID, id string) (*Message, error)

GetByID retrieves a single message by userID and ID.

func (*MemoryStore) List

func (m *MemoryStore) List(userID string, page, size int, filter, titleKeyword, contentKeyword string, startTime, endTime time.Time) (PageResult, error)

List returns a paginated, filtered view of a user's messages.

func (*MemoryStore) MarkAllRead

func (m *MemoryStore) MarkAllRead(userID string) error

MarkAllRead marks every unread message for the user as read.

func (*MemoryStore) MarkRead

func (m *MemoryStore) MarkRead(userID, id string) error

MarkRead marks a single message as read.

func (*MemoryStore) UnreadCount

func (m *MemoryStore) UnreadCount(userID string) (int64, error)

UnreadCount returns the number of unread messages for the user.

type Message

type Message struct {
	ID          string    // unique message ID
	UserID      string    // recipient user ID
	Title       string    // short title / subject
	Content     string    // message body
	ActionURL   string    // optional action link
	ActionLabel string    // optional action button label
	Read        bool      // whether the message has been read
	CreatedAt   time.Time // creation time
	UpdatedAt   time.Time // last update time
}

Message is a single in-app inbox notification addressed to a user.

type PageResult

type PageResult struct {
	List        []Message // messages for the requested page
	Total       int64     // total messages matching the filter
	TotalUnread int64     // total unread messages for the user
	TotalRead   int64     // total read messages for the user
}

PageResult is the paginated result returned by List.

type Store

type Store interface {
	// Create persists a new message. The store is responsible for
	// assigning an ID and timestamps when they are zero.
	Create(msg Message) error

	// GetByID retrieves a single message by userID and ID.
	GetByID(userID, id string) (*Message, error)

	// List returns a paginated, filtered view of a user's messages.
	// filter is one of FilterAll / FilterUnread / FilterRead. The
	// titleKeyword and contentKeyword perform substring matches when
	// non-empty; startTime / endTime bound CreatedAt when non-zero.
	List(userID string, page, size int, filter, titleKeyword, contentKeyword string, startTime, endTime time.Time) (PageResult, error)

	// UnreadCount returns the number of unread messages for the user.
	UnreadCount(userID string) (int64, error)

	// MarkRead marks a single message as read.
	MarkRead(userID, id string) error

	// MarkAllRead marks every unread message for the user as read.
	MarkAllRead(userID string) error

	// Delete removes a single message by ID.
	Delete(userID, id string) error

	// BatchDelete removes multiple messages by ID and returns the
	// number actually removed.
	BatchDelete(userID string, ids []string) (int64, error)

	// CleanOldUnread deletes unread messages older than `before` across
	// all users and returns the number removed.
	CleanOldUnread(before time.Time) (int64, error)
}

Store is the pluggable persistence interface for inbox messages. Applications provide a concrete implementation (e.g. gorm-backed); the in-box package ships an in-memory implementation for testing.

Jump to

Keyboard shortcuts

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