2023-03-16 09:24:03 +00:00
|
|
|
module Matrix exposing
|
|
|
|
( Vault, fromLoginCredentials, fromAccessToken
|
|
|
|
, sync, VaultUpdate, updateWith
|
2023-04-04 14:11:16 +00:00
|
|
|
, getRooms, getRoomById, getInvites, accountData, username
|
2023-03-31 14:39:43 +00:00
|
|
|
, joinRoomById, setAccountData
|
2023-03-16 09:24:03 +00:00
|
|
|
)
|
2023-03-15 14:32:02 +00:00
|
|
|
|
|
|
|
{-| This is the main module of the SDK. Here, you will find basic functions to
|
|
|
|
interact with the API.
|
|
|
|
|
|
|
|
|
|
|
|
# Create a new Vault
|
|
|
|
|
|
|
|
@docs Vault, fromLoginCredentials, fromAccessToken
|
|
|
|
|
|
|
|
|
|
|
|
# Keeping your Vault up-to-date
|
|
|
|
|
|
|
|
@docs sync, VaultUpdate, updateWith
|
|
|
|
|
|
|
|
|
|
|
|
# Exploring your vault
|
|
|
|
|
2023-04-04 14:11:16 +00:00
|
|
|
@docs getRooms, getRoomById, getInvites, accountData, username
|
2023-03-16 09:24:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Taking action
|
|
|
|
|
2023-03-31 14:39:43 +00:00
|
|
|
@docs joinRoomById, setAccountData
|
2023-03-15 14:32:02 +00:00
|
|
|
|
|
|
|
-}
|
|
|
|
|
|
|
|
import Internal.Api.VaultUpdate as Api
|
|
|
|
import Internal.Invite exposing (RoomInvite)
|
|
|
|
import Internal.Room exposing (Room)
|
|
|
|
import Internal.Tools.Exceptions as X
|
|
|
|
import Internal.Vault
|
2023-03-31 14:39:43 +00:00
|
|
|
import Json.Encode as E
|
2023-03-15 14:32:02 +00:00
|
|
|
import Task exposing (Task)
|
|
|
|
|
|
|
|
|
|
|
|
{-| The Matrix API requires you to keep track of a lot of tokens, keys, values and more.
|
|
|
|
Luckily, you don't need to!
|
|
|
|
|
|
|
|
You can view the `Vault` as a large box of keys that will help you get
|
|
|
|
the information that you need. The type also caches information that it receives
|
|
|
|
from the API, so you can also ask it for information that it has seen before.
|
|
|
|
|
|
|
|
-}
|
|
|
|
type alias Vault =
|
|
|
|
Internal.Vault.Vault
|
|
|
|
|
|
|
|
|
|
|
|
{-| The `VaultUpdate` is a type that helps you keep your `Vault` type up-to-date.
|
|
|
|
Sometimes, the API will tell you to change certain tokens, and this SDK will
|
|
|
|
translate those instructions to a `VaultUpdate` that you can feed to your `Vault`.
|
|
|
|
-}
|
|
|
|
type alias VaultUpdate =
|
|
|
|
Api.VaultUpdate
|
|
|
|
|
|
|
|
|
|
|
|
{-| Create a new vault based on an access token.
|
|
|
|
Keep in mind that access tokens might eventually be revoked or expire,
|
|
|
|
so it is better to use login credentials if you're planning to use a Vault long-term.
|
|
|
|
-}
|
|
|
|
fromAccessToken : { accessToken : String, baseUrl : String } -> Vault
|
|
|
|
fromAccessToken =
|
|
|
|
Internal.Vault.fromAccessToken
|
|
|
|
|
|
|
|
|
|
|
|
{-| Based on a user's username and password, you can create a vault that will automatically
|
|
|
|
log in if an access token ever happens to run out, expire or lose contact in any other way.
|
|
|
|
-}
|
|
|
|
fromLoginCredentials : { baseUrl : String, username : String, password : String } -> Vault
|
|
|
|
fromLoginCredentials =
|
|
|
|
Internal.Vault.fromLoginVault
|
|
|
|
|
|
|
|
|
|
|
|
{-| Whenever you're asking the Matrix API for information that your Vault doesn't have,
|
|
|
|
you will receive a `VaultUpdate` type. This will reorganise your Vault with the newly
|
|
|
|
gained information.
|
|
|
|
|
|
|
|
After having updated your vault, it (usually) has all the information you need.
|
|
|
|
|
|
|
|
-}
|
|
|
|
updateWith : VaultUpdate -> Vault -> Vault
|
|
|
|
updateWith =
|
|
|
|
Internal.Vault.updateWith
|
|
|
|
|
|
|
|
|
|
|
|
{-| The Matrix API is always looking for new ways to optimize synchronising events to your client.
|
|
|
|
Luckily, you don't need to worry about keeping up a connection!
|
|
|
|
|
|
|
|
Your vault is always a snapshot of changes since the last time you updated it.
|
|
|
|
If you'd like to update it once more, simply run this function and the API will make sure that your Vault has the latest changes.
|
|
|
|
|
|
|
|
-}
|
2023-04-19 13:09:10 +00:00
|
|
|
sync : Vault -> (VaultUpdate -> msg) -> Cmd msg
|
2023-03-15 14:32:02 +00:00
|
|
|
sync =
|
|
|
|
Internal.Vault.sync
|
|
|
|
|
|
|
|
|
|
|
|
{-| Get all the rooms your user has joined, according to your vault.
|
|
|
|
-}
|
|
|
|
getRooms : Vault -> List Room
|
|
|
|
getRooms =
|
|
|
|
Internal.Vault.rooms
|
|
|
|
|
|
|
|
|
|
|
|
{-| Get a Matrix room by its id.
|
|
|
|
This will only return the room if you have joined the room.
|
|
|
|
-}
|
|
|
|
getRoomById : String -> Vault -> Maybe Room
|
|
|
|
getRoomById =
|
|
|
|
Internal.Vault.getRoomById
|
|
|
|
|
|
|
|
|
|
|
|
{-| Get all invites that the user is invited to.
|
|
|
|
-}
|
|
|
|
getInvites : Vault -> List RoomInvite
|
|
|
|
getInvites =
|
2023-04-12 13:36:56 +00:00
|
|
|
Internal.Vault.invites
|
2023-03-15 14:32:02 +00:00
|
|
|
|
|
|
|
|
2023-03-31 14:39:43 +00:00
|
|
|
{-| Account data is personal information that the homeserver will remember for you.
|
|
|
|
|
|
|
|
The information will be kept there, and will remain visible if you log in elsewhere.
|
|
|
|
Other users cannot see this information.
|
|
|
|
|
|
|
|
-}
|
|
|
|
accountData : String -> Vault -> Maybe E.Value
|
|
|
|
accountData =
|
|
|
|
Internal.Vault.accountData
|
|
|
|
|
|
|
|
|
2023-04-04 14:11:16 +00:00
|
|
|
{-| Get the username of the account that we're using.
|
|
|
|
|
|
|
|
The username is a `Maybe String` if the Vault hasn't had its first sync yet,
|
|
|
|
and the API might not always be consistent on the username.
|
2023-04-05 10:50:15 +00:00
|
|
|
|
2023-04-04 14:11:16 +00:00
|
|
|
-}
|
|
|
|
username : Vault -> Maybe String
|
|
|
|
username =
|
|
|
|
Internal.Vault.getUsername
|
|
|
|
|
|
|
|
|
2023-03-15 14:32:02 +00:00
|
|
|
{-| Join a Matrix room based on its room id.
|
|
|
|
-}
|
2023-04-19 13:09:10 +00:00
|
|
|
joinRoomById : { roomId : String, onResponse : VaultUpdate -> msg, vault : Vault } -> Cmd msg
|
2023-03-15 14:32:02 +00:00
|
|
|
joinRoomById =
|
|
|
|
Internal.Vault.joinRoomById
|
2023-03-31 14:39:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
{-| Update the user's personal account data. This saves the information on the homeserver's side and keeps it available for future use.
|
|
|
|
-}
|
2023-04-19 13:09:10 +00:00
|
|
|
setAccountData : { key : String, value : E.Value, onResponse : VaultUpdate -> msg, vault : Vault } -> Cmd msg
|
2023-03-31 14:39:43 +00:00
|
|
|
setAccountData =
|
|
|
|
Internal.Vault.setAccountData
|