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

View File

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

View File

@ -1,6 +1,6 @@
module Internal.Tools.DecodeExtra exposing module Internal.Tools.DecodeExtra exposing
( opField, opFieldWithDefault ( 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 ## 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 dg dh)
(D.map2 Tuple.pair di dj) (D.map2 Tuple.pair di dj)
(D.map2 Tuple.pair dk dl) (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 , Docs(..), RequiredField(..), toDocs
, list, listWithOne, slowDict, fastDict, fastIntDict, set, iddict, maybe , list, listWithOne, slowDict, fastDict, fastIntDict, set, iddict, maybe
, Field, field, parser , 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. 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. {-| 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 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.Hashdict as Hashdict exposing (Hashdict)
import Internal.Tools.Json as Json import Internal.Tools.Json as Json
import Internal.Tools.Timestamp as Timestamp exposing (Timestamp) import Internal.Tools.Timestamp as Timestamp exposing (Timestamp)
import Internal.Values.User as User exposing (User)
import Set exposing (Set) import Set exposing (Set)
import Time import Time
@ -101,6 +102,7 @@ type alias Context =
, serverName : String , serverName : String
, suggestedAccessToken : Maybe String , suggestedAccessToken : Maybe String
, transaction : Maybe String , transaction : Maybe String
, user : Maybe User
, username : Maybe String , username : Maybe String
, versions : Maybe Versions , versions : Maybe Versions
} }
@ -152,7 +154,7 @@ fromApiFormat (APIContext c) =
-} -}
coder : Json.Coder Context coder : Json.Coder Context
coder = coder =
Json.object12 Json.object13
{ name = Text.docs.context.name { name = Text.docs.context.name
, description = Text.docs.context.description , description = Text.docs.context.description
, init = Context , init = Context
@ -227,6 +229,13 @@ coder =
, coder = Json.string , coder = Json.string
} }
) )
(Json.field.optional.value
{ fieldName = "user"
, toField = .user
, description = Text.fields.context.user
, coder = User.coder
}
)
(Json.field.optional.value (Json.field.optional.value
{ fieldName = "username" { fieldName = "username"
, toField = .username , toField = .username
@ -305,8 +314,8 @@ encode =
{-| A basic, untouched version of the Context, containing no information. {-| A basic, untouched version of the Context, containing no information.
-} -}
init : String -> Context init : String -> Maybe User -> Context
init sn = init sn mu =
{ accessTokens = Hashdict.empty .value { accessTokens = Hashdict.empty .value
, baseUrl = Nothing , baseUrl = Nothing
, deviceId = Nothing , deviceId = Nothing
@ -317,6 +326,7 @@ init sn =
, serverName = sn , serverName = sn
, suggestedAccessToken = Nothing , suggestedAccessToken = Nothing
, transaction = Nothing , transaction = Nothing
, user = mu
, username = Nothing , username = Nothing
, versions = Nothing , versions = Nothing
} }

View File

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

View File

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

View File

@ -97,8 +97,9 @@ fromUserId uid =
|> Maybe.map |> Maybe.map
(\u -> (\u ->
Envelope.init Envelope.init
{ serverName = "https://" ++ User.domain u { content = Internal.init
, content = Internal.init (Just u) , serverName = "https://" ++ User.domain u
, user = Just u
} }
|> Envelope.mapContext (\c -> { c | username = Just uid }) |> 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 : String, host : String, port_ : Maybe Int } -> Vault
fromUsername { username, host, port_ } = fromUsername { username, host, port_ } =
{ serverName = { content = Internal.init
, serverName =
port_ port_
|> Maybe.map String.fromInt |> Maybe.map String.fromInt
|> Maybe.map ((++) ":") |> Maybe.map ((++) ":")
|> Maybe.withDefault "" |> Maybe.withDefault ""
|> (++) host |> (++) host
, content = Internal.init (User.fromString username) , user = User.fromString username
} }
|> Envelope.init |> Envelope.init
|> Envelope.mapContext (\c -> { c | username = Just username }) |> Envelope.mapContext (\c -> { c | username = Just username })