Skip to content

Commit

Permalink
Fix review
Browse files Browse the repository at this point in the history
  • Loading branch information
Frenaud14 committed Sep 20, 2021
1 parent e37fb31 commit fa3abec
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ data class MatrixConfiguration(
"https://scalar-staging.riot.im/scalar/api"
),
/**
* Optional base url to create client permalinks instead of Matrix ones (matrix.to links).
* Optional base url to create client permalinks (eg. https://www.example.com/#/) instead of Matrix ones (matrix.to links).
* Do not forget to add the "#" which is required by the permalink parser.
*
* Note: this field is only used for permalinks creation, you will also have to edit the string-array `permalink_supported_hosts` in the config file
* and add it to your manifest to handle these links in the application.
*/
val clientPermalinkBaseUrl: String? = null,
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import android.net.Uri
/**
* Mapping of an input URI to a matrix.to compliant URI.
*/
object MatrixToMapper {
object MatrixToConverter {

/**
* Try to convert a URL from an element web instance or from a client permalink to a matrix.to url.
Expand All @@ -31,7 +31,7 @@ object MatrixToMapper {
* - https://app.element.io/#/room/#element-android:matrix.org -> https://matrix.to/#/#element-android:matrix.org
* - https://www.example.org/#/room/#element-android:matrix.org -> https://matrix.to/#/#element-android:matrix.org
*/
fun map(uri: Uri): Uri? {
fun convert(uri: Uri): Uri? {
val uriString = uri.toString()

return when {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import java.net.URLDecoder
* This class turns a uri to a [PermalinkData]
* element-based domains (e.g. https://app.element.io/#/user/@chagai95:matrix.org) permalinks
* or matrix.to permalinks (e.g. https://matrix.to/#/@chagai95:matrix.org)
* or client permalinks (e.g. https://www.example.com/#/user/@chagai95:matrix.org)
* or client permalinks (e.g. <clientPermalinkBaseUrl>user/@chagai95:matrix.org)
*/
object PermalinkParser {

Expand All @@ -46,12 +46,12 @@ object PermalinkParser {
// the client or element-based domain permalinks (e.g. https://app.element.io/#/user/@chagai95:matrix.org) don't have the
// mxid in the first param (like matrix.to does - https://matrix.to/#/@chagai95:matrix.org) but rather in the second after /user/ so /user/mxid
// so convert URI to matrix.to to simplify parsing process
val matrixToUri = MatrixToMapper.map(uri) ?: return PermalinkData.FallbackLink(uri)
val matrixToUri = MatrixToConverter.convert(uri) ?: return PermalinkData.FallbackLink(uri)

// We can't use uri.fragment as it is decoding to early and it will break the parsing
// of parameters that represents url (like signurl)
val fragment = matrixToUri.toString().substringAfter("#") // uri.fragment
if (fragment.isNullOrEmpty()) {
if (fragment.isEmpty()) {
return PermalinkData.FallbackLink(uri)
}
val safeFragment = fragment.substringBefore('?')
Expand Down Expand Up @@ -122,22 +122,21 @@ object PermalinkParser {
}
}

private fun safeExtractParams(fragment: String) = fragment.substringAfter("?").split('&').mapNotNull {
val splitNameValue = it.split("=")
if (splitNameValue.size == 2) {
Pair(splitNameValue[0], URLDecoder.decode(splitNameValue[1], "UTF-8"))
} else null
}
private fun safeExtractParams(fragment: String) =
fragment.substringAfter("?").split('&').mapNotNull {
val splitNameValue = it.split("=")
if (splitNameValue.size == 2) {
Pair(splitNameValue[0], URLDecoder.decode(splitNameValue[1], "UTF-8"))
} else null
}

private fun String.getViaParameters(): List<String> {
return UrlQuerySanitizer(this)
.parameterList
.filter {
it.mParameter == "via"
}.map {
it.mValue.let {
URLDecoder.decode(it, "UTF-8")
}
URLDecoder.decode(it.mValue, "UTF-8")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import org.matrix.android.sdk.api.session.events.model.Event

/**
* Useful methods to create permalink (like matrix.to links or client permalinks).
* See [org.matrix.android.sdk.api.MatrixConfiguration.clientPermalinkBaseUrl] to setup a custom permalink base url.
*/
interface PermalinkService {

Expand Down
9 changes: 7 additions & 2 deletions vector/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@
<activity android:name=".features.home.HomeActivity" />
<activity
android:name=".features.login.LoginActivity"
android:launchMode="singleTask"
android:enabled="@bool/useLoginV1"
android:launchMode="singleTask"
android:windowSoftInputMode="adjustResize">
<!-- Add intent filter to handle redirection URL after SSO login in external browser -->
<intent-filter>
Expand All @@ -122,8 +122,8 @@
</activity>
<activity
android:name=".features.login2.LoginActivity2"
android:launchMode="singleTask"
android:enabled="@bool/useLoginV2"
android:launchMode="singleTask"
android:windowSoftInputMode="adjustResize">
<!-- Add intent filter to handle redirection URL after SSO login in external browser -->
<intent-filter>
Expand Down Expand Up @@ -180,8 +180,11 @@
<activity android:name=".features.createdirect.CreateDirectRoomActivity" />
<activity android:name=".features.invite.InviteUsersToRoomActivity" />
<activity android:name=".features.webview.VectorWebViewActivity" />

<!-- Activity to intercept links coming from a web instance -->
<activity
android:name=".features.link.LinkHandlerActivity"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
Expand All @@ -198,8 +201,10 @@
</intent-filter>
</activity>

<!-- Activity alias for matrix.to or element permalinks -->
<activity-alias
android:name=".features.permalink.PermalinkHandlerActivity"
android:enabled="true"
android:exported="true"
android:launchMode="singleTask"
android:targetActivity=".features.link.LinkHandlerActivity">
Expand Down

0 comments on commit fa3abec

Please sign in to comment.