Expose account_data getter/setter functions

pull/1/head
Bram van den Heuvel 2023-03-31 16:39:43 +02:00
parent ff84d70d7f
commit 54f99f0f05
5 changed files with 61 additions and 4 deletions

View File

@ -13,6 +13,7 @@ import Internal.Api.JoinRoomById.Main exposing (JoinRoomByIdInput)
import Internal.Api.JoinedMembers.Main exposing (JoinedMembersInput) import Internal.Api.JoinedMembers.Main exposing (JoinedMembersInput)
import Internal.Api.Leave.Main exposing (LeaveInput) import Internal.Api.Leave.Main exposing (LeaveInput)
import Internal.Api.SendStateKey.Main exposing (SendStateKeyInput) import Internal.Api.SendStateKey.Main exposing (SendStateKeyInput)
import Internal.Api.SetAccountData.Main exposing (SetAccountInput)
import Internal.Api.Sync.Main exposing (SyncInput) import Internal.Api.Sync.Main exposing (SyncInput)
import Internal.Api.VaultUpdate as C import Internal.Api.VaultUpdate as C
import Json.Encode as E import Json.Encode as E
@ -137,6 +138,13 @@ sendStateEvent data cred =
|> C.toTask |> C.toTask
setAccountData : SetAccountInput -> Credentials -> FutureTask
setAccountData data cred =
C.makeVBA cred
|> Chain.andThen (C.setAccountData data)
|> C.toTask
sync : SyncInput -> Credentials -> FutureTask sync : SyncInput -> Credentials -> FutureTask
sync data cred = sync data cred =
C.makeVBA cred C.makeVBA cred

View File

@ -260,3 +260,10 @@ sendMessages pieces (Room { context, room }) =
leave : Room -> Task X.Error VaultUpdate leave : Room -> Task X.Error VaultUpdate
leave ((Room { context }) as r) = leave ((Room { context }) as r) =
Api.leave { roomId = roomId r, reason = Nothing } context 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

View File

@ -335,6 +335,13 @@ updateWith vaultUpdate ((Vault ({ cred, context } as data)) as vault) =
Vault { data | context = Credentials.addToken output.accessToken context } 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 {-| Synchronize vault
-} -}
sync : Vault -> Task X.Error VaultUpdate sync : Vault -> Task X.Error VaultUpdate

View File

@ -1,8 +1,8 @@
module Matrix exposing module Matrix exposing
( Vault, fromLoginCredentials, fromAccessToken ( Vault, fromLoginCredentials, fromAccessToken
, sync, VaultUpdate, updateWith , sync, VaultUpdate, updateWith
, getRooms, getRoomById, getInvites , getRooms, getRoomById, getInvites, accountData
, joinRoomById , joinRoomById, setAccountData
) )
{-| This is the main module of the SDK. Here, you will find basic functions to {-| 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 # Exploring your vault
@docs getRooms, getRoomById, getInvites @docs getRooms, getRoomById, getInvites, accountData
# Taking action # Taking action
@docs joinRoomById @docs joinRoomById, setAccountData
-} -}
@ -35,6 +35,7 @@ import Internal.Invite exposing (RoomInvite)
import Internal.Room exposing (Room) import Internal.Room exposing (Room)
import Internal.Tools.Exceptions as X import Internal.Tools.Exceptions as X
import Internal.Vault import Internal.Vault
import Json.Encode as E
import Task exposing (Task) import Task exposing (Task)
@ -121,8 +122,26 @@ getInvites =
Internal.Vault.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. {-| Join a Matrix room based on its room id.
-} -}
joinRoomById : String -> Vault -> Task X.Error VaultUpdate joinRoomById : String -> Vault -> Task X.Error VaultUpdate
joinRoomById = joinRoomById =
Internal.Vault.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

View File

@ -2,6 +2,7 @@ module Matrix.Room exposing
( Room, roomId, mostRecentEvents, findOlderEvents ( Room, roomId, mostRecentEvents, findOlderEvents
, stateEvent, accountData , stateEvent, accountData
, sendMessage, sendMessages, sendOneEvent, sendMultipleEvents , sendMessage, sendMessages, sendOneEvent, sendMultipleEvents
, setAccountData
) )
{-| This module provides functions for working with Matrix rooms. {-| 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 @docs sendMessage, sendMessages, sendOneEvent, sendMultipleEvents
# Changing a room
@docs setAccountData
-} -}
import Internal.Api.VaultUpdate exposing (VaultUpdate) 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 : List { content : E.Value, eventType : String, stateKey : Maybe String } -> Room -> List (Task X.Error VaultUpdate)
sendMultipleEvents = sendMultipleEvents =
Internal.sendEvents 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