-
Notifications
You must be signed in to change notification settings - Fork 654
Fixed unfurling urls #52
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
base: main
Are you sure you want to change the base?
Conversation
5153c1c to
2cefcf6
Compare
2cefcf6 to
47f13a8
Compare
Created an optional flag for unfurling urls. It looks like this fixes the original problem, and now gives a option to turn it on or off via an environmental variable. Signed-off-by: JJ Asghar <[email protected]>
47f13a8 to
e3f67b4
Compare
|
Is there a case where you'd want to disable unfurling of links globally for the whole instance? |
| auto_link h(ContentFilters::TextMessagePresentationFilters.apply(message.body.body)), html: { target: "_blank" } | ||
| # Render the full ActionText content to preserve unfurled links and other attachments | ||
| # Use ActionText's built-in rendering | ||
| message.body.to_s.html_safe |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm somewhat concerned about calling .html_safe on user inputs. The TextMessagePresentationFilters did a few things like sanitize the input, and style the unfurled links.
| perform() { | ||
| // Check if unfurling is enabled via environment variable | ||
| if (!this.#isUnfurlingEnabled()) { | ||
| console.log("URL unfurling is disabled via ENABLE_URL_UNFURLING environment variable") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems a bit noisy, maybe it would be better to just resolve the promise?
| .then(response => { | ||
| console.log(`Unfurl response for ${this.url}:`, response.status, response.statusText) | ||
|
|
||
| if (!response.ok) { | ||
| console.warn(`Failed to fetch OpenGraph metadata for ${this.url}: ${response.status} ${response.statusText}`) | ||
| return null | ||
| } | ||
| return response.json | ||
| }) | ||
| .then(data => { | ||
| if (data) { | ||
| console.log(`Successfully unfurled ${this.url}:`, data) | ||
| this.#insertOpengraphAttachment(data) | ||
| } else { | ||
| console.log(`No data returned for ${this.url}`) | ||
| } | ||
| }) | ||
| .catch(error => { | ||
| console.warn(`Error unfurling URL ${this.url}:`, error) | ||
| }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like the same code just with logging?
Maybe it would be simpler to just log the error in the catch branch?
| #escapeHtml(text) { | ||
| const div = document.createElement('div') | ||
| div.textContent = text | ||
| return div.innerHTML | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can remove this sanitization completely since the unfurl payload is already sanitizied.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really understand how this improves the reliability of the fetch process?
Could you walk me through you r thought process here?
| <%= link_to truncate(opengraph_embed.filename, length: 280, omission: "…"), opengraph_embed.href, rel: "noreferrer", target: "_blank" %> | ||
| </div> | ||
| <div class="og-embed__description"><%= truncate(opengraph_embed.caption, length: 560, omission: "…").html_safe %></div> | ||
| <div class="og-embed__description"><%= truncate(opengraph_embed.description, length: 560, omission: "…").html_safe %></div> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think #caption is the correct method to call in this case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This shouldn't be part of the PR :)
| config.autoload_paths += %w[ test/test_helpers ] | ||
|
|
||
| # Enable URL unfurling for tests | ||
| ENV["ENABLE_URL_UNFURLING"] = "true" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should test everything unfurling related with this flag on and off
| event.target.addEventListener("trix-paste", this.#didPaste.bind(this)) | ||
| event.target.addEventListener("trix-change", this.#didChange.bind(this)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We prefere is the events are bound from the HTML elements, this makes the controllers more reusable.
| #didChange(event) { | ||
| this.#debouncedCheckForUrls(event) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe there is a better name for #didChange that communicates what it does better? E.g. checkForUrls or unfurl?
|
Awesome, I'll answer your suggestions/questions as quickly as I can, thank you for the review! |
Created an optional flag for unfurling urls.
It looks like this fixes the original problem, and now gives a option to turn it on or off via an environmental variable.