Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Add Account fails when full server uri is used #805

Conversation

sockenklaus
Copy link
Contributor

Implemented a function that extracts the host part from a URL, removes the protocol and anything that may come after a :, a / or a ?.

Closes #739

…RL, removes the protocol and anything that may come after a :, a / or a ?.

Closes LemmyNet#739
Copy link
Member

@dessalines dessalines left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, although I really wish we didn't have to use regex for something like this, and could use a library.

@@ -1231,6 +1231,18 @@ fun scrollToPreviousParentComment(
}
}

/**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Someone should really make a library for this, if its functionality isn't already included in URL.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this is the java ecosystem, of course something like this exists already.

Patterns.WEB_URL.matcher(url).matches() // To check if a valid URL
println(URL("https://lemmy.ml").host) // lemmy.ml

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect

Copy link
Contributor

@twizmwazin twizmwazin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use @MV-GH's solution

@sockenklaus
Copy link
Contributor Author

sockenklaus commented Jun 22, 2023

Let's use @MV-GH's solution

But regex is fun! 😢

No, seriously: @MV-GH's solution is clearly more elegant. I will adjust the PR later the day.

I saw that there are at least a few other occurrences where URLs are handled with either Regex or similar methods. Maybe we could address those too?

@twizmwazin
Copy link
Contributor

In some cases regex is being used because the links are metaverse links that aren't actually valid URLs, or at least I don't think they are valid. @user@instance, !community@instance mainly. If it isn't one of those I agree using regex is sub-optimal and fixing those would be welcome.

@sockenklaus
Copy link
Contributor Author

sockenklaus commented Jun 22, 2023

I reworked the function but didn't ended up using Patterns.WEB_URL to match against because for some reason it doesn't handle hosts without domain-part (i.e. localhost). I think local hosts are useful for local testing.

Also I added Roboelectric to the project because I didn't see any other way to test my function without mocking. But I'm fairly new to using and writing unit tests to correct me if I was wrong using that package.

@MV-GH
Copy link
Collaborator

MV-GH commented Jun 22, 2023

Could you not use @RunWith(JUnitParamsRunner::class) ?
see com/jerboa/ui/components/common/LemmyLinkPluginTest.kt

@sockenklaus
Copy link
Contributor Author

Yes, thanks! That worked!

@twizmwazin twizmwazin enabled auto-merge (squash) June 22, 2023 18:06
@twizmwazin twizmwazin merged commit 2ce1d83 into LemmyNet:main Jun 22, 2023
@sockenklaus sockenklaus deleted the 739_add_account_fails_when_full_server_uri_is_used branch June 22, 2023 20:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Account fails when full URI of server instance used
4 participants