Make username optional

pull/25/head
Bram 2024-05-30 13:54:30 +02:00
parent 994c99af15
commit 85d767414d
3 changed files with 49 additions and 18 deletions

View File

@ -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 }

View File

@ -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,8 +120,18 @@ 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 =
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

View File

@ -19,4 +19,4 @@ vault =
|> Fuzz.map Dict.fromList
)
(TestHashdict.fuzzer .roomId TestRoom.fuzzer)
TestUser.fuzzer
(Fuzz.maybe TestUser.fuzzer)