From a2582f36f9a81af3070703be5a317b6715a0fb77 Mon Sep 17 00:00:00 2001 From: Bram Date: Mon, 22 Jul 2024 12:58:52 +0200 Subject: [PATCH] Add invite function --- .../Api/{Invite => InviteUser}/Api.elm | 8 +++--- src/Internal/Api/Main.elm | 27 +++++++++++++++++-- src/Internal/Api/SendStateEvent/Api.elm | 1 - src/Internal/Api/SetAccountData/Api.elm | 1 - src/Internal/Api/SetRoomAccountData/Api.elm | 1 - src/Internal/Api/Task.elm | 15 +++++++++-- src/Matrix/Room.elm | 21 ++++++++++++++- 7 files changed, 62 insertions(+), 12 deletions(-) rename src/Internal/Api/{Invite => InviteUser}/Api.elm (94%) diff --git a/src/Internal/Api/Invite/Api.elm b/src/Internal/Api/InviteUser/Api.elm similarity index 94% rename from src/Internal/Api/Invite/Api.elm rename to src/Internal/Api/InviteUser/Api.elm index e39c118..3b9efe8 100644 --- a/src/Internal/Api/Invite/Api.elm +++ b/src/Internal/Api/InviteUser/Api.elm @@ -1,4 +1,4 @@ -module Internal.Api.Invite.Api exposing (InviteInput, Phantom, invite) +module Internal.Api.InviteUser.Api exposing (InviteInput, Phantom, inviteUser) {-| @@ -14,7 +14,7 @@ room. If the user was invited to the room, the homeserver will append a m.room.member event to the room. -@docs InviteInput, Phantom, invite +@docs InviteInput, Phantom, inviteUser -} @@ -31,8 +31,8 @@ import Internal.Values.Vault as V {-| Invite a user to a room. -} -invite : InviteInput -> A.TaskChain (Phantom ph1) (Phantom ph1) -invite = +inviteUser : InviteInput -> A.TaskChain (Phantom ph1) (Phantom ph1) +inviteUser = A.startWithVersion "r0.0.0" inviteV1 |> A.sameForVersion "r0.0.1" |> A.sameForVersion "r0.1.0" diff --git a/src/Internal/Api/Main.elm b/src/Internal/Api/Main.elm index 76baebb..6be46ab 100644 --- a/src/Internal/Api/Main.elm +++ b/src/Internal/Api/Main.elm @@ -1,6 +1,6 @@ module Internal.Api.Main exposing ( Msg - , sendMessageEvent, sendStateEvent, setAccountData, setRoomAccountData, sync + , inviteUser, sendMessageEvent, sendStateEvent, setAccountData, setRoomAccountData, sync ) {-| @@ -18,7 +18,7 @@ This module is used as reference for getting ## Actions -@docs sendMessageEvent, sendStateEvent, setAccountData, setRoomAccountData, sync +@docs inviteUser, sendMessageEvent, sendStateEvent, setAccountData, setRoomAccountData, sync -} @@ -28,6 +28,7 @@ import Internal.Values.Context as Context import Internal.Values.Envelope as E import Internal.Values.User as User import Internal.Values.Vault as V +import Internal.Values.User exposing (User) {-| Update message type that is being returned. @@ -35,6 +36,28 @@ import Internal.Values.Vault as V type alias Msg = Backpack +{-| Invite a user to a room. +-} +inviteUser : + E.Envelope a + -> + { reason : Maybe String + , roomId : String + , toMsg : Msg -> msg + , user : User + } + -> Cmd msg +inviteUser env data = + ITask.run + data.toMsg + (ITask.inviteUser + { reason = data.reason + , roomId = data.roomId + , user = data.user + } + ) + (Context.apiFormat env.context) + {-| Send a message event. -} diff --git a/src/Internal/Api/SendStateEvent/Api.elm b/src/Internal/Api/SendStateEvent/Api.elm index f69e377..a1e6656 100644 --- a/src/Internal/Api/SendStateEvent/Api.elm +++ b/src/Internal/Api/SendStateEvent/Api.elm @@ -12,7 +12,6 @@ This module sends state events to Matrix rooms. -} import Internal.Api.Api as A -import Internal.Api.Invite.Api exposing (Phantom) import Internal.Api.Request as R import Internal.Config.Log exposing (log) import Internal.Config.Text as Text diff --git a/src/Internal/Api/SetAccountData/Api.elm b/src/Internal/Api/SetAccountData/Api.elm index 32eabe7..f55a3b3 100644 --- a/src/Internal/Api/SetAccountData/Api.elm +++ b/src/Internal/Api/SetAccountData/Api.elm @@ -12,7 +12,6 @@ This module allows the developer to set global account data. -} import Internal.Api.Api as A -import Internal.Api.Invite.Api exposing (Phantom) import Internal.Api.Request as R import Internal.Config.Log exposing (log) import Internal.Config.Text as Text diff --git a/src/Internal/Api/SetRoomAccountData/Api.elm b/src/Internal/Api/SetRoomAccountData/Api.elm index a23005e..eb74e0f 100644 --- a/src/Internal/Api/SetRoomAccountData/Api.elm +++ b/src/Internal/Api/SetRoomAccountData/Api.elm @@ -12,7 +12,6 @@ This module allows the developer to set account data to a Matrix room. -} import Internal.Api.Api as A -import Internal.Api.Invite.Api exposing (Phantom) import Internal.Api.Request as R import Internal.Config.Log exposing (log) import Internal.Config.Text as Text diff --git a/src/Internal/Api/Task.elm b/src/Internal/Api/Task.elm index deb1585..da2297a 100644 --- a/src/Internal/Api/Task.elm +++ b/src/Internal/Api/Task.elm @@ -1,6 +1,6 @@ module Internal.Api.Task exposing ( Task, run, Backpack - , sendMessageEvent, sendStateEvent, setAccountData, setRoomAccountData, sync + , inviteUser, sendMessageEvent, sendStateEvent, setAccountData, setRoomAccountData, sync ) {-| @@ -23,12 +23,13 @@ up-to-date. ## Tasks -@docs sendMessageEvent, sendStateEvent, setAccountData, setRoomAccountData, sync +@docs inviteUser, sendMessageEvent, sendStateEvent, setAccountData, setRoomAccountData, sync -} import Internal.Api.BaseUrl.Api import Internal.Api.Chain as C +import Internal.Api.InviteUser.Api import Internal.Api.LoginWithUsernameAndPassword.Api import Internal.Api.Now.Api import Internal.Api.Request as Request @@ -46,6 +47,7 @@ import Internal.Values.Envelope as E exposing (EnvelopeUpdate(..)) import Internal.Values.Room exposing (RoomUpdate(..)) import Internal.Values.Vault exposing (VaultUpdate(..)) import Task +import Internal.Values.User exposing (User) {-| A Backpack is the ultimate message type that gets sent back by the Elm @@ -207,6 +209,15 @@ finishTask uftask = ) +{-| Invite a user to a room. +-} +inviteUser : { reason : Maybe String, roomId : String, user : User } -> Task +inviteUser input = + makeVBA + |> C.andThen (Internal.Api.InviteUser.Api.inviteUser input) + |> finishTask + + {-| Establish a Task Chain context where the base URL and supported list of versions are known. -} diff --git a/src/Matrix/Room.elm b/src/Matrix/Room.elm index 95d10b9..5f3f456 100644 --- a/src/Matrix/Room.elm +++ b/src/Matrix/Room.elm @@ -44,7 +44,7 @@ room. These events are JSON objects that can be shaped in any way or form that you like. To help other users with decoding your JSON objects, you pass an `eventType` string which helps them figure out the nature of your JSON object. -@docs sendMessageEvent, sendStateEvent +@docs inviteUser, sendMessageEvent, sendStateEvent -} @@ -68,6 +68,25 @@ getAccountData key (Room room) = Envelope.extract (Internal.getAccountData key) room +{-| Invite a user to a room. +-} +invite : + { reason : Maybe String + , room : Room + , toMsg : Types.VaultUpdate -> msg + , user : Types.User + } + -> Cmd msg +invite data = + case (data.room, data.user) of + (Room room, Types.User user) -> + Api.inviteUser room + { reason = data.reason + , roomId = roomId data.room + , toMsg = Types.VaultUpdate >> data.toMsg + , user = user.content + } + {-| Get a room's room id. This is an opaque string that distinguishes rooms from each other. -}