Documentation
¶
Overview ¶
Package store owns the SQLite database. Nothing else issues SQL.
Index ¶
- Constants
- Variables
- func CleanupIdentity(account, folder int64, validity, uid uint32) string
- type Account
- type Address
- type CleanupQuery
- type Contact
- type Counts
- type Folder
- type JunkInput
- type Message
- type Part
- type Query
- type Store
- func (s *Store) Account(ctx context.Context, id int64) (Account, error)
- func (s *Store) Accounts(ctx context.Context) ([]Account, error)
- func (s *Store) AddAccount(ctx context.Context, a Account) (int64, error)
- func (s *Store) AllowSender(ctx context.Context, addr string) error
- func (s *Store) CleanupIDs(ctx context.Context, q CleanupQuery) ([]int64, error)
- func (s *Store) CleanupSnapshot(ctx context.Context, q CleanupQuery) ([]int64, map[int64]string, error)
- func (s *Store) Close() error
- func (s *Store) DeleteAccount(ctx context.Context, id int64) error
- func (s *Store) DeleteMessages(ctx context.Context, ids []int64) error
- func (s *Store) DisallowSender(ctx context.Context, addr string) error
- func (s *Store) Folder(ctx context.Context, id int64) (Folder, error)
- func (s *Store) FolderByRole(ctx context.Context, accountID int64, role string) (Folder, error)
- func (s *Store) FolderUIDs(ctx context.Context, folderID int64) (map[uint32]UIDState, error)
- func (s *Store) Folders(ctx context.Context, accountID int64) ([]Folder, error)
- func (s *Store) InboxIDs(ctx context.Context, limit int) ([]int64, error)
- func (s *Store) JunkInputs(ctx context.Context, ids []int64) ([]JunkInput, error)
- func (s *Store) JunkTokenCount(ctx context.Context) (int, error)
- func (s *Store) JunkTotals(ctx context.Context) (nSpam, nHam int)
- func (s *Store) KnownSender(ctx context.Context, addr string) (bool, error)
- func (s *Store) LabelTriage(ctx context.Context, accountID int64, key, category, sender string) error
- func (s *Store) Learn(ctx context.Context, accountID int64, key string, tokens []string, ...) (bool, error)
- func (s *Store) LiftKnownJunk(ctx context.Context) ([]int64, error)
- func (s *Store) List(ctx context.Context, q Query) ([]Message, error)
- func (s *Store) Message(ctx context.Context, id int64) (Message, error)
- func (s *Store) Messages(ctx context.Context, ids []int64) ([]Message, error)
- func (s *Store) MissingJunkHeader(ctx context.Context, folderID int64, limit int) ([]UIDRow, error)
- func (s *Store) NoteContact(ctx context.Context, a Address, weight int, when int64) error
- func (s *Store) PendingBodies(ctx context.Context, folderID int64, limit int) ([]Message, error)
- func (s *Store) PruneJunkTokens(ctx context.Context, max int) error
- func (s *Store) RecountFolder(ctx context.Context, folderID int64) error
- func (s *Store) RecountInbox(ctx context.Context, accountID int64) error
- func (s *Store) RemoveFoldersExcept(ctx context.Context, accountID int64, keep []string) error
- func (s *Store) ResetFolder(ctx context.Context, folderID int64, uidValidity uint32) error
- func (s *Store) SaveTriage(ctx context.Context, t Triage, sender string) error
- func (s *Store) SetAuth(ctx context.Context, id int64, auth string) error
- func (s *Store) SetBody(ctx context.Context, id int64, text, html, snippet string) error
- func (s *Store) SetFlags(ctx context.Context, id int64, seen, flagged, answered bool) error
- func (s *Store) SetJunk(ctx context.Context, id int64, junk bool, score int, why string) error
- func (s *Store) SetJunkHeader(ctx context.Context, id int64, hdr string) error
- func (s *Store) SetLastUID(ctx context.Context, folderID int64, uid uint32) error
- func (s *Store) SetSetting(ctx context.Context, key, value string) error
- func (s *Store) SetSyncState(ctx context.Context, id int64, syncErr string) error
- func (s *Store) Setting(ctx context.Context, key, def string) string
- func (s *Store) SuggestContacts(ctx context.Context, prefix string, limit int) ([]Contact, error)
- func (s *Store) Thread(ctx context.Context, accountID int64, key string) ([]Message, error)
- func (s *Store) ThreadHasSent(ctx context.Context, accountID int64, threadKey string) (bool, error)
- func (s *Store) ThreadKeyFor(ctx context.Context, accountID int64, msgIDs []string) (string, error)
- func (s *Store) ThreadMail(ctx context.Context, accountID int64, key string, n int) ([]Message, error)
- func (s *Store) TokenCounts(ctx context.Context, tokens []string) (map[string][2]int, error)
- func (s *Store) TriageCounts(ctx context.Context) ([]TriageCount, error)
- func (s *Store) TriageOf(ctx context.Context, accountID int64, key string) (Triage, error)
- func (s *Store) TriagePending(ctx context.Context, window int) ([]TriageTodo, error)
- func (s *Store) UnifiedCounts(ctx context.Context) (Counts, error)
- func (s *Store) UnspamTriage(ctx context.Context, accountID int64, key, sender string) error
- func (s *Store) UpdateAccount(ctx context.Context, a Account) error
- func (s *Store) UpsertFolder(ctx context.Context, accountID int64, name, delim, role string) (Folder, error)
- func (s *Store) UpsertHeader(ctx context.Context, m *Message) (int64, error)
- type Structure
- type Triage
- type TriageCount
- type TriageTodo
- type UIDRow
- type UIDState
Constants ¶
const SentWeight = 5
SentWeight is the NoteContact weight of an address the user wrote to.
const SpamSQL = `` /* 129-byte string literal not displayed */
SpamSQL is true for a message (as m) that is spam in the inbox's sense: the local junk filter flagged it, or the AI labelled its conversation spam.
Variables ¶
var ErrNotFound = errors.New("not found")
ErrNotFound is returned when a row does not exist.
Functions ¶
func CleanupIdentity ¶ added in v0.135.0
CleanupIdentity identifies one message across local row reuse and mailbox resets.
Types ¶
type Account ¶
type Account struct {
ID int64 `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
Username string `json:"username"`
IMAPHost string `json:"imap_host"`
IMAPPort int `json:"imap_port"`
IMAPTLS string `json:"imap_tls"`
SMTPHost string `json:"smtp_host"`
SMTPPort int `json:"smtp_port"`
SMTPTLS string `json:"smtp_tls"`
Color string `json:"color"`
Signature string `json:"signature"`
CreatedAt int64 `json:"created_at"`
LastSync int64 `json:"last_sync"`
LastError string `json:"last_error"`
// Auth is how the account signs in: "password" (the default; the secret
// store holds the password) or "oauth2:google" / "oauth2:microsoft" (it
// holds the refresh token).
Auth string `json:"auth"`
}
Account is a configured mailbox. The password lives in the secret store.
type CleanupQuery ¶ added in v0.135.0
type CleanupQuery struct {
View string `json:"view"`
AccountID int64 `json:"account"`
Before int64 `json:"before"`
MinScore int `json:"min_score"`
}
CleanupQuery describes the synced messages a user can review for cleanup. Before is a message-date cutoff in Unix milliseconds, not time spent in Trash.
func (CleanupQuery) Validate ¶ added in v0.135.0
func (q CleanupQuery) Validate() error
type Counts ¶
type Counts struct {
Inbox int `json:"inbox"`
Unread int `json:"unread"`
Flagged int `json:"flagged"`
// Junk is the inbox conversations that are spam, by the junk filter or
// the AI (not the Junk folder).
Junk int `json:"junk"`
}
Counts are the sidebar badges.
type Folder ¶
type Folder struct {
ID int64 `json:"id"`
AccountID int64 `json:"account_id"`
Name string `json:"name"`
Delim string `json:"delim"`
Role string `json:"role"`
UIDValidity uint32 `json:"-"`
LastUID uint32 `json:"-"`
Total int `json:"total"`
Unread int `json:"unread"`
}
Folder is a server mailbox.
type JunkInput ¶ added in v0.125.0
type JunkInput struct {
ID int64
AccountID int64
FolderID int64
UID uint32
Role string // its folder's role
MessageID string
ThreadKey string
Subject string
FromName string
FromAddr string
ReplyTo string
Seen bool
Flagged bool
Answered bool
BodyText string
BodyHTML string
BodyState int
Hdr string // JSON spam.Header, ” when not read yet
Junk bool
JunkScore int
}
JunkInput is a message as the junk filter reads it.
type Message ¶
type Message struct {
ID int64 `json:"id"`
AccountID int64 `json:"account_id"`
FolderID int64 `json:"folder_id"`
UID uint32 `json:"uid"`
MessageID string `json:"message_id"`
InReplyTo string `json:"in_reply_to"`
Refs string `json:"references"`
ThreadKey string `json:"thread_key"`
Subject string `json:"subject"`
FromName string `json:"from_name"`
FromAddr string `json:"from_addr"`
To []Address `json:"to"`
Cc []Address `json:"cc"`
ReplyTo string `json:"reply_to"`
Date int64 `json:"date"`
Size int64 `json:"size"`
Seen bool `json:"seen"`
Flagged bool `json:"flagged"`
Answered bool `json:"answered"`
HasAttach bool `json:"has_attach"`
Snippet string `json:"snippet"`
BodyText string `json:"body_text,omitempty"`
BodyHTML string `json:"body_html,omitempty"`
Structure Structure `json:"structure"`
BodyState int `json:"body_state"`
ThreadSize int `json:"thread_size,omitempty"`
// The local junk filter's verdict (internal/mail/spam), inbox mail only.
Junk bool `json:"junk"`
JunkScore int `json:"junk_score"`
JunkWhy string `json:"junk_why,omitempty"`
// JunkHdr is the filter's facts from the header (JSON spam.Header),
// written once when the header is stored.
JunkHdr string `json:"-"`
// AI is the assistant's label for the conversation (List only).
AI *Triage `json:"ai,omitempty"`
}
Message is a stored message.
type Part ¶
type Part struct {
Path string `json:"path"` // "1.2"
Type string `json:"type"` // text/plain
Charset string `json:"charset,omitempty"`
Encoding string `json:"encoding,omitempty"`
Filename string `json:"filename,omitempty"`
Size int64 `json:"size"`
ContentID string `json:"cid,omitempty"`
Inline bool `json:"inline,omitempty"`
IsAttach bool `json:"attachment"`
IsTextBody bool `json:"-"`
IsHTMLBody bool `json:"-"`
}
Part is a body part addressed by its IMAP part path.
type Query ¶
type Query struct {
View string // inbox unread flagged sent drafts trash junk archive folder all
AccountID int64
FolderID int64
Search string
// Junk decides what happens to mail the junk filter flagged: "" leaves
// it out of inbox, unread and all (it shows everywhere else),
// "include" keeps it, "only" lists nothing else.
Junk string
// Category narrows the list to conversations the assistant labelled so:
// important updates newsletter spam, or "nospam" for everything not
// labelled spam. "" is no filter.
Category string
Before int64 // date cursor, exclusive
BeforeID int64
Limit int
}
Query selects a message list.
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store wraps the database.
func (*Store) AddAccount ¶
AddAccount inserts an account and returns its id.
func (*Store) AllowSender ¶ added in v0.125.0
AllowSender remembers that the user said "Not spam" for an address, and lifts the verdict from every inbox message it sent.
func (*Store) CleanupIDs ¶ added in v0.135.0
CleanupIDs includes all matching synced messages, independent of list paging. Spam is the server's Junk folder plus the filter/triage catch in the Inbox.
func (*Store) CleanupSnapshot ¶ added in v0.135.0
func (s *Store) CleanupSnapshot(ctx context.Context, q CleanupQuery) ([]int64, map[int64]string, error)
CleanupSnapshot binds local IDs to IMAP identities: SQLite may reuse a deleted ID.
func (*Store) DeleteAccount ¶
DeleteAccount removes an account and everything stored for it.
func (*Store) DeleteMessages ¶
DeleteMessages removes rows (expunged or moved away).
func (*Store) DisallowSender ¶ added in v0.125.0
DisallowSender forgets a vouched-for address (the user marked its mail as spam).
func (*Store) FolderByRole ¶
FolderByRole finds an account's folder with a role.
func (*Store) FolderUIDs ¶
FolderUIDs returns uid → state for a folder.
func (*Store) InboxIDs ¶ added in v0.125.0
InboxIDs lists the newest messages of every inbox, for judging again.
func (*Store) JunkInputs ¶ added in v0.125.0
JunkInputs loads messages with what the filter needs, bodies included.
func (*Store) JunkTokenCount ¶ added in v0.125.0
JunkTokenCount is the classifier's size, for tests and diagnostics.
func (*Store) JunkTotals ¶ added in v0.125.0
JunkTotals is how many spam and ham messages the classifier has learned.
func (*Store) KnownSender ¶ added in v0.125.0
KnownSender: the user vouched for the address (Not spam), wrote to it, or it is one of the user's own accounts. Mail from a known sender is never junk.
func (*Store) LabelTriage ¶ added in v0.125.0
func (s *Store) LabelTriage(ctx context.Context, accountID int64, key, category, sender string) error
LabelTriage is the user's own label for a conversation, kept from then on. A label other than spam also clears the sender, so their later mail is never sorted as spam.
func (*Store) Learn ¶ added in v0.125.0
func (s *Store) Learn(ctx context.Context, accountID int64, key string, tokens []string, spam, byUser bool) (bool, error)
Learn teaches the classifier one message. A message already learned the same way is skipped; one learned the other way has its tokens moved across. An automatic lesson (byUser false) never overrides the user's. Reports whether anything changed.
func (*Store) LiftKnownJunk ¶ added in v0.125.0
LiftKnownJunk clears the verdict from inbox mail that turned out to come from a known sender: someone the user has since written to (the Sent folder syncs after the inbox), or a conversation the user has replied in. Returns the folders whose counts changed.
func (*Store) MissingJunkHeader ¶ added in v0.125.0
MissingJunkHeader lists a folder's messages, newest first, whose header the filter has not read (stored before it existed).
func (*Store) NoteContact ¶
NoteContact remembers an address; sent-to weighs more than received-from.
func (*Store) PendingBodies ¶
PendingBodies lists messages of a folder that still need their body, newest first.
func (*Store) PruneJunkTokens ¶ added in v0.125.0
PruneJunkTokens keeps the classifier bounded: past max tokens, the least seen are dropped down to nine tenths of max (a token seen once or twice says almost nothing; Robinson's smoothing already keeps it near 0.5).
func (*Store) RecountFolder ¶
RecountFolder refreshes total and unread from stored rows.
func (*Store) RecountInbox ¶ added in v0.125.0
RecountInbox refreshes the counts of an account's inbox folders (a spam label changed: the unread count leaves spam out).
func (*Store) RemoveFoldersExcept ¶
RemoveFoldersExcept drops folders the server no longer lists.
func (*Store) ResetFolder ¶
ResetFolder drops every stored message (UIDVALIDITY changed) and sets the new validity.
func (*Store) SaveTriage ¶ added in v0.125.0
SaveTriage stores the model's label. A row the user labelled is left as it is; a spam verdict for a sender the user cleared takes the user's label.
func (*Store) SetJunkHeader ¶ added in v0.125.0
SetJunkHeader stores the filter's header facts of a message stored before the filter existed.
func (*Store) SetLastUID ¶
SetLastUID records the highest UID stored in a folder.
func (*Store) SetSetting ¶
SetSetting writes a setting.
func (*Store) SetSyncState ¶
SetSyncState records the outcome of a sync pass.
func (*Store) SuggestContacts ¶
SuggestContacts completes a prefix of a name or address.
func (*Store) Thread ¶
Thread returns every message of an account sharing the thread key, oldest first.
func (*Store) ThreadHasSent ¶ added in v0.125.0
ThreadHasSent: the conversation includes a message the user sent.
func (*Store) ThreadKeyFor ¶
ThreadKeyFor looks up the stored thread key of any message with this Message-ID.
func (*Store) ThreadMail ¶ added in v0.125.0
func (s *Store) ThreadMail(ctx context.Context, accountID int64, key string, n int) ([]Message, error)
ThreadMail is the newest n messages of a conversation in an inbox, with whatever body is stored (no server call), newest last.
func (*Store) TokenCounts ¶ added in v0.125.0
TokenCounts returns {spam, ham} for each token the classifier has seen.
func (*Store) TriageCounts ¶ added in v0.125.0
func (s *Store) TriageCounts(ctx context.Context) ([]TriageCount, error)
TriageCounts counts labelled conversations that still have mail in an inbox, by category.
func (*Store) TriageOf ¶ added in v0.125.0
TriageOf loads one conversation's label; ErrNotFound when it has none.
func (*Store) TriagePending ¶ added in v0.125.0
TriagePending lists, newest first, the conversations among the newest window inbox conversations that need sorting.
func (*Store) UnifiedCounts ¶
UnifiedCounts counts across accounts. Inbox and unread leave out spam (SpamSQL: the junk filter's flag or the AI's label); Junk counts the inbox conversations that are spam either way.
func (*Store) UnspamTriage ¶ added in v0.125.0
UnspamTriage is the AI's half of the user's "Not spam": a conversation the AI labelled spam takes the user's label "important", and the sender's later mail is never sorted as spam. A conversation the AI sorted otherwise keeps its label.
func (*Store) UpdateAccount ¶
UpdateAccount rewrites the editable fields; not auth (SetAuth).
type Structure ¶
type Structure struct {
Text *Part `json:"text,omitempty"`
HTML *Part `json:"html,omitempty"`
Parts []Part `json:"parts"` // attachments and inline images
}
Structure is what the body structure told us.
type Triage ¶ added in v0.125.0
type Triage struct {
AccountID int64 `json:"-"`
ThreadKey string `json:"-"`
MessageID int64 `json:"-"`
Category string `json:"category"`
Reply bool `json:"reply,omitempty"`
Summary string `json:"summary,omitempty"`
Model string `json:"model,omitempty"`
User bool `json:"user,omitempty"` // the user gave this label
At int64 `json:"at"`
}
Triage is the assistant's label for one conversation.
type TriageCount ¶ added in v0.125.0
type TriageCount struct {
AccountID int64 `json:"account_id"`
Category string `json:"category"`
N int `json:"n"`
}
TriageCount is how many inbox conversations of an account carry a label.
type TriageTodo ¶ added in v0.125.0
type TriageTodo struct {
AccountID int64
ThreadKey string
Newest int64 // the highest message row id of the conversation
}
TriageTodo is an inbox conversation that needs sorting: never sorted, or grown since (and not labelled by the user).