mirror of
https://github.com/warmbly/warmbly.git
synced 2026-11-29 08:06:27 +00:00
145 lines
7.2 KiB
Go
145 lines
7.2 KiB
Go
package unibox
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/warmbly/warmbly/internal/errx"
|
|
"github.com/warmbly/warmbly/internal/events"
|
|
"github.com/warmbly/warmbly/internal/infrastructure/cache"
|
|
"github.com/warmbly/warmbly/internal/infrastructure/storage"
|
|
"github.com/warmbly/warmbly/internal/models"
|
|
"github.com/warmbly/warmbly/internal/repository"
|
|
"github.com/warmbly/warmbly/internal/tasksched"
|
|
)
|
|
|
|
type UniboxService interface {
|
|
Search(
|
|
ctx context.Context,
|
|
orgID uuid.UUID,
|
|
params *models.MailSearchParams,
|
|
) (*models.MailSearchResult, *errx.Error)
|
|
GetByID(
|
|
ctx context.Context,
|
|
orgID, id uuid.UUID,
|
|
) (*models.EmailMessage, *errx.Error)
|
|
// Peek is GetByID without marking the message read.
|
|
Peek(ctx context.Context, orgID, id uuid.UUID) (*models.EmailMessage, *errx.Error)
|
|
// ForwardSource is the message a forward carries, read without marking it
|
|
// seen. NotFound when the id names no message in the organization.
|
|
ForwardSource(ctx context.Context, orgID, id uuid.UUID) (*models.ForwardedMessage, *errx.Error)
|
|
GetByThread(
|
|
ctx context.Context,
|
|
orgID, emailID uuid.UUID,
|
|
threadID, limit, cursor string,
|
|
) (*models.MailSearchResult, *errx.Error)
|
|
// GetByThreadWithin is GetByThread limited to the allowed mailboxes; nil allows every mailbox.
|
|
GetByThreadWithin(ctx context.Context, orgID, emailID uuid.UUID, threadID, limit, cursor string, allowed []uuid.UUID) (*models.MailSearchResult, *errx.Error)
|
|
// LatestMessageIDInThread is the RFC Message-ID a reply into this thread
|
|
// should name in its In-Reply-To header. Returns "" when the thread is
|
|
// unknown or holds no message id, which callers treat as "no backfill".
|
|
LatestMessageIDInThread(ctx context.Context, orgID uuid.UUID, threadID string) (string, *errx.Error)
|
|
GetUnseenCount(
|
|
ctx context.Context,
|
|
orgID uuid.UUID,
|
|
emailAccountID *uuid.UUID,
|
|
) (int64, *errx.Error)
|
|
MarkSeen(ctx context.Context, userID, emailID uuid.UUID, seen bool) *errx.Error
|
|
MarkSeenBulk(ctx context.Context, orgID uuid.UUID, data *models.MarkSeen) (*models.MarkSeen, *errx.Error)
|
|
MoveFolderBulk(ctx context.Context, orgID uuid.UUID, data *models.MoveFolder) (*models.MoveFolder, *errx.Error)
|
|
|
|
// Snooze hides conversations until `until`. Unsnooze drops the rows. Both
|
|
// take a set so the list's selection bar is one call, not one per row.
|
|
Snooze(ctx context.Context, orgID, userID uuid.UUID, threadIDs []string, until time.Time) ([]models.UniboxSnooze, *errx.Error)
|
|
Unsnooze(ctx context.Context, orgID, userID uuid.UUID, threadIDs []string) *errx.Error
|
|
ListSnoozes(ctx context.Context, orgID, userID uuid.UUID) ([]models.UniboxSnooze, *errx.Error)
|
|
|
|
// Overview powers the scope rail + top metric strip in one call.
|
|
Overview(ctx context.Context, orgID, userID uuid.UUID) (*models.UniboxOverview, *errx.Error)
|
|
// ForgetOverview makes the next Overview for the organization compute afresh.
|
|
ForgetOverview(orgID uuid.UUID)
|
|
// OverviewForMailboxes and UnseenCountForMailboxes count only the named
|
|
// mailboxes, for a credential limited to them.
|
|
OverviewForMailboxes(ctx context.Context, orgID uuid.UUID, accountIDs []uuid.UUID) (*models.UniboxOverview, *errx.Error)
|
|
UnseenCountForMailboxes(ctx context.Context, orgID uuid.UUID, accountIDs []uuid.UUID) (int64, *errx.Error)
|
|
// MessageMailboxes lists the mailboxes the named messages and
|
|
// conversations sit in, within the organization.
|
|
MessageMailboxes(ctx context.Context, orgID uuid.UUID, ids []uuid.UUID, threadIDs []string) ([]uuid.UUID, *errx.Error)
|
|
// CategoriesForMailboxes lists categories attached to messages in the named mailboxes; empty grants see none.
|
|
CategoriesForMailboxes(ctx context.Context, orgID uuid.UUID, accountIDs []uuid.UUID) ([]models.Group, *errx.Error)
|
|
|
|
// Conversation labels. SetThreadLabels replaces a thread's full
|
|
// label set (idempotent); ListThreadLabels reads the current set.
|
|
SetThreadLabels(ctx context.Context, orgID, userID uuid.UUID, threadID string, categoryIDs []uuid.UUID) ([]models.MiniCategory, *errx.Error)
|
|
ListThreadLabels(ctx context.Context, orgID uuid.UUID, threadID string) ([]models.MiniCategory, *errx.Error)
|
|
ListThreadLabelsWithin(ctx context.Context, orgID uuid.UUID, threadID string, accountIDs []uuid.UUID) ([]models.MiniCategory, *errx.Error)
|
|
|
|
// Scheduled-sends review + cancel. CancelScheduled is DB-only: we
|
|
// flip status to 'cancelled' and let the queued Cloud Task fire as
|
|
// a no-op (handler short-circuits on non-pending status). Avoids
|
|
// per-cancel API calls against Cloud Tasks.
|
|
// All three are scoped to the organization, whose mailboxes send them, and
|
|
// a non-empty accountIDs (an API key's allowlist) narrows them further.
|
|
ListScheduled(ctx context.Context, orgID uuid.UUID, accountIDs []uuid.UUID) ([]models.UniboxScheduledItem, *errx.Error)
|
|
// ListScheduledByThread returns the pending queued sends for a single
|
|
// thread. ThreadView calls this so queued replies render inline
|
|
// alongside already-sent messages.
|
|
ListScheduledByThread(ctx context.Context, orgID uuid.UUID, threadID string, accountIDs []uuid.UUID) ([]models.UniboxScheduledItem, *errx.Error)
|
|
CancelScheduled(ctx context.Context, orgID, taskID uuid.UUID, accountIDs []uuid.UUID) *errx.Error
|
|
|
|
// ThreadGrounding and AddressGrounding return message text for AI prompts:
|
|
// the stored body when it exists, the preview snippet as the fallback.
|
|
ThreadGrounding(ctx context.Context, orgID uuid.UUID, threadID string, limit int) ([]models.MessageGrounding, *errx.Error)
|
|
AddressGrounding(ctx context.Context, orgID uuid.UUID, address string, limit int) ([]models.MessageGrounding, *errx.Error)
|
|
|
|
// StartBodyTextBackfill fills in the searchable text of messages stored
|
|
// before bodies were indexed. Runs until the archive is caught up, then
|
|
// returns; blocking, so callers run it in a goroutine.
|
|
StartBodyTextBackfill(ctx context.Context)
|
|
|
|
// WireProviderRelay attaches the worker bus, after which a read/unread
|
|
// change or a filing made here is carried out to the mailbox provider too.
|
|
WireProviderRelay(p events.Publisher)
|
|
}
|
|
|
|
type uniboxService struct {
|
|
uniboxRepository repository.UniboxRepository
|
|
taskRepo repository.TaskRepository
|
|
tasksClient tasksched.Scheduler
|
|
cache *cache.Cache
|
|
blob storage.Store
|
|
// publisher relays read/unread changes and filings out to the mailbox
|
|
// providers. Optional: without it the unibox still works and only
|
|
// Warmbly's own copy changes.
|
|
publisher events.Publisher
|
|
// overview shares one overview computation per organization between concurrent readers.
|
|
overview *overviewCache
|
|
}
|
|
|
|
// WireProviderRelay attaches the bus the unibox relays read state through.
|
|
// Wired after construction, like the webhook dispatcher on the mailbox
|
|
// service, because a deployment without a worker bus is still a working
|
|
// unibox.
|
|
func (s *uniboxService) WireProviderRelay(p events.Publisher) {
|
|
s.publisher = p
|
|
}
|
|
|
|
func NewService(
|
|
cache *cache.Cache,
|
|
blob storage.Store,
|
|
uniboxRepository repository.UniboxRepository,
|
|
taskRepo repository.TaskRepository,
|
|
tasksClient tasksched.Scheduler,
|
|
) UniboxService {
|
|
s := &uniboxService{
|
|
uniboxRepository: uniboxRepository,
|
|
taskRepo: taskRepo,
|
|
tasksClient: tasksClient,
|
|
cache: cache,
|
|
blob: blob,
|
|
}
|
|
s.overview = newOverviewCache(s.computeOverview)
|
|
return s
|
|
}
|