From bc2d1a183bba4bd3251457a76eef0231faf004f6 Mon Sep 17 00:00:00 2001 From: Bram van den Heuvel Date: Tue, 4 Apr 2023 16:11:16 +0200 Subject: [PATCH] Add function to get username --- src/Internal/Api/Credentials.elm | 5 +++++ src/Internal/Tools/LoginValues.elm | 14 ++++++++++++++ src/Matrix.elm | 14 ++++++++++++-- 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/Internal/Api/Credentials.elm b/src/Internal/Api/Credentials.elm index 808533b..556ef7d 100644 --- a/src/Internal/Api/Credentials.elm +++ b/src/Internal/Api/Credentials.elm @@ -76,6 +76,11 @@ fromBaseUrl url = , vs = Nothing } +{-| Get the user id registered by the `Credentials` type. +-} +getUserId : Credentials -> Maybe String +getUserId (Credentials { access }) = + Login.getUserId access {-| Retrieves the spec versions from a given `Credentials` value. -} diff --git a/src/Internal/Tools/LoginValues.elm b/src/Internal/Tools/LoginValues.elm index 1dcd988..f7a2a8f 100644 --- a/src/Internal/Tools/LoginValues.elm +++ b/src/Internal/Tools/LoginValues.elm @@ -56,6 +56,20 @@ getToken t = UsernameAndPassword { token } -> token +getUserId : AccessToken -> Maybe String +getUserId t = + case t of + NoAccess -> + Nothing + + RawAccessToken _ -> + Nothing + + DetailedAccessToken { userId } -> + Just userId + + UsernameAndPassword { userId } -> + userId addToken : String -> AccessToken -> AccessToken addToken s t = diff --git a/src/Matrix.elm b/src/Matrix.elm index 0547d6f..7bee038 100644 --- a/src/Matrix.elm +++ b/src/Matrix.elm @@ -1,7 +1,7 @@ module Matrix exposing ( Vault, fromLoginCredentials, fromAccessToken , sync, VaultUpdate, updateWith - , getRooms, getRoomById, getInvites, accountData + , getRooms, getRoomById, getInvites, accountData, username , joinRoomById, setAccountData ) @@ -21,7 +21,7 @@ interact with the API. # Exploring your vault -@docs getRooms, getRoomById, getInvites, accountData +@docs getRooms, getRoomById, getInvites, accountData, username # Taking action @@ -133,6 +133,16 @@ accountData = Internal.Vault.accountData +{-| Get the username of the account that we're using. + +The username is a `Maybe String` if the Vault hasn't had its first sync yet, +and the API might not always be consistent on the username. +-} +username : Vault -> Maybe String +username = + Internal.Vault.getUsername + + {-| Join a Matrix room based on its room id. -} joinRoomById : String -> Vault -> Task X.Error VaultUpdate