2024-05-17 16:00:33 +00:00
|
|
|
module Internal.Api.Task exposing (Task, run)
|
2024-05-18 22:22:12 +00:00
|
|
|
|
|
|
|
{-|
|
|
|
|
|
|
|
|
|
|
|
|
# Task module
|
2024-05-17 16:00:33 +00:00
|
|
|
|
|
|
|
This module is used to define how API calls are made. These completed API tasks
|
|
|
|
can be directly converted to Cmd types that the end user of the SDK can access.
|
|
|
|
|
|
|
|
These tasks do not affect the `Vault` directly, but instead, return a
|
|
|
|
`VaultUpdate` type that the user can apply to keep their `Vault` type
|
|
|
|
up-to-date.
|
|
|
|
|
2024-05-18 22:22:12 +00:00
|
|
|
|
2024-05-17 16:00:33 +00:00
|
|
|
## Use
|
|
|
|
|
|
|
|
@docs Task, run
|
2024-05-18 22:22:12 +00:00
|
|
|
|
2024-05-17 16:00:33 +00:00
|
|
|
-}
|
2024-05-17 12:28:06 +00:00
|
|
|
|
2024-05-22 18:52:07 +00:00
|
|
|
import Internal.Api.BaseUrl.Api
|
2024-05-17 12:28:06 +00:00
|
|
|
import Internal.Api.Chain as C
|
|
|
|
import Internal.Api.Request as Request
|
2024-05-22 18:52:07 +00:00
|
|
|
import Internal.Api.Versions.Api
|
|
|
|
import Internal.Config.Log exposing (Log, log)
|
|
|
|
import Internal.Values.Context as Context exposing (APIContext)
|
2024-05-17 12:28:06 +00:00
|
|
|
import Internal.Values.Envelope exposing (EnvelopeUpdate(..))
|
|
|
|
import Internal.Values.Room exposing (RoomUpdate(..))
|
|
|
|
import Internal.Values.Vault exposing (VaultUpdate(..))
|
|
|
|
import Task
|
|
|
|
|
|
|
|
|
|
|
|
{-| A Backpack is the ultimate message type that gets sent back by the Elm
|
|
|
|
runtime, which can be accessed, viewed and inspected.
|
|
|
|
-}
|
|
|
|
type alias Backpack =
|
|
|
|
{ messages : List (EnvelopeUpdate VaultUpdate), logs : List Log }
|
|
|
|
|
|
|
|
|
|
|
|
{-| A Task is a task that is ready to be sent to the outside world.
|
|
|
|
-}
|
|
|
|
type alias Task =
|
|
|
|
C.TaskChain Never (EnvelopeUpdate VaultUpdate) {} {}
|
|
|
|
|
|
|
|
|
|
|
|
{-| An UnFinished Task that is used somewhere else in this module to write a
|
|
|
|
complete Task type.
|
|
|
|
-}
|
|
|
|
type alias UFTask a b =
|
|
|
|
C.TaskChain Request.Error (EnvelopeUpdate VaultUpdate) a b
|
|
|
|
|
|
|
|
|
2024-05-22 18:52:07 +00:00
|
|
|
{-| Get the base URL where the Matrix API can be accessed
|
|
|
|
-}
|
|
|
|
getBaseUrl : UFTask a { a | baseUrl : () }
|
|
|
|
getBaseUrl c =
|
|
|
|
case Context.fromApiFormat c |> .baseUrl of
|
|
|
|
Just b ->
|
|
|
|
C.succeed
|
|
|
|
{ messages = []
|
|
|
|
, logs = [ log.debug "Using cached baseURL from Vault" ]
|
|
|
|
, contextChange = Context.setBaseUrl b
|
|
|
|
}
|
|
|
|
c
|
|
|
|
|
|
|
|
Nothing ->
|
|
|
|
Internal.Api.BaseUrl.Api.baseUrl
|
|
|
|
{ url = Context.fromApiFormat c |> .serverName }
|
|
|
|
c
|
|
|
|
|
|
|
|
|
|
|
|
{-| Get the versions that are potentially supported by the Matrix API
|
|
|
|
-}
|
|
|
|
getVersions : UFTask { a | baseUrl : () } { a | baseUrl : (), versions : () }
|
|
|
|
getVersions c =
|
|
|
|
case Context.fromApiFormat c |> .versions of
|
|
|
|
Just v ->
|
|
|
|
C.succeed
|
|
|
|
{ messages = []
|
|
|
|
, logs = [ log.debug "Using cached versions from Vault" ]
|
|
|
|
, contextChange = Context.setVersions v
|
|
|
|
}
|
|
|
|
c
|
|
|
|
|
|
|
|
Nothing ->
|
|
|
|
Internal.Api.Versions.Api.versions c
|
|
|
|
|
|
|
|
|
|
|
|
{-| Establish a Task Chain context where the base URL and supported list of
|
|
|
|
versions are known.
|
|
|
|
-}
|
2024-05-23 16:57:55 +00:00
|
|
|
makeVB : UFTask a { a | baseUrl : (), versions : () }
|
2024-05-22 18:52:07 +00:00
|
|
|
makeVB =
|
|
|
|
C.andThen getVersions getBaseUrl
|
|
|
|
|
|
|
|
|
2024-05-17 12:28:06 +00:00
|
|
|
{-| Transform a completed task into a Cmd.
|
|
|
|
-}
|
|
|
|
run : (Backpack -> msg) -> Task -> APIContext {} -> Cmd msg
|
|
|
|
run toMsg task context =
|
|
|
|
context
|
|
|
|
|> C.toTask task
|
|
|
|
|> Task.perform toMsg
|