Widget URL: properly encode parameters

This commit is contained in:
Benoit Marty 2021-02-05 11:47:35 +01:00
parent b068dc39eb
commit 557a5c715a
1 changed files with 20 additions and 12 deletions

View File

@ -65,23 +65,31 @@ internal class WidgetFactory @Inject constructor(private val userDataSource: Use
) )
} }
// Ref: https://github.com/matrix-org/matrix-widget-api/blob/master/src/templating/url-template.ts#L29-L33
private fun WidgetContent.computeURL(roomId: String?, widgetId: String): String? { private fun WidgetContent.computeURL(roomId: String?, widgetId: String): String? {
var computedUrl = url ?: return null var computedUrl = url ?: return null
val myUser = userDataSource.getUser(userId) val myUser = userDataSource.getUser(userId)
computedUrl = computedUrl
.replace("\$matrix_user_id", userId)
.replace("\$matrix_display_name", myUser?.getBestName() ?: userId)
.replace("\$matrix_avatar_url", myUser?.avatarUrl ?: "")
.replace("\$matrix_widget_id", widgetId)
if (roomId != null) { val keyValue = data.mapKeys { "\$$it" }.toMutableMap()
computedUrl = computedUrl.replace("\$matrix_room_id", roomId)
} keyValue[WIDGET_PATTERN_MATRIX_USER_ID] = userId
for ((key, value) in data) { keyValue[WIDGET_PATTERN_MATRIX_DISPLAY_NAME] = myUser?.getBestName() ?: userId
if (value is String) { keyValue[WIDGET_PATTERN_MATRIX_AVATAR_URL] = myUser?.avatarUrl ?: ""
computedUrl = computedUrl.replace("$$key", URLEncoder.encode(value, "utf-8")) keyValue[WIDGET_PATTERN_MATRIX_WIDGET_ID] = widgetId
} keyValue[WIDGET_PATTERN_MATRIX_ROOM_ID] = roomId ?: ""
for ((key, value) in keyValue) {
computedUrl = computedUrl.replace(key, URLEncoder.encode(value.toString(), "utf-8"))
} }
return computedUrl return computedUrl
} }
companion object {
// Value to be replaced in URLS
const val WIDGET_PATTERN_MATRIX_USER_ID = "\$matrix_user_id"
const val WIDGET_PATTERN_MATRIX_DISPLAY_NAME = "\$matrix_display_name"
const val WIDGET_PATTERN_MATRIX_AVATAR_URL = "\$matrix_avatar_url"
const val WIDGET_PATTERN_MATRIX_WIDGET_ID = "\$matrix_widget_id"
const val WIDGET_PATTERN_MATRIX_ROOM_ID = "\$matrix_room_id"
}
} }