Transform API results into CredUpdate types

pull/1/head
Bram van den Heuvel 2023-03-01 11:40:31 +01:00
parent 68d93180c1
commit 205263f5cd
1 changed files with 114 additions and 46 deletions

View File

@ -16,6 +16,19 @@ import Json.Encode as E
import Task exposing (Task) import Task exposing (Task)
type CredUpdate
= MultipleUpdates (List CredUpdate)
-- Updates as a result of API calls
| GetEvent GetEvent.EventInput GetEvent.EventOutput
| JoinedMembersToRoom JoinedMembers.JoinedMembersInput JoinedMembers.JoinedMembersOutput
| MessageEventSent SendMessageEvent.SendMessageEventInput SendMessageEvent.SendMessageEventOutput
| StateEventSent SendStateKey.SendStateKeyInput SendStateKey.SendStateKeyOutput
| SyncUpdate Sync.SyncInput Sync.SyncOutput
-- Updates as a result of getting data early
| UpdateAccessToken String
| UpdateVersions V.Versions
type alias Future a = type alias Future a =
Task X.Error a Task X.Error a
@ -31,17 +44,28 @@ type alias GetEventInput =
{-| Get a specific event from the Matrix API. {-| Get a specific event from the Matrix API.
-} -}
getEvent : GetEventInput -> Future GetEvent.EventOutput getEvent : GetEventInput -> Future CredUpdate
getEvent data = getEvent data =
VG.withInfo2 VG.withInfo2
(\accessToken versions -> (\accessToken versions ->
GetEvent.getEvent let
versions.versions input : GetEvent.EventInput
{ accessToken = accessToken input =
, baseUrl = data.baseUrl { accessToken = accessToken
, eventId = data.eventId , baseUrl = data.baseUrl
, roomId = data.roomId , eventId = data.eventId
} , roomId = data.roomId
}
in
GetEvent.getEvent versions.versions input
|> Task.map
(\output ->
MultipleUpdates
[ GetEvent input output
, UpdateAccessToken accessToken
, UpdateVersions versions
]
)
) )
(PreApi.accessToken data.baseUrl data.accessToken) (PreApi.accessToken data.baseUrl data.accessToken)
(PreApi.versions data.baseUrl data.versions) (PreApi.versions data.baseUrl data.versions)
@ -57,16 +81,27 @@ type alias JoinedMembersInput =
{-| Get a list of members who are part of a Matrix room. {-| Get a list of members who are part of a Matrix room.
-} -}
joinedMembers : JoinedMembersInput -> Future JoinedMembers.JoinedMembersOutput joinedMembers : JoinedMembersInput -> Future CredUpdate
joinedMembers data = joinedMembers data =
VG.withInfo2 VG.withInfo2
(\accessToken versions -> (\accessToken versions ->
JoinedMembers.joinedMembers let
versions.versions input : JoinedMembers.JoinedMembersInput
{ accessToken = accessToken input =
, baseUrl = data.baseUrl { accessToken = accessToken
, roomId = data.roomId , baseUrl = data.baseUrl
} , roomId = data.roomId
}
in
JoinedMembers.joinedMembers versions.versions input
|> Task.map
(\output ->
MultipleUpdates
[ JoinedMembersToRoom input output
, UpdateAccessToken accessToken
, UpdateVersions versions
]
)
) )
(PreApi.accessToken data.baseUrl data.accessToken) (PreApi.accessToken data.baseUrl data.accessToken)
(PreApi.versions data.baseUrl data.versions) (PreApi.versions data.baseUrl data.versions)
@ -85,19 +120,30 @@ type alias SendMessageEventInput =
{-| Send a message event into a Matrix room. {-| Send a message event into a Matrix room.
-} -}
sendMessageEvent : SendMessageEventInput -> Future SendMessageEvent.SendMessageEventOutput sendMessageEvent : SendMessageEventInput -> Future CredUpdate
sendMessageEvent data = sendMessageEvent data =
VG.withInfo3 VG.withInfo3
(\accessToken versions transactionId -> (\accessToken versions transactionId ->
SendMessageEvent.sendMessageEvent let
versions.versions input : SendMessageEvent.SendMessageEventInput
{ accessToken = accessToken input =
, baseUrl = data.baseUrl { accessToken = accessToken
, content = data.content , baseUrl = data.baseUrl
, eventType = data.eventType , content = data.content
, roomId = data.roomId , eventType = data.eventType
, transactionId = transactionId , roomId = data.roomId
} , transactionId = transactionId
}
in
SendMessageEvent.sendMessageEvent versions.versions input
|> Task.map
(\output ->
MultipleUpdates
[ MessageEventSent input output
, UpdateAccessToken accessToken
, UpdateVersions versions
]
)
) )
(PreApi.accessToken data.baseUrl data.accessToken) (PreApi.accessToken data.baseUrl data.accessToken)
(PreApi.versions data.baseUrl data.versions) (PreApi.versions data.baseUrl data.versions)
@ -129,19 +175,30 @@ type alias SendStateKeyInput =
{-| Send a state event into a Matrix room. {-| Send a state event into a Matrix room.
-} -}
sendStateEvent : SendStateKeyInput -> Future SendStateKey.SendStateKeyOutput sendStateEvent : SendStateKeyInput -> Future CredUpdate
sendStateEvent data = sendStateEvent data =
VG.withInfo2 VG.withInfo2
(\accessToken versions -> (\accessToken versions ->
SendStateKey.sendStateKey let
versions.versions input : SendStateKey.SendStateKeyInput
{ accessToken = accessToken input =
, baseUrl = data.baseUrl { accessToken = accessToken
, content = data.content , baseUrl = data.baseUrl
, eventType = data.eventType , content = data.content
, roomId = data.roomId , eventType = data.eventType
, stateKey = data.stateKey , roomId = data.roomId
} , stateKey = data.stateKey
}
in
SendStateKey.sendStateKey versions.versions input
|> Task.map
(\output ->
MultipleUpdates
[ StateEventSent input output
, UpdateAccessToken accessToken
, UpdateVersions versions
]
)
) )
(PreApi.accessToken data.baseUrl data.accessToken) (PreApi.accessToken data.baseUrl data.accessToken)
(PreApi.versions data.baseUrl data.versions) (PreApi.versions data.baseUrl data.versions)
@ -161,20 +218,31 @@ type alias SyncInput =
{-| Get the latest sync from the Matrix API. {-| Get the latest sync from the Matrix API.
-} -}
syncCredentials : SyncInput -> Future Sync.SyncOutput syncCredentials : SyncInput -> Future CredUpdate
syncCredentials data = syncCredentials data =
VG.withInfo2 VG.withInfo2
(\accessToken versions -> (\accessToken versions ->
Sync.sync let
versions.versions input : Sync.SyncInput
{ accessToken = accessToken input =
, baseUrl = data.baseUrl { accessToken = accessToken
, filter = data.filter , baseUrl = data.baseUrl
, fullState = data.fullState , filter = data.filter
, setPresence = data.setPresence , fullState = data.fullState
, since = data.since , setPresence = data.setPresence
, timeout = data.timeout , since = data.since
} , timeout = data.timeout
}
in
Sync.sync versions.versions input
|> Task.map
(\output ->
MultipleUpdates
[ SyncUpdate input output
, UpdateAccessToken accessToken
, UpdateVersions versions
]
)
) )
(PreApi.accessToken data.baseUrl data.accessToken) (PreApi.accessToken data.baseUrl data.accessToken)
(PreApi.versions data.baseUrl data.versions) (PreApi.versions data.baseUrl data.versions)