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: twitter oembed image not showing #34079

Merged
merged 7 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fuzzy-coins-lay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rocket.chat/meteor': patch
---

Fixes missing images in Twitter article links to ensure proper display by relying on meta tags.
24 changes: 21 additions & 3 deletions apps/meteor/app/oembed/server/providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { camelCase } from 'change-case';

import { callbacks } from '../../../lib/callbacks';
import { SystemLogger } from '../../../server/lib/logger/system';
import { settings } from '../../settings/server';
import { Info } from '../../utils/rocketchat.info';

class Providers {
private providers: OEmbedProvider[];
Expand All @@ -11,13 +13,20 @@ class Providers {
this.providers = [];
}

static getConsumerUrl(provider: OEmbedProvider, url: string): string {
static getConsumerUrl(provider: OEmbedProvider, url: string): string | undefined {
if (!provider.endPoint) {
scuciatto marked this conversation as resolved.
Show resolved Hide resolved
return;
}
const urlObj = new URL(provider.endPoint);
urlObj.searchParams.set('url', url);

return urlObj.toString();
}

static getCustomHeaders(provider: OEmbedProvider): { [k: string]: string } {
return provider.getHeaderOverrides?.() || {};
}

registerProvider(provider: OEmbedProvider): number {
return this.providers.push(provider);
}
Expand Down Expand Up @@ -75,7 +84,11 @@ providers.registerProvider({

providers.registerProvider({
urls: [new RegExp('https?://(twitter|x)\\.com/[^/]+/status/\\S+')],
endPoint: 'https://publish.twitter.com/oembed',
getHeaderOverrides: () => {
return {
'User-Agent': `${settings.get('API_Embed_UserAgent')} Rocket.Chat/${Info.version} Googlebot/2.1`,
};
},
});

providers.registerProvider({
Expand Down Expand Up @@ -104,7 +117,12 @@ callbacks.add(

const consumerUrl = Providers.getConsumerUrl(provider, url);

return { ...data, urlObj: new URL(consumerUrl) };
const headerOverrides = Providers.getCustomHeaders(provider);
if (!consumerUrl) {
return { ...data, headerOverrides };
}

return { ...data, headerOverrides, urlObj: new URL(consumerUrl) };
},
callbacks.priority.MEDIUM,
'oembed-providers-before',
Expand Down
1 change: 1 addition & 0 deletions apps/meteor/app/oembed/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ const getUrlContent = async (urlObj: URL, redirectCount = 5): Promise<OEmbedUrlC
headers: {
'User-Agent': `${settings.get('API_Embed_UserAgent')} Rocket.Chat/${Info.version}`,
'Accept-Language': settings.get('Language') || 'en',
...data.headerOverrides,
},
size: sizeLimit, // max size of the response body, this was not working as expected so I'm also manually verifying that on the iterator
},
Expand Down
1 change: 1 addition & 0 deletions apps/meteor/lib/callbacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ type ChainedCallbackSignatures = {
'renderMessage': <T extends IMessage & { html: string }>(message: T) => T;
'oembed:beforeGetUrlContent': (data: { urlObj: URL }) => {
urlObj: URL;
headerOverrides?: { [k: string]: string };
};
'oembed:afterParseContent': (data: { url: string; meta: OEmbedMeta; headers: { [k: string]: string }; content: OEmbedUrlContent }) => {
url: string;
Expand Down
3 changes: 2 additions & 1 deletion packages/core-typings/src/IOembed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export type OEmbedUrlContent = {

export type OEmbedProvider = {
urls: RegExp[];
endPoint: string;
endPoint?: string;
getHeaderOverrides?: () => { [k: string]: string };
};

export type OEmbedUrlContentResult = {
Expand Down
Loading