Add /sync as task

pull/31/head
Bram 2024-07-09 17:33:41 +02:00
parent 0978e43fc0
commit 4e378a5f50
5 changed files with 68 additions and 33 deletions

View File

@ -1,6 +1,6 @@
module Internal.Api.Main exposing
( Msg
, sendMessageEvent
, sendMessageEvent, sync
)
{-|
@ -18,7 +18,7 @@ This module is used as reference for getting
## Actions
@docs sendMessageEvent
@docs sendMessageEvent, sync
-}
@ -57,3 +57,22 @@ sendMessageEvent env data =
}
)
(Context.apiFormat env.context)
{-| Sync with the Matrix API to stay up-to-date.
-}
sync :
E.Envelope a
-> { timeout : Int, toMsg : Msg -> msg }
-> Cmd msg
sync env data =
ITask.run
data.toMsg
(ITask.sync
{ fullState = Nothing
, presence = env.settings.presence
, since = env.context.nextBatch
, timeout = Just data.timeout
}
)
(Context.apiFormat env.context)

View File

@ -1,4 +1,4 @@
module Internal.Api.Sync.Api exposing (..)
module Internal.Api.Sync.Api exposing (sync, Phantom)
{-|
@ -9,7 +9,7 @@ The sync module might be one of the most crucial parts of the Elm SDK. It offers
users the guarantee that the `Vault` type remains up-to-date, and it helps
communicate with the Matrix server about the Vault's needs.
@docs Phantom
@docs sync, Phantom
-}
@ -57,16 +57,10 @@ type alias PhantomV1 a =
{ a | accessToken : (), baseUrl : () }
type PresenceV1
= OfflineV1
| OnlineV1
| UnavailableV1
type alias SyncInput =
{ -- filter : FilterV1,
fullState : Maybe Bool
, presenceV1 : Maybe PresenceV1
, presence : Maybe String
, since : Maybe String
, timeout : Maybe Int
}
@ -77,22 +71,21 @@ type alias SyncInputV1 a =
| -- filter : FilterV1 ,
since : Maybe String
, fullState : Maybe Bool
, presenceV1 : Maybe PresenceV1
, presence : Maybe String
, timeout : Maybe Int
}
presenceV1ToString : PresenceV1 -> String
presenceV1ToString p =
case p of
OfflineV1 ->
"offline"
presenceFromOptions : List String -> Maybe String -> Maybe String
presenceFromOptions options =
Maybe.andThen
(\v ->
if List.member v options then
Just v
OnlineV1 ->
"online"
UnavailableV1 ->
"unavailable"
else
Nothing
)
syncV1 : SyncInputV1 i -> A.TaskChain (PhantomV1 a) (PhantomV1 a)
@ -102,8 +95,8 @@ syncV1 data =
[ R.accessToken
, R.queryOpString "filter" Nothing -- FILTER HERE
, R.queryOpBool "full_state" data.fullState
, data.presenceV1
|> Maybe.map presenceV1ToString
, data.presence
|> presenceFromOptions [ "offline", "online", "unavailable" ]
|> R.queryOpString "set_presence"
, R.queryOpString "since" data.since
, R.queryOpInt "timeout" data.timeout
@ -124,8 +117,8 @@ syncV2 data =
[ R.accessToken
, R.queryOpString "filter" Nothing
, R.queryOpBool "full_state" data.fullState
, data.presenceV1
|> Maybe.map presenceV1ToString
, data.presence
|> presenceFromOptions [ "offline", "online", "unavailable" ]
|> R.queryOpString "set_presence"
, R.queryOpString "since" data.since
, R.queryOpInt "timeout" data.timeout
@ -146,8 +139,8 @@ syncV3 data =
[ R.accessToken
, R.queryOpString "filter" Nothing
, R.queryOpBool "full_state" data.fullState
, data.presenceV1
|> Maybe.map presenceV1ToString
, data.presence
|> presenceFromOptions [ "offline", "online", "unavailable" ]
|> R.queryOpString "set_presence"
, R.queryOpString "since" data.since
, R.queryOpInt "timeout" data.timeout
@ -168,8 +161,8 @@ syncV4 data =
[ R.accessToken
, R.queryOpString "filter" Nothing
, R.queryOpBool "full_state" data.fullState
, data.presenceV1
|> Maybe.map presenceV1ToString
, data.presence
|> presenceFromOptions [ "offline", "online", "unavailable" ]
|> R.queryOpString "set_presence"
, R.queryOpString "since" data.since
, R.queryOpInt "timeout" data.timeout

View File

@ -1,6 +1,6 @@
module Internal.Api.Task exposing
( Task, run, Backpack
, sendMessageEvent
, sendMessageEvent, sync
)
{-|
@ -23,7 +23,7 @@ up-to-date.
## Tasks
@docs sendMessageEvent
@docs sendMessageEvent, sync
-}
@ -33,6 +33,7 @@ import Internal.Api.LoginWithUsernameAndPassword.Api
import Internal.Api.Now.Api
import Internal.Api.Request as Request
import Internal.Api.SendMessageEvent.Api
import Internal.Api.Sync.Api
import Internal.Api.Versions.Api
import Internal.Config.Log exposing (Log, log)
import Internal.Tools.Json as Json
@ -217,6 +218,15 @@ sendMessageEvent input =
|> finishTask
{-| Sync with the Matrix API to stay up-to-date.
-}
sync : { fullState : Maybe Bool, presence : Maybe String, since : Maybe String, timeout : Maybe Int } -> Task
sync input =
makeVBA
|> C.andThen (Internal.Api.Sync.Api.sync input)
|> finishTask
{-| Transform a completed task into a Cmd.
-}
run : (Backpack -> msg) -> Task -> APIContext {} -> Cmd msg

View File

@ -330,6 +330,7 @@ fields :
, settings :
{ currentVersion : Desc
, deviceName : Desc
, presence : Desc
, removePasswordOnLogin : Desc
, syncTime : Desc
}
@ -519,6 +520,9 @@ fields =
, deviceName =
[ "Indicates the device name that is communicated to the Matrix API."
]
, presence =
[ "Controls whether the client is automatically marked as online. The value is passed on to the Matrix API."
]
, removePasswordOnLogin =
[ "Remove the password as soon as a valid access token has been received."
]

View File

@ -35,6 +35,7 @@ behave under the user's preferred settings.
type alias Settings =
{ currentVersion : String
, deviceName : String
, presence : Maybe String
, removePasswordOnLogin : Bool
, syncTime : Int
}
@ -44,7 +45,7 @@ type alias Settings =
-}
coder : Json.Coder Settings
coder =
Json.object4
Json.object5
{ name = Text.docs.settings.name
, description = Text.docs.settings.description
, init = Settings
@ -65,6 +66,13 @@ coder =
, default = Tuple.pair Default.deviceName []
}
)
(Json.field.optional.value
{ fieldName = "presence"
, toField = .presence
, description = Text.fields.settings.presence
, coder = Json.string
}
)
(Json.field.optional.withDefault
{ fieldName = "removePasswordOnLogin"
, toField = .removePasswordOnLogin
@ -103,6 +111,7 @@ init : Settings
init =
{ currentVersion = Default.currentVersion
, deviceName = Default.deviceName
, presence = Nothing
, removePasswordOnLogin = Default.removePasswordOnLogin
, syncTime = Default.syncTime
}