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

65 lines
1.4 KiB
Elm
Raw Normal View History

module Internal.Values.Vault exposing (..)
2023-02-17 10:30:59 +00:00
{-| The Credentials type is the keychain of the Matrix SDK.
It handles all communication with the homeserver.
-}
import Internal.Tools.Hashdict as Hashdict exposing (Hashdict)
2023-03-03 15:07:37 +00:00
import Internal.Values.Room as Room exposing (IRoom)
type IVault
= IVault
2023-03-03 15:07:37 +00:00
{ rooms : Hashdict IRoom
, since : Maybe String
}
{-| Add a new `since` token to sync from.
-}
addSince : String -> IVault -> IVault
addSince since (IVault data) =
IVault { data | since = Just since }
2023-02-17 10:20:54 +00:00
2023-03-03 15:07:37 +00:00
{-| Get a room from the Credentials type by the room's id.
2023-03-01 11:06:01 +00:00
-}
getRoomById : String -> IVault -> Maybe IRoom
getRoomById roomId (IVault cred) =
2023-03-03 15:07:37 +00:00
Hashdict.get roomId cred.rooms
2023-02-17 10:30:59 +00:00
2023-03-03 15:07:37 +00:00
{-| Get a list of all synchronised rooms.
2023-02-17 10:30:59 +00:00
-}
getRooms : IVault -> List IRoom
getRooms (IVault { rooms }) =
2023-03-03 15:07:37 +00:00
Hashdict.values rooms
2023-02-17 10:20:54 +00:00
{-| Get the latest `since` token.
-}
getSince : IVault -> Maybe String
getSince (IVault { since }) =
since
2023-03-03 15:07:37 +00:00
{-| Create new empty Credentials.
2023-02-17 10:30:59 +00:00
-}
init : IVault
2023-03-03 15:07:37 +00:00
init =
IVault
2023-03-03 15:07:37 +00:00
{ rooms = Hashdict.empty Room.roomId
, since = Nothing
}
2023-02-17 10:30:59 +00:00
{-| Add a new room to the Credentials type. If a room with this id already exists, it is overwritten.
This function can hence also be used as an update function for rooms.
-}
insertRoom : IRoom -> IVault -> IVault
insertRoom room (IVault cred) =
IVault
{ cred | rooms = Hashdict.insert room cred.rooms }