Documentation
¶
Overview ¶
Package notifications provides the Notifications Service for the Lesser project's API alignment. This service handles all notification operations including creation, reading, clearing, and notification preferences. It emits appropriate events for real-time streaming.
Index ¶
- Variables
- type ClearCommand
- type ClearResult
- type CreateNotificationCommand
- type GetNotificationQuery
- type GroupedNotification
- type GroupedNotificationsService
- func (gns *GroupedNotificationsService) GenerateGroupSummary(group *GroupedNotification) string
- func (gns *GroupedNotificationsService) GroupNotifications(_ context.Context, notifications []*models.Notification, ...) ([]*GroupedNotification, error)
- func (gns *GroupedNotificationsService) MarkGroupAsRead(ctx context.Context, group *GroupedNotification, ...) error
- type GroupingStrategy
- type ListNotificationsQuery
- type MarkAsReadCommand
- type NotificationAccount
- type NotificationListResult
- type NotificationResult
- type NotificationStatus
- type NotificationSummary
- type Service
- func (s *Service) ClearNotifications(ctx context.Context, cmd *ClearCommand) (*ClearResult, error)
- func (s *Service) CreateNotification(ctx context.Context, cmd *CreateNotificationCommand) (*NotificationResult, error)
- func (s *Service) DispatchPushForNotification(ctx context.Context, notification *models.Notification)
- func (s *Service) GetNotification(ctx context.Context, query *GetNotificationQuery) (*models.Notification, error)
- func (s *Service) ListNotifications(ctx context.Context, query *ListNotificationsQuery) (*NotificationListResult, error)
- func (s *Service) MarkAsRead(ctx context.Context, cmd *MarkAsReadCommand) (*NotificationResult, error)
Constants ¶
This section is empty.
Variables ¶
var ( // General validation errors ErrValidationFailed = errors.ValidationFailedWithField("notification") ErrInvalidInput = errors.NewValidationError("input", "invalid") // Notification operation errors ErrNotificationNotFound = errors.NewAppError(errors.CodeNotFound, errors.CategoryBusiness, "notification not found") ErrNotificationAccessDenied = errors.AccessDeniedForResource("notification", "unknown") ErrNotificationCreateFailed = errors.FailedToCreate("notification", stdErrors.New("failed to create notification")) ErrNotificationUpdateFailed = errors.FailedToUpdate("notification", stdErrors.New("failed to update notification")) ErrNotificationClearFailed = errors.ProcessingFailed("notification clearing", stdErrors.New("notification clearing failed")) ErrNotificationQueryFailed = errors.FailedToQuery("notifications", stdErrors.New("failed to query notifications")) ErrNoClearCriteria = errors.NewValidationError("clear_criteria", "no criteria specified") ErrNoClearMethodSpecified = errors.NewValidationError("clear_method", "at least one criteria must be specified") ErrUnreadCountFailed = errors.FailedToQuery("unread count", stdErrors.New("failed to get unread count")) ErrCountsByTypeFailed = errors.FailedToQuery("counts by type", stdErrors.New("failed to get counts by type")) )
Service-specific errors for the notifications package
Functions ¶
This section is empty.
Types ¶
type ClearCommand ¶
type ClearCommand struct {
UserID string `json:"user_id" validate:"required"`
NotificationIDs []string `json:"notification_ids"` // Clear specific notifications
Types []string `json:"types"` // Clear notifications of specific types
ClearAll bool `json:"clear_all"` // Clear all notifications
OlderThanSeconds int64 `json:"older_than_seconds"` // Clear notifications older than N seconds
}
ClearCommand contains data needed to clear notifications
type ClearResult ¶
type ClearResult struct {
ClearedCount int64 `json:"cleared_count"`
Events []*streaming.Event `json:"events"`
}
ClearResult contains the results of a clear operation
type CreateNotificationCommand ¶
type CreateNotificationCommand struct {
UserID string `json:"user_id" validate:"required"` // Recipient user ID
Type string `json:"type" validate:"required"` // Notification type
ActorID string `json:"actor_id" validate:"required"` // Who triggered the notification
ActorType string `json:"actor_type"` // Type of actor (user, remote_actor)
TargetID string `json:"target_id"` // What the notification is about
TargetType string `json:"target_type"` // Type of target (status, user, account)
Title string `json:"title"` // Notification title
Body string `json:"body"` // Notification body
Data map[string]interface{} `json:"data"` // Additional data
GroupKey string `json:"group_key"` // Custom group key for consolidation
}
CreateNotificationCommand contains all data needed to create a new notification
type GetNotificationQuery ¶
type GetNotificationQuery struct {
NotificationID string `json:"notification_id" validate:"required"`
UserID string `json:"user_id" validate:"required"` // For privacy checks
}
GetNotificationQuery contains parameters for retrieving a single notification
type GroupedNotification ¶
type GroupedNotification struct {
ID string `json:"id"`
Type string `json:"type"`
GroupKey string `json:"group_key"`
Count int `json:"count"`
LatestCreatedAt time.Time `json:"latest_created_at"`
EarliestCreatedAt time.Time `json:"earliest_created_at"`
IsRead bool `json:"is_read"`
SampleAccounts []NotificationAccount `json:"sample_accounts"`
TargetStatus *NotificationStatus `json:"status,omitempty"`
MostRecentNotif *models.Notification `json:"most_recent_notification"`
AllNotifications []*models.Notification `json:"all_notifications,omitempty"`
}
GroupedNotification represents a group of similar notifications
type GroupedNotificationsService ¶
type GroupedNotificationsService struct {
// contains filtered or unexported fields
}
GroupedNotificationsService provides advanced notification grouping functionality
func NewGroupedNotificationsService ¶
func NewGroupedNotificationsService(logger *zap.Logger) *GroupedNotificationsService
NewGroupedNotificationsService creates a new grouped notifications service
func (*GroupedNotificationsService) GenerateGroupSummary ¶
func (gns *GroupedNotificationsService) GenerateGroupSummary( group *GroupedNotification, ) string
GenerateGroupSummary generates a human-readable summary for a group
func (*GroupedNotificationsService) GroupNotifications ¶
func (gns *GroupedNotificationsService) GroupNotifications( _ context.Context, notifications []*models.Notification, strategy *GroupingStrategy, ) ([]*GroupedNotification, error)
GroupNotifications groups similar notifications together
func (*GroupedNotificationsService) MarkGroupAsRead ¶
func (gns *GroupedNotificationsService) MarkGroupAsRead( ctx context.Context, group *GroupedNotification, markReadFunc func(context.Context, string) error, ) error
MarkGroupAsRead marks all notifications in a group as read
type GroupingStrategy ¶
type GroupingStrategy struct {
TimeWindow time.Duration `json:"time_window"` // Group notifications within this time window
MaxGroupSize int `json:"max_group_size"` // Maximum notifications per group
MinGroupSize int `json:"min_group_size"` // Minimum to consider grouping
SampleSize int `json:"sample_size"` // Number of sample accounts to include
GroupByType bool `json:"group_by_type"` // Group by notification type
GroupByTarget bool `json:"group_by_target"` // Group by target object
}
GroupingStrategy defines how notifications should be grouped
func DefaultGroupingStrategy ¶
func DefaultGroupingStrategy() *GroupingStrategy
DefaultGroupingStrategy returns the default grouping strategy
type ListNotificationsQuery ¶
type ListNotificationsQuery struct {
UserID string `json:"user_id" validate:"required"`
Types []string `json:"types"` // Filter by notification types
ExcludeTypes []string `json:"exclude_types"` // Exclude specific types
OnlyUnread bool `json:"only_unread"` // Only unread notifications
IncludeRead bool `json:"include_read"` // Include read notifications (default: true)
GroupedOnly bool `json:"grouped_only"` // Only grouped notifications
ActorID string `json:"actor_id"` // Filter by specific actor
TargetType string `json:"target_type"` // Filter by target type
Pagination interfaces.PaginationOptions `json:"pagination"`
}
ListNotificationsQuery contains parameters for listing notifications with filters
type MarkAsReadCommand ¶
type MarkAsReadCommand struct {
NotificationID string `json:"notification_id" validate:"required"`
UserID string `json:"user_id" validate:"required"`
}
MarkAsReadCommand contains data needed to mark a notification as read
type NotificationAccount ¶
type NotificationAccount struct {
ID string `json:"id"`
Username string `json:"username"`
DisplayName string `json:"display_name"`
Avatar string `json:"avatar"`
IsBot bool `json:"bot"`
CreatedAt time.Time `json:"created_at"`
}
NotificationAccount represents an account in grouped notifications
type NotificationListResult ¶
type NotificationListResult struct {
Notifications []*models.Notification `json:"notifications"`
Pagination *interfaces.PaginatedResult[*models.Notification] `json:"pagination"`
Summary *NotificationSummary `json:"summary"`
Events []*streaming.Event `json:"events"`
}
NotificationListResult contains multiple notifications and pagination information
type NotificationResult ¶
type NotificationResult struct {
Notification *models.Notification `json:"notification"`
Events []*streaming.Event `json:"events"`
}
NotificationResult contains a notification and associated events that were emitted
type NotificationStatus ¶
type NotificationStatus struct {
ID string `json:"id"`
Content string `json:"content"`
CreatedAt time.Time `json:"created_at"`
URL string `json:"url"`
Visibility string `json:"visibility"`
}
NotificationStatus represents a status in grouped notifications
type NotificationSummary ¶
type NotificationSummary struct {
TotalCount int64 `json:"total_count"`
UnreadCount int64 `json:"unread_count"`
CountsByType map[string]int64 `json:"counts_by_type"`
LastNotificationAt *time.Time `json:"last_notification_at,omitempty"`
}
NotificationSummary provides summary statistics
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service provides notification operations
func NewService ¶
func NewService( notificationRepo interfaces.NotificationRepository, accountRepo interfaces.AccountRepository, publisher streaming.Publisher, logger *zap.Logger, domainName string, pushService pushQueue, ) *Service
NewService creates a new Notifications Service with the required dependencies
func (*Service) ClearNotifications ¶
func (s *Service) ClearNotifications(ctx context.Context, cmd *ClearCommand) (*ClearResult, error)
ClearNotifications clears notifications based on the specified criteria and emits events
func (*Service) CreateNotification ¶
func (s *Service) CreateNotification(ctx context.Context, cmd *CreateNotificationCommand) (*NotificationResult, error)
CreateNotification creates a new notification, stores it, and emits events
func (*Service) DispatchPushForNotification ¶
func (s *Service) DispatchPushForNotification(ctx context.Context, notification *models.Notification)
DispatchPushForNotification replays the push queue logic for an already persisted notification.
func (*Service) GetNotification ¶
func (s *Service) GetNotification(ctx context.Context, query *GetNotificationQuery) (*models.Notification, error)
GetNotification retrieves a single notification with privacy checks
func (*Service) ListNotifications ¶
func (s *Service) ListNotifications(ctx context.Context, query *ListNotificationsQuery) (*NotificationListResult, error)
ListNotifications retrieves notifications based on filters and pagination
func (*Service) MarkAsRead ¶
func (s *Service) MarkAsRead(ctx context.Context, cmd *MarkAsReadCommand) (*NotificationResult, error)
MarkAsRead marks a notification as read and emits events