Move user from Vault to Envelop Context

4-transfer-api
Bram 2024-07-19 08:51:19 +02:00
parent 87ebcbcd21
commit 5319f47145
8 changed files with 159 additions and 39 deletions

View File

@ -191,7 +191,7 @@ loginWithUsernameAndPasswordV1 { username, password } =
}
, E.RemovePasswordIfNecessary
, out.user
|> Maybe.map (V.SetUser >> E.ContentUpdate)
|> Maybe.map E.SetUser
|> E.Optional
]
, Text.logs.loggedInAs username
@ -233,7 +233,7 @@ loginWithUsernameAndPasswordV2 { deviceId, initialDeviceDisplayName, username, p
}
, E.RemovePasswordIfNecessary
, out.user
|> Maybe.map (V.SetUser >> E.ContentUpdate)
|> Maybe.map E.SetUser
|> E.Optional
, out.deviceId
|> Maybe.map E.SetDeviceId
@ -285,7 +285,7 @@ loginWithUsernameAndPasswordV3 { deviceId, initialDeviceDisplayName, username, p
}
, E.RemovePasswordIfNecessary
, out.user
|> Maybe.map (V.SetUser >> E.ContentUpdate)
|> Maybe.map E.SetUser
|> E.Optional
, out.deviceId
|> Maybe.map E.SetDeviceId
@ -337,7 +337,7 @@ loginWithUsernameAndPasswordV4 { deviceId, initialDeviceDisplayName, username, p
}
, E.RemovePasswordIfNecessary
, out.user
|> Maybe.map (V.SetUser >> E.ContentUpdate)
|> Maybe.map E.SetUser
|> E.Optional
, out.wellKnown
|> Maybe.map (.homeserver >> .baseUrl)
@ -393,7 +393,7 @@ loginWithUsernameAndPasswordV5 { deviceId, initialDeviceDisplayName, username, p
}
, E.RemovePasswordIfNecessary
, out.user
|> Maybe.map (V.SetUser >> E.ContentUpdate)
|> Maybe.map E.SetUser
|> E.Optional
, out.wellKnown
|> Maybe.map (.homeserver >> .baseUrl)
@ -450,7 +450,7 @@ loginWithUsernameAndPasswordV6 { deviceId, enableRefreshToken, initialDeviceDisp
}
, E.RemovePasswordIfNecessary
, out.user
|> Maybe.map (V.SetUser >> E.ContentUpdate)
|> Maybe.map E.SetUser
|> E.Optional
, out.wellKnown
|> Maybe.map (.homeserver >> .baseUrl)
@ -506,7 +506,7 @@ loginWithUsernameAndPasswordV7 { deviceId, enableRefreshToken, initialDeviceDisp
, value = out.accessToken
}
, E.RemovePasswordIfNecessary
, E.ContentUpdate (V.SetUser out.user)
, E.SetUser out.user
, out.wellKnown
|> Maybe.map (.homeserver >> .baseUrl)
|> Maybe.map E.SetBaseUrl

View File

@ -278,6 +278,7 @@ fields :
, serverName : Desc
, suggestedAccessToken : Desc
, transaction : Desc
, user : Desc
, versions : Desc
}
, envelope :
@ -409,6 +410,9 @@ fields =
, transaction =
[ "A unique identifier for a transaction initiated by the user."
]
, user =
[ "The Matrix user the Vault is representing."
]
, versions =
[ "The versions of the Matrix protocol that are supported by the server."
]

View File

@ -1,6 +1,6 @@
module Internal.Tools.DecodeExtra exposing
( opField, opFieldWithDefault
, map9, map10, map11, map12
, map9, map10, map11, map12, map13
)
{-|
@ -18,7 +18,7 @@ This module contains helper functions that help decode JSON.
## Extended map functions
@docs map9, map10, map11, map12
@docs map9, map10, map11, map12, map13
-}
@ -185,3 +185,36 @@ map12 func da db dc dd de df dg dh di dj dk dl =
(D.map2 Tuple.pair dg dh)
(D.map2 Tuple.pair di dj)
(D.map2 Tuple.pair dk dl)
{-| Try 12 decoders and combine the result.
-}
map13 :
(a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m -> value)
-> D.Decoder a
-> D.Decoder b
-> D.Decoder c
-> D.Decoder d
-> D.Decoder e
-> D.Decoder f
-> D.Decoder g
-> D.Decoder h
-> D.Decoder i
-> D.Decoder j
-> D.Decoder k
-> D.Decoder l
-> D.Decoder m
-> D.Decoder value
map13 func da db dc dd de df dg dh di dj dk dl dm =
D.map8
(\a b c ( d, e ) ( f, g ) ( h, i ) ( j, k ) ( l, m ) ->
func a b c d e f g h i j k l m
)
da
db
dc
(D.map2 Tuple.pair dd de)
(D.map2 Tuple.pair df dg)
(D.map2 Tuple.pair dh di)
(D.map2 Tuple.pair dj dk)
(D.map2 Tuple.pair dl dm)

View File

@ -5,7 +5,7 @@ module Internal.Tools.Json exposing
, Docs(..), RequiredField(..), toDocs
, list, listWithOne, slowDict, fastDict, fastIntDict, set, iddict, maybe
, Field, field, parser
, object1, object2, object3, object4, object5, object6, object7, object8, object9, object10, object11, object12
, object1, object2, object3, object4, object5, object6, object7, object8, object9, object10, object11, object12, object13
)
{-|
@ -62,7 +62,7 @@ first.
Once all fields are constructed, the user can create JSON objects.
@docs object1, object2, object3, object4, object5, object6, object7, object8, object9, object10, object11, object12
@docs object1, object2, object3, object4, object5, object6, object7, object8, object9, object10, object11, object12, object13
-}
@ -1272,6 +1272,85 @@ object12 { name, description, init } fa fb fc fd fe ff fg fh fi fj fk fl =
}
{-| Define an object with 13 keys
-}
object13 :
Descriptive { init : a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m -> object }
-> Field a object
-> Field b object
-> Field c object
-> Field d object
-> Field e object
-> Field f object
-> Field g object
-> Field h object
-> Field i object
-> Field j object
-> Field k object
-> Field l object
-> Field m object
-> Coder object
object13 { name, description, init } fa fb fc fd fe ff fg fh fi fj fk fl fm =
Coder
{ encoder =
objectEncoder
[ toEncodeField fa
, toEncodeField fb
, toEncodeField fc
, toEncodeField fd
, toEncodeField fe
, toEncodeField ff
, toEncodeField fg
, toEncodeField fh
, toEncodeField fi
, toEncodeField fj
, toEncodeField fk
, toEncodeField fl
, toEncodeField fm
]
, decoder =
D.map13
(\( a, la ) ( b, lb ) ( c, lc ) ( d, ld ) ( e, le ) ( f, lf ) ( g, lg ) ( h, lh ) ( i, li ) ( j, lj ) ( k, lk ) ( l, ll ) ( m, lm ) ->
( init a b c d e f g h i j k l m
, List.concat [ la, lb, lc, ld, le, lf, lg, lh, li, lj, lk, ll, lm ]
)
)
(toDecoderField fa)
(toDecoderField fb)
(toDecoderField fc)
(toDecoderField fd)
(toDecoderField fe)
(toDecoderField ff)
(toDecoderField fg)
(toDecoderField fh)
(toDecoderField fi)
(toDecoderField fj)
(toDecoderField fk)
(toDecoderField fl)
(toDecoderField fm)
, docs =
DocsObject
{ name = name
, description = description
, keys =
[ toDocsField fa
, toDocsField fb
, toDocsField fc
, toDocsField fd
, toDocsField fe
, toDocsField ff
, toDocsField fg
, toDocsField fh
, toDocsField fi
, toDocsField fj
, toDocsField fk
, toDocsField fl
, toDocsField fm
]
}
}
{-| Define a parser that converts a string into a custom Elm type.
-}
parser : { name : String, p : P.Parser ( a, List Log ), toString : a -> String } -> Coder a

View File

@ -71,6 +71,7 @@ import Internal.Config.Text as Text
import Internal.Tools.Hashdict as Hashdict exposing (Hashdict)
import Internal.Tools.Json as Json
import Internal.Tools.Timestamp as Timestamp exposing (Timestamp)
import Internal.Values.User as User exposing (User)
import Set exposing (Set)
import Time
@ -101,6 +102,7 @@ type alias Context =
, serverName : String
, suggestedAccessToken : Maybe String
, transaction : Maybe String
, user : Maybe User
, username : Maybe String
, versions : Maybe Versions
}
@ -152,7 +154,7 @@ fromApiFormat (APIContext c) =
-}
coder : Json.Coder Context
coder =
Json.object12
Json.object13
{ name = Text.docs.context.name
, description = Text.docs.context.description
, init = Context
@ -227,6 +229,13 @@ coder =
, coder = Json.string
}
)
(Json.field.optional.value
{ fieldName = "user"
, toField = .user
, description = Text.fields.context.user
, coder = User.coder
}
)
(Json.field.optional.value
{ fieldName = "username"
, toField = .username
@ -305,8 +314,8 @@ encode =
{-| A basic, untouched version of the Context, containing no information.
-}
init : String -> Context
init sn =
init : String -> Maybe User -> Context
init sn mu =
{ accessTokens = Hashdict.empty .value
, baseUrl = Nothing
, deviceId = Nothing
@ -317,6 +326,7 @@ init sn =
, serverName = sn
, suggestedAccessToken = Nothing
, transaction = Nothing
, user = mu
, username = Nothing
, versions = Nothing
}

View File

@ -56,6 +56,7 @@ import Internal.Tools.Json as Json
import Internal.Tools.Timestamp exposing (Timestamp)
import Internal.Values.Context as Context exposing (AccessToken, Context, Versions)
import Internal.Values.Settings as Settings
import Internal.Values.User exposing (User)
import Recursion
import Recursion.Fold
@ -87,6 +88,7 @@ type EnvelopeUpdate a
| SetNextBatch String
| SetNow Timestamp
| SetRefreshToken String
| SetUser User
| SetVersions Versions
@ -188,10 +190,10 @@ getContent =
{-| Create a new enveloped data type. All settings are set to default values
from the [Internal.Config.Default](Internal-Config-Default) module.
-}
init : { serverName : String, content : a } -> Envelope a
init : { content : a, serverName : String, user : Maybe User } -> Envelope a
init data =
{ content = data.content
, context = Context.init data.serverName
, context = Context.init data.serverName data.user
, settings = Settings.init
}
@ -374,6 +376,12 @@ update updateContent eu startData =
{ data | context = { context | refreshToken = Just r } }
)
SetUser u ->
Recursion.base
(\({ context } as data) ->
{ data | context = { context | user = Just u } }
)
SetVersions vs ->
Recursion.base
(\({ context } as data) ->

View File

@ -54,7 +54,6 @@ type alias Vault =
{ accountData : Dict String Json.Value
, nextBatch : Maybe String
, rooms : Hashdict Room
, user : Maybe User
}
@ -68,14 +67,13 @@ type VaultUpdate
| Optional (Maybe VaultUpdate)
| SetAccountData String Json.Value
| SetNextBatch String
| SetUser User
{-| Convert a Vault to and from a JSON object.
-}
coder : Json.Coder Vault
coder =
Json.object4
Json.object3
{ name = Text.docs.vault.name
, description = Text.docs.vault.description
, init = Vault
@ -101,13 +99,6 @@ coder =
, coder = Hashdict.coder .roomId Room.coder
}
)
(Json.field.optional.value
{ fieldName = "user"
, toField = .user
, description = Text.fields.vault.user
, coder = User.coder
}
)
{-| Get a given room by its room id.
@ -126,12 +117,11 @@ getAccountData key vault =
{-| Initiate a new Vault type.
-}
init : Maybe User -> Vault
init mUser =
init : Vault
init =
{ accountData = Dict.empty
, nextBatch = Nothing
, rooms = Hashdict.empty .roomId
, user = mUser
}
@ -196,12 +186,6 @@ update vaultUpdate startVault =
(\vault ->
{ vault | nextBatch = Just nb }
)
SetUser user ->
Recursion.base
(\vault ->
{ vault | user = Just user }
)
)
vaultUpdate
startVault

View File

@ -97,8 +97,9 @@ fromUserId uid =
|> Maybe.map
(\u ->
Envelope.init
{ serverName = "https://" ++ User.domain u
, content = Internal.init (Just u)
{ content = Internal.init
, serverName = "https://" ++ User.domain u
, user = Just u
}
|> Envelope.mapContext (\c -> { c | username = Just uid })
)
@ -113,13 +114,14 @@ you can either insert `alice` or `@alice:example.org`.
-}
fromUsername : { username : String, host : String, port_ : Maybe Int } -> Vault
fromUsername { username, host, port_ } =
{ serverName =
{ content = Internal.init
, serverName =
port_
|> Maybe.map String.fromInt
|> Maybe.map ((++) ":")
|> Maybe.withDefault ""
|> (++) host
, content = Internal.init (User.fromString username)
, user = User.fromString username
}
|> Envelope.init
|> Envelope.mapContext (\c -> { c | username = Just username })