Skip to content
This repository has been archived by the owner on Apr 12, 2022. It is now read-only.

Commit

Permalink
Use the hashed v2 lookup API for 3PIDs (#3257)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmarty committed Aug 29, 2019
1 parent 711fcb6 commit 73a3f50
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 32 deletions.
2 changes: 1 addition & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:
-
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion vector/src/main/java/im/vector/activity/LoginActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -426,8 +424,11 @@ private void createRoom(final List<ParticipantAdapterItem> participants) {

CreateRoomParams params = new CreateRoomParams();

// First participant is self, so remove
List<ParticipantAdapterItem> participantsWithoutMe = participants.subList(1, participants.size());

List<String> ids = new ArrayList<>();
for (ParticipantAdapterItem item : participants) {
for (ParticipantAdapterItem item : participantsWithoutMe) {
if (null != item.mUserId) {
ids.add(item.mUserId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ public void run() {
}
});
}

@Override
public void onIdentityServerTermsNotSigned(String token) {
Log.w(LOG_TAG, "onIdentityServerTermsNotSigned()");
}
};

// refresh the presence asap
Expand Down
16 changes: 16 additions & 0 deletions vector/src/main/java/im/vector/contacts/ContactsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion vector/src/main/java/im/vector/contacts/PIDsRetriever.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -54,6 +55,8 @@ public interface PIDsRetrieverListener {
* Called the PIDs retrieval fails.
*/
void onFailure(String accountId);

void onIdentityServerTermsNotSigned(String token);
}

// current instance
Expand Down Expand Up @@ -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());
}
}
});
}
Expand Down
21 changes: 21 additions & 0 deletions vector/src/main/java/im/vector/fragments/PeopleFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -89,6 +90,11 @@ public void run() {
});
}
}

@Override
public void onIdentityServerTermsNotSigned(String token) {
Log.w("VectorSearchPeopleListFragment", "onIdentityServerTermsNotSigned()");
}
};

// refresh the presence asap
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
36 changes: 19 additions & 17 deletions vector/src/main/java/im/vector/widgets/WidgetsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Void>(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<Void>(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);
}
}
});
}
}
});
}
Expand Down

0 comments on commit 73a3f50

Please sign in to comment.