mirror of
https://github.com/warmbly/warmbly.git
synced 2026-11-28 08:05:45 +00:00
692 lines
27 KiB
Go
692 lines
27 KiB
Go
package models
|
|
|
|
import (
|
|
"slices"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type EmailMessage struct { // used for sending to the user
|
|
ID uuid.UUID `json:"id"` // Gmail
|
|
GmailID string `json:"gmail_id"` // Gmail
|
|
UID uint32 `json:"uid"` // IMAP
|
|
|
|
// EmailID is the connected mailbox the message belongs to.
|
|
EmailID uuid.UUID `json:"email_id"`
|
|
// Folder is the canonical folder (Folder* consts) the message sits in.
|
|
Folder string `json:"folder"`
|
|
|
|
ParentID string `json:"parent_id"`
|
|
ThreadID string `json:"thread_id"`
|
|
|
|
Flags []string `json:"flags"` // or labelids
|
|
|
|
BCC []string `json:"bcc"`
|
|
CC []string `json:"cc"`
|
|
Date time.Time `json:"date"`
|
|
From []string `json:"from"`
|
|
InReplyTo []string `json:"in_reply_to"`
|
|
MessageID string `json:"message_id"`
|
|
ReplyTo []string `json:"ReplyTo"`
|
|
To []string `json:"to"`
|
|
Subject string `json:"subject"`
|
|
|
|
Size int64 `json:"size"`
|
|
|
|
// Internal Date
|
|
InternalDate time.Time `json:"internal_date"`
|
|
|
|
// ModSeq (CONDSTORE)
|
|
ModSeq uint64 `json:"mod_seq"`
|
|
|
|
// Body. BodyHTML is sanitized for display: scripts, event handlers, and
|
|
// unsafe URL schemes are stripped before it leaves the API.
|
|
BodyPlain string `json:"body_plain"`
|
|
BodyHTML string `json:"body_html"`
|
|
// BodyTruncated marks a message whose stored body could not be read, so
|
|
// BodyPlain holds only the preview snippet. Clients show a notice instead
|
|
// of presenting a partial message as the whole thing.
|
|
BodyTruncated bool `json:"body_truncated"`
|
|
}
|
|
|
|
// ForwardedMessage is a stored message as a forward carries it: the envelope
|
|
// and the body as synced, before any rendering.
|
|
type ForwardedMessage struct {
|
|
// EmailID is the mailbox the message belongs to.
|
|
EmailID uuid.UUID
|
|
// ThreadID is the Unibox conversation the message is in, where the forward
|
|
// is filed once sent.
|
|
ThreadID string
|
|
From []string
|
|
To []string
|
|
CC []string
|
|
Subject string
|
|
Date time.Time
|
|
// BodyHTML is the stored HTML, unsanitized; empty when the message has none.
|
|
BodyHTML string
|
|
// BodyPlain is the preview snippet when the full body is not stored.
|
|
BodyPlain string
|
|
}
|
|
|
|
type EmailMessageData struct { // used when for kafka when an email arrives
|
|
ID uuid.UUID `json:"id"`
|
|
|
|
// Gmail Only
|
|
GmailID string `json:"gmail_id"` // msg.Id (Unique)
|
|
Snippet string `json:"snippet"`
|
|
|
|
// Threading
|
|
ParentID string `json:"parent_id"` // Last ID in In-Reply-To
|
|
ThreadID string `json:"thread_id"` // Root Message ID
|
|
|
|
// Imap UID (Not Unique, it can change if the email moves)
|
|
UID uint32 `json:"uid"`
|
|
|
|
// Flags
|
|
Flags []string `json:"flags"`
|
|
|
|
// Canonical folder the provider reported the message in (Folder* consts);
|
|
// empty when the source path predates folder tracking.
|
|
Folder string `json:"folder,omitempty"`
|
|
|
|
// Envelope
|
|
BCC []string `json:"bcc"`
|
|
CC []string `json:"cc"`
|
|
Date time.Time `json:"date"`
|
|
From []string `json:"from"`
|
|
InReplyTo []string `json:"in_reply_to"`
|
|
MessageID string `json:"message_id"` // Unique
|
|
ReplyTo []string `json:"reply_to"`
|
|
Sender []string `json:"sender"`
|
|
Subject string `json:"subject"`
|
|
To []string `json:"to"`
|
|
|
|
// RFC822 Size
|
|
Size int64 `json:"size"`
|
|
|
|
// Internal Date
|
|
InternalDate time.Time `json:"internal_date"`
|
|
|
|
// ModSeq (CONDSTORE)
|
|
ModSeq uint64 `json:"mod_seq"`
|
|
|
|
// Body
|
|
BodyPlain string `json:"body_plain"`
|
|
BodyHTML string `json:"body_html"`
|
|
}
|
|
|
|
type EmailMessageStoreData struct {
|
|
ID uuid.UUID `json:"id" avro:"id"`
|
|
EmailID uuid.UUID `json:"email_id" avro:"email_id"`
|
|
// Mailbox is the source folder's UIDVALIDITY at sync time, which is the
|
|
// generation UID belongs to. It is not the folder's identity: see
|
|
// FolderPath.
|
|
Mailbox uint32 `json:"mailbox" avro:"mailbox"`
|
|
// FolderPath is the source folder's name, the identity IMAP actually
|
|
// guarantees. Empty on events from workers predating the field and on
|
|
// providers with no folders (Gmail).
|
|
FolderPath string `json:"folder_path,omitempty" avro:"folder_path"`
|
|
// Folder is the canonical folder (see the Folder* constants) the message
|
|
// was in at sync time. Empty on events from workers predating the field;
|
|
// the consumer normalizes before storing.
|
|
Folder string `json:"folder,omitempty" avro:"folder"`
|
|
// ProviderFolder is where the PROVIDER last reported the message, which
|
|
// Folder stops tracking once the user files the message in Warmbly. The
|
|
// two are compared to tell a real provider move from a flag scan that
|
|
// keeps naming the folder the provider still has it in.
|
|
ProviderFolder string `json:"provider_folder,omitempty" avro:"provider_folder"`
|
|
ThreadID string `json:"thread_id" avro:"thread_id"`
|
|
MessageID string `json:"message_id" avro:"message_id"`
|
|
GmailID string `json:"gmail_id" avro:"gmail_id"`
|
|
ParentID string `json:"parent_id" avro:"parent_id"`
|
|
UID uint32 `json:"uid" avro:"uid"`
|
|
ModSeq uint64 `json:"mod_seq" avro:"mod_seq"`
|
|
Flags []string `json:"flags" avro:"flags"`
|
|
BCC []string `json:"bcc" avro:"bcc"`
|
|
CC []string `json:"cc" avro:"cc"`
|
|
FromAddr []string `json:"from_addr" avro:"from_addr"`
|
|
InReplyTo []string `json:"in_reply_to" avro:"in_reply_to"`
|
|
ReplyTo []string `json:"reply_to" avro:"reply_to"`
|
|
ToAddr []string `json:"to_addr" avro:"to_addr"`
|
|
Subject string `json:"subject" avro:"subject"`
|
|
Size int64 `json:"size" avro:"size"`
|
|
InternalDate time.Time `json:"internal_date" avro:"internal_date"`
|
|
SentDate time.Time `json:"sent_date" avro:"sent_date"`
|
|
Snippet string `json:"snippet" avro:"snippet"`
|
|
Seen bool `json:"seen" avro:"seen"`
|
|
// BodyText is a bounded plain-text rendering of the message, carried on the
|
|
// new-email event so the consumer can make the message findable by what it
|
|
// says. The full body goes to object storage, never here.
|
|
BodyText string `json:"body_text,omitempty" avro:"body_text"`
|
|
UpdatedAt time.Time `json:"updated_at" avro:"updated_at"`
|
|
CreatedAt time.Time `json:"created_at" avro:"created_at"`
|
|
}
|
|
|
|
// ValidText makes every text field storable: Postgres refuses invalid UTF-8
|
|
// and NUL, and an unlabelled 8-bit header or body carries both.
|
|
func (e *EmailMessageStoreData) ValidText() {
|
|
for _, s := range []*string{&e.FolderPath, &e.Folder, &e.ProviderFolder, &e.ThreadID, &e.MessageID,
|
|
&e.GmailID, &e.ParentID, &e.Subject, &e.Snippet, &e.BodyText} {
|
|
*s = validText(*s)
|
|
}
|
|
for _, list := range [][]string{e.Flags, e.BCC, e.CC, e.FromAddr, e.InReplyTo, e.ReplyTo, e.ToAddr} {
|
|
for i := range list {
|
|
list[i] = validText(list[i])
|
|
}
|
|
}
|
|
}
|
|
|
|
func validText(s string) string {
|
|
return strings.ToValidUTF8(strings.ReplaceAll(s, "\x00", ""), "\uFFFD")
|
|
}
|
|
|
|
type EmailMessageStoreDataPreview struct {
|
|
ID uuid.UUID `json:"id"`
|
|
EmailID uuid.UUID `json:"email_id"`
|
|
ThreadID string `json:"thread_id"`
|
|
FromAddr []string `json:"from_addr"`
|
|
ToAddr []string `json:"to_addr"`
|
|
Subject string `json:"subject"`
|
|
Snippet string `json:"snippet"`
|
|
InternalDate time.Time `json:"internal_date"`
|
|
Seen bool `json:"seen"`
|
|
|
|
// Thread-stacking fields. The inbox list collapses to one row per
|
|
// thread (the newest message), so these summarise the whole
|
|
// conversation behind the row:
|
|
// MessageCount — number of messages in the thread (within the
|
|
// current filter scope); 1 for a singleton.
|
|
// HasUnread — true if any message in the thread is unseen, so
|
|
// the row can bold the whole conversation Gmail-style
|
|
// rather than only when the latest message is unread.
|
|
// Both are zero-valued on the message-level paths (GetByThread /
|
|
// GetBySender / GetIncoming) that don't collapse.
|
|
MessageCount int64 `json:"message_count"`
|
|
HasUnread bool `json:"has_unread"`
|
|
|
|
// Labels are the conversation's assigned categories (denormalised
|
|
// id/title/color so the row renders chips without a second lookup).
|
|
// Always non-nil so it marshals to [] not null.
|
|
Labels []MiniCategory `json:"labels"`
|
|
|
|
// AnswersMailboxID is the workspace mailbox that sent the email this
|
|
// message replies to, when that is not the mailbox holding it: a reply
|
|
// that landed in a shared reply inbox. Thread reads only.
|
|
AnswersMailboxID *uuid.UUID `json:"answers_mailbox_id,omitempty"`
|
|
|
|
// CampaignID is the campaign a send in this conversation belongs to. List reads only.
|
|
CampaignID *uuid.UUID `json:"campaign_id,omitempty"`
|
|
}
|
|
|
|
// MessageGrounding is one message rendered for an AI prompt: the stored body
|
|
// text when it exists, with the preview snippet as the fallback for mail synced
|
|
// before bodies were indexed. Deliberately separate from the preview shape so a
|
|
// 16 KB body can never leak into a list response by accident.
|
|
type MessageGrounding struct {
|
|
ID uuid.UUID `json:"id"`
|
|
FromAddr []string `json:"from_addr"`
|
|
ToAddr []string `json:"to_addr"`
|
|
Subject string `json:"subject"`
|
|
BodyText string `json:"body_text"`
|
|
Snippet string `json:"snippet"`
|
|
SentAt time.Time `json:"sent_at"`
|
|
}
|
|
|
|
type EmailParent struct { // used to get information from the parent email
|
|
ID uuid.UUID `json:"id" avro:"id"`
|
|
MessageID string `json:"message_id" avro:"message_id"`
|
|
ThreadID string `json:"thread_id" avro:"thread_id"`
|
|
}
|
|
|
|
type MailThreadResult struct {
|
|
Data []EmailMessageStoreData `json:"data"`
|
|
Pagination CPagination `json:"pagination"`
|
|
}
|
|
|
|
// Canonical mail folders. Every unibox message carries exactly one, derived
|
|
// from provider placement at sync time (IMAP special-use attributes, Gmail
|
|
// labels, Graph well-known folders) and enforced by a CHECK on the column.
|
|
const (
|
|
FolderInbox = "inbox"
|
|
FolderSent = "sent"
|
|
FolderDrafts = "drafts"
|
|
FolderArchive = "archive"
|
|
FolderSpam = "spam"
|
|
FolderTrash = "trash"
|
|
)
|
|
|
|
// MailFolders lists every canonical folder, in sidebar order.
|
|
var MailFolders = []string{FolderInbox, FolderSent, FolderDrafts, FolderArchive, FolderSpam, FolderTrash}
|
|
|
|
// ValidFolder reports whether f is one of the canonical folder values.
|
|
func ValidFolder(f string) bool {
|
|
switch f {
|
|
case FolderInbox, FolderSent, FolderDrafts, FolderArchive, FolderSpam, FolderTrash:
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
// ResolveFolderSync decides what one sync event does to a message's placement.
|
|
//
|
|
// reported is compared against the folder the PROVIDER was last seen to have
|
|
// the message in, never against the stored folder. A message the user filed in
|
|
// Warmbly still turns up in the provider's inbox on every scan, and comparing
|
|
// against the stored folder would read each of those as a move back and undo
|
|
// them. An empty storedProvider is a row written before the column existed
|
|
// (migration 000146), so it adopts. An invalid reported folder is a worker
|
|
// predating the field and changes nothing.
|
|
func ResolveFolderSync(storedFolder, storedProvider, reported string) (folder, provider string, changed bool) {
|
|
if !ValidFolder(reported) || reported == storedProvider {
|
|
return storedFolder, storedProvider, false
|
|
}
|
|
return reported, reported, true
|
|
}
|
|
|
|
// FilableFolder reports whether f is a folder a user may move mail INTO from
|
|
// the unibox. sent/drafts/spam are provider verdicts, never a user's choice.
|
|
func FilableFolder(f string) bool {
|
|
switch f {
|
|
case FolderInbox, FolderArchive, FolderTrash:
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
// NormalizeFolder resolves the folder to persist for a message: the worker's
|
|
// value when it sent a valid one, otherwise a flag-derived fallback so events
|
|
// from workers predating the folder field still file spam and drafts sanely.
|
|
func NormalizeFolder(folder string, flags []string) string {
|
|
if ValidFolder(folder) {
|
|
return folder
|
|
}
|
|
// Deletion outranks the rest: a message can carry \Deleted alongside a
|
|
// spam or draft flag, and the provider-driven paths treat trash as the
|
|
// stronger placement. Scanning for it first keeps a flag-ordering
|
|
// accident from filing deleted mail as spam.
|
|
for _, f := range flags {
|
|
if f == "\\Deleted" {
|
|
return FolderTrash
|
|
}
|
|
}
|
|
for _, f := range flags {
|
|
if IsSpamFlag(f) {
|
|
return FolderSpam
|
|
}
|
|
if f == "\\Draft" {
|
|
return FolderDrafts
|
|
}
|
|
}
|
|
return FolderInbox
|
|
}
|
|
|
|
// IsSpamFlag reports whether a provider flag or label marks a message as spam:
|
|
// Gmail's SPAM label, the Junk flag Graph and IMAP set, and the IMAP keywords.
|
|
// The one list every spam check reads, so they cannot disagree.
|
|
func IsSpamFlag(f string) bool {
|
|
switch f {
|
|
case "SPAM", "\\Junk", "\\Spam", "Junk":
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
// HasSpamFlag reports whether any flag marks the message as spam.
|
|
func HasSpamFlag(flags []string) bool {
|
|
return slices.ContainsFunc(flags, IsSpamFlag)
|
|
}
|
|
|
|
func outboundOnlyFolder(folder string) bool {
|
|
return folder == FolderSent || folder == FolderDrafts
|
|
}
|
|
|
|
// MayBeInbound reports whether neither folder placement proves the message outbound.
|
|
func (e *EmailMessageStoreData) MayBeInbound() bool {
|
|
if e == nil {
|
|
return false
|
|
}
|
|
return !outboundOnlyFolder(NormalizeFolder(e.Folder, e.Flags)) &&
|
|
!outboundOnlyFolder(e.ProviderFolder)
|
|
}
|
|
|
|
type MailSearchResult struct {
|
|
Data []EmailMessageStoreDataPreview `json:"data"`
|
|
Pagination CPagination `json:"pagination"`
|
|
}
|
|
|
|
type MailSearchParams struct {
|
|
Sender *string
|
|
// Address matches either side of the exchange (from_addr OR to_addr),
|
|
// giving "every conversation with this person" regardless of direction.
|
|
// Substring match on the raw header entries, like Sender.
|
|
Address *string
|
|
// Direction narrows to messages the org sent ("sent") or received
|
|
// ("received"), resolved against the org's own mailbox addresses.
|
|
// nil = both directions.
|
|
Direction *string
|
|
Unseen *bool
|
|
Subject *string
|
|
// Since is the inclusive lower bound on internal_date.
|
|
Since *time.Time
|
|
// Before is the exclusive upper bound on internal_date.
|
|
Before *time.Time
|
|
// CampaignID narrows to conversations traced to one of the campaign's sends.
|
|
CampaignID *uuid.UUID
|
|
// ContactID narrows to conversations traced to a campaign send to the
|
|
// contact, or, without CampaignID, exchanged with the contact's address.
|
|
ContactID *uuid.UUID
|
|
// EmailAccountIDs restricts results to messages received by one of
|
|
// these mailboxes. Empty = no account filter.
|
|
EmailAccountIDs []uuid.UUID
|
|
// TagIDs restricts results to mailboxes carrying any of these workspace
|
|
// mailbox tags, resolved in SQL. Combined with EmailAccountIDs it is the intersection.
|
|
TagIDs []uuid.UUID
|
|
// Snoozed scopes the result set:
|
|
// nil → exclude snoozed threads (default inbox behaviour)
|
|
// true → only snoozed threads
|
|
// false → ignore the snooze filter entirely (raw view)
|
|
Snoozed *bool
|
|
// AwaitingReply, when true, narrows to threads where the latest
|
|
// message in the thread was sent by the user (i.e. the recipient
|
|
// hasn't replied yet). nil = no filter.
|
|
AwaitingReply *bool
|
|
// AgentDraft, when true, narrows to threads that have a pending
|
|
// inbox-agent draft awaiting human review (M10). nil = no filter.
|
|
AgentDraft *bool
|
|
// CategoryIDs restricts results to threads carrying at least one of
|
|
// these conversation labels. Empty = no category filter.
|
|
CategoryIDs []uuid.UUID
|
|
// Uncategorized, when true, narrows to threads carrying no
|
|
// conversation labels at all. nil = no filter.
|
|
Uncategorized *bool
|
|
// Automated narrows to conversations no person wrote in (true) or leaves
|
|
// them out (false). nil = both.
|
|
Automated *bool
|
|
// Folder narrows to one canonical folder (inbox/sent/drafts/archive/
|
|
// spam/trash). nil = every working folder, so junk and filed mail never
|
|
// bleed into the combined view.
|
|
Folder *string
|
|
// IncludeArchived puts archived conversations back into an unscoped
|
|
// result. Filing is how a conversation leaves the working views, so it
|
|
// has to leave all of them; only "All mail" and reference reads (compose
|
|
// history) ask for it. Ignored when Folder names one.
|
|
IncludeArchived *bool
|
|
PageSize int
|
|
Cursor string
|
|
}
|
|
|
|
type MarkSeen struct {
|
|
EmailIDs []uuid.UUID `json:"email_ids"`
|
|
// ThreadIDs marks whole conversations, so a caller holding a list row
|
|
// does not have to fetch the thread to learn its message ids. Each entry
|
|
// is a thread id, or a message id for mail that never got one.
|
|
ThreadIDs []string `json:"thread_ids,omitempty"`
|
|
// Folder, when set, marks every unread message in that folder for the
|
|
// whole workspace instead of the explicit id list.
|
|
Folder string `json:"folder,omitempty"`
|
|
Seen bool `json:"seen"`
|
|
}
|
|
|
|
// MoveFolder re-files messages into one canonical folder (Archive = archive,
|
|
// Delete = trash, Move to inbox = inbox). The store changes first; the move
|
|
// is then relayed to each mailbox that has relay_folder_moves on.
|
|
type MoveFolder struct {
|
|
EmailIDs []uuid.UUID `json:"email_ids"`
|
|
// ThreadIDs files whole conversations. A row in the list knows its thread
|
|
// but not the ids inside it, and filing half a conversation leaves it in
|
|
// the view it was filed out of. Each entry is a thread id, or a message id
|
|
// for mail that never got one.
|
|
ThreadIDs []string `json:"thread_ids,omitempty"`
|
|
Folder string `json:"folder"`
|
|
}
|
|
|
|
// UniboxSnooze hides a thread from the user's inbox until SnoozedUntil
|
|
// passes. UNIQUE per (user, thread); a second snooze on the same
|
|
// thread updates SnoozedUntil in place.
|
|
type UniboxSnooze struct {
|
|
ID uuid.UUID `json:"id"`
|
|
UserID uuid.UUID `json:"user_id"`
|
|
ThreadID string `json:"thread_id"`
|
|
SnoozedUntil time.Time `json:"snoozed_until"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
}
|
|
|
|
// UniboxMailboxOverview captures per-mailbox counters for the scope
|
|
// rail. Total counts every message in the mailbox; unread is the
|
|
// classic dot-badge number.
|
|
type UniboxMailboxOverview struct {
|
|
ID uuid.UUID `json:"id"`
|
|
Email string `json:"email"`
|
|
Name string `json:"name"`
|
|
Unread int64 `json:"unread"`
|
|
Total int64 `json:"total"`
|
|
}
|
|
|
|
// UniboxTagOverview gives the rail per-tag counts; resolved by joining
|
|
// emails through the mailboxes that carry the tag.
|
|
type UniboxTagOverview struct {
|
|
ID uuid.UUID `json:"id"`
|
|
Title string `json:"title"`
|
|
Color string `json:"color"`
|
|
Unread int64 `json:"unread"`
|
|
Total int64 `json:"total"`
|
|
}
|
|
|
|
// UniboxCategoryOverview gives the rail per-conversation-label counts.
|
|
// Resolved by joining threads through unibox_thread_labels. Unread/Total
|
|
// are counted as THREADS (matching the thread-stacked list), not
|
|
// messages.
|
|
type UniboxCategoryOverview struct {
|
|
ID uuid.UUID `json:"id"`
|
|
Title string `json:"title"`
|
|
Color string `json:"color"`
|
|
Unread int64 `json:"unread"`
|
|
Total int64 `json:"total"`
|
|
}
|
|
|
|
// UniboxFolderOverview gives the rail per-folder thread counts. Always
|
|
// emitted for all six canonical folders, zero-filled, so the sidebar
|
|
// renders a stable list.
|
|
type UniboxFolderOverview struct {
|
|
Folder string `json:"folder"`
|
|
Unread int64 `json:"unread"`
|
|
Total int64 `json:"total"`
|
|
}
|
|
|
|
// UniboxOverview powers the scope rail + top metric strip in one
|
|
// request. Computed at /unibox/overview.
|
|
type UniboxOverview struct {
|
|
Total int64 `json:"total"`
|
|
Unread int64 `json:"unread"`
|
|
Today int64 `json:"today"`
|
|
Week int64 `json:"week"`
|
|
Snoozed int64 `json:"snoozed"`
|
|
AwaitingReply int64 `json:"awaiting_reply"`
|
|
// Automated counts conversations no person wrote in. They are left out of
|
|
// Unread, Today, Week, the Inbox folder and the mailbox and tag counts.
|
|
Automated int64 `json:"automated"`
|
|
AutomatedUnread int64 `json:"automated_unread"`
|
|
// AwaitingAgentDraft is the count of threads with a pending inbox-agent draft
|
|
// waiting for human review (M10).
|
|
AwaitingAgentDraft int64 `json:"awaiting_agent_draft"`
|
|
// Drafts counts each visible thread once across mailbox and pending agent drafts.
|
|
Drafts int64 `json:"drafts"`
|
|
ScheduledPending int64 `json:"scheduled_pending"`
|
|
// ScheduledPendingMax is the hard cap on pending scheduled email
|
|
// tasks per workspace. The dashboard shows current/max so the user
|
|
// sees how close they are to the limit before hitting it.
|
|
ScheduledPendingMax int64 `json:"scheduled_pending_max"`
|
|
Folders []UniboxFolderOverview `json:"folders"`
|
|
Mailboxes []UniboxMailboxOverview `json:"mailboxes"`
|
|
Tags []UniboxTagOverview `json:"tags"`
|
|
Categories []UniboxCategoryOverview `json:"categories"`
|
|
GeneratedAt time.Time `json:"generated_at"`
|
|
WindowTodayStart time.Time `json:"window_today_start"`
|
|
WindowWeekStart time.Time `json:"window_week_start"`
|
|
}
|
|
|
|
// UniboxThreadLabels is the assign/replace payload for a conversation's
|
|
// labels. CategoryIDs is the full desired set — the handler diffs it
|
|
// against what's stored, so a PUT-style replace is idempotent.
|
|
type UniboxThreadLabels struct {
|
|
ThreadID string `json:"thread_id" binding:"required"`
|
|
CategoryIDs []uuid.UUID `json:"category_ids"`
|
|
}
|
|
|
|
// UniboxScheduledItem describes one queued outbound message the user
|
|
// can review or cancel before it fires. The shape mirrors what the
|
|
// scheduled-list view needs to render: who it's going to, when, the
|
|
// thread it'll thread into, and which mailbox is sending.
|
|
type UniboxScheduledItem struct {
|
|
TaskID uuid.UUID `json:"task_id"`
|
|
ScheduledAt time.Time `json:"scheduled_at"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
|
|
// Sending mailbox.
|
|
AccountID uuid.UUID `json:"account_id"`
|
|
AccountEmail string `json:"account_email"`
|
|
AccountName string `json:"account_name"`
|
|
|
|
// Recipients + message contents (preview only).
|
|
To []string `json:"to"`
|
|
CC []string `json:"cc,omitempty"`
|
|
BCC []string `json:"bcc,omitempty"`
|
|
Subject string `json:"subject"`
|
|
Snippet string `json:"snippet"`
|
|
|
|
// Thread the reply will land in (when the user queued from unibox).
|
|
ThreadID *string `json:"thread_id,omitempty"`
|
|
}
|
|
|
|
// MessageSeenAction relays a read/unread change from the unibox to the
|
|
// mailbox's provider, for one mailbox and up to SeenRelayChunk messages.
|
|
//
|
|
// The unibox used to be the only place that knew: a thread read here stayed
|
|
// bold in Gmail and one archived here stayed in the inbox, which reads as a
|
|
// broken client rather than a design decision. Only the explicit change
|
|
// travels; nothing reconciles the two stores in the background, because the
|
|
// provider's own state is what the next sync brings back anyway.
|
|
type MessageSeenAction struct {
|
|
// EmailID is the mailbox, which is how the worker finds the live client.
|
|
EmailID uuid.UUID `json:"email_id" avro:"email_id"`
|
|
// Seen is the state to apply to every message in the batch.
|
|
Seen bool `json:"seen" avro:"seen"`
|
|
Messages []MessageSeenRef `json:"messages" avro:"messages"`
|
|
}
|
|
|
|
// MessageSeenRef names one message in whichever way its provider needs.
|
|
// Every field is optional because the three providers use different halves:
|
|
// Gmail and Graph a message id, IMAP a folder and a UID.
|
|
type MessageSeenRef struct {
|
|
// ProviderID is the Gmail message id or the Graph message id. The column
|
|
// behind it is provider-agnostic despite its name.
|
|
ProviderID string `json:"provider_id,omitempty" avro:"provider_id"`
|
|
UID uint32 `json:"uid,omitempty" avro:"uid"`
|
|
// Folder is the IMAP folder holding UID. UIDs are only unique within one.
|
|
Folder string `json:"folder,omitempty" avro:"folder"`
|
|
// RFCMessageID is the immutable Message-ID. Graph ids change when a
|
|
// message moves, so the worker re-resolves from this when it is present.
|
|
RFCMessageID string `json:"rfc_message_id,omitempty" avro:"rfc_message_id"`
|
|
}
|
|
|
|
// SeenRelayChunk bounds one MESSAGE_SEEN event. Gmail accepts 1000 ids per
|
|
// batchModify; this leaves room under it and keeps one "mark all as read" on
|
|
// a large folder from becoming a single enormous event on the bus.
|
|
const SeenRelayChunk = 500
|
|
|
|
// SeenRelayTarget is one message resolved for the relay: which mailbox it
|
|
// belongs to, which worker holds that mailbox, and how its provider names it.
|
|
type SeenRelayTarget struct {
|
|
EmailID uuid.UUID
|
|
WorkerID uuid.UUID
|
|
// Seen is the state the row holds now, read back rather than taken from
|
|
// the request that caused the relay. Two people toggling the same
|
|
// conversation in opposite directions at once would otherwise be able to
|
|
// leave the provider holding the earlier answer.
|
|
Seen bool
|
|
Ref MessageSeenRef
|
|
}
|
|
|
|
// MessageFolderAction relays a unibox filing to one mailbox's provider. The
|
|
// worker answers each message it moved with a Relayed UPDATE_FOLDER.
|
|
type MessageFolderAction struct {
|
|
EmailID uuid.UUID `json:"email_id" avro:"email_id"`
|
|
// Folder is the canonical destination: inbox, archive or trash.
|
|
Folder string `json:"folder" avro:"folder"`
|
|
Messages []MessageFolderRef `json:"messages" avro:"messages"`
|
|
}
|
|
|
|
// MessageFolderRef names one message to move, in every provider's terms.
|
|
type MessageFolderRef struct {
|
|
// ID is the unibox row, named in the worker's answer.
|
|
ID uuid.UUID `json:"id" avro:"id"`
|
|
ProviderID string `json:"provider_id,omitempty" avro:"provider_id"`
|
|
UID uint32 `json:"uid,omitempty" avro:"uid"`
|
|
FolderPath string `json:"folder_path,omitempty" avro:"folder_path"`
|
|
RFCMessageID string `json:"rfc_message_id,omitempty" avro:"rfc_message_id"`
|
|
// ThreadID re-keys a Graph message in the map under the id its move gives it.
|
|
ThreadID string `json:"thread_id,omitempty" avro:"thread_id"`
|
|
// ProviderFolder is where the provider last reported the message.
|
|
ProviderFolder string `json:"provider_folder,omitempty" avro:"provider_folder"`
|
|
}
|
|
|
|
// FiledMessage is one row a filing matched, and whether it left another
|
|
// folder to get there.
|
|
type FiledMessage struct {
|
|
ID uuid.UUID
|
|
Moved bool
|
|
}
|
|
|
|
// FolderRelayChunk bounds one MESSAGE_FOLDER event, under Gmail's 1000 ids
|
|
// per batchModify.
|
|
const FolderRelayChunk = 500
|
|
|
|
// FolderRelayTarget is one message resolved for the folder relay.
|
|
type FolderRelayTarget struct {
|
|
EmailID uuid.UUID
|
|
WorkerID uuid.UUID
|
|
Provider string
|
|
// Folder is the canonical folder the row holds now, read back rather than
|
|
// taken from the request, like SeenRelayTarget.Seen.
|
|
Folder string
|
|
Ref MessageFolderRef
|
|
}
|
|
|
|
// RelaysFolderMove reports whether a filing has to happen at the provider. A
|
|
// row the filing moved goes even when provider_folder agrees, since an Undo
|
|
// can beat the previous filing's answer. Sent copies stay in Sent on IMAP and
|
|
// Outlook; Gmail files whole conversations, and archiving a sent copy is a
|
|
// no-op there. Drafts never move.
|
|
func RelaysFolderMove(provider, providerFolder, folder string, moved bool) bool {
|
|
if !FilableFolder(folder) || (providerFolder == folder && !moved) {
|
|
return false
|
|
}
|
|
switch providerFolder {
|
|
case FolderDrafts:
|
|
return false
|
|
case FolderSent:
|
|
return provider == string(InboxProviderGoogle) && folder != FolderArchive
|
|
}
|
|
return true
|
|
}
|
|
|
|
// FlagSeen is the RFC 3501 read-state flag. Every provider is mapped onto it
|
|
// before it reaches the platform: IMAP reports it directly, the Gmail sync
|
|
// adds it when the UNREAD label is absent, and the Graph sync adds it for
|
|
// isRead.
|
|
const FlagSeen = `\Seen`
|
|
|
|
// FlagFlagged is the star: Gmail STARRED, IMAP \Flagged.
|
|
const FlagFlagged = `\Flagged`
|
|
|
|
// SeenFromFlags reads a message's read state out of its flags. The stored
|
|
// `seen` column has to follow the provider: mail the customer already read in
|
|
// their own client is read in Warmbly, and a copy the worker files in Sent
|
|
// (appended \Seen, since the sender wrote it) must never arrive as unread.
|
|
func SeenFromFlags(flags []string) bool {
|
|
return slices.Contains(flags, FlagSeen)
|
|
}
|