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

187 lines
4.5 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
2024-07-13 07:50:39 +00:00
, rooms, 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.
2024-07-13 07:50:39 +00:00
@docs rooms, fromRoomId, mapRoom, updateRoom
2024-05-08 05:57:08 +00:00
## 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)
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
2024-06-08 13:25:49 +00:00
, nextBatch : Maybe String
2024-05-08 05:57:08 +00:00
, 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)
2024-06-08 13:25:49 +00:00
| Optional (Maybe VaultUpdate)
2024-05-09 16:39:17 +00:00
| SetAccountData String Json.Value
2024-06-08 13:25:49 +00:00
| SetNextBatch String
2024-05-24 13:15:44 +00:00
| SetUser User
2024-05-09 16:39:17 +00:00
coder : Json.Coder Vault
coder =
2024-06-08 13:25:49 +00:00
Json.object4
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
}
)
2024-06-08 13:25:49 +00:00
(Json.field.optional.value
{ fieldName = "nextBatch"
, toField = .nextBatch
, description = Text.fields.vault.nextBatch
, coder = Json.string
}
)
2024-05-09 16:39:17 +00:00
(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
2024-06-08 13:25:49 +00:00
, nextBatch = Nothing
2024-05-26 11:12:03 +00:00
, 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 }
2024-07-13 07:50:39 +00:00
{-| Get a list of all joined rooms present in the vault.
-}
rooms : Vault -> List Room
rooms vault =
Hashdict.values vault.rooms
2024-05-08 05:57:08 +00:00
{-| 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 vu vault =
case vu of
CreateRoomIfNotExists roomId ->
updateRoom roomId
(Maybe.withDefault (Room.init roomId) >> Maybe.Just)
vault
MapRoom roomId ru ->
mapRoom roomId (Room.update ru) vault
More items ->
List.foldl update vault items
2024-06-08 13:25:49 +00:00
Optional (Just u) ->
update u vault
Optional Nothing ->
vault
2024-05-09 16:39:17 +00:00
SetAccountData key value ->
setAccountData key value vault
2024-05-24 13:15:44 +00:00
2024-06-08 13:25:49 +00:00
SetNextBatch nb ->
{ vault | nextBatch = Just nb }
2024-05-24 13:15:44 +00:00
SetUser user ->
2024-05-30 11:54:30 +00:00
{ vault | user = Just user }