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 = type alias Vault =
{ accountData : Dict String Json.Value { accountData : Dict String Json.Value
, rooms : Hashdict Room , rooms : Hashdict Room
, user : User , user : Maybe User
} }
@ -81,7 +81,7 @@ coder =
, coder = Hashdict.coder .roomId Room.coder , coder = Hashdict.coder .roomId Room.coder
} }
) )
(Json.field.required (Json.field.optional.value
{ fieldName = "user" { fieldName = "user"
, toField = .user , toField = .user
, description = Text.fields.vault.user , description = Text.fields.vault.user
@ -106,11 +106,11 @@ getAccountData key vault =
{-| Initiate a new Vault type. {-| Initiate a new Vault type.
-} -}
init : User -> Vault init : Maybe User -> Vault
init user = init mUser =
{ accountData = Dict.empty { accountData = Dict.empty
, rooms = Hashdict.empty .roomId , rooms = Hashdict.empty .roomId
, user = user , user = mUser
} }
@ -156,4 +156,4 @@ update vu vault =
setAccountData key value vault setAccountData key value vault
SetUser user -> SetUser user ->
{ vault | user = user } { vault | user = Just user }

View File

@ -1,5 +1,5 @@
module Matrix exposing module Matrix exposing
( Vault, fromUserId ( Vault, fromUserId, fromUsername
, VaultUpdate, update , VaultUpdate, update
, addAccessToken, sendMessageEvent , addAccessToken, sendMessageEvent
) )
@ -19,7 +19,7 @@ support a monolithic public registry. (:
## Vault ## Vault
@docs Vault, fromUserId @docs Vault, fromUserId, fromUsername
## Keeping the Vault up-to-date ## Keeping the Vault up-to-date
@ -91,6 +91,27 @@ fromUserId uid =
|> Maybe.map Vault |> 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. {-| Send a message event to a room.
This function can be used in a scenario where the user does not want to sync 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. request to the Matrix API.
-} -}
sendMessageEvent : Vault -> { content : E.Value, eventType : String, roomId : String, toMsg : VaultUpdate -> msg, transactionId : String } -> Cmd msg sendMessageEvent :
sendMessageEvent (Vault vault) data = { content : E.Value
Api.sendMessageEvent vault , eventType : String
{ content = data.content , roomId : String
, eventType = data.eventType , toMsg : VaultUpdate -> msg
, roomId = data.roomId , transactionId : String
, toMsg = Types.VaultUpdate >> data.toMsg , vault : Vault
, transactionId = data.transactionId }
} -> 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. {-| Using new VaultUpdate information, update the Vault accordingly.

View File

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