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

Commit

Permalink
Merge pull request #2546 from vector-im/feature/bot
Browse files Browse the repository at this point in the history
Make the creation of Room with riot-bot safer
  • Loading branch information
bmarty authored Aug 24, 2018
2 parents 20c07c8 + 16e8153 commit 94b7925
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 30 deletions.
10 changes: 10 additions & 0 deletions vector/src/main/java/im/vector/LoginHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ public void getSupportedLoginFlows(Context ctx, final HomeServerConnectionConfig
LoginRestClient client = new LoginRestClient(hsConfig);

client.getSupportedLoginFlows(new UnrecognizedCertApiCallback<List<LoginFlow>>(hsConfig, callback) {
@Override
public void onSuccess(List<LoginFlow> info) {
callback.onSuccess(info);
}

@Override
public void onAcceptedCert() {
getSupportedLoginFlows(appCtx, hsConfig, callback);
Expand Down Expand Up @@ -221,6 +226,11 @@ public void submitEmailTokenValidation(final Context aCtx,
ThirdPidRestClient restClient = new ThirdPidRestClient(aHomeServerConfig);

pid.submitValidationToken(restClient, aToken, aClientSecret, aSid, new UnrecognizedCertApiCallback<Boolean>(aHomeServerConfig, aRespCallback) {
@Override
public void onSuccess(Boolean info) {
aRespCallback.onSuccess(info);
}

@Override
public void onAcceptedCert() {
submitEmailTokenValidation(aCtx, aHomeServerConfig, aToken, aClientSecret, aSid, aRespCallback);
Expand Down
22 changes: 22 additions & 0 deletions vector/src/main/java/im/vector/RegistrationManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,12 @@ public void onRegistrationSuccess() {
public void onRegistrationFailed(String message) {
listener.onUsernameAvailabilityChecked(!TextUtils.equals(MatrixError.USER_IN_USE, message));
}

@Override
public void onResourceLimitExceeded(MatrixError e) {
// Should not happen, consider user is available, registration will fail later on
listener.onUsernameAvailabilityChecked(true);
}
});
}
}
Expand Down Expand Up @@ -353,6 +359,11 @@ public void onRegistrationFailed(String message) {
listener.onRegistrationFailed(message);
}
}

@Override
public void onResourceLimitExceeded(MatrixError e) {
listener.onResourceLimitExceeded(e);
}
});
}
}
Expand Down Expand Up @@ -402,6 +413,11 @@ public void onRegistrationFailed(String message) {
listener.onRegistrationFailed(message);
}
}

@Override
public void onResourceLimitExceeded(MatrixError e) {
listener.onResourceLimitExceeded(e);
}
});
}

Expand Down Expand Up @@ -973,6 +989,8 @@ public void onMatrixError(MatrixError e) {
Log.e(LOG_TAG, "JsonUtils.toRegistrationFlowResponse " + castExcept.getLocalizedMessage(), castExcept);
}
listener.onRegistrationFailed(ERROR_MISSING_STAGE);
} else if (TextUtils.equals(e.errcode, MatrixError.RESOURCE_LIMIT_EXCEEDED)) {
listener.onResourceLimitExceeded(e);
} else {
listener.onRegistrationFailed("");
}
Expand All @@ -991,6 +1009,8 @@ private interface InternalRegistrationListener {
void onRegistrationSuccess();

void onRegistrationFailed(String message);

void onResourceLimitExceeded(MatrixError e);
}

/*
Expand Down Expand Up @@ -1023,5 +1043,7 @@ public interface RegistrationListener {
void onWaitingCaptcha();

void onThreePidRequestFailed(String message);

void onResourceLimitExceeded(MatrixError e);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/**
* Handle certificate errors in API callbacks
*/
public class UnrecognizedCertApiCallback<T> extends SimpleApiCallback<T> {
public abstract class UnrecognizedCertApiCallback<T> extends SimpleApiCallback<T> {

private HomeServerConnectionConfig mHsConfig;
private ApiCallback mCallback;
Expand All @@ -27,9 +27,7 @@ public UnrecognizedCertApiCallback(HomeServerConnectionConfig hsConfig) {
*
* The usual behavior is to play the request again
*/
public void onAcceptedCert() {

}
public abstract void onAcceptedCert();

/**
* The request failed because of an unknown TLS certificate or a network error
Expand Down
6 changes: 6 additions & 0 deletions vector/src/main/java/im/vector/activity/LoginActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2497,4 +2497,10 @@ public void onUsernameAvailabilityChecked(boolean isAvailable) {
}
}
}

@Override
public void onResourceLimitExceeded(MatrixError e) {
enableLoadingScreen(false);
mResourceLimitDialogHelper.displayDialog(e);
}
}
15 changes: 13 additions & 2 deletions vector/src/main/java/im/vector/activity/VectorRoomActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,9 @@ public class VectorRoomActivity extends MXCActionBarActivity implements

private ImageView mAvatarImageView;

@BindView(R.id.bottom_separator)
View mBottomSeparator;

@BindView(R.id.room_cannot_post_textview)
View mCanNotPostTextView;

Expand Down Expand Up @@ -788,7 +791,7 @@ public void onTextChanged(CharSequence s, int start, int before, int count) {
if ((!TextUtils.isEmpty(mEventId) || (null != sRoomPreviewData)) || hasBeenKicked) {
if (!mIsUnreadPreviewMode || hasBeenKicked) {
mNotificationsArea.setVisibility(View.GONE);
findViewById(R.id.bottom_separator).setVisibility(View.GONE);
mBottomSeparator.setVisibility(View.GONE);
findViewById(R.id.room_notification_separator).setVisibility(View.GONE);
}

Expand Down Expand Up @@ -2128,7 +2131,12 @@ private void cancelTypingNotification() {

mLastTypingDate = 0;

mRoom.sendTypingNotification(false, -1, new SimpleApiCallback<Void>(this));
mRoom.sendTypingNotification(false, -1, new SimpleApiCallback<Void>(this) {
@Override
public void onSuccess(Void info) {
// Ignore
}
});
}
}

Expand Down Expand Up @@ -2944,11 +2952,14 @@ private void checkSendEventStatus() {

if (canSendMessages(state)) {
mBottomLayout.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
mBottomSeparator.setVisibility(View.VISIBLE);
mSendingMessagesLayout.setVisibility(View.VISIBLE);
mCanNotPostTextView.setVisibility(View.GONE);
} else if (state.isVersioned() || mResourceLimitExceededError != null) {
mBottomSeparator.setVisibility(View.GONE);
mBottomLayout.getLayoutParams().height = 0;
} else {
mBottomSeparator.setVisibility(View.GONE);
mSendingMessagesLayout.setVisibility(View.GONE);
mCanNotPostTextView.setVisibility(View.VISIBLE);
}
Expand Down
27 changes: 15 additions & 12 deletions vector/src/main/java/im/vector/dialogs/ResourceLimitDialogHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ import com.binaryfork.spanny.Spanny
import im.vector.R
import im.vector.activity.interfaces.Restorable
import im.vector.error.ResourceLimitErrorFormatter
import im.vector.util.sendMailTo
import im.vector.util.openUri
import org.matrix.androidsdk.rest.model.MatrixError
import org.matrix.androidsdk.util.Log

private const val LOG_TAG = "ResourceLimitDialogHelper"

Expand All @@ -48,22 +47,26 @@ class ResourceLimitDialogHelper private constructor(private val activity: Activi
* Display the resource limit dialog, if not already displayed
*/
fun displayDialog(matrixError: MatrixError) {
if (matrixError.adminContact == null) {
Log.e(LOG_TAG, "Missing required parameter 'admin_contact'")
return
}

dialogLocker.displayDialog {
val title = Spanny(activity.getString(R.string.resource_limit_exceeded_title), StyleSpan(Typeface.BOLD))
val message = formatter.format(matrixError, ResourceLimitErrorFormatter.Mode.Hard, separator = "\n\n")

AlertDialog.Builder(activity, R.style.AppTheme_Dialog_Light)
val builder = AlertDialog.Builder(activity, R.style.AppTheme_Dialog_Light)
.setTitle(title)
.setMessage(message)
.setPositiveButton(R.string.resource_limit_contact_action) { _, _ ->
sendMailTo(matrixError.adminContact, activity = activity)
}
.setNegativeButton(R.string.cancel, null)

if (matrixError.adminUri != null) {
builder
.setPositiveButton(R.string.resource_limit_contact_action) { _, _ ->
openUri(activity, matrixError.adminUri!!)
}
.setNegativeButton(R.string.cancel, null)

} else {
builder.setPositiveButton(R.string.ok, null)
}

builder
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,30 +38,31 @@ class ResourceLimitErrorFormatter(private val context: Context) {
mode: Mode,
separator: CharSequence = " ",
clickable: Boolean = false): CharSequence {

val error = if (MatrixError.LIMITE_TYPE_MAU == matrixError.limitType) {
val error = if (MatrixError.LIMIT_TYPE_MAU == matrixError.limitType) {
context.getString(mode.mauErrorRes)
} else {
context.getString(mode.defaultErrorRes)
}
val contact = if (clickable) {
val contactSubString = contactAsLink(matrixError.adminContact)

val contact = if (clickable && matrixError.adminUri != null) {
val contactSubString = uriAsLink(matrixError.adminUri!!)
val contactFullString = context.getString(mode.contactRes, contactSubString)
Html.fromHtml(contactFullString)
} else {
val contactSubString = context.getString(R.string.resource_limit_contact_admin)
context.getString(mode.contactRes, contactSubString)
}

return Spanny(error)
.append(separator)
.append(contact)
}

/**
* Create a HTML link with an email address
* Create a HTML link with a uri
*/
private fun contactAsLink(email: String): String {
private fun uriAsLink(uri: String): String {
val contactStr = context.getString(R.string.resource_limit_contact_admin)
return "<a href=\"mailto:$email\">$contactStr</a>"
return "<a href=\"$uri\">$contactStr</a>"
}
}
15 changes: 14 additions & 1 deletion vector/src/main/java/im/vector/util/ExternalApplicationsUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,17 @@ fun sendMailTo(address: String, subject: String? = null, message: String? = null
} catch (activityNotFoundException: ActivityNotFoundException) {
activity.toast(R.string.error_no_external_application_found)
}
}
}

/**
* Open an arbitrary uri
*/
fun openUri(activity: Activity, uri: String) {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(uri))

try {
activity.startActivity(intent)
} catch (activityNotFoundException: ActivityNotFoundException) {
activity.toast(R.string.error_no_external_application_found)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@

package im.vector.webview

import android.text.TextUtils
import im.vector.Matrix
import im.vector.activity.VectorAppCompatActivity
import im.vector.util.weak
import org.matrix.androidsdk.rest.callback.ApiCallback
import org.matrix.androidsdk.rest.callback.SimpleApiCallback
import org.matrix.androidsdk.rest.model.MatrixError
import org.matrix.androidsdk.rest.model.RoomMember
import org.matrix.androidsdk.util.Log

private const val SUCCESS_URL = "https://matrix.org/_matrix/consent"
private const val SUCCESS_URL_SUFFIX = "/_matrix/consent"
private const val RIOT_BOT_ID = "@riot-bot:matrix.org"
private const val LOG_TAG = "ConsentWebViewEventListener"

Expand All @@ -40,7 +40,7 @@ class ConsentWebViewEventListener(activity: VectorAppCompatActivity, private val

override fun onPageFinished(url: String) {
delegate.onPageFinished(url)
if (TextUtils.equals(url, SUCCESS_URL)) {
if (url.endsWith(SUCCESS_URL_SUFFIX)) {
createRiotBotRoomIfNeeded()
}
}
Expand All @@ -56,7 +56,14 @@ class ConsentWebViewEventListener(activity: VectorAppCompatActivity, private val
}
if (joinedRooms.isEmpty()) {
it.showWaitingView()
session.createDirectMessageRoom(RIOT_BOT_ID, createRiotBotRoomCallback)
// Ensure we can create a Room with riot-bot. Error can be a MatrixError: "Federation denied with matrix.org.", or any other error.
session.profileApiClient
.displayname(RIOT_BOT_ID, object : SimpleApiCallback<String>(createRiotBotRoomCallback) {
override fun onSuccess(info: String?) {
// Ok, the Home Server knows riot-Bot, so create a Room with him
session.createDirectMessageRoom(RIOT_BOT_ID, createRiotBotRoomCallback)
}
})
} else {
it.finish()
}
Expand Down
1 change: 1 addition & 0 deletions vector/src/main/res/layout/activity_vector_room.xml
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@
android:paddingBottom="8dp"
android:paddingTop="8dp"
android:visibility="invisible"
tools:background="@color/vector_fuchsia_color"
tools:visibility="visible" />

<View
Expand Down
1 change: 1 addition & 0 deletions vector/src/main/res/layout/view_notification_area.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
android:minHeight="42dp"
android:paddingBottom="8dp"
android:paddingTop="8dp"
tools:background="@color/vector_fuchsia_color"
tools:parentTag="android.widget.RelativeLayout">

<ImageView
Expand Down

0 comments on commit 94b7925

Please sign in to comment.