elm-matrix-sdk-beta/src/Internal/Values/Vault.elm

170 lines
4.3 KiB
Elm
Raw Normal View History

2024-05-08 05:57:08 +00:00
module Internal.Values.Vault exposing
2024-05-26 11:12:03 +00:00
( Vault, init
2024-05-09 16:39:17 +00:00
, VaultUpdate(..), update
, fromRoomId, mapRoom, updateRoom
2024-05-08 05:57:08 +00:00
, getAccountData, setAccountData
)
2023-12-18 00:32:21 +00:00
2024-05-08 05:57:08 +00:00
{-| This module hosts the Vault module. The Vault is the data type storing all
credentials, all user information and all other information that the user
can receive from the Matrix API.
2023-12-18 00:32:21 +00:00
2024-05-08 05:57:08 +00:00
2024-05-09 16:39:17 +00:00
## Vault type
2024-05-26 11:12:03 +00:00
@docs Vault, init
2024-05-09 16:39:17 +00:00
To update the Vault, one uses VaultUpdate types.
@docs VaultUpdate, update
2024-05-08 05:57:08 +00:00
## Rooms
Rooms are environments where people can have a conversation with each other.
@docs fromRoomId, mapRoom, updateRoom
## Account data
@docs getAccountData, setAccountData
2023-12-18 00:32:21 +00:00
-}
2024-05-08 05:57:08 +00:00
import FastDict as Dict exposing (Dict)
2024-05-09 16:39:17 +00:00
import Internal.Config.Text as Text
2024-05-08 05:57:08 +00:00
import Internal.Tools.Hashdict as Hashdict exposing (Hashdict)
import Internal.Tools.Json as Json
2024-05-09 16:39:17 +00:00
import Internal.Values.Room as Room exposing (Room)
2024-05-24 13:15:44 +00:00
import Internal.Values.User as User exposing (User)
import Recursion
import Recursion.Fold
2024-05-08 05:57:08 +00:00
2023-12-18 00:32:21 +00:00
{-| This is the Vault type.
-}
2023-12-24 10:17:43 +00:00
type alias Vault =
2024-05-08 05:57:08 +00:00
{ accountData : Dict String Json.Value
, rooms : Hashdict Room
2024-05-30 11:54:30 +00:00
, user : Maybe User
2024-05-08 05:57:08 +00:00
}
2024-05-09 16:39:17 +00:00
{-| The VaultUpdate type is a type that instructs the Vault to update itself
based on new information provided by the Matrix API.
-}
type VaultUpdate
= CreateRoomIfNotExists String
| MapRoom String Room.RoomUpdate
| More (List VaultUpdate)
| SetAccountData String Json.Value
2024-05-24 13:15:44 +00:00
| SetUser User
2024-05-09 16:39:17 +00:00
coder : Json.Coder Vault
coder =
2024-05-24 13:15:44 +00:00
Json.object3
2024-05-09 16:39:17 +00:00
{ name = Text.docs.vault.name
, description = Text.docs.vault.description
, init = Vault
}
(Json.field.required
{ fieldName = "accountData"
, toField = .accountData
, description = Text.fields.vault.accountData
, coder = Json.fastDict Json.value
}
)
(Json.field.required
{ fieldName = "rooms"
, toField = .rooms
, description = Text.fields.vault.rooms
, coder = Hashdict.coder .roomId Room.coder
}
)
2024-05-30 11:54:30 +00:00
(Json.field.optional.value
2024-05-24 13:15:44 +00:00
{ fieldName = "user"
, toField = .user
2024-05-28 16:20:01 +00:00
, description = Text.fields.vault.user
2024-05-24 13:15:44 +00:00
, coder = User.coder
}
)
2024-05-09 16:39:17 +00:00
2024-05-08 05:57:08 +00:00
{-| Get a given room by its room id.
-}
fromRoomId : String -> Vault -> Maybe Room
fromRoomId roomId vault =
Hashdict.get roomId vault.rooms
{-| Get a piece of account data as information from the room.
-}
getAccountData : String -> Vault -> Maybe Json.Value
getAccountData key vault =
Dict.get key vault.accountData
2024-05-26 11:12:03 +00:00
{-| Initiate a new Vault type.
-}
2024-05-30 11:54:30 +00:00
init : Maybe User -> Vault
init mUser =
2024-05-26 11:12:03 +00:00
{ accountData = Dict.empty
, rooms = Hashdict.empty .roomId
2024-05-30 11:54:30 +00:00
, user = mUser
2024-05-26 11:12:03 +00:00
}
2024-05-08 05:57:08 +00:00
{-| Update a room, if it exists. If the room isn´t known, this operation is
ignored.
-}
mapRoom : String -> (Room -> Room) -> Vault -> Vault
mapRoom roomId f vault =
{ vault | rooms = Hashdict.map roomId f vault.rooms }
{-| Set a piece of account data as information in the global vault data.
-}
setAccountData : String -> Json.Value -> Vault -> Vault
setAccountData key value vault =
{ vault | accountData = Dict.insert key value vault.accountData }
{-| Update a Room based on whether it exists or not.
-}
updateRoom : String -> (Maybe Room -> Maybe Room) -> Vault -> Vault
updateRoom roomId f vault =
{ vault | rooms = Hashdict.update roomId f vault.rooms }
2024-05-09 16:39:17 +00:00
{-| Update the Vault using a VaultUpdate type.
-}
update : VaultUpdate -> Vault -> Vault
update vaultUpdate startVault =
Recursion.runRecursion
(\vu ->
case vu of
CreateRoomIfNotExists roomId ->
(Maybe.withDefault (Room.init roomId) >> Maybe.Just)
|> updateRoom roomId
|> Recursion.base
MapRoom roomId ru ->
Recursion.base (mapRoom roomId (Room.update ru))
More items ->
Recursion.Fold.foldList (<<) identity items
SetAccountData key value ->
Recursion.base (setAccountData key value)
SetUser user ->
Recursion.base
(\vault ->
{ vault | user = Just user }
)
)
vaultUpdate
startVault