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 module Internal.Api.Main exposing
( Msg ( Msg
, sendMessageEvent , sendMessageEvent, sync
) )
{-| {-|
@ -18,7 +18,7 @@ This module is used as reference for getting
## Actions ## Actions
@docs sendMessageEvent @docs sendMessageEvent, sync
-} -}
@ -57,3 +57,22 @@ sendMessageEvent env data =
} }
) )
(Context.apiFormat env.context) (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 users the guarantee that the `Vault` type remains up-to-date, and it helps
communicate with the Matrix server about the Vault's needs. 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 : () } { a | accessToken : (), baseUrl : () }
type PresenceV1
= OfflineV1
| OnlineV1
| UnavailableV1
type alias SyncInput = type alias SyncInput =
{ -- filter : FilterV1, { -- filter : FilterV1,
fullState : Maybe Bool fullState : Maybe Bool
, presenceV1 : Maybe PresenceV1 , presence : Maybe String
, since : Maybe String , since : Maybe String
, timeout : Maybe Int , timeout : Maybe Int
} }
@ -77,22 +71,21 @@ type alias SyncInputV1 a =
| -- filter : FilterV1 , | -- filter : FilterV1 ,
since : Maybe String since : Maybe String
, fullState : Maybe Bool , fullState : Maybe Bool
, presenceV1 : Maybe PresenceV1 , presence : Maybe String
, timeout : Maybe Int , timeout : Maybe Int
} }
presenceV1ToString : PresenceV1 -> String presenceFromOptions : List String -> Maybe String -> Maybe String
presenceV1ToString p = presenceFromOptions options =
case p of Maybe.andThen
OfflineV1 -> (\v ->
"offline" if List.member v options then
Just v
OnlineV1 -> else
"online" Nothing
)
UnavailableV1 ->
"unavailable"
syncV1 : SyncInputV1 i -> A.TaskChain (PhantomV1 a) (PhantomV1 a) syncV1 : SyncInputV1 i -> A.TaskChain (PhantomV1 a) (PhantomV1 a)
@ -102,8 +95,8 @@ syncV1 data =
[ R.accessToken [ R.accessToken
, R.queryOpString "filter" Nothing -- FILTER HERE , R.queryOpString "filter" Nothing -- FILTER HERE
, R.queryOpBool "full_state" data.fullState , R.queryOpBool "full_state" data.fullState
, data.presenceV1 , data.presence
|> Maybe.map presenceV1ToString |> presenceFromOptions [ "offline", "online", "unavailable" ]
|> R.queryOpString "set_presence" |> R.queryOpString "set_presence"
, R.queryOpString "since" data.since , R.queryOpString "since" data.since
, R.queryOpInt "timeout" data.timeout , R.queryOpInt "timeout" data.timeout
@ -124,8 +117,8 @@ syncV2 data =
[ R.accessToken [ R.accessToken
, R.queryOpString "filter" Nothing , R.queryOpString "filter" Nothing
, R.queryOpBool "full_state" data.fullState , R.queryOpBool "full_state" data.fullState
, data.presenceV1 , data.presence
|> Maybe.map presenceV1ToString |> presenceFromOptions [ "offline", "online", "unavailable" ]
|> R.queryOpString "set_presence" |> R.queryOpString "set_presence"
, R.queryOpString "since" data.since , R.queryOpString "since" data.since
, R.queryOpInt "timeout" data.timeout , R.queryOpInt "timeout" data.timeout
@ -146,8 +139,8 @@ syncV3 data =
[ R.accessToken [ R.accessToken
, R.queryOpString "filter" Nothing , R.queryOpString "filter" Nothing
, R.queryOpBool "full_state" data.fullState , R.queryOpBool "full_state" data.fullState
, data.presenceV1 , data.presence
|> Maybe.map presenceV1ToString |> presenceFromOptions [ "offline", "online", "unavailable" ]
|> R.queryOpString "set_presence" |> R.queryOpString "set_presence"
, R.queryOpString "since" data.since , R.queryOpString "since" data.since
, R.queryOpInt "timeout" data.timeout , R.queryOpInt "timeout" data.timeout
@ -168,8 +161,8 @@ syncV4 data =
[ R.accessToken [ R.accessToken
, R.queryOpString "filter" Nothing , R.queryOpString "filter" Nothing
, R.queryOpBool "full_state" data.fullState , R.queryOpBool "full_state" data.fullState
, data.presenceV1 , data.presence
|> Maybe.map presenceV1ToString |> presenceFromOptions [ "offline", "online", "unavailable" ]
|> R.queryOpString "set_presence" |> R.queryOpString "set_presence"
, R.queryOpString "since" data.since , R.queryOpString "since" data.since
, R.queryOpInt "timeout" data.timeout , R.queryOpInt "timeout" data.timeout

View File

@ -1,6 +1,6 @@
module Internal.Api.Task exposing module Internal.Api.Task exposing
( Task, run, Backpack ( Task, run, Backpack
, sendMessageEvent , sendMessageEvent, sync
) )
{-| {-|
@ -23,7 +23,7 @@ up-to-date.
## Tasks ## Tasks
@docs sendMessageEvent @docs sendMessageEvent, sync
-} -}
@ -33,6 +33,7 @@ import Internal.Api.LoginWithUsernameAndPassword.Api
import Internal.Api.Now.Api import Internal.Api.Now.Api
import Internal.Api.Request as Request import Internal.Api.Request as Request
import Internal.Api.SendMessageEvent.Api import Internal.Api.SendMessageEvent.Api
import Internal.Api.Sync.Api
import Internal.Api.Versions.Api import Internal.Api.Versions.Api
import Internal.Config.Log exposing (Log, log) import Internal.Config.Log exposing (Log, log)
import Internal.Tools.Json as Json import Internal.Tools.Json as Json
@ -217,6 +218,15 @@ sendMessageEvent input =
|> finishTask |> 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. {-| Transform a completed task into a Cmd.
-} -}
run : (Backpack -> msg) -> Task -> APIContext {} -> Cmd msg run : (Backpack -> msg) -> Task -> APIContext {} -> Cmd msg

View File

@ -330,6 +330,7 @@ fields :
, settings : , settings :
{ currentVersion : Desc { currentVersion : Desc
, deviceName : Desc , deviceName : Desc
, presence : Desc
, removePasswordOnLogin : Desc , removePasswordOnLogin : Desc
, syncTime : Desc , syncTime : Desc
} }
@ -519,6 +520,9 @@ fields =
, deviceName = , deviceName =
[ "Indicates the device name that is communicated to the Matrix API." [ "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 = , removePasswordOnLogin =
[ "Remove the password as soon as a valid access token has been received." [ "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 = type alias Settings =
{ currentVersion : String { currentVersion : String
, deviceName : String , deviceName : String
, presence : Maybe String
, removePasswordOnLogin : Bool , removePasswordOnLogin : Bool
, syncTime : Int , syncTime : Int
} }
@ -44,7 +45,7 @@ type alias Settings =
-} -}
coder : Json.Coder Settings coder : Json.Coder Settings
coder = coder =
Json.object4 Json.object5
{ name = Text.docs.settings.name { name = Text.docs.settings.name
, description = Text.docs.settings.description , description = Text.docs.settings.description
, init = Settings , init = Settings
@ -65,6 +66,13 @@ coder =
, default = Tuple.pair Default.deviceName [] , 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 (Json.field.optional.withDefault
{ fieldName = "removePasswordOnLogin" { fieldName = "removePasswordOnLogin"
, toField = .removePasswordOnLogin , toField = .removePasswordOnLogin
@ -103,6 +111,7 @@ init : Settings
init = init =
{ currentVersion = Default.currentVersion { currentVersion = Default.currentVersion
, deviceName = Default.deviceName , deviceName = Default.deviceName
, presence = Nothing
, removePasswordOnLogin = Default.removePasswordOnLogin , removePasswordOnLogin = Default.removePasswordOnLogin
, syncTime = Default.syncTime , syncTime = Default.syncTime
} }