reminder

package
v0.72.0 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package reminder provides reminder management for schedules and todos.

Package reminder provides reminder management for schedules and todos.

Package reminder provides reminder management for schedules and todos.

Package reminder provides reminder management for schedules and todos.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AppNotification

type AppNotification struct {
	CreatedAt time.Time      `json:"created_at"`
	Metadata  map[string]any `json:"metadata,omitempty"`
	ID        string         `json:"id"`
	Type      string         `json:"type"`
	Title     string         `json:"title"`
	Message   string         `json:"message"`
	UserID    int32          `json:"user_id"`
	Read      bool           `json:"read"`
}

AppNotification represents an in-app notification.

type AppNotificationSender

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

AppNotificationSender sends in-app notifications.

func NewAppNotificationSender

func NewAppNotificationSender(store AppNotificationStore) *AppNotificationSender

NewAppNotificationSender creates a new app notification sender.

func (*AppNotificationSender) Name

func (s *AppNotificationSender) Name() string

Name returns the sender name.

func (*AppNotificationSender) Send

func (s *AppNotificationSender) Send(ctx context.Context, userID int32, message string, metadata map[string]any) error

Send sends an in-app notification.

type AppNotificationStore

type AppNotificationStore interface {
	CreateNotification(ctx context.Context, notification *AppNotification) error
}

AppNotificationStore defines storage for in-app notifications.

type Channel

type Channel string

Channel defines notification channel types.

const (
	ChannelApp     Channel = "app"
	ChannelEmail   Channel = "email"
	ChannelWebhook Channel = "webhook"
)

type ChannelSender

type ChannelSender interface {
	Send(ctx context.Context, userID int32, message string, metadata map[string]any) error
	Name() string
}

ChannelSender defines the interface for sending notifications.

type CreateReminderRequest

type CreateReminderRequest struct {
	TriggerAt   time.Time
	Type        ReminderType
	TargetID    string
	Message     string
	Channels    []Channel
	LeadMinutes int
	UserID      int32
}

CreateReminderRequest represents a request to create a reminder.

type EmailConfig

type EmailConfig struct {
	SMTPHost     string
	Username     string
	Password     string
	FromAddress  string
	FromName     string
	TemplateHTML string
	SMTPPort     int
}

EmailConfig holds email configuration.

type EmailSender

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

EmailSender sends email notifications.

func NewEmailSender

func NewEmailSender(config EmailConfig, resolver UserEmailResolver) *EmailSender

NewEmailSender creates a new email sender.

func (*EmailSender) Name

func (s *EmailSender) Name() string

Name returns the sender name.

func (*EmailSender) Send

func (s *EmailSender) Send(ctx context.Context, userID int32, message string, metadata map[string]any) error

Send sends an email notification.

type HabitAnalyzer

type HabitAnalyzer interface {
	GetUserHabits(ctx context.Context, userID int32) (*habit.UserHabits, error)
}

HabitAnalyzer provides user habit analysis.

type HealthCheck

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

HealthCheck provides health check for the scheduler.

func NewHealthCheck

func NewHealthCheck(scheduler *Scheduler) *HealthCheck

NewHealthCheck creates a new health check for the scheduler.

func (*HealthCheck) Check

func (h *HealthCheck) Check() HealthStatus

Check returns the health status.

type HealthStatus

type HealthStatus struct {
	LastCheck  time.Time `json:"last_check"`
	CheckCount int64     `json:"check_count"`
	Healthy    bool      `json:"healthy"`
}

HealthStatus represents the health of the scheduler.

type Integrator

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

Integrator provides integration between reminders and other entities.

func NewIntegrator

func NewIntegrator(service *Service, habitAnalyzer HabitAnalyzer) *Integrator

NewIntegrator creates a new reminder integrator.

func (*Integrator) BatchCreateForSchedules

func (i *Integrator) BatchCreateForSchedules(ctx context.Context, userID int32, schedules []*ScheduleInfo) ([]*Reminder, error)

BatchCreateForSchedules creates reminders for multiple schedules.

func (*Integrator) CreateSmartReminder

func (i *Integrator) CreateSmartReminder(ctx context.Context, userID int32, suggestion SmartSuggestion) (*Reminder, error)

CreateSmartReminder creates an AI-suggested reminder based on context.

func (*Integrator) GetUpcomingReminders

func (i *Integrator) GetUpcomingReminders(ctx context.Context, userID int32) ([]*Reminder, error)

GetUpcomingReminders gets all upcoming reminders for a user.

func (*Integrator) OnScheduleCreated

func (i *Integrator) OnScheduleCreated(ctx context.Context, userID int32, schedule *ScheduleInfo) (*Reminder, error)

OnScheduleCreated handles schedule creation event.

func (*Integrator) OnScheduleDeleted

func (i *Integrator) OnScheduleDeleted(ctx context.Context, scheduleID string) error

OnScheduleDeleted handles schedule deletion event.

func (*Integrator) OnScheduleUpdated

func (i *Integrator) OnScheduleUpdated(ctx context.Context, userID int32, schedule *ScheduleInfo) (*Reminder, error)

OnScheduleUpdated handles schedule update event.

func (*Integrator) OnTodoCompleted

func (i *Integrator) OnTodoCompleted(ctx context.Context, todoID string) error

OnTodoCompleted handles todo completion event.

func (*Integrator) OnTodoCreated

func (i *Integrator) OnTodoCreated(ctx context.Context, userID int32, todo *TodoInfo) (*Reminder, error)

OnTodoCreated handles todo creation event.

func (*Integrator) OnTodoDeleted

func (i *Integrator) OnTodoDeleted(ctx context.Context, todoID string) error

OnTodoDeleted handles todo deletion event.

func (*Integrator) OnTodoUpdated

func (i *Integrator) OnTodoUpdated(ctx context.Context, userID int32, todo *TodoInfo) (*Reminder, error)

OnTodoUpdated handles todo update event.

func (*Integrator) SyncScheduleReminders

func (i *Integrator) SyncScheduleReminders(ctx context.Context, userID int32, schedules []*ScheduleInfo) error

SyncScheduleReminders syncs reminders with schedule changes.

type MemoryAppNotificationStore

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

MemoryAppNotificationStore is an in-memory implementation for testing.

func NewMemoryAppNotificationStore

func NewMemoryAppNotificationStore() *MemoryAppNotificationStore

NewMemoryAppNotificationStore creates a new in-memory notification store.

func (*MemoryAppNotificationStore) CreateNotification

func (s *MemoryAppNotificationStore) CreateNotification(ctx context.Context, notification *AppNotification) error

CreateNotification stores a notification.

func (*MemoryAppNotificationStore) GetAll

GetAll returns all notifications (for testing).

type MemoryStore

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

MemoryStore is an in-memory implementation of ReminderStore for testing.

func NewMemoryStore

func NewMemoryStore() *MemoryStore

NewMemoryStore creates a new in-memory reminder store.

func (*MemoryStore) Create

func (s *MemoryStore) Create(ctx context.Context, reminder *Reminder) error

Create stores a new reminder.

func (*MemoryStore) Delete

func (s *MemoryStore) Delete(ctx context.Context, id string) error

Delete removes a reminder.

func (*MemoryStore) Get

func (s *MemoryStore) Get(ctx context.Context, id string) (*Reminder, error)

Get retrieves a reminder by ID.

func (*MemoryStore) GetByTarget

func (s *MemoryStore) GetByTarget(ctx context.Context, targetID string) ([]*Reminder, error)

GetByTarget retrieves all reminders for a target.

func (*MemoryStore) GetByUser

func (s *MemoryStore) GetByUser(ctx context.Context, userID int32, status ReminderStatus) ([]*Reminder, error)

GetByUser retrieves all reminders for a user with optional status filter.

func (*MemoryStore) GetDueReminders

func (s *MemoryStore) GetDueReminders(ctx context.Context, before time.Time) ([]*Reminder, error)

GetDueReminders retrieves all pending reminders due before the given time.

func (*MemoryStore) MarkFailed

func (s *MemoryStore) MarkFailed(ctx context.Context, id string, reason string) error

MarkFailed marks a reminder as failed.

func (*MemoryStore) MarkSent

func (s *MemoryStore) MarkSent(ctx context.Context, id string) error

MarkSent marks a reminder as sent.

func (*MemoryStore) Update

func (s *MemoryStore) Update(ctx context.Context, reminder *Reminder) error

Update updates an existing reminder.

type MetricsCollector

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

MetricsCollector collects scheduler metrics.

func NewMetricsCollector

func NewMetricsCollector() *MetricsCollector

NewMetricsCollector creates a new metrics collector.

func (*MetricsCollector) GetStats

func (m *MetricsCollector) GetStats() Stats

GetStats returns current statistics.

func (*MetricsCollector) RecordFailed

func (m *MetricsCollector) RecordFailed(count int)

RecordFailed records failed reminders.

func (*MetricsCollector) RecordProcessed

func (m *MetricsCollector) RecordProcessed(count int)

RecordProcessed records processed reminders.

type MockEmailResolver

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

MockEmailResolver is a mock implementation of UserEmailResolver.

func NewMockEmailResolver

func NewMockEmailResolver() *MockEmailResolver

NewMockEmailResolver creates a new mock email resolver.

func (*MockEmailResolver) GetEmail

func (r *MockEmailResolver) GetEmail(ctx context.Context, userID int32) (string, error)

GetEmail returns the email for a user.

func (*MockEmailResolver) SetEmail

func (r *MockEmailResolver) SetEmail(userID int32, email string)

SetEmail sets an email for a user.

type MockNotifier

type MockNotifier struct {
	SentMessages []SentMessage
	ShouldFail   bool
	// contains filtered or unexported fields
}

MockNotifier is a mock implementation of Notifier for testing.

func NewMockNotifier

func NewMockNotifier() *MockNotifier

NewMockNotifier creates a new mock notifier.

func (*MockNotifier) Clear

func (n *MockNotifier) Clear()

Clear clears all sent messages.

func (*MockNotifier) GetSentCount

func (n *MockNotifier) GetSentCount() int

GetSentCount returns the number of messages sent.

func (*MockNotifier) Send

func (n *MockNotifier) Send(ctx context.Context, userID int32, channel Channel, message string) error

Send records a sent message.

type NotificationDispatcher

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

NotificationDispatcher routes notifications to appropriate channels.

func NewNotificationDispatcher

func NewNotificationDispatcher() *NotificationDispatcher

NewNotificationDispatcher creates a new notification dispatcher.

func (*NotificationDispatcher) Broadcast

func (d *NotificationDispatcher) Broadcast(ctx context.Context, userID int32, message string) []error

Broadcast sends a notification through all registered channels.

func (*NotificationDispatcher) Register

func (d *NotificationDispatcher) Register(channel Channel, sender ChannelSender)

Register registers a channel sender.

func (*NotificationDispatcher) Send

func (d *NotificationDispatcher) Send(ctx context.Context, userID int32, channel Channel, message string) error

Send sends a notification through the specified channel.

func (*NotificationDispatcher) SendWithMetadata

func (d *NotificationDispatcher) SendWithMetadata(ctx context.Context, userID int32, channel Channel, message string, metadata map[string]any) error

SendWithMetadata sends a notification with additional metadata.

type Notifier

type Notifier interface {
	Send(ctx context.Context, userID int32, channel Channel, message string) error
}

Notifier defines the notification interface.

type Reminder

type Reminder struct {
	TriggerAt time.Time      `json:"trigger_at"`
	CreatedAt time.Time      `json:"created_at"`
	SentAt    *time.Time     `json:"sent_at,omitempty"`
	Metadata  map[string]any `json:"metadata,omitempty"`
	ID        string         `json:"id"`
	Type      ReminderType   `json:"type"`
	TargetID  string         `json:"target_id"`
	Message   string         `json:"message"`
	Status    ReminderStatus `json:"status"`
	Channels  []Channel      `json:"channels"`
	UserID    int32          `json:"user_id"`
}

Reminder represents a reminder entity.

type ReminderStatus

type ReminderStatus string

ReminderStatus defines the status of a reminder.

const (
	StatusPending   ReminderStatus = "pending"
	StatusSent      ReminderStatus = "sent"
	StatusCancelled ReminderStatus = "cancelled"
	StatusFailed    ReminderStatus = "failed"
)

type ReminderStore

type ReminderStore interface {
	Create(ctx context.Context, reminder *Reminder) error
	Get(ctx context.Context, id string) (*Reminder, error)
	GetByTarget(ctx context.Context, targetID string) ([]*Reminder, error)
	GetDueReminders(ctx context.Context, before time.Time) ([]*Reminder, error)
	GetByUser(ctx context.Context, userID int32, status ReminderStatus) ([]*Reminder, error)
	Update(ctx context.Context, reminder *Reminder) error
	Delete(ctx context.Context, id string) error
	MarkSent(ctx context.Context, id string) error
	MarkFailed(ctx context.Context, id string, reason string) error
}

ReminderStore defines the storage interface for reminders.

type ReminderType

type ReminderType string

ReminderType defines the type of reminder.

const (
	ReminderTypeSchedule ReminderType = "schedule"
	ReminderTypeTodo     ReminderType = "todo"
	ReminderTypeSmart    ReminderType = "smart"
)

type ScheduleInfo

type ScheduleInfo struct {
	ID        string
	Title     string
	StartTime time.Time
	Location  string
}

ScheduleInfo contains schedule information for reminder creation.

type Scheduler

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

Scheduler runs background tasks for processing due reminders.

func NewScheduler

func NewScheduler(service *Service, config SchedulerConfig) *Scheduler

NewScheduler creates a new reminder scheduler.

func (*Scheduler) EnableTestMode

func (s *Scheduler) EnableTestMode() <-chan int

EnableTestMode enables test mode with a channel for processed counts.

func (*Scheduler) IsRunning

func (s *Scheduler) IsRunning() bool

IsRunning returns whether the scheduler is running.

func (*Scheduler) RunOnce

func (s *Scheduler) RunOnce(ctx context.Context) (int, error)

RunOnce processes due reminders once (for manual triggering).

func (*Scheduler) SetLogger

func (s *Scheduler) SetLogger(logger *slog.Logger)

SetLogger sets a custom logger.

func (*Scheduler) Start

func (s *Scheduler) Start(ctx context.Context) error

Start begins the scheduler loop.

func (*Scheduler) Stop

func (s *Scheduler) Stop()

Stop gracefully stops the scheduler.

type SchedulerConfig

type SchedulerConfig struct {
	Interval   time.Duration // How often to check for due reminders
	MaxRetries int           // Max retries for failed reminders
	RetryDelay time.Duration // Delay between retries
	BatchSize  int           // Max reminders to process per cycle
}

SchedulerConfig holds configuration for the scheduler.

func DefaultSchedulerConfig

func DefaultSchedulerConfig() SchedulerConfig

DefaultSchedulerConfig returns default scheduler configuration.

type SentMessage

type SentMessage struct {
	SentAt  time.Time
	Channel Channel
	Message string
	UserID  int32
}

SentMessage represents a message that was sent.

type Service

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

Service provides reminder management functionality.

func NewService

func NewService(store ReminderStore, notifier Notifier) *Service

NewService creates a new reminder service.

func (*Service) Cancel

func (s *Service) Cancel(ctx context.Context, id string) error

Cancel cancels a pending reminder.

func (*Service) CancelByTarget

func (s *Service) CancelByTarget(ctx context.Context, targetID string) error

CancelByTarget cancels all pending reminders for a target.

func (*Service) CreateCustom

func (s *Service) CreateCustom(ctx context.Context, req *CreateReminderRequest) (*Reminder, error)

CreateCustom creates a custom reminder.

func (*Service) CreateForSchedule

func (s *Service) CreateForSchedule(ctx context.Context, userID int32, schedule *ScheduleInfo, leadMinutes int) (*Reminder, error)

CreateForSchedule creates a reminder for a schedule.

func (*Service) GetReminder

func (s *Service) GetReminder(ctx context.Context, id string) (*Reminder, error)

GetReminder gets a specific reminder.

func (*Service) GetUserReminders

func (s *Service) GetUserReminders(ctx context.Context, userID int32, status ReminderStatus) ([]*Reminder, error)

GetUserReminders gets all reminders for a user.

func (*Service) ProcessDueReminders

func (s *Service) ProcessDueReminders(ctx context.Context) (int, error)

ProcessDueReminders processes all reminders that are due.

func (*Service) SetDefaultChannels

func (s *Service) SetDefaultChannels(channels []Channel)

SetDefaultChannels sets the default notification channels.

func (*Service) SetDefaultLeadTime

func (s *Service) SetDefaultLeadTime(minutes int)

SetDefaultLeadTime sets the default lead time for reminders.

type SmartSuggestion

type SmartSuggestion struct {
	SuggestedTime   time.Time
	RelatedEntityID string
	Message         string
	Reason          string
	Confidence      float64
}

SmartSuggestion represents an AI-generated reminder suggestion.

type Stats

type Stats struct {
	LastRunAt      time.Time `json:"last_run_at"`
	TotalProcessed int64     `json:"total_processed"`
	TotalFailed    int64     `json:"total_failed"`
	AverageLatency float64   `json:"average_latency_ms"`
}

Stats holds scheduler statistics.

type TodoInfo

type TodoInfo struct {
	ID       string
	Title    string
	DueTime  *time.Time
	Priority string // high, medium, low
}

TodoInfo contains todo information for reminder creation.

type UserEmailResolver

type UserEmailResolver interface {
	GetEmail(ctx context.Context, userID int32) (string, error)
}

UserEmailResolver resolves user ID to email address.

type WebhookConfig

type WebhookConfig struct {
	Headers       map[string]string
	URL           string
	Secret        string
	Timeout       time.Duration
	RetryAttempts int
}

WebhookConfig holds webhook configuration.

type WebhookPayload

type WebhookPayload struct {
	Timestamp time.Time      `json:"timestamp"`
	Metadata  map[string]any `json:"metadata,omitempty"`
	Event     string         `json:"event"`
	Message   string         `json:"message"`
	UserID    int32          `json:"user_id"`
}

WebhookPayload represents the webhook request body.

type WebhookSender

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

WebhookSender sends webhook notifications.

func NewWebhookSender

func NewWebhookSender(config WebhookConfig) *WebhookSender

NewWebhookSender creates a new webhook sender.

func (*WebhookSender) Name

func (s *WebhookSender) Name() string

Name returns the sender name.

func (*WebhookSender) Send

func (s *WebhookSender) Send(ctx context.Context, userID int32, message string, metadata map[string]any) error

Send sends a webhook notification.

type Worker

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

Worker handles reminder processing with retry logic.

func NewWorker

func NewWorker(store ReminderStore, notifier Notifier, maxRetries int) *Worker

NewWorker creates a new reminder worker.

func (*Worker) ProcessBatch

func (w *Worker) ProcessBatch(ctx context.Context, reminders []*Reminder) (processed int, failed int)

ProcessBatch processes a batch of reminders.

func (*Worker) ProcessReminder

func (w *Worker) ProcessReminder(ctx context.Context, reminder *Reminder) error

ProcessReminder processes a single reminder with retry logic.

Jump to

Keyboard shortcuts

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