Karta.Messages (Karta v1.0.0)

Stored messages: metadata rows (Karta.Messages.Message) and their encrypted bodies (Karta.Storage).

The storage protocol keeps an acknowledged message from ever being lost and a blob from ever being left without a row:

  1. create_pending/1 inserts the row as :pending (committed);
  2. the body is written to the blob store;
  3. mark_accepted/1 moves the row from :pending to :accepted with a conditional update; only then is the message acknowledged.

discard/3 removes messages: it moves rows to :discarding with a conditional update, deletes their blobs, and deletes the rows whose blob is gone. A failed blob delete leaves the row :discarding for the next sweep. Because both transitions out of :pending are conditional, a message is either acknowledged with its blob intact, or discarded and never acknowledged.

Reads are scoped by server: a message is found only through its own server.

Accepting a message announces it on its server's topic (:message_accepted), and removing accepted messages announces them on their servers' and their own topics (:messages_removed), see Karta.Changes.

Summary

Functions

How many of the server's accepted messages that match search were created after since.

Inserts a :pending message row.

How many messages the server accepted on each UTC day from the day days - 1 days before now's through it: [%{date: Date, incoming: n, outgoing: n}], oldest first, with the days without messages (count 0).

Removes the messages selected by ids (a list or an Ecto.Query selecting ids) that are in one of the from statuses. Returns how many were removed completely. opts go to Karta.Storage (e.g. blob_store:). Accepted messages that were removed are announced (:messages_removed).

A message of this server, or nil.

How many messages the server accepted in each hour from the hour of since to the hour of now: [{hour, count}], oldest first, with the hours without messages (count 0). An hour is the DateTime it starts at.

The server's accepted messages, newest first (limit: default 50): the first page of search/3 with no search, through Karta.Pager.

Moves a message from :pending to :accepted and announces it on its server's topic (after the commit, inside Karta.Changes.transaction/2); {:error, :not_pending} when it is no longer pending.

The server's accepted messages among ids that match search, in no order: what a page that shows them may show of them now.

The stored body of an accepted message, decrypted.

A page of the server's accepted messages that match search, newest first. Options: after, before (a message id) and limit (default 50), as for Karta.Pager.page/2.

Folds the stored body of an accepted message, decrypted, chunk by chunk (Karta.Storage.stream/4): see there for the folder's contract and the errors. A body is read this way when it is sent or checked, so that a message of any size costs the same memory.

Functions

count_since(server, search, since)

How many of the server's accepted messages that match search were created after since.

create_pending(attrs)

@spec create_pending(map()) :: {:ok, Karta.Messages.Message.t()}

Inserts a :pending message row.

daily_counts(server, days, now)

@spec daily_counts(Karta.Servers.Server.t(), pos_integer(), DateTime.t()) :: [
  %{date: Date.t(), incoming: non_neg_integer(), outgoing: non_neg_integer()}
]

How many messages the server accepted on each UTC day from the day days - 1 days before now's through it: [%{date: Date, incoming: n, outgoing: n}], oldest first, with the days without messages (count 0).

discard(ids, from_statuses, opts \\ [])

Removes the messages selected by ids (a list or an Ecto.Query selecting ids) that are in one of the from statuses. Returns how many were removed completely. opts go to Karta.Storage (e.g. blob_store:). Accepted messages that were removed are announced (:messages_removed).

get(server, id)

A message of this server, or nil.

hourly_counts(server, since, now \\ DateTime.utc_now())

@spec hourly_counts(Karta.Servers.Server.t(), DateTime.t(), DateTime.t()) :: [
  {DateTime.t(), non_neg_integer()}
]

How many messages the server accepted in each hour from the hour of since to the hour of now: [{hour, count}], oldest first, with the hours without messages (count 0). An hour is the DateTime it starts at.

list(server, opts \\ [])

The server's accepted messages, newest first (limit: default 50): the first page of search/3 with no search, through Karta.Pager.

mark_accepted(id)

@spec mark_accepted(Ecto.UUID.t()) :: :ok | {:error, :not_pending}

Moves a message from :pending to :accepted and announces it on its server's topic (after the commit, inside Karta.Changes.transaction/2); {:error, :not_pending} when it is no longer pending.

matching(server, search, ids)

The server's accepted messages among ids that match search, in no order: what a page that shows them may show of them now.

raw(message, opts \\ [])

@spec raw(Karta.Messages.Message.t(), keyword()) :: {:ok, binary()} | {:error, term()}

The stored body of an accepted message, decrypted.

search(server, search, opts \\ [])

A page of the server's accepted messages that match search, newest first. Options: after, before (a message id) and limit (default 50), as for Karta.Pager.page/2.

stream(message, acc, folder, opts)

@spec stream(
  Karta.Messages.Message.t(),
  acc,
  (binary(), acc -> {:ok, acc} | {:error, term()}),
  keyword()
) :: {:ok, acc} | {:error, term()}
when acc: var

Folds the stored body of an accepted message, decrypted, chunk by chunk (Karta.Storage.stream/4): see there for the folder's contract and the errors. A body is read this way when it is sent or checked, so that a message of any size costs the same memory.