Skip to content

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

Merged
twizmwazin merged 7 commits into
LemmyNet:mainfrom
sockenklaus:739_add_account_fails_when_full_server_uri_is_used
Jun 22, 2023
Merged

Fix: Add Account fails when full server uri is used#805
twizmwazin merged 7 commits into
LemmyNet:mainfrom
sockenklaus:739_add_account_fails_when_full_server_uri_is_used

Conversation

@sockenklaus

Copy link
Copy Markdown

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

@dessalines dessalines left a comment

Copy link
Copy Markdown
Member

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.

}
}

/**

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Member

Choose a reason for hiding this comment

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

Perfect

@twizmwazin twizmwazin left a comment

Copy link
Copy Markdown
Contributor

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

sockenklaus commented Jun 22, 2023

Copy link
Copy Markdown
Author

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
Copy Markdown
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

sockenklaus commented Jun 22, 2023

Copy link
Copy Markdown
Author

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

MV-GH commented Jun 22, 2023

Copy link
Copy Markdown
Collaborator

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

@sockenklaus

Copy link
Copy Markdown
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