From 45142509d3ad92bc1d07edb6aba07d25d954a062 Mon Sep 17 00:00:00 2001 From: Bram van den Heuvel Date: Tue, 14 Mar 2023 23:04:42 +0100 Subject: [PATCH] Add functionality to join rooms by their id --- src/Internal/Api/JoinRoomById/Api.elm | 40 +++++++++++++++++++++++ src/Internal/Api/JoinRoomById/Main.elm | 45 ++++++++++++++++++++++++++ src/Internal/Api/README.md | 28 ++++++++-------- src/Internal/Api/Task.elm | 8 +++++ src/Internal/Api/VaultUpdate.elm | 15 +++++++++ src/Internal/Vault.elm | 8 +++-- 6 files changed, 128 insertions(+), 16 deletions(-) create mode 100644 src/Internal/Api/JoinRoomById/Api.elm create mode 100644 src/Internal/Api/JoinRoomById/Main.elm diff --git a/src/Internal/Api/JoinRoomById/Api.elm b/src/Internal/Api/JoinRoomById/Api.elm new file mode 100644 index 0000000..390d8ec --- /dev/null +++ b/src/Internal/Api/JoinRoomById/Api.elm @@ -0,0 +1,40 @@ +module Internal.Api.JoinRoomById.Api exposing (..) + +import Internal.Api.Request as R +import Internal.Tools.Context exposing (Context, VBA) +import Internal.Tools.Exceptions as X +import Json.Decode as D +import Task exposing (Task) + + +type alias JoinRoomByIdInputV1 = + { roomId : String } + + +type alias JoinRoomByIdInputV2 = + { roomId : String, reason : Maybe String } + + +type alias JoinRoomByIdOutputV1 = + { roomId : String } + + +joinRoomByIdV1 : JoinRoomByIdInputV1 -> Context (VBA a) -> Task X.Error JoinRoomByIdOutputV1 +joinRoomByIdV1 { roomId } = + R.callApi "POST" "/_matrix/client/r0/rooms/{roomId}/join" + >> R.withAttributes + [ R.accessToken + , R.replaceInUrl "roomId" roomId + ] + >> R.toTask (D.map (\r -> { roomId = r }) (D.field "room_id" D.string)) + + +joinRoomByIdV2 : JoinRoomByIdInputV2 -> Context (VBA a) -> Task X.Error JoinRoomByIdOutputV1 +joinRoomByIdV2 { roomId, reason } = + R.callApi "POST" "/_matrix/client/v3/rooms/{roomId}/join" + >> R.withAttributes + [ R.accessToken + , R.replaceInUrl "roomId" roomId + , R.bodyOpString "reason" reason + ] + >> R.toTask (D.map (\r -> { roomId = r }) (D.field "room_id" D.string)) diff --git a/src/Internal/Api/JoinRoomById/Main.elm b/src/Internal/Api/JoinRoomById/Main.elm new file mode 100644 index 0000000..94052e9 --- /dev/null +++ b/src/Internal/Api/JoinRoomById/Main.elm @@ -0,0 +1,45 @@ +module Internal.Api.JoinRoomById.Main exposing (..) + +import Internal.Api.JoinRoomById.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) + + +joinRoomById : Context (VBA a) -> JoinRoomByIdInput -> Task X.Error JoinRoomByIdOutput +joinRoomById context input = + VC.withBottomLayer + { current = Api.joinRoomByIdV1 + , 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 = \data -> { roomId = data.roomId } + , current = Api.joinRoomByIdV2 + , upcast = identity + , version = "v1.1" + } + |> VC.sameForVersion "v1.2" + |> VC.sameForVersion "v1.3" + |> VC.sameForVersion "v1.4" + |> VC.sameForVersion "v1.5" + |> VC.mostRecentFromVersionList (Context.getVersions context) + |> Maybe.withDefault (always <| always <| Task.fail X.UnsupportedSpecVersion) + |> (|>) input + |> (|>) context + + +type alias JoinRoomByIdInput = + Api.JoinRoomByIdInputV2 + + +type alias JoinRoomByIdOutput = + Api.JoinRoomByIdOutputV1 diff --git a/src/Internal/Api/README.md b/src/Internal/Api/README.md index ef644e3..2aed557 100644 --- a/src/Internal/Api/README.md +++ b/src/Internal/Api/README.md @@ -41,20 +41,20 @@ Note that **under development** doesn't always mean that it _will be_ supported. | **Spec version** | | Inviting | Joining room id | | ---------------- | - | -------- | --------------- | | v1.6 || ⚠️ | ⚠️ | -| v1.5 || ✔️ | ⚠️ | -| v1.4 || ✔️ | ⚠️ | -| v1.3 || ✔️ | ⚠️ | -| v1.2 || ✔️ | ⚠️ | -| v1.1 || ✔️ | ⚠️ | -| r0.6.1 || ✔️ | ⚠️ | -| r0.6.0 || ✔️ | ⚠️ | -| r0.5.0 || ✔️ | ⚠️ | -| r0.4.0 || ✔️ | ⚠️ | -| r0.3.0 || ✔️ | ⚠️ | -| r0.2.0 || ✔️ | ⚠️ | -| r0.1.0 || ✔️ | ⚠️ | -| r0.0.1 || ✔️ | ⚠️ | -| r0.0.0 || ✔️ | ⚠️ | +| v1.5 || ✔️ | ✔️ | +| v1.4 || ✔️ | ✔️ | +| v1.3 || ✔️ | ✔️ | +| v1.2 || ✔️ | ✔️ | +| v1.1 || ✔️ | ✔️ | +| r0.6.1 || ✔️ | ✔️ | +| r0.6.0 || ✔️ | ✔️ | +| r0.5.0 || ✔️ | ✔️ | +| r0.4.0 || ✔️ | ✔️ | +| r0.3.0 || ✔️ | ✔️ | +| r0.2.0 || ✔️ | ✔️ | +| r0.1.0 || ✔️ | ✔️ | +| r0.0.1 || ✔️ | ✔️ | +| r0.0.0 || ✔️ | ✔️ | ## Getting events for a room diff --git a/src/Internal/Api/Task.elm b/src/Internal/Api/Task.elm index bece683..c55a42f 100644 --- a/src/Internal/Api/Task.elm +++ b/src/Internal/Api/Task.elm @@ -8,6 +8,7 @@ import Internal.Api.Chain as Chain import Internal.Api.Credentials as Cred exposing (Credentials) import Internal.Api.GetEvent.Main exposing (EventInput) import Internal.Api.Invite.Main exposing (InviteInput) +import Internal.Api.JoinRoomById.Main exposing (JoinRoomByIdInput) import Internal.Api.JoinedMembers.Main exposing (JoinedMembersInput) import Internal.Api.SendStateKey.Main exposing (SendStateKeyInput) import Internal.Api.Sync.Main exposing (SyncInput) @@ -47,6 +48,13 @@ joinedMembers data cred = |> C.toTask +joinRoomById : JoinRoomByIdInput -> Credentials -> FutureTask +joinRoomById data cred = + C.makeVBA cred + |> Chain.andThen (C.joinRoomById data) + |> C.toTask + + type alias RedactInput = { eventId : String , extraTransactionNoise : String diff --git a/src/Internal/Api/VaultUpdate.elm b/src/Internal/Api/VaultUpdate.elm index c133c91..c5f0080 100644 --- a/src/Internal/Api/VaultUpdate.elm +++ b/src/Internal/Api/VaultUpdate.elm @@ -4,6 +4,7 @@ import Internal.Api.Chain as Chain exposing (IdemChain, TaskChain) import Internal.Api.Credentials as Credentials exposing (Credentials) import Internal.Api.GetEvent.Main as GetEvent import Internal.Api.Invite.Main as Invite +import Internal.Api.JoinRoomById.Main as JoinRoomById import Internal.Api.JoinedMembers.Main as JoinedMembers import Internal.Api.LoginWithUsernameAndPassword.Main as LoginWithUsernameAndPassword import Internal.Api.Redact.Main as Redact @@ -25,6 +26,7 @@ type VaultUpdate | GetEvent GetEvent.EventInput GetEvent.EventOutput | InviteSent Invite.InviteInput Invite.InviteOutput | JoinedMembersToRoom JoinedMembers.JoinedMembersInput JoinedMembers.JoinedMembersOutput + | JoinedRoom JoinRoomById.JoinRoomByIdInput JoinRoomById.JoinRoomByIdOutput | LoggedInWithUsernameAndPassword LoginWithUsernameAndPassword.LoginWithUsernameAndPasswordInput LoginWithUsernameAndPassword.LoginWithUsernameAndPasswordOutput | MessageEventSent SendMessageEvent.SendMessageEventInput SendMessageEvent.SendMessageEventOutput | RedactedEvent Redact.RedactInput Redact.RedactOutput @@ -150,6 +152,19 @@ joinedMembers input = input +joinRoomById : JoinRoomById.JoinRoomByIdInput -> IdemChain VaultUpdate (VBA a) +joinRoomById input = + toChain + (\output -> + Chain.TaskChainPiece + { contextChange = identity + , messages = [ JoinedRoom input output ] + } + ) + JoinRoomById.joinRoomById + input + + loginWithUsernameAndPassword : LoginWithUsernameAndPassword.LoginWithUsernameAndPasswordInput -> TaskChain VaultUpdate (VB a) (VBA a) loginWithUsernameAndPassword input = toChain diff --git a/src/Internal/Vault.elm b/src/Internal/Vault.elm index f57d0aa..eaec74d 100644 --- a/src/Internal/Vault.elm +++ b/src/Internal/Vault.elm @@ -111,9 +111,14 @@ updateWith vaultUpdate ((Vault ({ cred, context } as data)) as credentials) = InviteSent _ _ -> credentials + -- TODO JoinedMembersToRoom _ _ -> credentials + -- TODO + JoinedRoom _ _ -> + credentials + -- TODO MessageEventSent _ _ -> credentials @@ -126,7 +131,6 @@ updateWith vaultUpdate ((Vault ({ cred, context } as data)) as credentials) = StateEventSent _ _ -> credentials - -- TODO SyncUpdate input output -> let jRooms : List IRoom.IRoom @@ -186,7 +190,7 @@ updateWith vaultUpdate ((Vault ({ cred, context } as data)) as credentials) = UpdateVersions versions -> Vault { data | context = Credentials.addVersions versions context } - -- TODO: Save all info + -- TODO: Save ALL info LoggedInWithUsernameAndPassword _ output -> Vault { data | context = Credentials.addToken output.accessToken context }