ultisuite-backend/internal/mail/imap/sync_state.go
R3D347HR4Y bb5be669c1 Implement IMAP sync pipeline with rules and webhook support
- Introduced a new sync pipeline for IMAP that integrates a rules engine and webhook execution.
- Enhanced the `SyncWorker` to support attachment management and folder synchronization.
- Added functionality to detect special folder types (Sent, Drafts, Trash, Archive, Spam) during sync.
- Implemented a database schema for tracking rule executions and their outcomes.
- Created unit tests for the new rules engine and webhook execution logic.
- Updated migration scripts to accommodate new database structures for rule executions and folder states.
- Enhanced error handling and logging throughout the sync process for better observability.
2026-05-22 17:38:39 +02:00

46 lines
1.3 KiB
Go

package imap
import (
"context"
"github.com/jackc/pgx/v5/pgxpool"
)
type folderSyncState struct {
FolderID string
UIDValidity uint32
HighestModSeq uint64
LastUID uint32
}
func loadFolderSyncState(ctx context.Context, db *pgxpool.Pool, accountID, remoteName string) (folderSyncState, bool, error) {
var state folderSyncState
err := db.QueryRow(ctx, `
SELECT id, uidvalidity, highest_modseq, last_uid
FROM mail_folders
WHERE account_id = $1 AND remote_name = $2
`, accountID, remoteName).Scan(&state.FolderID, &state.UIDValidity, &state.HighestModSeq, &state.LastUID)
if err != nil {
return folderSyncState{}, false, err
}
return state, true, nil
}
func saveFolderSyncState(ctx context.Context, db *pgxpool.Pool, folderID string, uidValidity uint32, highestModSeq uint64, lastUID uint32, messageCount int) error {
_, err := db.Exec(ctx, `
UPDATE mail_folders
SET uidvalidity = $2,
highest_modseq = $3,
last_uid = $4,
message_count = $5,
updated_at = NOW()
WHERE id = $1
`, folderID, uidValidity, highestModSeq, lastUID, messageCount)
return err
}
func resetFolderMessages(ctx context.Context, db *pgxpool.Pool, folderID string) error {
_, err := db.Exec(ctx, `DELETE FROM messages WHERE folder_id = $1`, folderID)
return err
}