From 73a3f50d9c248ff612fd36c0776d4def2545b585 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Thu, 22 Aug 2019 15:35:19 +0200 Subject: [PATCH] Use the hashed v2 lookup API for 3PIDs (#3257) --- CHANGES.rst | 2 +- .../vector/activity/AbstractWidgetActivity.kt | 2 +- .../im/vector/activity/LoginActivity.java | 3 +- .../PhoneNumberVerificationActivity.java | 2 +- .../im/vector/activity/ReviewTermsActivity.kt | 2 +- .../activity/VectorRoomCreationActivity.java | 17 ++++----- .../VectorRoomInviteMembersActivity.java | 5 +++ .../im/vector/contacts/ContactsManager.java | 16 +++++++++ .../im/vector/contacts/PIDsRetriever.java | 11 +++++- .../im/vector/fragments/PeopleFragment.java | 21 +++++++++++ .../VectorSearchPeopleListFragment.java | 6 ++++ .../fragments/terms/ServiceTermsArgs.kt | 2 +- .../im/vector/widgets/WidgetsManager.java | 36 ++++++++++--------- 13 files changed, 93 insertions(+), 32 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 20abb69dad..ba350a6598 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -6,7 +6,7 @@ MatrixSdk: - Changelog: https://github.com/matrix-org/matrix-android-sdk/releases/tag/v0.X.Y Features: - - + - Use the hashed v2 lookup API for 3PIDs (#3257) Improvements: - diff --git a/vector/src/main/java/im/vector/activity/AbstractWidgetActivity.kt b/vector/src/main/java/im/vector/activity/AbstractWidgetActivity.kt index f4ef4254e2..a4905a2152 100755 --- a/vector/src/main/java/im/vector/activity/AbstractWidgetActivity.kt +++ b/vector/src/main/java/im/vector/activity/AbstractWidgetActivity.kt @@ -148,7 +148,7 @@ abstract class AbstractWidgetActivity : VectorAppCompatActivity() { } } - private fun presentTermsForServices(token: String?) { + private fun presentTermsForServices(token: String) { val wm = WidgetManagerProvider.getWidgetManager(this) if (wm == null) { // should not happen finish() diff --git a/vector/src/main/java/im/vector/activity/LoginActivity.java b/vector/src/main/java/im/vector/activity/LoginActivity.java index 08262cfca5..d39b1de3f8 100644 --- a/vector/src/main/java/im/vector/activity/LoginActivity.java +++ b/vector/src/main/java/im/vector/activity/LoginActivity.java @@ -61,6 +61,7 @@ import org.matrix.androidsdk.core.callback.SimpleApiCallback; import org.matrix.androidsdk.core.model.HttpException; import org.matrix.androidsdk.core.model.MatrixError; +import org.matrix.androidsdk.features.identityserver.IdentityServerManager; import org.matrix.androidsdk.login.AutoDiscovery; import org.matrix.androidsdk.rest.client.LoginRestClient; import org.matrix.androidsdk.rest.client.ProfileRestClient; @@ -2338,7 +2339,7 @@ private HomeServerConnectionConfig getHsConfig() { return null; } - if (!identityServerUrlString.startsWith("http://") && !identityServerUrlString.startsWith("https://")) { + if (!TextUtils.isEmpty(identityServerUrlString) & !identityServerUrlString.startsWith("http://") && !identityServerUrlString.startsWith("https://")) { identityServerUrlString = "https://" + identityServerUrlString; } diff --git a/vector/src/main/java/im/vector/activity/PhoneNumberVerificationActivity.java b/vector/src/main/java/im/vector/activity/PhoneNumberVerificationActivity.java index 4eb5223dbb..2f2b67c478 100644 --- a/vector/src/main/java/im/vector/activity/PhoneNumberVerificationActivity.java +++ b/vector/src/main/java/im/vector/activity/PhoneNumberVerificationActivity.java @@ -147,7 +147,7 @@ private void submitCode() { mPhoneNumberCodeLayout.setError(getString(R.string.settings_phone_number_verification_error_empty_code)); } else { showWaitingView(); - mSession.getThirdPidRestClient().submitValidationToken(mThreePid.medium, + mSession.getIdentityServerManager().submitValidationToken(mThreePid.medium, mPhoneNumberCode.getText().toString(), mThreePid.clientSecret, mThreePid.sid, diff --git a/vector/src/main/java/im/vector/activity/ReviewTermsActivity.kt b/vector/src/main/java/im/vector/activity/ReviewTermsActivity.kt index 46115a1350..a009fe53b9 100644 --- a/vector/src/main/java/im/vector/activity/ReviewTermsActivity.kt +++ b/vector/src/main/java/im/vector/activity/ReviewTermsActivity.kt @@ -47,7 +47,7 @@ class ReviewTermsActivity : SimpleFragmentActivity() { private const val EXTRA_INFO = "EXTRA_INFO" - fun intent(context: Context, serviceType: TermsManager.ServiceType, baseUrl: String, token: String?): Intent { + fun intent(context: Context, serviceType: TermsManager.ServiceType, baseUrl: String, token: String): Intent { return Intent(context, ReviewTermsActivity::class.java).also { it.putExtra(EXTRA_INFO, ServiceTermsArgs(serviceType, baseUrl, token)) } diff --git a/vector/src/main/java/im/vector/activity/VectorRoomCreationActivity.java b/vector/src/main/java/im/vector/activity/VectorRoomCreationActivity.java index 57f2f628ab..9a1a68744a 100755 --- a/vector/src/main/java/im/vector/activity/VectorRoomCreationActivity.java +++ b/vector/src/main/java/im/vector/activity/VectorRoomCreationActivity.java @@ -292,19 +292,17 @@ public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_create_room: if (mParticipants.isEmpty()) { - createRoom(mParticipants); + // Should not happen, mParticipant contain myself at first position + // createRoom(mParticipants); } else { - // the first entry is self so ignore - mParticipants.remove(0); - - if (mParticipants.isEmpty()) { + if (mParticipants.size() == 1) { // standalone case : should be accepted ? createRoom(mParticipants); - } else if (mParticipants.size() > 1) { + } else if (mParticipants.size() > 2) { createRoom(mParticipants); } else { // 1 other participant - openOrCreateDirectChatRoom(mParticipants.get(0).mUserId); + openOrCreateDirectChatRoom(mParticipants.get(1).mUserId); } } return true; @@ -426,8 +424,11 @@ private void createRoom(final List participants) { CreateRoomParams params = new CreateRoomParams(); + // First participant is self, so remove + List participantsWithoutMe = participants.subList(1, participants.size()); + List ids = new ArrayList<>(); - for (ParticipantAdapterItem item : participants) { + for (ParticipantAdapterItem item : participantsWithoutMe) { if (null != item.mUserId) { ids.add(item.mUserId); } diff --git a/vector/src/main/java/im/vector/activity/VectorRoomInviteMembersActivity.java b/vector/src/main/java/im/vector/activity/VectorRoomInviteMembersActivity.java index ed382e8e7a..045c1e265d 100755 --- a/vector/src/main/java/im/vector/activity/VectorRoomInviteMembersActivity.java +++ b/vector/src/main/java/im/vector/activity/VectorRoomInviteMembersActivity.java @@ -120,6 +120,11 @@ public void run() { } }); } + + @Override + public void onIdentityServerTermsNotSigned(String token) { + Log.w(LOG_TAG, "onIdentityServerTermsNotSigned()"); + } }; // refresh the presence asap diff --git a/vector/src/main/java/im/vector/contacts/ContactsManager.java b/vector/src/main/java/im/vector/contacts/ContactsManager.java index ed2e40d847..245e18088b 100755 --- a/vector/src/main/java/im/vector/contacts/ContactsManager.java +++ b/vector/src/main/java/im/vector/contacts/ContactsManager.java @@ -75,6 +75,11 @@ public interface ContactsManagerListener { * Called when an user presence has been updated */ void onContactPresenceUpdate(Contact contact, String matrixId); + + /** + * Called when the Terms of the Identity server has not being accepted + */ + void onIdentityServerTermsNotSigned(String token); } // singleton @@ -159,6 +164,17 @@ public void onFailure(String accountId) { onPIDsUpdate(); } + @Override + public void onIdentityServerTermsNotSigned(String token) { + for (ContactsManagerListener listener : mListeners) { + try { + listener.onIdentityServerTermsNotSigned(token); + } catch (Exception e) { + Log.e(LOG_TAG, "onTermsNotSigned failed " + e.getMessage(), e); + } + } + } + @Override public void onSuccess(final String accountId) { // ignore the current response because the request has been cancelled diff --git a/vector/src/main/java/im/vector/contacts/PIDsRetriever.java b/vector/src/main/java/im/vector/contacts/PIDsRetriever.java index 7471a2affe..52b9cfdce3 100755 --- a/vector/src/main/java/im/vector/contacts/PIDsRetriever.java +++ b/vector/src/main/java/im/vector/contacts/PIDsRetriever.java @@ -26,6 +26,7 @@ import org.matrix.androidsdk.core.Log; import org.matrix.androidsdk.core.callback.ApiCallback; import org.matrix.androidsdk.core.model.MatrixError; +import org.matrix.androidsdk.features.terms.TermsNotSignedException; import org.matrix.androidsdk.rest.model.pid.ThreePid; import java.util.ArrayList; @@ -54,6 +55,8 @@ public interface PIDsRetrieverListener { * Called the PIDs retrieval fails. */ void onFailure(String accountId); + + void onIdentityServerTermsNotSigned(String token); } // current instance @@ -268,7 +271,13 @@ public void onMatrixError(MatrixError e) { @Override public void onUnexpectedError(Exception e) { - onError(e.getMessage()); + if (e instanceof TermsNotSignedException) { + if (null != mListener) { + mListener.onIdentityServerTermsNotSigned(((TermsNotSignedException) e).getToken()); + } + } else { + onError(e.getMessage()); + } } }); } diff --git a/vector/src/main/java/im/vector/fragments/PeopleFragment.java b/vector/src/main/java/im/vector/fragments/PeopleFragment.java index 89939622c9..bea613f5cd 100644 --- a/vector/src/main/java/im/vector/fragments/PeopleFragment.java +++ b/vector/src/main/java/im/vector/fragments/PeopleFragment.java @@ -17,6 +17,7 @@ package im.vector.fragments; +import android.app.Activity; import android.content.Intent; import android.content.pm.PackageManager; import android.os.AsyncTask; @@ -39,6 +40,7 @@ import org.matrix.androidsdk.core.callback.ApiCallback; import org.matrix.androidsdk.core.model.MatrixError; import org.matrix.androidsdk.data.Room; +import org.matrix.androidsdk.features.terms.TermsManager; import org.matrix.androidsdk.listeners.MXEventListener; import org.matrix.androidsdk.rest.model.Event; import org.matrix.androidsdk.rest.model.User; @@ -51,7 +53,9 @@ import butterknife.BindView; import im.vector.R; +import im.vector.activity.ReviewTermsActivity; import im.vector.activity.VectorMemberDetailsActivity; +import im.vector.activity.util.RequestCodesKt; import im.vector.adapters.ParticipantAdapterItem; import im.vector.adapters.PeopleAdapter; import im.vector.contacts.Contact; @@ -140,6 +144,14 @@ public void onPresenceUpdate(final Event event, final User user) { initKnownContacts(); } + @Override + public void onActivityResult(int requestCode, int resultCode, Intent data) { + if (requestCode == RequestCodesKt.TERMS_REQUEST_CODE && resultCode == Activity.RESULT_OK) { + // Launch again the request + ContactsManager.getInstance().retrievePids(); + } + } + @Override public void onResume() { super.onResume(); @@ -552,6 +564,15 @@ public void onContactPresenceUpdate(Contact contact, String matrixId) { //TODO } + @Override + public void onIdentityServerTermsNotSigned(String token) { + if (isAdded()) { + startActivityForResult(ReviewTermsActivity.Companion.intent(getActivity(), + TermsManager.ServiceType.IdentityService, mSession.getHomeServerConfig().getIdentityServerUri().toString(), token), + RequestCodesKt.TERMS_REQUEST_CODE); + } + } + @Override public void onToggleDirectChat(String roomId, boolean isDirectChat) { if (!isDirectChat) { diff --git a/vector/src/main/java/im/vector/fragments/VectorSearchPeopleListFragment.java b/vector/src/main/java/im/vector/fragments/VectorSearchPeopleListFragment.java index f45c6dbeb2..7b7e8adfca 100644 --- a/vector/src/main/java/im/vector/fragments/VectorSearchPeopleListFragment.java +++ b/vector/src/main/java/im/vector/fragments/VectorSearchPeopleListFragment.java @@ -26,6 +26,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.matrix.androidsdk.MXSession; +import org.matrix.androidsdk.core.Log; import org.matrix.androidsdk.core.MXPatterns; import org.matrix.androidsdk.fragments.MatrixMessageListFragment; import org.matrix.androidsdk.listeners.MXEventListener; @@ -89,6 +90,11 @@ public void run() { }); } } + + @Override + public void onIdentityServerTermsNotSigned(String token) { + Log.w("VectorSearchPeopleListFragment", "onIdentityServerTermsNotSigned()"); + } }; // refresh the presence asap diff --git a/vector/src/main/java/im/vector/fragments/terms/ServiceTermsArgs.kt b/vector/src/main/java/im/vector/fragments/terms/ServiceTermsArgs.kt index 3a1ce7c5f8..267b0da996 100644 --- a/vector/src/main/java/im/vector/fragments/terms/ServiceTermsArgs.kt +++ b/vector/src/main/java/im/vector/fragments/terms/ServiceTermsArgs.kt @@ -23,5 +23,5 @@ import org.matrix.androidsdk.features.terms.TermsManager data class ServiceTermsArgs( val type: TermsManager.ServiceType, val baseURL: String, - val token: String? + val token: String ) : Parcelable diff --git a/vector/src/main/java/im/vector/widgets/WidgetsManager.java b/vector/src/main/java/im/vector/widgets/WidgetsManager.java index 8e23fc7046..be536e1a88 100755 --- a/vector/src/main/java/im/vector/widgets/WidgetsManager.java +++ b/vector/src/main/java/im/vector/widgets/WidgetsManager.java @@ -568,28 +568,30 @@ public void onSuccess(RequestOpenIdTokenResponse info) { public void onSuccess(RegisterResponse info) { String token = info.scalarToken; - if (null != token) { + if (token == null) { + // Should not happen + callback.onUnexpectedError(new IllegalStateException("token is null")); + } else { tokensStore.setToken(session.getMyUserId(), config.getApiUrl(), token); - } - - // Validate it (this mostly checks to see if the IM needs us to agree to some terms) - widgetsRestClient.validateToken(token, new SimpleApiCallback(callback) { - @Override - public void onSuccess(Void info) { - callback.onSuccess(token); - } + // Validate it (this mostly checks to see if the IM needs us to agree to some terms) - @Override - public void onMatrixError(MatrixError e) { - if (MatrixError.TERMS_NOT_SIGNED.equals(e.errcode)) { - callback.onUnexpectedError(new TermsNotSignedException(token)); - } else { - super.onMatrixError(e); + widgetsRestClient.validateToken(token, new SimpleApiCallback(callback) { + @Override + public void onSuccess(Void info) { + callback.onSuccess(token); } - } - }); + @Override + public void onMatrixError(MatrixError e) { + if (MatrixError.TERMS_NOT_SIGNED.equals(e.errcode)) { + callback.onUnexpectedError(new TermsNotSignedException(token)); + } else { + super.onMatrixError(e); + } + } + }); + } } }); }