Rename CredUpdate -> VaultUpdate

pull/1/head
Bram van den Heuvel 2023-03-14 15:50:23 +01:00
parent e90f823000
commit 10903b0e05
7 changed files with 68 additions and 66 deletions

View File

@ -14,18 +14,18 @@ submodule, this can lead to indentation hell.
This module aims to allow for simple task chaining without adding too much complexity
if you wish to pass on values.
The model is like a snake: _____
/ o \
/-|------------ | ------- | ------------- | -------- | |\/\/
The model is like a snake: \_\_\_\_\_
/ o \\
/-|------------ | ------- | ------------- | -------- | |//
< | accessToken | baseUrl | transactionId | API call | |------< Final API call
\-|------------ | ------- | ------------- | -------- | |/\/\
\-----/
-|------------ | ------- | ------------- | -------- | |//\\
-----/
(You're not allowed to judge my ASCII art skills unless you submit a PR with a
superior ASCII snake model.)
Every task will add another value to an extensible record, which can be used
by later tasks in the chain. Additionally, every subtask can leave a `CredUpdate`
by later tasks in the chain. Additionally, every subtask can leave a `VaultUpdate`
type as a message to the Credentials to update certain information.
-}

View File

@ -1,13 +1,14 @@
module Internal.Api.GetEvent.Main exposing (..)
import Internal.Api.GetEvent.Api as Api
import Internal.Tools.Context as Context exposing (Context, VBA)
import Internal.Tools.Exceptions as X
import Internal.Tools.VersionControl as VC
import Task exposing (Task)
getEvent : List String -> EventInput -> Task X.Error EventOutput
getEvent versions =
getEvent : Context (VBA a) -> EventInput -> Task X.Error EventOutput
getEvent context input =
VC.withBottomLayer
{ current = Api.getEventInputV1
, version = "r0.5.0"
@ -19,8 +20,10 @@ getEvent versions =
|> VC.sameForVersion "v1.3"
|> VC.sameForVersion "v1.4"
|> VC.sameForVersion "v1.5"
|> VC.mostRecentFromVersionList versions
|> Maybe.withDefault (always <| Task.fail X.UnsupportedSpecVersion)
|> VC.mostRecentFromVersionList (Context.getVersions context)
|> Maybe.withDefault (always <| always <| Task.fail X.UnsupportedSpecVersion)
|> (|>) input
|> (|>) context
type alias EventOutput =

View File

@ -18,7 +18,9 @@ sync context input =
|> VC.addMiddleLayer
{ current = Api.syncV2
, downcast = identity
, upcast = Task.map U2.upcastSync
, upcast =
\f c ->
Task.map U2.upcastSync (f c)
, version = "v1.4"
}
|> VC.sameForVersion "v1.5"

View File

@ -5,8 +5,13 @@ module Internal.Api.Task exposing (..)
import Hash
import Internal.Api.Chain as Chain
import Internal.Api.CredUpdate as C
import Internal.Api.Credentials exposing (Credentials)
import Internal.Api.GetEvent.Main exposing (EventInput)
import Internal.Api.Invite.Main exposing (InviteInput)
import Internal.Api.JoinedMembers.Main exposing (JoinedMembersInput)
import Internal.Api.SendStateKey.Main exposing (SendStateKeyInput)
import Internal.Api.Sync.Main exposing (SyncInput)
import Internal.Api.VaultUpdate as C
import Json.Encode as E
@ -14,21 +19,21 @@ type alias FutureTask =
C.FutureTask
getEvent : C.GetEventInput -> Credentials -> FutureTask
getEvent : EventInput -> Credentials -> FutureTask
getEvent data cred =
C.makeVBA cred
|> Chain.andThen (C.getEvent data)
|> C.toTask
invite : C.InviteInput -> Credentials -> FutureTask
invite : InviteInput -> Credentials -> FutureTask
invite data cred =
C.makeVBA cred
|> Chain.andThen (C.invite data)
|> C.toTask
joinedMembers : C.JoinedMembersInput -> Credentials -> FutureTask
joinedMembers : JoinedMembersInput -> Credentials -> FutureTask
joinedMembers data cred =
C.makeVBA cred
|> Chain.andThen (C.joinedMembers data)
@ -90,15 +95,15 @@ sendMessageEvent { content, eventType, extraTransactionNoise, roomId } cred =
|> C.toTask
sendStateKey : C.SendStateEventInput -> Credentials -> FutureTask
sendStateKey data cred =
sendStateEvent : SendStateKeyInput -> Credentials -> FutureTask
sendStateEvent data cred =
C.makeVBA cred
|> Chain.andThen (C.sendStateEvent data)
-- TODO: Get event from API to see what it looks like
|> C.toTask
sync : C.SyncInput -> Credentials -> FutureTask
sync : SyncInput -> Credentials -> FutureTask
sync data cred =
C.makeVBA cred
|> Chain.andThen (C.sync data)

View File

@ -1,4 +1,4 @@
module Internal.Api.CredUpdate exposing (..)
module Internal.Api.VaultUpdate exposing (..)
import Internal.Api.Chain as Chain exposing (IdemChain, TaskChain)
import Internal.Api.Credentials as Credentials exposing (Credentials)
@ -15,14 +15,12 @@ import Internal.Api.Versions.V1.Versions as V
import Internal.Tools.Context as Context exposing (VB, VBA, VBAT)
import Internal.Tools.Exceptions as X
import Internal.Tools.LoginValues exposing (AccessToken(..))
import Internal.Tools.SpecEnums as Enums
import Json.Encode as E
import Task exposing (Task)
import Time
type CredUpdate
= MultipleUpdates (List CredUpdate)
type VaultUpdate
= MultipleUpdates (List VaultUpdate)
-- Updates as a result of API calls
| GetEvent GetEvent.EventInput GetEvent.EventOutput
| InviteSent Invite.InviteInput Invite.InviteOutput
@ -38,12 +36,12 @@ type CredUpdate
type alias FutureTask =
Task X.Error CredUpdate
Task X.Error VaultUpdate
{-| Turn an API Task into a taskchain.
-}
toChain : (cout -> Chain.TaskChainPiece CredUpdate ph1 ph2) -> (Context.Context ph1 -> cin -> Task X.Error cout) -> cin -> TaskChain CredUpdate ph1 ph2
toChain : (cout -> Chain.TaskChainPiece VaultUpdate ph1 ph2) -> (Context.Context ph1 -> cin -> Task X.Error cout) -> cin -> TaskChain VaultUpdate ph1 ph2
toChain transform task input context =
task context input
|> Task.map transform
@ -51,7 +49,7 @@ toChain transform task input context =
{-| Turn a chain of tasks into a full executable task.
-}
toTask : TaskChain CredUpdate {} b -> FutureTask
toTask : TaskChain VaultUpdate {} b -> FutureTask
toTask =
Chain.toTask
>> Task.map
@ -67,7 +65,7 @@ toTask =
{-| Get a functional access token.
-}
accessToken : AccessToken -> TaskChain CredUpdate (VB a) (VBA a)
accessToken : AccessToken -> TaskChain VaultUpdate (VB a) (VBA a)
accessToken ctoken =
case ctoken of
NoAccess ->
@ -96,7 +94,7 @@ accessToken ctoken =
{-| Get an event from the API.
-}
getEvent : GetEvent.EventInput -> IdemChain CredUpdate (VBA a)
getEvent : GetEvent.EventInput -> IdemChain VaultUpdate (VBA a)
getEvent input =
toChain
(\output ->
@ -111,7 +109,7 @@ getEvent input =
{-| Get the supported spec versions from the homeserver.
-}
getVersions : TaskChain CredUpdate { a | baseUrl : () } (VB a)
getVersions : TaskChain VaultUpdate { a | baseUrl : () } (VB a)
getVersions =
toChain
(\output ->
@ -126,7 +124,7 @@ getVersions =
{-| Invite a user to a room.
-}
invite : Invite.InviteInput -> IdemChain CredUpdate (VBA a)
invite : Invite.InviteInput -> IdemChain VaultUpdate (VBA a)
invite input =
toChain
(\output ->
@ -139,7 +137,7 @@ invite input =
input
joinedMembers : JoinedMembers.JoinedMembersInput -> IdemChain CredUpdate (VBA a)
joinedMembers : JoinedMembers.JoinedMembersInput -> IdemChain VaultUpdate (VBA a)
joinedMembers input =
toChain
(\output ->
@ -152,7 +150,7 @@ joinedMembers input =
input
loginWithUsernameAndPassword : LoginWithUsernameAndPassword.LoginWithUsernameAndPasswordInput -> TaskChain CredUpdate (VB a) (VBA a)
loginWithUsernameAndPassword : LoginWithUsernameAndPassword.LoginWithUsernameAndPasswordInput -> TaskChain VaultUpdate (VB a) (VBA a)
loginWithUsernameAndPassword input =
toChain
(\output ->
@ -171,7 +169,7 @@ loginWithUsernameAndPassword input =
{-| Make a VB-context based chain.
-}
makeVB : Credentials -> TaskChain CredUpdate {} (VB {})
makeVB : Credentials -> TaskChain VaultUpdate {} (VB {})
makeVB cred =
cred
|> Credentials.baseUrl
@ -181,7 +179,7 @@ makeVB cred =
{-| Make a VBA-context based chain.
-}
makeVBA : Credentials -> TaskChain CredUpdate {} (VBA {})
makeVBA : Credentials -> TaskChain VaultUpdate {} (VBA {})
makeVBA cred =
cred
|> makeVB
@ -190,7 +188,7 @@ makeVBA cred =
{-| Make a VBAT-context based chain.
-}
makeVBAT : (Int -> String) -> Credentials -> TaskChain CredUpdate {} (VBAT {})
makeVBAT : (Int -> String) -> Credentials -> TaskChain VaultUpdate {} (VBAT {})
makeVBAT toString cred =
cred
|> makeVBA
@ -199,7 +197,7 @@ makeVBAT toString cred =
{-| Redact an event from a room.
-}
redact : Redact.RedactInput -> TaskChain CredUpdate (VBAT a) (VBA a)
redact : Redact.RedactInput -> TaskChain VaultUpdate (VBAT a) (VBA a)
redact input =
toChain
(\output ->
@ -214,7 +212,7 @@ redact input =
{-| Send a message event to a room.
-}
sendMessageEvent : SendMessageEvent.SendMessageEventInput -> TaskChain CredUpdate (VBAT a) (VBA a)
sendMessageEvent : SendMessageEvent.SendMessageEventInput -> TaskChain VaultUpdate (VBAT a) (VBA a)
sendMessageEvent input =
toChain
(\output ->
@ -229,7 +227,7 @@ sendMessageEvent input =
{-| Send a state key event to a room.
-}
sendStateEvent : SendStateKey.SendStateKeyInput -> IdemChain CredUpdate (VBA a)
sendStateEvent : SendStateKey.SendStateKeyInput -> IdemChain VaultUpdate (VBA a)
sendStateEvent input =
toChain
(\output ->
@ -244,7 +242,7 @@ sendStateEvent input =
{-| Sync the latest updates.
-}
sync : Sync.SyncInput -> IdemChain CredUpdate (VBA a)
sync : Sync.SyncInput -> IdemChain VaultUpdate (VBA a)
sync input =
toChain
(\output ->
@ -259,7 +257,7 @@ sync input =
{-| Insert versions, or get them if they are not provided.
-}
versions : Maybe V.Versions -> TaskChain CredUpdate { a | baseUrl : () } (VB a)
versions : Maybe V.Versions -> TaskChain VaultUpdate { a | baseUrl : () } (VB a)
versions mVersions =
case mVersions of
Just vs ->
@ -271,7 +269,7 @@ versions mVersions =
{-| Create a task that insert the base URL into the context.
-}
withBaseUrl : String -> TaskChain CredUpdate a { a | baseUrl : () }
withBaseUrl : String -> TaskChain VaultUpdate a { a | baseUrl : () }
withBaseUrl baseUrl =
{ contextChange = Context.setBaseUrl baseUrl
, messages = []
@ -283,7 +281,7 @@ withBaseUrl baseUrl =
{-| Create a task that inserts a transaction id into the context.
-}
withTransactionId : (Int -> String) -> TaskChain CredUpdate a { a | transactionId : () }
withTransactionId : (Int -> String) -> TaskChain VaultUpdate a { a | transactionId : () }
withTransactionId toString =
Time.now
|> Task.map
@ -302,7 +300,7 @@ withTransactionId toString =
{-| Create a task that inserts versions into the context.
-}
withVersions : V.Versions -> TaskChain CredUpdate { a | baseUrl : () } (VB a)
withVersions : V.Versions -> TaskChain VaultUpdate { a | baseUrl : () } (VB a)
withVersions vs =
{ contextChange = Context.setVersions vs.versions
, messages = []

View File

@ -4,10 +4,10 @@ module Internal.Room exposing (..)
-}
import Dict
import Internal.Api.CredUpdate exposing (CredUpdate)
import Internal.Api.Credentials as Credentials exposing (Credentials)
import Internal.Api.Sync.V2.SpecObjects as Sync
import Internal.Api.Task as Api
import Internal.Api.VaultUpdate exposing (VaultUpdate)
import Internal.Event as Event exposing (Event)
import Internal.Tools.Exceptions as X
import Internal.Tools.Hashdict as Hashdict
@ -136,33 +136,29 @@ roomId =
{-| Sends a new event to the Matrix room associated with the given `Room`.
-}
sendEvent : Room -> { eventType : String, content : E.Value } -> Task X.Error CredUpdate
sendEvent : Room -> { eventType : String, content : E.Value } -> Task X.Error VaultUpdate
sendEvent (Room { context, room }) { eventType, content } =
Api.sendMessageEvent
{ accessToken = Credentials.accessToken context
, baseUrl = Credentials.baseUrl context
, content = content
{ content = content
, eventType = eventType
, roomId = Internal.roomId room
, versions = Credentials.versions context
, extraTransactionNoise = "content-value:<object>"
, roomId = Internal.roomId room
}
context
{-| Sends a new text message to the Matrix room associated with the given `Room`.
-}
sendMessage : Room -> String -> Task X.Error CredUpdate
sendMessage : Room -> String -> Task X.Error VaultUpdate
sendMessage (Room { context, room }) text =
Api.sendMessageEvent
{ accessToken = Credentials.accessToken context
, baseUrl = Credentials.baseUrl context
, content =
{ content =
E.object
[ ( "msgtype", E.string "m.text" )
, ( "body", E.string text )
]
, eventType = "m.room.message"
, roomId = Internal.roomId room
, versions = Credentials.versions context
, extraTransactionNoise = "literal-message:" ++ text
, roomId = Internal.roomId room
}
context

View File

@ -8,9 +8,9 @@ This file combines the internal functions with the API endpoints to create a ful
-}
import Dict
import Internal.Api.CredUpdate exposing (CredUpdate(..))
import Internal.Api.Credentials as Credentials exposing (Credentials)
import Internal.Api.Task as Api
import Internal.Api.VaultUpdate exposing (VaultUpdate(..))
import Internal.Event as Event
import Internal.Room as Room
import Internal.Tools.Exceptions as X
@ -88,9 +88,9 @@ insertRoom =
{-| Update the Vault type with new values
-}
updateWith : CredUpdate -> Vault -> Vault
updateWith credUpdate ((Vault ({ cred, context } as data)) as credentials) =
case credUpdate of
updateWith : VaultUpdate -> Vault -> Vault
updateWith vaultUpdate ((Vault ({ cred, context } as data)) as credentials) =
case vaultUpdate of
MultipleUpdates updates ->
List.foldl updateWith credentials updates
@ -193,18 +193,16 @@ updateWith credUpdate ((Vault ({ cred, context } as data)) as credentials) =
{-| Synchronize credentials
-}
sync : Vault -> Task X.Error CredUpdate
sync : Vault -> Task X.Error VaultUpdate
sync (Vault { cred, context }) =
Api.sync
{ accessToken = Credentials.accessToken context
, baseUrl = Credentials.baseUrl context
, filter = Nothing
{ filter = Nothing
, fullState = Nothing
, setPresence = Nothing
, since = Internal.getSince cred
, timeout = Just 30
, versions = Credentials.versions context
}
context
{-| Get a list of all synchronised rooms.