diff --git a/src/Internal/Values/Vault.elm b/src/Internal/Values/Vault.elm index 5153553..2725744 100644 --- a/src/Internal/Values/Vault.elm +++ b/src/Internal/Values/Vault.elm @@ -45,7 +45,7 @@ import Internal.Values.User as User exposing (User) type alias Vault = { accountData : Dict String Json.Value , rooms : Hashdict Room - , user : User + , user : Maybe User } @@ -81,7 +81,7 @@ coder = , coder = Hashdict.coder .roomId Room.coder } ) - (Json.field.required + (Json.field.optional.value { fieldName = "user" , toField = .user , description = Text.fields.vault.user @@ -106,11 +106,11 @@ getAccountData key vault = {-| Initiate a new Vault type. -} -init : User -> Vault -init user = +init : Maybe User -> Vault +init mUser = { accountData = Dict.empty , rooms = Hashdict.empty .roomId - , user = user + , user = mUser } @@ -156,4 +156,4 @@ update vu vault = setAccountData key value vault SetUser user -> - { vault | user = user } + { vault | user = Just user } diff --git a/src/Matrix.elm b/src/Matrix.elm index 0991107..f59136a 100644 --- a/src/Matrix.elm +++ b/src/Matrix.elm @@ -1,5 +1,5 @@ module Matrix exposing - ( Vault, fromUserId + ( Vault, fromUserId, fromUsername , VaultUpdate, update , addAccessToken, sendMessageEvent ) @@ -19,7 +19,7 @@ support a monolithic public registry. (: ## Vault -@docs Vault, fromUserId +@docs Vault, fromUserId, fromUsername ## Keeping the Vault up-to-date @@ -91,6 +91,27 @@ fromUserId uid = |> Maybe.map Vault +{-| Using a username and an address, create a Vault. + +The username can either be the localpart or the full Matrix ID. For example, +you can either insert `alice` or `@alice:example.org`. + +-} +fromUsername : { username : String, host : String, port_ : Maybe Int } -> Vault +fromUsername { username, host, port_ } = + { serverName = + port_ + |> Maybe.map String.fromInt + |> Maybe.map ((++) ":") + |> Maybe.withDefault "" + |> (++) host + , content = Internal.init (User.fromString username) + } + |> Envelope.init + |> Envelope.mapContext (\c -> { c | username = Just username }) + |> Vault + + {-| Send a message event to a room. This function can be used in a scenario where the user does not want to sync @@ -99,15 +120,25 @@ exists and the user is able to send a message to, and instead just sends the request to the Matrix API. -} -sendMessageEvent : Vault -> { content : E.Value, eventType : String, roomId : String, toMsg : VaultUpdate -> msg, transactionId : String } -> Cmd msg -sendMessageEvent (Vault vault) data = - Api.sendMessageEvent vault - { content = data.content - , eventType = data.eventType - , roomId = data.roomId - , toMsg = Types.VaultUpdate >> data.toMsg - , transactionId = data.transactionId - } +sendMessageEvent : + { content : E.Value + , eventType : String + , roomId : String + , toMsg : VaultUpdate -> msg + , transactionId : String + , vault : Vault + } + -> Cmd msg +sendMessageEvent data = + case data.vault of + Vault vault -> + Api.sendMessageEvent vault + { content = data.content + , eventType = data.eventType + , roomId = data.roomId + , toMsg = Types.VaultUpdate >> data.toMsg + , transactionId = data.transactionId + } {-| Using new VaultUpdate information, update the Vault accordingly. diff --git a/tests/Test/Values/Vault.elm b/tests/Test/Values/Vault.elm index 96922a8..3982791 100644 --- a/tests/Test/Values/Vault.elm +++ b/tests/Test/Values/Vault.elm @@ -19,4 +19,4 @@ vault = |> Fuzz.map Dict.fromList ) (TestHashdict.fuzzer .roomId TestRoom.fuzzer) - TestUser.fuzzer + (Fuzz.maybe TestUser.fuzzer)