db

package
v0.0.0-...-056225c Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2025 License: AGPL-3.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrMessageNotFound = errors.New("message not found")

ErrMessageNotFound is returned when a requested message cannot be found.

View Source
var ErrThreadNotFound = errors.New("thread not found")

ErrThreadNotFound is returned when a requested thread cannot be found.

View Source
var ErrUserSettingsNotFound = errors.New("user settings not found")

ErrUserSettingsNotFound is returned when user settings cannot be found.

Functions

func CloseConnection

func CloseConnection(pool *pgxpool.Pool)

CloseConnection closes the given database connection pool.

func EnrichThreadsWithFirstMessageFromAddress

func EnrichThreadsWithFirstMessageFromAddress(ctx context.Context, pool *pgxpool.Pool, threads []*models.Thread) error

EnrichThreadsWithFirstMessageFromAddress enriches threads with the first message's from_address. This is useful for search results and other cases where threads don't have messages populated.

func EnrichThreadsWithPreviewAndAttachments

func EnrichThreadsWithPreviewAndAttachments(ctx context.Context, pool *pgxpool.Pool, threads []*models.Thread) error

EnrichThreadsWithPreviewAndAttachments enriches threads with preview snippet, attachment info, message count, and last sent date. This is useful for search results and other cases where threads don't have these fields populated.

func GetAttachmentByID

func GetAttachmentByID(ctx context.Context, pool *pgxpool.Pool, attachmentID string) (*models.Attachment, error)

GetAttachmentByID returns an attachment by its database ID.

func GetAttachmentsForMessage

func GetAttachmentsForMessage(ctx context.Context, pool *pgxpool.Pool, messageID string) ([]*models.Attachment, error)

GetAttachmentsForMessage returns all attachments for a message.

func GetAttachmentsForMessages

func GetAttachmentsForMessages(ctx context.Context, pool *pgxpool.Pool, messageIDs []string) (map[string][]*models.Attachment, error)

GetAttachmentsForMessages returns all attachments for multiple messages in a single query. Returns a map from message ID to a slice of attachments.

func GetMessageByID

func GetMessageByID(ctx context.Context, pool *pgxpool.Pool, messageID string) (*models.Message, error)

GetMessageByID returns a message by its database ID.

func GetMessageByMessageID

func GetMessageByMessageID(ctx context.Context, pool *pgxpool.Pool, userID, messageID string) (*models.Message, error)

GetMessageByMessageID returns a message by its Message-ID header.

func GetMessageByUID

func GetMessageByUID(ctx context.Context, pool *pgxpool.Pool, userID, folderName string, imapUID int64) (*models.Message, error)

GetMessageByUID returns a message by its IMAP UID and folder.

func GetMessagesForThread

func GetMessagesForThread(ctx context.Context, pool *pgxpool.Pool, threadID string) ([]*models.Message, error)

GetMessagesForThread returns all messages for a thread.

func GetOrCreateUser

func GetOrCreateUser(ctx context.Context, pool *pgxpool.Pool, email string) (string, error)

GetOrCreateUser returns the user's id for the given email. If no user exists with that email, it creates a new one.

func GetThreadByID

func GetThreadByID(ctx context.Context, pool *pgxpool.Pool, threadID string) (*models.Thread, error)

GetThreadByID returns a thread by its database ID.

func GetThreadByStableID

func GetThreadByStableID(ctx context.Context, pool *pgxpool.Pool, userID, stableThreadID string) (*models.Thread, error)

GetThreadByStableID returns a thread by its stable thread ID.

func GetThreadCountForFolder

func GetThreadCountForFolder(ctx context.Context, pool *pgxpool.Pool, userID, folderName string) (int, error)

GetThreadCountForFolder returns the total count of threads for a specific folder. Uses the materialized count from folder_sync_timestamps if available, otherwise falls back to calculating it on the fly.

func GetThreadsForFolder

func GetThreadsForFolder(ctx context.Context, pool *pgxpool.Pool, userID, folderName string, limit, offset int) ([]*models.Thread, error)

GetThreadsForFolder returns threads for a specific folder. It returns threads that have at least one message in the specified folder. Each thread includes message_count (number of messages), last_sent_at (most recent message date), preview_snippet, has_attachments, and first_message_from_address for efficient list view rendering.

func GetUserSettings

func GetUserSettings(ctx context.Context, pool *pgxpool.Pool, userID string) (*models.UserSettings, error)

GetUserSettings returns the user settings for the given user.

func NewConnection

func NewConnection(ctx context.Context, cfg *config.Config) (*pgxpool.Pool, error)

NewConnection creates a new PostgreSQL connection pool with the given configuration.

func SaveAttachment

func SaveAttachment(ctx context.Context, pool *pgxpool.Pool, attachment *models.Attachment) error

SaveAttachment saves an attachment to the database.

func SaveMessage

func SaveMessage(ctx context.Context, pool *pgxpool.Pool, message *models.Message) error

SaveMessage saves or updates a message in the database. If a message with the same Message-ID already exists for this user in a DIFFERENT folder, it is skipped to avoid duplicates (e.g., when you email yourself, the same message appears in both Inbox and Sent with the same Message-ID). Updates to the same folder+UID are allowed (for refreshing message data).

func SaveThread

func SaveThread(ctx context.Context, pool *pgxpool.Pool, thread *models.Thread) error

SaveThread saves or updates a thread in the database.

func SaveUserSettings

func SaveUserSettings(ctx context.Context, pool *pgxpool.Pool, settings *models.UserSettings) error

SaveUserSettings saves the user settings for the given user.

func SetFolderSyncInfo

func SetFolderSyncInfo(ctx context.Context, pool *pgxpool.Pool, userID, folderName string, lastSyncedUID *int64) error

SetFolderSyncInfo sets the sync information for the given folder.

func UpdateThreadCount

func UpdateThreadCount(ctx context.Context, pool *pgxpool.Pool, userID, folderName string) error

UpdateThreadCount updates the materialized thread count for a folder. This should be called in the background after syncing.

func UserSettingsExist

func UserSettingsExist(ctx context.Context, pool *pgxpool.Pool, userID string) (bool, error)

UserSettingsExist returns true if the user settings exist.

Types

type FolderSyncInfo

type FolderSyncInfo struct {
	SyncedAt      *time.Time
	LastSyncedUID *int64
	ThreadCount   int
}

FolderSyncInfo contains information about folder sync status.

func GetFolderSyncInfo

func GetFolderSyncInfo(ctx context.Context, pool *pgxpool.Pool, userID, folderName string) (*FolderSyncInfo, error)

GetFolderSyncInfo returns the sync information for the given folder. Returns nil if we've never synced it.

type ThreadCountUpdater

type ThreadCountUpdater interface {
	UpdateThreadCount(ctx context.Context, userID, folderName string) error
}

ThreadCountUpdater defines an interface for updating thread counts. This allows the Service to be tested with mock implementations.

func NewThreadCountUpdater

func NewThreadCountUpdater(pool *pgxpool.Pool) ThreadCountUpdater

NewThreadCountUpdater creates a ThreadCountUpdater that uses the given database pool.

Jump to

Keyboard shortcuts

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