UX: show complete URL path if website domain is same as instance domain#6
UX: show complete URL path if website domain is same as instance domain#6everettbu wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR implements a UX improvement to show complete URL paths when a user's website domain matches the Discourse instance domain, instead of just showing the hostname. This allows users to see more specific website information when the domains are related.
Key changes:
- Moved website name logic from frontend to backend serializer for better performance and consistency
- Enhanced domain comparison logic to handle parent/subdomain relationships
- Updated frontend template to use the new serialized website_name field
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
app/serializers/user_serializer.rb |
Added website_name method with domain comparison logic and path inclusion |
spec/serializers/user_serializer_spec.rb |
Added comprehensive test cases for different domain scenarios |
app/assets/javascripts/discourse/templates/user/user.hbs |
Updated template to use model.website_name instead of controller property |
app/assets/javascripts/discourse/controllers/user.js.es6 |
Removed obsolete websiteName computed property |
app/assets/javascripts/discourse/models/user.js.es6 |
Fixed incorrect JSDoc comment |
| end | ||
|
|
||
| def website_name | ||
| website_host = URI(website.to_s).host rescue nil |
There was a problem hiding this comment.
Using rescue nil in inline form can hide different types of exceptions. Consider using explicit exception handling with begin/rescue to catch specific URI parsing errors.
| website_host = URI(website.to_s).host rescue nil | |
| website_host = begin | |
| URI(website.to_s).host | |
| rescue URI::InvalidURIError | |
| nil | |
| end |
| if website_host == discourse_host | ||
| # example.com == example.com | ||
| website_host + URI(website.to_s).path | ||
| elsif (website_host.split('.').length == discourse_host.split('.').length) && discourse_host.split('.').length > 2 | ||
| # www.example.com == forum.example.com | ||
| website_host.split('.')[1..-1].join('.') == discourse_host.split('.')[1..-1].join('.') ? website_host + URI(website.to_s).path : website_host | ||
| else | ||
| # example.com == forum.example.com | ||
| discourse_host.ends_with?("." << website_host) ? website_host + URI(website.to_s).path : website_host |
There was a problem hiding this comment.
The condition is complex and the domain splitting logic is repeated multiple times. Consider extracting the domain level calculation into a helper method to improve readability and reduce duplication.
| if website_host == discourse_host | |
| # example.com == example.com | |
| website_host + URI(website.to_s).path | |
| elsif (website_host.split('.').length == discourse_host.split('.').length) && discourse_host.split('.').length > 2 | |
| # www.example.com == forum.example.com | |
| website_host.split('.')[1..-1].join('.') == discourse_host.split('.')[1..-1].join('.') ? website_host + URI(website.to_s).path : website_host | |
| else | |
| # example.com == forum.example.com | |
| discourse_host.ends_with?("." << website_host) ? website_host + URI(website.to_s).path : website_host | |
| compare_domains(website_host, discourse_host, URI(website.to_s).path) | |
| end | |
| private | |
| def compare_domains(website_host, discourse_host, website_path) | |
| if website_host == discourse_host | |
| # example.com == example.com | |
| website_host + website_path | |
| elsif (website_host.split('.').length == discourse_host.split('.').length) && discourse_host.split('.').length > 2 | |
| # www.example.com == forum.example.com | |
| website_host.split('.')[1..-1].join('.') == discourse_host.split('.')[1..-1].join('.') ? website_host + website_path : website_host | |
| else | |
| # example.com == forum.example.com | |
| discourse_host.ends_with?("." << website_host) ? website_host + website_path : website_host |
| website_host = URI(website.to_s).host rescue nil | ||
| discourse_host = Discourse.current_hostname | ||
| return if website_host.nil? | ||
| if website_host == discourse_host | ||
| # example.com == example.com | ||
| website_host + URI(website.to_s).path | ||
| elsif (website_host.split('.').length == discourse_host.split('.').length) && discourse_host.split('.').length > 2 | ||
| # www.example.com == forum.example.com | ||
| website_host.split('.')[1..-1].join('.') == discourse_host.split('.')[1..-1].join('.') ? website_host + URI(website.to_s).path : website_host | ||
| else | ||
| # example.com == forum.example.com | ||
| discourse_host.ends_with?("." << website_host) ? website_host + URI(website.to_s).path : website_host |
There was a problem hiding this comment.
URI parsing is called multiple times for the same website URL. Consider parsing the URI once and storing the result to avoid redundant parsing operations.
| website_host = URI(website.to_s).host rescue nil | |
| discourse_host = Discourse.current_hostname | |
| return if website_host.nil? | |
| if website_host == discourse_host | |
| # example.com == example.com | |
| website_host + URI(website.to_s).path | |
| elsif (website_host.split('.').length == discourse_host.split('.').length) && discourse_host.split('.').length > 2 | |
| # www.example.com == forum.example.com | |
| website_host.split('.')[1..-1].join('.') == discourse_host.split('.')[1..-1].join('.') ? website_host + URI(website.to_s).path : website_host | |
| else | |
| # example.com == forum.example.com | |
| discourse_host.ends_with?("." << website_host) ? website_host + URI(website.to_s).path : website_host | |
| parsed_website = URI(website.to_s) rescue nil | |
| website_host = parsed_website&.host | |
| discourse_host = Discourse.current_hostname | |
| return if website_host.nil? | |
| if website_host == discourse_host | |
| # example.com == example.com | |
| website_host + parsed_website.path | |
| elsif (website_host.split('.').length == discourse_host.split('.').length) && discourse_host.split('.').length > 2 | |
| # www.example.com == forum.example.com | |
| website_host.split('.')[1..-1].join('.') == discourse_host.split('.')[1..-1].join('.') ? website_host + parsed_website.path : website_host | |
| else | |
| # example.com == forum.example.com | |
| discourse_host.ends_with?("." << website_host) ? website_host + parsed_website.path : website_host |
| website_host.split('.')[1..-1].join('.') == discourse_host.split('.')[1..-1].join('.') ? website_host + URI(website.to_s).path : website_host | ||
| else | ||
| # example.com == forum.example.com | ||
| discourse_host.ends_with?("." << website_host) ? website_host + URI(website.to_s).path : website_host |
There was a problem hiding this comment.
The string concatenation using << modifies the original string. Consider using + for string concatenation to avoid side effects: discourse_host.ends_with?("." + website_host).
| discourse_host.ends_with?("." << website_host) ? website_host + URI(website.to_s).path : website_host | |
| discourse_host.ends_with?("." + website_host) ? website_host + URI(website.to_s).path : website_host |
Test 6