From 54f99f0f0597a43415053a9db5c8fc603fb14bd1 Mon Sep 17 00:00:00 2001 From: Bram van den Heuvel Date: Fri, 31 Mar 2023 16:39:43 +0200 Subject: [PATCH] Expose account_data getter/setter functions --- src/Internal/Api/Task.elm | 8 ++++++++ src/Internal/Room.elm | 7 +++++++ src/Internal/Vault.elm | 7 +++++++ src/Matrix.elm | 27 +++++++++++++++++++++++---- src/Matrix/Room.elm | 16 ++++++++++++++++ 5 files changed, 61 insertions(+), 4 deletions(-) diff --git a/src/Internal/Api/Task.elm b/src/Internal/Api/Task.elm index a7d7710..71eaad3 100644 --- a/src/Internal/Api/Task.elm +++ b/src/Internal/Api/Task.elm @@ -13,6 +13,7 @@ import Internal.Api.JoinRoomById.Main exposing (JoinRoomByIdInput) import Internal.Api.JoinedMembers.Main exposing (JoinedMembersInput) import Internal.Api.Leave.Main exposing (LeaveInput) import Internal.Api.SendStateKey.Main exposing (SendStateKeyInput) +import Internal.Api.SetAccountData.Main exposing (SetAccountInput) import Internal.Api.Sync.Main exposing (SyncInput) import Internal.Api.VaultUpdate as C import Json.Encode as E @@ -137,6 +138,13 @@ sendStateEvent data cred = |> C.toTask +setAccountData : SetAccountInput -> Credentials -> FutureTask +setAccountData data cred = + C.makeVBA cred + |> Chain.andThen (C.setAccountData data) + |> C.toTask + + sync : SyncInput -> Credentials -> FutureTask sync data cred = C.makeVBA cred diff --git a/src/Internal/Room.elm b/src/Internal/Room.elm index 58bf907..10be7e4 100644 --- a/src/Internal/Room.elm +++ b/src/Internal/Room.elm @@ -260,3 +260,10 @@ sendMessages pieces (Room { context, room }) = leave : Room -> Task X.Error VaultUpdate leave ((Room { context }) as r) = Api.leave { roomId = roomId r, reason = Nothing } context + + +{-| Set account data. +-} +setAccountData : String -> E.Value -> Room -> Task X.Error VaultUpdate +setAccountData key value ((Room { context }) as r) = + Api.setAccountData { content = value, eventType = key, roomId = Just (roomId r) } context diff --git a/src/Internal/Vault.elm b/src/Internal/Vault.elm index 4d09d02..14afe1e 100644 --- a/src/Internal/Vault.elm +++ b/src/Internal/Vault.elm @@ -335,6 +335,13 @@ updateWith vaultUpdate ((Vault ({ cred, context } as data)) as vault) = Vault { data | context = Credentials.addToken output.accessToken context } +{-| Set personal account data +-} +setAccountData : String -> E.Value -> Vault -> Task X.Error VaultUpdate +setAccountData key value (Vault { context }) = + Api.setAccountData { content = value, eventType = key, roomId = Nothing } context + + {-| Synchronize vault -} sync : Vault -> Task X.Error VaultUpdate diff --git a/src/Matrix.elm b/src/Matrix.elm index d752fcd..0547d6f 100644 --- a/src/Matrix.elm +++ b/src/Matrix.elm @@ -1,8 +1,8 @@ module Matrix exposing ( Vault, fromLoginCredentials, fromAccessToken , sync, VaultUpdate, updateWith - , getRooms, getRoomById, getInvites - , joinRoomById + , getRooms, getRoomById, getInvites, accountData + , joinRoomById, setAccountData ) {-| This is the main module of the SDK. Here, you will find basic functions to @@ -21,12 +21,12 @@ interact with the API. # Exploring your vault -@docs getRooms, getRoomById, getInvites +@docs getRooms, getRoomById, getInvites, accountData # Taking action -@docs joinRoomById +@docs joinRoomById, setAccountData -} @@ -35,6 +35,7 @@ import Internal.Invite exposing (RoomInvite) import Internal.Room exposing (Room) import Internal.Tools.Exceptions as X import Internal.Vault +import Json.Encode as E import Task exposing (Task) @@ -121,8 +122,26 @@ getInvites = Internal.Vault.getInvites +{-| Account data is personal information that the homeserver will remember for you. + +The information will be kept there, and will remain visible if you log in elsewhere. +Other users cannot see this information. + +-} +accountData : String -> Vault -> Maybe E.Value +accountData = + Internal.Vault.accountData + + {-| Join a Matrix room based on its room id. -} joinRoomById : String -> Vault -> Task X.Error VaultUpdate joinRoomById = Internal.Vault.joinRoomById + + +{-| Update the user's personal account data. This saves the information on the homeserver's side and keeps it available for future use. +-} +setAccountData : String -> E.Value -> Vault -> Task X.Error VaultUpdate +setAccountData = + Internal.Vault.setAccountData diff --git a/src/Matrix/Room.elm b/src/Matrix/Room.elm index 89bcc97..af7a2e0 100644 --- a/src/Matrix/Room.elm +++ b/src/Matrix/Room.elm @@ -2,6 +2,7 @@ module Matrix.Room exposing ( Room, roomId, mostRecentEvents, findOlderEvents , stateEvent, accountData , sendMessage, sendMessages, sendOneEvent, sendMultipleEvents + , setAccountData ) {-| This module provides functions for working with Matrix rooms. @@ -23,6 +24,11 @@ A room represents a channel of communication within a Matrix home server. @docs sendMessage, sendMessages, sendOneEvent, sendMultipleEvents + +# Changing a room + +@docs setAccountData + -} import Internal.Api.VaultUpdate exposing (VaultUpdate) @@ -161,3 +167,13 @@ Keep in mind that this function doesn't send the events in order, it just makes sendMultipleEvents : List { content : E.Value, eventType : String, stateKey : Maybe String } -> Room -> List (Task X.Error VaultUpdate) sendMultipleEvents = Internal.sendEvents + + +{-| Save personal account data on this room. + +The homeserver will save this information on this room, but it will only be visible to the user who sent it. + +-} +setAccountData : String -> E.Value -> Room -> Task X.Error VaultUpdate +setAccountData = + Internal.setAccountData