tuskyapp / Tusky

An Android client for the microblogging server Mastodon
https://tusky.app
GNU General Public License v3.0
2.42k stars 388 forks source link

/users/:name/statuses/:id does not look like Fediverse URL #3092

Closed sk22 closed 1 year ago

sk22 commented 1 year ago

Hi! I'm using Tusky's looksLikeMastodonUrl helper function in Megalodon and I stumbled upon (an Akkoma user shared it) a URL that looks like this, which was not detected:

https://example.social/users/[username]/statuses/[numeric_id]

Adding this RegEx seems to fix the issue:

"^/users/[^/]+/statuses/[a-zA-Z0-9]+$"


kylegoetz commented 1 year ago

@Tak is the idea behind this function that the BottomSheetActivity receives instructions to open a URL, and if the URL does not look like a Mastodon one (seems like it's doing some regex with what you'd see after api/v1 or api/v2) it opens in browser but otherwise it does some other stuff?

If so, then how about we extract MastodonApi urls to their own const strings and then refer to those in the MastodonApi definition, and then you can do

//mastodonapi.kt
private const val FOLLOW_TAG_URL = "/......./{hash_id}"
mastodonApiUrlList = listOf(FOLLOW_TAG_URL, ...)

@POST(FOLLOW_TAG_URL)
suspend fun followTag(): Obs....

// looksLikeMastodonApi function
return mastodonApiUrlList
        .map { it.replace(Regex("\\{(.+?)\\}"), "\\\\w+").toRegex() }
        .any(uri.path::matches)

This will keep every {tagId} in the Mastodon API URLs but will convert them into regex-appropriate word boundaries for the URL, and now there's no duplication of hard-coded URLs anywhere.

This will resolve the issue, probably make the function more complete.

Edit Probably need to append "$" to each URL in the list too for regex purposes, like

.map { "${it.replace(...)}$".toRegex() }
Tak commented 1 year ago

It's not about whether a url is a mastodon api point, but whether it's likely to be a fediverse object Tusky knows how to handle, or whether we should open it in the browser.

kylegoetz commented 1 year ago

Oh OK so no reusing of URLs from the MastodonApi then!