Kantoor. Chat
(Kantoor v1.0.0)
A simple chat with rooms, for the people of a mail domain.
The organisation is the mail domain: a user belongs to the chat of every domain
in which one of their own mailboxes lies (an administrator does not get a domain's
chat just by being an administrator). A user with mailboxes in two domains sees both,
kept apart. Every function takes a Kantoor.Accounts.Scope and checks that
membership again on every call, so a guessed id of a room of another domain answers
{:error, :not_found} and nothing else.
Postgres is the truth. After a write the change is broadcast on the PubSub topic of
the domain (topic/1) so that open pages update at once, also on other replicas
(Kantoor.Cluster); a page that missed a broadcast catches up from the database
(latest_seq/1). Messages are ordered, paged and counted as unread by seq, a
database sequence.
Audit. The chat keeps its data in Kantoor and keeps what an audit needs: an edit
stores the previous text (Kantoor.Chat.Edit), a delete is soft (the row keeps the
text, who deleted and when; it is blanked on the way out) until retention removes
it, and creating a room and changes of a private room's members go to the audit log
(Kantoor.Audit).
Sharing across organizations. A private room may hold members from other
organizations too. Its creator is its first administrator; an administrator may
make another member an administrator, remove members, end sharing (remove every member
from outside the room's organization) or close the room. The last administrator can
neither demote themself nor leave until they have named another one. An outside member
is brought in with a single-use invite link (Kantoor.Chat.Invite): only the hash
of the token is kept, the link expires (seven days by default) and can be revoked. The
person opening it signs in, says which of their organizations joins, sees the room,
the inviter and the organization, and joins; the link is consumed atomically, so of
two people using it at the same moment exactly one wins. An outside member sees that
room only: not the organization's other rooms, its people or its search. Files in a
shared room are readable by its members only, and the room's host organization's
retention and deletion rules apply.
The feature is off unless config :kantoor, Kantoor.Chat, enabled: true
(KANTOOR_CHAT=true).
Summary
Functions
The same as end_sharing/2 for a Kantoor administrator, who may do it for any shared
room (also one of an organization they have no mailbox in).
Archives a room (its creator or an administrator): it leaves the room strip, stays readable and can be restored, but nothing more can be posted or edited in it. Files stay downloadable by the people who can read the room.
The archived rooms the user may read, by name: public rooms of their organizations and private rooms they are in (a shared room of another organization too). They are not in the room strip, but stay readable.
Whether the user may archive or restore this room: its creator (who can still read it), an administrator of the room, or a Kantoor administrator.
The people of the room's own organization who are not in a private room yet (who a member of that organization may add). Empty for a public room, and for a person from another organization: a shared room shows no directory of the host organization.
Creates a single-use invite link for a private room (an administrator of the room does
it). Options: :days, how long the link lasts (default 7, at most
30). Returns {:ok, invite, token}; the token is shown once and only
its hash is stored. {:error, :archived} for an archived room.
Creates a room in a domain the user belongs to. Everybody may. attrs:
"name" (1 to 40 characters; the name of a public room is unique in the domain whatever
the case), "topic", "kind" ("private" for a room only its members see; the creator
is its first member).
Deletes the user's own message: it stays in the room as "message deleted" and its text is no longer shown. The row keeps the text, who deleted it and when until retention removes it.
Deletes a room for good (an administrator, or the :system actor of the operator
command): the messages, their edit history, the file rows and the stored bytes. The
caller must give the room's name again, exactly, as confirmation
({:error, :confirmation} otherwise).
Sends a file as the response, streamed or by send_file, never read whole into memory
(see get_file/2 for who may; Kantoor.Chat.Storage.deliver/2).
The domains whose chat the user belongs to: those of their own mailboxes.
Changes the text of the user's own message (it then shows as edited). Nobody else's message can be changed, and a deleted one cannot.
Whether the chat is switched on (KANTOOR_CHAT).
Ends sharing: removes every member whose organization is not the room's own. An administrator of the room does it; the room and its own members stay. Returns the number of members removed.
One file of a room the user may read: {:ok, file}, or {:error, :not_found} for a file
of another domain, of a private room they are not in, a deleted one, or a made-up id.
One message of a room the user may read (deleted ones without their text).
One room the user may see: a public room of one of their organizations, or any room they are a member of (also a shared room of another organization).
The handle a person is mentioned by: the part of their e-mail before the @, lower case.
The first bytes bytes of a file's content (to check what it really is).
Adds a person of the room's own organization to a private room. A member of that organization may (an outside member may not: they see no directory of it); a person of another organization cannot be added this way (a shared invite link is the way in).
The days a new invite link lasts unless another number (1 to 30) is asked.
What the person who opens a link sees before joining: the room's name, its
organization and who made the link. {:error, reason} with the state above.
Where an invite token stands: :open, :used, :revoked, :expired, :closed (the
room was archived) or :invalid. Needs no signed-in person and shows nothing of the room.
The newest message number in the rooms the user may read (for a page to notice that it missed something): their organizations' public rooms and every room they are a member of.
Leaves a private room. The room stays; the messages stay under the person's name. The
last administrator of a shared room cannot leave until they have made another member an
administrator ({:error, :last_admin}).
The open invite links of a private room (an administrator of the room does it).
Marks the room read up to seq (default: its newest message).
The longest message, in characters.
The longest an invite link may be made to last, in days.
Whether the user belongs to the chat of the domain.
The user's role in a room: "admin", "member" or nil when they have no member row.
The messages of a room, oldest first within the page. before: seq pages back.
Returns {:ok, messages, more?}; a page is 50 messages.
The messages around one message of a room (the 25 before it, it, and the 25 after),
oldest first: how a search result opens its room. {:ok, messages, more_before?, more_after?}.
The organizations that have a member in the room, by name (the room's own organization included). A room with more than one is shown as shared.
The people who can read a room, with the organization each is in and, in a private
room, their role: everyone with a mailbox in its domain for a public room, its members
for a private one. [%{id:, name:, handle:, org:, role:}], by name.
Posts a message. The body is plain text: control characters are dropped, line
breaks kept, at most 4000 characters; empty is refused unless files come with it.
20 messages per 10 seconds per user. files: [%{path:, name:}] attaches up to 5 files
(see Kantoor.Chat.Files: size, kind, scan, the domain's quota; 30 files per 10 minutes
per user). A message and its files are written together or not at all.
Uses an invite link: the person joins the room as the organization they chose
(domain_id must be one of their own). The invite is consumed atomically under a row
lock, so if two people use the same link at the same moment exactly one joins and the
other gets {:error, :used}. A person who is already a member joins without consuming
the link. Errors: :invalid, :used, :revoked, :expired, :closed,
:not_in_organization.
Removes a member from a private room (an administrator of the room does it). Removing yourself is leaving, with the last-administrator rule; the last administrator cannot be removed by anyone.
Restores an archived room (same people as archive_room/2).
Revokes an open invite link of a room (an administrator of the room does it).
Whether the user administers a room: the room's creator, a member an administrator made an administrator, or a Kantoor administrator.
The rooms the user may see, by organization, each with its unread count (messages of
others after what the user has read, and how many of those mention them) and whether
the room has members from another organization. A private room is listed only for its
members, also when the room belongs to another organization (a shared room).
[%{domain:, rooms: [%{room:, unread:, mentions:, shared:}]}], by organization name.
Searches the messages of the rooms the user may read: public rooms of their domains
and the private rooms they are in, never anybody else's. Words are matched whole
(the simple configuration, no stemming; quotes, - and or work as in the mail
search). Deleted messages are not found. Best match first, at most 50.
Options: room_id to search one room. 30 searches a minute per user.
Makes a member of a private room an administrator ("admin"), or an administrator a
member again ("member"). An administrator of the room does it. The last administrator
cannot demote themself ({:error, :last_admin}).
Every room with a member from another organization, with the organizations and the number of outside members (Kantoor administrators only). How the administrator of the room's own organization sees the shared rooms of the installation.
Subscribes the caller to the chat of every domain of the scope, and to their own events.
The PubSub topic of a domain's chat.
The total number of unread messages in the rooms the user may see (the dashboard badge).
The PubSub topic of one user. A private room's events are sent here, to each of its members, and never on the topic of the domain: a person who is not in the room never receives its messages, not even to ignore them.
Types
@type scope() :: Kantoor.Accounts.Scope.t() | nil
Functions
@spec admin_end_sharing(scope(), term()) :: {:ok, non_neg_integer()} | {:error, :not_found}
The same as end_sharing/2 for a Kantoor administrator, who may do it for any shared
room (also one of an organization they have no mailbox in).
@spec archive_room(scope(), term()) :: {:ok, Kantoor.Chat.Room.t()} | {:error, :not_found}
Archives a room (its creator or an administrator): it leaves the room strip, stays readable and can be restored, but nothing more can be posted or edited in it. Files stay downloadable by the people who can read the room.
@spec archived_rooms(scope()) :: [Kantoor.Chat.Room.t()]
The archived rooms the user may read, by name: public rooms of their organizations and private rooms they are in (a shared room of another organization too). They are not in the room strip, but stay readable.
@spec can_manage_room?(scope(), Kantoor.Chat.Room.t()) :: boolean()
Whether the user may archive or restore this room: its creator (who can still read it), an administrator of the room, or a Kantoor administrator.
The people of the room's own organization who are not in a private room yet (who a member of that organization may add). Empty for a public room, and for a person from another organization: a shared room shows no directory of the host organization.
@spec create_invite(scope(), term(), keyword()) :: {:ok, Kantoor.Chat.Invite.t(), String.t()} | {:error, :not_found | :archived | :rate_limited}
Creates a single-use invite link for a private room (an administrator of the room does
it). Options: :days, how long the link lasts (default 7, at most
30). Returns {:ok, invite, token}; the token is shown once and only
its hash is stored. {:error, :archived} for an archived room.
@spec create_room(scope(), term(), map()) :: {:ok, Kantoor.Chat.Room.t()} | {:error, Ecto.Changeset.t() | :not_found | :rate_limited}
Creates a room in a domain the user belongs to. Everybody may. attrs:
"name" (1 to 40 characters; the name of a public room is unique in the domain whatever
the case), "topic", "kind" ("private" for a room only its members see; the creator
is its first member).
@spec delete(scope(), term()) :: {:ok, Kantoor.Chat.Message.t()} | {:error, :not_found}
Deletes the user's own message: it stays in the room as "message deleted" and its text is no longer shown. The row keeps the text, who deleted it and when until retention removes it.
@spec delete_room(scope() | :system, term(), String.t()) :: {:ok, %{messages: integer(), files: integer(), files_removed: boolean()}} | {:error, :not_found | :confirmation}
Deletes a room for good (an administrator, or the :system actor of the operator
command): the messages, their edit history, the file rows and the stored bytes. The
caller must give the room's name again, exactly, as confirmation
({:error, :confirmation} otherwise).
Order: the rows and the audit row chat.room.delete go in one transaction (the room row
is locked first, so posts in flight finish or fail cleanly and are counted), the stored
bytes after the commit. When the store fails then, possibly halfway, the room is gone all
the same and the result says files_removed: false: it is logged and recorded as an audit
row chat.room.files_orphaned (with the room and domain ids) for an operator, who runs
Kantoor.Release.delete_chat_files/2. A leftover byte can be cleaned that way; a row
pointing at missing bytes could not be put right.
@spec deliver_file(Plug.Conn.t(), Kantoor.Chat.File.t()) :: {:ok, Plug.Conn.t()} | {:error, term()} | {:partial, Plug.Conn.t(), term()}
Sends a file as the response, streamed or by send_file, never read whole into memory
(see get_file/2 for who may; Kantoor.Chat.Storage.deliver/2).
@spec domains(scope()) :: [Kantoor.Mailboxes.Domain.t()]
The domains whose chat the user belongs to: those of their own mailboxes.
@spec edit(scope(), term(), String.t()) :: {:ok, Kantoor.Chat.Message.t()} | {:error, :not_found | :empty | :too_long | :rate_limited | :archived}
Changes the text of the user's own message (it then shows as edited). Nobody else's message can be changed, and a deleted one cannot.
@spec enabled?() :: boolean()
Whether the chat is switched on (KANTOOR_CHAT).
@spec end_sharing(scope(), term()) :: {:ok, non_neg_integer()} | {:error, :not_found}
Ends sharing: removes every member whose organization is not the room's own. An administrator of the room does it; the room and its own members stay. Returns the number of members removed.
@spec get_file(scope(), term()) :: {:ok, Kantoor.Chat.File.t()} | {:error, :not_found}
One file of a room the user may read: {:ok, file}, or {:error, :not_found} for a file
of another domain, of a private room they are not in, a deleted one, or a made-up id.
@spec get_message(scope(), term()) :: {:ok, Kantoor.Chat.Message.t()} | {:error, :not_found}
One message of a room the user may read (deleted ones without their text).
@spec get_room(scope(), term()) :: {:ok, Kantoor.Chat.Room.t()} | {:error, :not_found}
One room the user may see: a public room of one of their organizations, or any room they are a member of (also a shared room of another organization).
@spec handle(Kantoor.Accounts.User.t()) :: String.t()
The handle a person is mentioned by: the part of their e-mail before the @, lower case.
@spec head_file(Kantoor.Chat.File.t(), pos_integer()) :: {:ok, binary()} | {:error, term()}
The first bytes bytes of a file's content (to check what it really is).
Adds a person of the room's own organization to a private room. A member of that organization may (an outside member may not: they see no directory of it); a person of another organization cannot be added this way (a shared invite link is the way in).
The days a new invite link lasts unless another number (1 to 30) is asked.
@spec invite_preview(term()) :: {:ok, %{ room_name: String.t(), organization: String.t(), inviter: String.t() | nil, expires_at: DateTime.t() }} | {:error, atom()}
What the person who opens a link sees before joining: the room's name, its
organization and who made the link. {:error, reason} with the state above.
@spec invite_state(term()) :: :open | :used | :revoked | :expired | :closed | :invalid
Where an invite token stands: :open, :used, :revoked, :expired, :closed (the
room was archived) or :invalid. Needs no signed-in person and shows nothing of the room.
The newest message number in the rooms the user may read (for a page to notice that it missed something): their organizations' public rooms and every room they are a member of.
Leaves a private room. The room stays; the messages stay under the person's name. The
last administrator of a shared room cannot leave until they have made another member an
administrator ({:error, :last_admin}).
@spec list_invites(scope(), term()) :: {:ok, [Kantoor.Chat.Invite.t()]} | {:error, :not_found}
The open invite links of a private room (an administrator of the room does it).
Marks the room read up to seq (default: its newest message).
The longest message, in characters.
The longest an invite link may be made to last, in days.
Whether the user belongs to the chat of the domain.
The user's role in a room: "admin", "member" or nil when they have no member row.
@spec messages(scope(), term(), keyword()) :: {:ok, [Kantoor.Chat.Message.t()], boolean()} | {:error, :not_found}
The messages of a room, oldest first within the page. before: seq pages back.
Returns {:ok, messages, more?}; a page is 50 messages.
@spec messages_around(scope(), term(), integer()) :: {:ok, [Kantoor.Chat.Message.t()], boolean(), boolean()} | {:error, :not_found}
The messages around one message of a room (the 25 before it, it, and the 25 after),
oldest first: how a search result opens its room. {:ok, messages, more_before?, more_after?}.
@spec organizations(Kantoor.Chat.Room.t()) :: [String.t()]
The organizations that have a member in the room, by name (the room's own organization included). A room with more than one is shown as shared.
The people who can read a room, with the organization each is in and, in a private
room, their role: everyone with a mailbox in its domain for a public room, its members
for a private one. [%{id:, name:, handle:, org:, role:}], by name.
@spec post(scope(), term(), String.t(), keyword()) :: {:ok, Kantoor.Chat.Message.t()} | {:error, :not_found | :empty | :too_long | :rate_limited | :too_many_files | :quota | :storage | :archived | {:file, String.t(), atom()}}
Posts a message. The body is plain text: control characters are dropped, line
breaks kept, at most 4000 characters; empty is refused unless files come with it.
20 messages per 10 seconds per user. files: [%{path:, name:}] attaches up to 5 files
(see Kantoor.Chat.Files: size, kind, scan, the domain's quota; 30 files per 10 minutes
per user). A message and its files are written together or not at all.
@spec redeem_invite(scope(), term(), term()) :: {:ok, Kantoor.Chat.Room.t()} | {:error, :invalid | :used | :revoked | :expired | :closed | :not_in_organization}
Uses an invite link: the person joins the room as the organization they chose
(domain_id must be one of their own). The invite is consumed atomically under a row
lock, so if two people use the same link at the same moment exactly one joins and the
other gets {:error, :used}. A person who is already a member joins without consuming
the link. Errors: :invalid, :used, :revoked, :expired, :closed,
:not_in_organization.
Removes a member from a private room (an administrator of the room does it). Removing yourself is leaving, with the last-administrator rule; the last administrator cannot be removed by anyone.
@spec restore_room(scope(), term()) :: {:ok, Kantoor.Chat.Room.t()} | {:error, :not_found}
Restores an archived room (same people as archive_room/2).
@spec revoke_invite(scope(), term()) :: {:ok, Kantoor.Chat.Invite.t()} | {:error, :not_found}
Revokes an open invite link of a room (an administrator of the room does it).
@spec room_admin?(scope(), Kantoor.Chat.Room.t()) :: boolean()
Whether the user administers a room: the room's creator, a member an administrator made an administrator, or a Kantoor administrator.
@spec rooms(scope()) :: [ %{ domain: Kantoor.Mailboxes.Domain.t(), rooms: [ %{ room: Kantoor.Chat.Room.t(), unread: integer(), mentions: integer(), shared: boolean() } ] } ]
The rooms the user may see, by organization, each with its unread count (messages of
others after what the user has read, and how many of those mention them) and whether
the room has members from another organization. A private room is listed only for its
members, also when the room belongs to another organization (a shared room).
[%{domain:, rooms: [%{room:, unread:, mentions:, shared:}]}], by organization name.
@spec search(scope(), String.t(), keyword()) :: {:ok, [%{message: Kantoor.Chat.Message.t(), room: Kantoor.Chat.Room.t()}]} | {:error, :rate_limited}
Searches the messages of the rooms the user may read: public rooms of their domains
and the private rooms they are in, never anybody else's. Words are matched whole
(the simple configuration, no stemming; quotes, - and or work as in the mail
search). Deleted messages are not found. Best match first, at most 50.
Options: room_id to search one room. 30 searches a minute per user.
Returns {:ok, [%{message:, room:}]}.
@spec set_member_role(scope(), term(), term(), String.t()) :: {:ok, Kantoor.Chat.Member.t()} | {:error, :not_found | :last_admin}
Makes a member of a private room an administrator ("admin"), or an administrator a
member again ("member"). An administrator of the room does it. The last administrator
cannot demote themself ({:error, :last_admin}).
@spec subscribe(scope()) :: :ok
Subscribes the caller to the chat of every domain of the scope, and to their own events.
@spec topic(Ecto.UUID.t()) :: String.t()
The PubSub topic of a domain's chat.
@spec unread_total(scope()) :: non_neg_integer()
The total number of unread messages in the rooms the user may see (the dashboard badge).
@spec user_topic(Ecto.UUID.t()) :: String.t()
The PubSub topic of one user. A private room's events are sent here, to each of its members, and never on the topic of the domain: a person who is not in the room never receives its messages, not even to ignore them.