Add ban API

pull/1/head
Bram van den Heuvel 2023-03-24 14:14:58 +01:00
parent e2ae6f582e
commit bf2cf7395a
3 changed files with 106 additions and 0 deletions

View File

@ -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)

View File

@ -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

View File

@ -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 : () })