From e786bebeb2a9737be2aaee1dd75309635993c480 Mon Sep 17 00:00:00 2001 From: Bram Date: Wed, 22 May 2024 19:13:20 +0200 Subject: [PATCH] Add /_matrix/client/versions endpoint --- src/Internal/Api/Versions/Api.elm | 91 +++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 src/Internal/Api/Versions/Api.elm diff --git a/src/Internal/Api/Versions/Api.elm b/src/Internal/Api/Versions/Api.elm new file mode 100644 index 0000000..a617ab3 --- /dev/null +++ b/src/Internal/Api/Versions/Api.elm @@ -0,0 +1,91 @@ +module Internal.Api.Versions.Api exposing (versions, Phantom) + +{-| + + +# Versions + +Ask the Matrix API which versions it supports. + +@docs versions, Phantom + +-} + +import Dict +import Internal.Api.Api as A +import Internal.Tools.Json as Json +import Internal.Values.Context as Context exposing (Versions) +import Internal.Values.Envelope as E +import Set + + +{-| Task chain to ask which spec versions the Matrix API supports. +-} +versions : A.TaskChain (Phantom ph) (Phantom { ph | versions : () }) +versions = + A.request + { attributes = [] + , coder = versionsCoder + , contextChange = Context.setVersions + , method = "GET" + , path = [ "_matrix", "client", "versions" ] + , toUpdate = \v -> ( E.SetVersions v, [] ) + } + + +{-| Context needed for asking the server's available spec versions +-} +type alias Phantom a = + { a | baseUrl : () } + + +versionsCoder : Json.Coder Versions +versionsCoder = + Json.object2 + { name = "Versions" + , description = + [ "Gets the versions of the specification supported by the server." + , "Values will take the form vX.Y or rX.Y.Z in historical cases. See the Specification Versioning for more information." + , "The server may additionally advertise experimental features it supports through unstable_features. These features should be namespaced and may optionally include version information within their name if desired. Features listed here are not for optionally toggling parts of the Matrix specification and should only be used to advertise support for a feature which has not yet landed in the spec. For example, a feature currently undergoing the proposal process may appear here and eventually be taken off this list once the feature lands in the spec and the server deems it reasonable to do so. Servers can choose to enable some features only for some users, so clients should include authentication in the request to get all the features available for the logged-in user. If no authentication is provided, the server should only return the features available to all users. Servers may wish to keep advertising features here after they’ve been released into the spec to give clients a chance to upgrade appropriately. Additionally, clients should avoid using unstable features in their stable releases." + ] + , init = Versions + } + (Json.field.required + { fieldName = "versions" + , toField = .versions + , description = + [ "The supported versions." + ] + , coder = Json.list Json.string + } + ) + (Json.field.optional.withDefault + { fieldName = "unstable_features" + , toField = .unstableFeatures + , description = + [ "Experimental features the server supports. Features not listed here, or the lack of this property all together, indicate that a feature is not supported." + ] + , coder = + Json.bool + |> Json.slowDict + |> Json.map + { name = "Dict to set" + , description = + [ "Turn a dictionary of supported values into a set that contains only supported values" + ] + , back = Set.foldl (\k d -> Dict.insert k True d) Dict.empty + , forth = + Dict.foldl + (\k v s -> + if v then + Set.insert k s + + else + s + ) + Set.empty + } + , default = ( Set.empty, [] ) + , defaultToString = always "{}" + } + )