From bf2cf7395a117dfac2f3a6edbc653b98b9f2e801 Mon Sep 17 00:00:00 2001 From: Bram van den Heuvel Date: Fri, 24 Mar 2023 14:14:58 +0100 Subject: [PATCH] Add ban API --- src/Internal/Api/Ban/Api.elm | 43 +++++++++++++++++++++++++++++ src/Internal/Api/Ban/Main.elm | 46 ++++++++++++++++++++++++++++++++ src/Internal/Api/VaultUpdate.elm | 17 ++++++++++++ 3 files changed, 106 insertions(+) create mode 100644 src/Internal/Api/Ban/Api.elm create mode 100644 src/Internal/Api/Ban/Main.elm diff --git a/src/Internal/Api/Ban/Api.elm b/src/Internal/Api/Ban/Api.elm new file mode 100644 index 0000000..c256a09 --- /dev/null +++ b/src/Internal/Api/Ban/Api.elm @@ -0,0 +1,43 @@ +module Internal.Api.Ban.Api exposing (..) + +import Internal.Api.GetEvent.V1.SpecObjects as SO1 +import Internal.Api.Request as R +import Internal.Tools.Context as Context exposing (Context) +import Internal.Tools.Exceptions as X +import Json.Decode as D +import Task exposing (Task) + + +type alias BanInputV1 = + { reason : Maybe String + , roomId : String + , userId : String + } + + +type alias BanOutputV1 = + () + + +banV1 : BanInputV1 -> Context { a | accessToken : (), baseUrl : () } -> Task X.Error BanOutputV1 +banV1 { reason, roomId, userId } = + R.callApi "POST" "/_matrix/client/r0/rooms/{roomId}/ban" + >> R.withAttributes + [ R.accessToken + , R.replaceInUrl "roomId" roomId + , R.bodyOpString "reason" reason + , R.bodyString "user_id" userId + ] + >> R.toTask (D.map (always ()) D.value) + + +banV2 : BanInputV1 -> Context { a | accessToken : (), baseUrl : () } -> Task X.Error BanOutputV1 +banV2 { reason, roomId, userId } = + R.callApi "POST" "/_matrix/client/v3/rooms/{roomId}/ban" + >> R.withAttributes + [ R.accessToken + , R.replaceInUrl "roomId" roomId + , R.bodyOpString "reason" reason + , R.bodyString "user_id" userId + ] + >> R.toTask (D.map (always ()) D.value) diff --git a/src/Internal/Api/Ban/Main.elm b/src/Internal/Api/Ban/Main.elm new file mode 100644 index 0000000..32cd9cc --- /dev/null +++ b/src/Internal/Api/Ban/Main.elm @@ -0,0 +1,46 @@ +module Internal.Api.Ban.Main exposing (..) + +import Internal.Api.Ban.Api as Api +import Internal.Tools.Context as Context exposing (Context, VBA) +import Internal.Tools.Exceptions as X +import Internal.Tools.VersionControl as VC +import Task exposing (Task) + + +ban : Context (VBA a) -> BanInput -> Task X.Error BanOutput +ban context input = + VC.withBottomLayer + { current = Api.banV1 + , version = "r0.0.0" + } + |> VC.sameForVersion "r0.0.1" + |> VC.sameForVersion "r0.1.0" + |> VC.sameForVersion "r0.2.0" + |> VC.sameForVersion "r0.3.0" + |> VC.sameForVersion "r0.4.0" + |> VC.sameForVersion "r0.5.0" + |> VC.sameForVersion "r0.6.0" + |> VC.sameForVersion "r0.6.1" + |> VC.addMiddleLayer + { downcast = identity + , current = Api.banV2 + , upcast = identity + , version = "v1.1" + } + |> VC.sameForVersion "v1.2" + |> VC.sameForVersion "v1.3" + |> VC.sameForVersion "v1.4" + |> VC.sameForVersion "v1.5" + |> VC.sameForVersion "v1.6" + |> VC.mostRecentFromVersionList (Context.getVersions context) + |> Maybe.withDefault (always <| always <| Task.fail X.UnsupportedSpecVersion) + |> (|>) input + |> (|>) context + + +type alias BanInput = + Api.BanInputV1 + + +type alias BanOutput = + Api.BanOutputV1 diff --git a/src/Internal/Api/VaultUpdate.elm b/src/Internal/Api/VaultUpdate.elm index cf66a4c..0b3e2f3 100644 --- a/src/Internal/Api/VaultUpdate.elm +++ b/src/Internal/Api/VaultUpdate.elm @@ -1,5 +1,6 @@ module Internal.Api.VaultUpdate exposing (..) +import Internal.Api.Ban.Main as Ban import Internal.Api.Chain as Chain exposing (IdemChain, TaskChain) import Internal.Api.Credentials as Credentials exposing (Credentials) import Internal.Api.GetEvent.Main as GetEvent @@ -25,6 +26,7 @@ import Time type VaultUpdate = MultipleUpdates (List VaultUpdate) -- Updates as a result of API calls + | BanUser Ban.BanInput Ban.BanOutput | GetEvent GetEvent.EventInput GetEvent.EventOutput | GetMessages GetMessages.GetMessagesInput GetMessages.GetMessagesOutput | InviteSent Invite.InviteInput Invite.InviteOutput @@ -102,6 +104,21 @@ accessToken ctoken = } +{-| Ban a user from a room. +-} +ban : Ban.BanInput -> IdemChain VaultUpdate (VBA a) +ban input = + toChain + (\output -> + Chain.TaskChainPiece + { contextChange = identity + , messages = [ BanUser input output ] + } + ) + Ban.ban + input + + {-| Get an event from the API. -} getEvent : GetEvent.EventInput -> IdemChain VaultUpdate (VBA { a | sentEvent : () })