From 344a7e5b3d33531a24498e59235f4614b5498bd0 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Mon, 12 Apr 2021 12:52:28 +0200 Subject: [PATCH] Add facility to get profile info to the login wizard --- .../sdk/api/auth/login/LoginProfileInfo.kt | 23 +++++++++ .../android/sdk/api/auth/login/LoginWizard.kt | 5 ++ .../android/sdk/internal/auth/AuthAPI.kt | 10 ++++ .../internal/auth/login/DefaultLoginWizard.kt | 11 +++++ .../sdk/internal/auth/login/GetProfileTask.kt | 48 +++++++++++++++++++ 5 files changed, 97 insertions(+) create mode 100644 matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/login/LoginProfileInfo.kt create mode 100644 matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/login/GetProfileTask.kt diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/login/LoginProfileInfo.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/login/LoginProfileInfo.kt new file mode 100644 index 0000000000..288a6d1232 --- /dev/null +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/login/LoginProfileInfo.kt @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2021 The Matrix.org Foundation C.I.C. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.matrix.android.sdk.api.auth.login + +data class LoginProfileInfo( + val matrixId: String, + val displayName: String?, + val fullAvatarUrl: String? +) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/login/LoginWizard.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/login/LoginWizard.kt index fff4f89bac..cce87176d1 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/login/LoginWizard.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/login/LoginWizard.kt @@ -21,6 +21,11 @@ import org.matrix.android.sdk.api.util.Cancelable interface LoginWizard { + /** + * Get some information about a matrixId: displayName and avatar url + */ + suspend fun getProfileInfo(matrixId: String): LoginProfileInfo + /** * @param login the login field * @param password the password field diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/AuthAPI.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/AuthAPI.kt index f93f285c6e..5a9fa9edf6 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/AuthAPI.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/AuthAPI.kt @@ -17,6 +17,7 @@ package org.matrix.android.sdk.internal.auth import org.matrix.android.sdk.api.auth.data.Credentials +import org.matrix.android.sdk.api.util.JsonDict import org.matrix.android.sdk.internal.auth.data.Availability import org.matrix.android.sdk.internal.auth.data.LoginFlowResponse import org.matrix.android.sdk.internal.auth.data.PasswordLoginParams @@ -73,6 +74,15 @@ internal interface AuthAPI { @GET(NetworkConstants.URI_API_PREFIX_PATH_R0 + "register/available") suspend fun registerAvailable(@Query("username") username: String): Availability + /** + * Get the combined profile information for this user. + * This API may be used to fetch the user's own profile information or other users; either locally or on remote homeservers. + * This API may return keys which are not limited to displayname or avatar_url. + * @param userId the user id to fetch profile info + */ + @GET(NetworkConstants.URI_API_PREFIX_PATH_R0 + "profile/{userId}") + suspend fun getProfile(@Path("userId") userId: String): JsonDict + /** * Add 3Pid during registration * Ref: https://gist.github.com/jryans/839a09bf0c5a70e2f36ed990d50ed928 diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/login/DefaultLoginWizard.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/login/DefaultLoginWizard.kt index 8b81f42e03..854caf8a62 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/login/DefaultLoginWizard.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/login/DefaultLoginWizard.kt @@ -17,6 +17,7 @@ package org.matrix.android.sdk.internal.auth.login import android.util.Patterns +import org.matrix.android.sdk.api.auth.login.LoginProfileInfo import org.matrix.android.sdk.api.auth.login.LoginWizard import org.matrix.android.sdk.api.auth.registration.RegisterThreePid import org.matrix.android.sdk.api.session.Session @@ -30,6 +31,7 @@ import org.matrix.android.sdk.internal.auth.db.PendingSessionData import org.matrix.android.sdk.internal.auth.registration.AddThreePidRegistrationParams import org.matrix.android.sdk.internal.auth.registration.RegisterAddThreePidTask import org.matrix.android.sdk.internal.network.executeRequest +import org.matrix.android.sdk.internal.session.content.DefaultContentUrlResolver internal class DefaultLoginWizard( private val authAPI: AuthAPI, @@ -39,6 +41,15 @@ internal class DefaultLoginWizard( private var pendingSessionData: PendingSessionData = pendingSessionStore.getPendingSessionData() ?: error("Pending session data should exist here") + private val getProfileTask: GetProfileTask = DefaultGetProfileTask( + authAPI, + DefaultContentUrlResolver(pendingSessionData.homeServerConnectionConfig) + ) + + override suspend fun getProfileInfo(matrixId: String): LoginProfileInfo { + return getProfileTask.execute(GetProfileTask.Params(matrixId)) + } + override suspend fun login(login: String, password: String, deviceName: String): Session { diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/login/GetProfileTask.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/login/GetProfileTask.kt new file mode 100644 index 0000000000..bb9faf49c4 --- /dev/null +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/login/GetProfileTask.kt @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2021 The Matrix.org Foundation C.I.C. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.matrix.android.sdk.internal.auth.login + +import org.matrix.android.sdk.api.auth.login.LoginProfileInfo +import org.matrix.android.sdk.api.session.content.ContentUrlResolver +import org.matrix.android.sdk.api.session.profile.ProfileService +import org.matrix.android.sdk.internal.auth.AuthAPI +import org.matrix.android.sdk.internal.network.executeRequest +import org.matrix.android.sdk.internal.task.Task + +internal interface GetProfileTask : Task { + data class Params( + val userId: String + ) +} + +internal class DefaultGetProfileTask( + private val authAPI: AuthAPI, + private val contentUrlResolver: ContentUrlResolver +) : GetProfileTask { + + override suspend fun execute(params: GetProfileTask.Params): LoginProfileInfo { + val info = executeRequest(null) { + authAPI.getProfile(params.userId) + } + + return LoginProfileInfo( + matrixId = params.userId, + displayName = info[ProfileService.DISPLAY_NAME_KEY] as? String, + fullAvatarUrl = contentUrlResolver.resolveFullSize(info[ProfileService.AVATAR_URL_KEY] as? String) + ) + } +}