Skip to content

A platform for community discussion. Free, open, simple.

FIX: Giant YouTube fallback to iframe in chat (#42758)

Martin Brennan5473f1e56f61
diff --git a/plugins/chat/assets/javascripts/discourse/components/chat-message-collapser.gjs b/plugins/chat/assets/javascripts/discourse/components/chat-message-collapser.gjs
index a2bddd350fb..e479b912f10 100644
--- a/plugins/chat/assets/javascripts/discourse/components/chat-message-collapser.gjs
+++ b/plugins/chat/assets/javascripts/discourse/components/chat-message-collapser.gjs
@@ -128,8 +128,11 @@ export default class ChatMessageCollapser extends Component {
: e.firstElementChild.href;
link = escapeExpression(link);
+ const title = youtubeThumbnailOneboxPredicate(e)
+ ? i18n("chat.youtube")
+ : link;
const header = trustHTML(
- `<a target="_blank" class="chat-message-collapser-link-small" rel="noopener noreferrer" href="${link}">${link}</a>`
+ `<a target="_blank" class="chat-message-collapser-link-small" rel="noopener noreferrer" href="${link}">${title}</a>`
);
acc.push({ header, body: e.outerHTML, needsCollapser: true });
} else {
@@ -260,6 +263,15 @@ function imageOneboxPredicate(e) {
return animatedImagePredicate(e) || externalImageOnebox(e);
}
+function youtubeThumbnailOneboxPredicate(e) {
+ return (
+ externalImageOnebox(e) &&
+ e.firstElementChild.firstElementChild.classList.contains(
+ "youtube-thumbnail"
+ )
+ );
+}
+
function hasImageOnebox(elements) {
return elements.some((e) => imageOneboxPredicate(e));
}
diff --git a/plugins/chat/assets/stylesheets/common/chat-message-images.scss b/plugins/chat/assets/stylesheets/common/chat-message-images.scss
index 8076e2bb43c..5b7af038b16 100644
--- a/plugins/chat/assets/stylesheets/common/chat-message-images.scss
+++ b/plugins/chat/assets/stylesheets/common/chat-message-images.scss
@@ -18,6 +18,10 @@ $max_video_height: 250px;
overflow: hidden;
}
+ iframe.youtube-onebox {
+ max-width: calc(#{$max_video_height} / 9 * 16);
+ }
+
.onebox {
&:not(img) {
container-type: inline-size;
@@ -74,6 +78,10 @@ $max_video_height: 250px;
}
}
+ .youtube-onebox {
+ margin-top: var(--space-1);
+ }
+
.onebox.twitterstatus {
max-width: 500px;
diff --git a/plugins/chat/config/locales/client.en.yml b/plugins/chat/config/locales/client.en.yml
index 4c9cd0efc06..cecdb77ba5e 100644
--- a/plugins/chat/config/locales/client.en.yml
+++ b/plugins/chat/config/locales/client.en.yml
@@ -360,6 +360,7 @@ en:
uploaded_files:
one: "%{count} file"
other: "%{count} files"
+ youtube: "YouTube"
you_flagged: "You flagged this message"
exit: "exit chat"
channel_status:
diff --git a/plugins/chat/test/javascripts/components/chat-message-collapser-test.gjs b/plugins/chat/test/javascripts/components/chat-message-collapser-test.gjs
index 102f2883c2e..80199ef68aa 100644
--- a/plugins/chat/test/javascripts/components/chat-message-collapser-test.gjs
+++ b/plugins/chat/test/javascripts/components/chat-message-collapser-test.gjs
@@ -24,6 +24,10 @@ const externalImageCooked =
'<p><a href="http://cat2.com" class="onebox"><img src="/images/d-logo-sketch-small.png" width=8" height="8"></img></a></p>' +
"<p>and even more</p>";
+const youtubeFallbackCooked =
+ '<p><a href="https://img.youtube.com/vi/vljEH7bMe1c/maxresdefault.jpg" class="onebox"><img class="youtube-thumbnail onebox" src="https://img.youtube.com/vi/vljEH7bMe1c/maxresdefault.jpg"></a></p>' +
+ '<iframe src="https://www.youtube.com/embed/vljEH7bMe1c" class="youtube-onebox"></iframe>';
+
const imageCooked =
"<p>written text</p>" +
'<p><img src="/images/avatar.png" alt="shows alt" width="8" height="8"></p>' +
@@ -196,6 +200,18 @@ module("Component | chat message collapser images", function (hooks) {
assert.dom(".chat-message-collapser-link-small").includesText("2 files");
});
+ test("labels a fallback youtube thumbnail without exposing its URL", async function (assert) {
+ this.set("cooked", youtubeFallbackCooked);
+
+ await render(
+ <template><ChatMessageCollapser @cooked={{this.cooked}} /></template>
+ );
+
+ assert
+ .dom(".chat-message-collapser-link-small")
+ .hasText("YouTube", "the fallback thumbnail has a readable label");
+ });
+
test("collapses and expands images", async function (assert) {
this.set("cooked", imageTextCooked);
this.set("uploads", [
diff --git a/plugins/discourse-lazy-videos/config/locales/server.en.yml b/plugins/discourse-lazy-videos/config/locales/server.en.yml
index 66ffba02e05..3f0ded33a99 100644
--- a/plugins/discourse-lazy-videos/config/locales/server.en.yml
+++ b/plugins/discourse-lazy-videos/config/locales/server.en.yml
@@ -3,4 +3,4 @@ en:
lazy_videos_enabled: "Enable the Lazy Videos plugin"
lazy_youtube_enabled: "Enable lazy loading for YouTube embeds"
lazy_vimeo_enabled: "Enable lazy loading for Vimeo embeds"
- lazy_tiktok_enabled: "Enable lazy loading for TikTok embeds (experimental)"
\ No newline at end of file
+ lazy_tiktok_enabled: "Enable lazy loading for TikTok embeds (experimental)"