Kantoor. Webmail
(Kantoor v1.0.0)
The webmail context: everything the UI does with mail. Every function takes
the Kantoor.Accounts.Scope first and authorizes the mailbox with
Kantoor.Accounts.can_access_mailbox?/2. A resource the scope may not see is
reported exactly like a missing one ({:error, :not_found}), so ids leak
nothing.
UID/MODSEQ/tombstone handling is delegated to Kantoor.Mailboxes (a move is
a new UID in the target plus a tombstone in the source). Every change
broadcasts on Kantoor.MailboxEvents.
Bodies come from the bounded cache (Kantoor.Bodies), falling back to Karta;
Karta failures surface as {:error, :body_unavailable}, never a crash.
Deleting from Trash expunges and asks Karta to delete the delivery in the
background, best effort.
Summary
Functions
Whether the message listing (envelope-only) shows an attachment.
Applies action to each message id the scope may access (others are skipped).
Actions: :read, :unread, :flag, :unflag, :delete, {:move, folder_id}.
Returns the number of messages changed.
Creates a folder in the scope's mailbox (/ makes a sub-folder).
{:error, :invalid | :exists | :not_found}.
Deletes a message. Outside Trash it moves to Trash; in Trash (or without a
Trash folder) it is expunged for good: tombstone, cached body dropped, a
best-effort background Karta delete, then held mail is released.
Returns {:ok, :trashed | :expunged}.
Removes a draft (the compose page's Discard). Only the scope's own draft in Drafts.
Deletes a folder with its mail for good. INBOX and the special folders are protected.
Deletes every message of the Trash or Junk folder for good. Any other folder
is refused ({:error, :not_allowed}). Returns the number of messages deleted.
Expunges one message without any authorization check (the caller has done
it): tombstone, cached body dropped, best-effort Karta delete, :removed
event. Callers expunging several messages call release_held/1 once after.
The attachments of a message, with their data, for a forward. Inline images that are only referenced by the HTML are not included.
Fetches a folder of the given (authorized) mailbox.
Fetches one of the scope's mailboxes.
Fetches a visible message the scope may read.
The special-use folder ("inbox" maps to INBOX) of the mailbox.
Count of messages held over quota (never listed).
Folders of the mailbox with unread counts, in the usual order.
The mailboxes the scope's user may open.
One page of a folder's visible messages, newest first, keyset-paginated on
the UID. Options: :before (UID cursor) and :limit.
Returns {:ok, messages, next_cursor | nil}.
The message's displayable content. Returns {:ok, message, content} (the
message is refreshed when a notify-only record was completed) or
{:error, :body_unavailable} when neither cache nor Karta can supply it.
Reopens a draft: its fields and attachments. Only drafts in the scope's own
Drafts folder qualify. A rich draft returns its HTML (with inline pictures
restored as data URIs) in "body_html" and "format" "html"; a draft
whose writer typed half an address returns it as it was typed.
The raw bytes of the message (cache, then Karta).
Marks every unread message of the folder read. Returns the count changed.
Marks every read message of the folder unread. Returns the count changed.
Moves a message to another folder of its mailbox (new UID, tombstone at the source).
The messages around message in the list it was opened from: {newer, older}
ids, each nil at the end of the list. Without a query that is the folder by
UID (the order of the list); with one, the search result in its own order.
Params to open a compose form for kind (:reply, :reply_all, :forward)
from message. {:error, :body_unavailable} when the body cannot be loaded.
Addresses used recently in the mailbox's Sent folder, newest first, for the
compose page's recipient suggestions. Each is %{name:, address:}.
Recipient suggestions for the compose page: the user's contacts (all address
books they can read) followed by recently used addresses, deduplicated by
address and capped at 400. Each is %{name:, address:}.
Releases over-quota held mail that now fits and announces it.
Renames a folder and the folders below it. {:error, :protected | :invalid | :exists | :not_found}.
Saves a draft in the Drafts folder. With a "draft_id" the same message row
is updated in place (same UID and message id), so autosave does not leave a
trail of copies; without one a new draft is created. Recipients may be empty
or half-written: the valid part goes in the address headers, the rest is
kept in X-Kantoor-Draft-* headers for the compose page. Drafts exist only
in Kantoor, so their cached body is pinned against eviction.
Builds the message now (so address and size errors are immediate), then sends
it after the undo-send window. The task is supervised and independent of the
page: navigating away still sends. {:cancel, token} sent to the returned
pid cancels the send and leaves any draft in place. When it is done, the task
sends {:send_result, token, result} to notify (a pid, or nil).
True when the message carries the flag.
Sends a message. params are the compose fields plus "from" (the id of one of
the user's own mailboxes) or "identity_id" (one of the user's Kantoor.Identities:
its name, address and reply-to are applied and its signature appended), and
optionally "draft_id". attachments is a list of
%{name:, content_type:, data:}.
Flags or unflags (star) a message.
Marks a message read or unread.
Stores a copy of a message that was sent outside the web UI (SMTP submission)
in the mailbox's Sent folder, the same way a webmail send does. response is
Karta's answer to the send. nil when there is no Sent folder or the copy
could not be stored; the message has been sent either way.
How long the undo-send window is, in milliseconds.
Types
Functions
@spec attachments?(Kantoor.Mailboxes.Message.t()) :: boolean()
Whether the message listing (envelope-only) shows an attachment.
@spec bulk(Kantoor.Accounts.Scope.t() | nil, [term()], atom() | tuple()) :: non_neg_integer()
Applies action to each message id the scope may access (others are skipped).
Actions: :read, :unread, :flag, :unflag, :delete, {:move, folder_id}.
Returns the number of messages changed.
@spec create_folder( Kantoor.Accounts.Scope.t() | nil, Kantoor.Mailboxes.Mailbox.t(), String.t() ) :: {:ok, Kantoor.Mailboxes.Folder.t()} | {:error, :invalid | :exists | :not_found}
Creates a folder in the scope's mailbox (/ makes a sub-folder).
{:error, :invalid | :exists | :not_found}.
@spec delete(Kantoor.Accounts.Scope.t() | nil, Kantoor.Mailboxes.Message.t()) :: {:ok, :trashed | :expunged} | not_found()
Deletes a message. Outside Trash it moves to Trash; in Trash (or without a
Trash folder) it is expunged for good: tombstone, cached body dropped, a
best-effort background Karta delete, then held mail is released.
Returns {:ok, :trashed | :expunged}.
@spec delete_draft(Kantoor.Accounts.Scope.t() | nil, term()) :: :ok
Removes a draft (the compose page's Discard). Only the scope's own draft in Drafts.
@spec delete_folder( Kantoor.Accounts.Scope.t() | nil, Kantoor.Mailboxes.Mailbox.t(), Kantoor.Mailboxes.Folder.t() ) :: :ok | {:error, :protected | :not_found}
Deletes a folder with its mail for good. INBOX and the special folders are protected.
@spec empty_folder(Kantoor.Accounts.Scope.t() | nil, Kantoor.Mailboxes.Folder.t()) :: {:ok, non_neg_integer()} | {:error, :not_found | :not_allowed}
Deletes every message of the Trash or Junk folder for good. Any other folder
is refused ({:error, :not_allowed}). Returns the number of messages deleted.
@spec expunge_one(Kantoor.Mailboxes.Message.t()) :: {:ok, Kantoor.Mailboxes.Message.t()} | {:error, term()}
Expunges one message without any authorization check (the caller has done
it): tombstone, cached body dropped, best-effort Karta delete, :removed
event. Callers expunging several messages call release_held/1 once after.
@spec flagged?(Kantoor.Mailboxes.Message.t()) :: boolean()
@spec forward_attachments( Kantoor.Accounts.Scope.t() | nil, Kantoor.Mailboxes.Message.t() ) :: [map()]
The attachments of a message, with their data, for a forward. Inline images that are only referenced by the HTML are not included.
@spec get_folder( Kantoor.Accounts.Scope.t() | nil, Kantoor.Mailboxes.Mailbox.t(), term() ) :: {:ok, Kantoor.Mailboxes.Folder.t()} | not_found()
Fetches a folder of the given (authorized) mailbox.
@spec get_mailbox(Kantoor.Accounts.Scope.t() | nil, term()) :: {:ok, Kantoor.Mailboxes.Mailbox.t()} | not_found()
Fetches one of the scope's mailboxes.
@spec get_message(Kantoor.Accounts.Scope.t() | nil, term()) :: {:ok, Kantoor.Mailboxes.Message.t()} | not_found()
Fetches a visible message the scope may read.
@spec get_special_folder( Kantoor.Accounts.Scope.t() | nil, Kantoor.Mailboxes.Mailbox.t(), String.t() ) :: {:ok, Kantoor.Mailboxes.Folder.t()} | not_found()
The special-use folder ("inbox" maps to INBOX) of the mailbox.
@spec held_count(Kantoor.Accounts.Scope.t() | nil, Kantoor.Mailboxes.Mailbox.t()) :: non_neg_integer()
Count of messages held over quota (never listed).
@spec list_folders(Kantoor.Accounts.Scope.t() | nil, Kantoor.Mailboxes.Mailbox.t()) :: [ %{folder: Kantoor.Mailboxes.Folder.t(), unread: integer()} ]
Folders of the mailbox with unread counts, in the usual order.
@spec list_mailboxes(Kantoor.Accounts.Scope.t() | nil) :: [ Kantoor.Mailboxes.Mailbox.t() ]
The mailboxes the scope's user may open.
@spec list_messages( Kantoor.Accounts.Scope.t() | nil, Kantoor.Mailboxes.Folder.t(), keyword() ) :: {:ok, [Kantoor.Mailboxes.Message.t()], integer() | nil} | not_found()
One page of a folder's visible messages, newest first, keyset-paginated on
the UID. Options: :before (UID cursor) and :limit.
Returns {:ok, messages, next_cursor | nil}.
@spec load_content(Kantoor.Accounts.Scope.t() | nil, Kantoor.Mailboxes.Message.t()) :: {:ok, Kantoor.Mailboxes.Message.t(), Kantoor.Mime.Content.t()} | {:error, :body_unavailable | :not_found}
The message's displayable content. Returns {:ok, message, content} (the
message is refreshed when a notify-only record was completed) or
{:error, :body_unavailable} when neither cache nor Karta can supply it.
@spec load_draft(Kantoor.Accounts.Scope.t() | nil, term()) :: {:ok, Kantoor.Mailboxes.Message.t(), map(), [map()]} | {:error, :not_found | :body_unavailable}
Reopens a draft: its fields and attachments. Only drafts in the scope's own
Drafts folder qualify. A rich draft returns its HTML (with inline pictures
restored as data URIs) in "body_html" and "format" "html"; a draft
whose writer typed half an address returns it as it was typed.
@spec load_raw(Kantoor.Accounts.Scope.t() | nil, Kantoor.Mailboxes.Message.t()) :: {:ok, binary()} | {:error, :body_unavailable | :not_found}
The raw bytes of the message (cache, then Karta).
@spec mark_all_read(Kantoor.Accounts.Scope.t() | nil, Kantoor.Mailboxes.Folder.t()) :: {:ok, non_neg_integer()} | not_found()
Marks every unread message of the folder read. Returns the count changed.
Marks every read message of the folder unread. Returns the count changed.
@spec move(Kantoor.Accounts.Scope.t() | nil, Kantoor.Mailboxes.Message.t(), term()) :: {:ok, Kantoor.Mailboxes.Message.t()} | {:error, :not_found | :same_folder}
Moves a message to another folder of its mailbox (new UID, tombstone at the source).
@spec neighbours( Kantoor.Accounts.Scope.t() | nil, Kantoor.Mailboxes.Message.t(), String.t() ) :: {Ecto.UUID.t() | nil, Ecto.UUID.t() | nil}
The messages around message in the list it was opened from: {newer, older}
ids, each nil at the end of the list. Without a query that is the folder by
UID (the order of the list); with one, the search result in its own order.
@spec prefill(Kantoor.Accounts.Scope.t() | nil, atom(), Kantoor.Mailboxes.Message.t()) :: {:ok, map()} | {:error, :not_found | :body_unavailable}
Params to open a compose form for kind (:reply, :reply_all, :forward)
from message. {:error, :body_unavailable} when the body cannot be loaded.
@spec recent_addresses( Kantoor.Accounts.Scope.t() | nil, Kantoor.Mailboxes.Mailbox.t(), pos_integer() ) :: [map()]
Addresses used recently in the mailbox's Sent folder, newest first, for the
compose page's recipient suggestions. Each is %{name:, address:}.
@spec recipient_suggestions( Kantoor.Accounts.Scope.t() | nil, Kantoor.Mailboxes.Mailbox.t() ) :: [map()]
Recipient suggestions for the compose page: the user's contacts (all address
books they can read) followed by recently used addresses, deduplicated by
address and capped at 400. Each is %{name:, address:}.
@spec release_held(Kantoor.Mailboxes.Mailbox.t()) :: :ok
Releases over-quota held mail that now fits and announces it.
@spec rename_folder( Kantoor.Accounts.Scope.t() | nil, Kantoor.Mailboxes.Mailbox.t(), Kantoor.Mailboxes.Folder.t(), String.t() ) :: :ok | {:error, :protected | :invalid | :exists | :not_found}
Renames a folder and the folders below it. {:error, :protected | :invalid | :exists | :not_found}.
@spec save_draft(Kantoor.Accounts.Scope.t() | nil, map(), [map()]) :: {:ok, Kantoor.Mailboxes.Message.t()} | {:error, :not_found | [String.t()]}
Saves a draft in the Drafts folder. With a "draft_id" the same message row
is updated in place (same UID and message id), so autosave does not leave a
trail of copies; without one a new draft is created. Recipients may be empty
or half-written: the valid part goes in the address headers, the rest is
kept in X-Kantoor-Draft-* headers for the compose page. Drafts exist only
in Kantoor, so their cached body is pinned against eviction.
@spec schedule_send(Kantoor.Accounts.Scope.t() | nil, map(), [map()], pid() | nil) :: {:ok, String.t(), pid()} | {:error, :not_found | :send_failed | [String.t()]}
Builds the message now (so address and size errors are immediate), then sends
it after the undo-send window. The task is supervised and independent of the
page: navigating away still sends. {:cancel, token} sent to the returned
pid cancels the send and leaves any draft in place. When it is done, the task
sends {:send_result, token, result} to notify (a pid, or nil).
Returns {:ok, token, pid} or the build error.
@spec seen?(Kantoor.Mailboxes.Message.t()) :: boolean()
True when the message carries the flag.
@spec send_message(Kantoor.Accounts.Scope.t() | nil, map(), [map()]) :: {:ok, Kantoor.Mailboxes.Message.t() | nil} | {:error, :not_found | :send_failed | [String.t()]}
Sends a message. params are the compose fields plus "from" (the id of one of
the user's own mailboxes) or "identity_id" (one of the user's Kantoor.Identities:
its name, address and reply-to are applied and its signature appended), and
optionally "draft_id". attachments is a list of
%{name:, content_type:, data:}.
On {:error, ...} nothing was stored or removed: an existing draft stays.
After a successful send the copy goes to the Sent folder (UID like any other
message, body cached) and the draft, if any, is removed.
@spec set_flagged( Kantoor.Accounts.Scope.t() | nil, Kantoor.Mailboxes.Message.t(), boolean() ) :: {:ok, Kantoor.Mailboxes.Message.t()} | not_found()
Flags or unflags (star) a message.
@spec set_seen( Kantoor.Accounts.Scope.t() | nil, Kantoor.Mailboxes.Message.t(), boolean() ) :: {:ok, Kantoor.Mailboxes.Message.t()} | not_found()
Marks a message read or unread.
@spec store_sent_copy( Kantoor.Mailboxes.Mailbox.t(), binary(), String.t(), [String.t()], term() ) :: Kantoor.Mailboxes.Message.t() | nil
Stores a copy of a message that was sent outside the web UI (SMTP submission)
in the mailbox's Sent folder, the same way a webmail send does. response is
Karta's answer to the send. nil when there is no Sent folder or the copy
could not be stored; the message has been sent either way.
@spec undo_send_ms() :: non_neg_integer()
How long the undo-send window is, in milliseconds.