Documentation
¶
Index ¶
- Constants
- func IsValidFilter(filter string) bool
- type Channel
- type GormMessage
- type GormStore
- func (s *GormStore) BatchDelete(userID string, ids []string) (int64, error)
- func (s *GormStore) CleanOldUnread(before time.Time) (int64, error)
- func (s *GormStore) Create(msg Message) error
- func (s *GormStore) Delete(userID, id string) error
- func (s *GormStore) GetByID(userID, id string) (*Message, error)
- func (s *GormStore) List(userID string, page, size int, filter, titleKeyword, contentKeyword string, ...) (PageResult, error)
- func (s *GormStore) MarkAllRead(userID string) error
- func (s *GormStore) MarkRead(userID, id string) error
- func (s *GormStore) UnreadCount(userID string) (int64, error)
- type MemoryStore
- func (m *MemoryStore) BatchDelete(userID string, ids []string) (int64, error)
- func (m *MemoryStore) CleanOldUnread(before time.Time) (int64, error)
- func (m *MemoryStore) Create(msg Message) error
- func (m *MemoryStore) Delete(userID, id string) error
- func (m *MemoryStore) GetByID(userID, id string) (*Message, error)
- func (m *MemoryStore) List(userID string, page, size int, filter, titleKeyword, contentKeyword string, ...) (PageResult, error)
- func (m *MemoryStore) MarkAllRead(userID string) error
- func (m *MemoryStore) MarkRead(userID, id string) error
- func (m *MemoryStore) UnreadCount(userID string) (int64, error)
- type Message
- type PageResult
- type Store
Constants ¶
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 ¶
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 ¶
NewChannel creates an inbox Channel backed by the given Store. The channel is enabled by default.
func (*Channel) Send ¶
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 ¶
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 ¶
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 ¶
BatchDelete removes multiple messages by ID and returns the number actually removed.
func (*GormStore) CleanOldUnread ¶
CleanOldUnread deletes unread messages older than `before` across all users and returns the number removed.
func (*GormStore) Create ¶
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) 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 ¶
MarkAllRead marks every unread message for the user as read.
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.