Skip to content

Commit

Permalink
fix: twitter oembed image not showing (#34079)
Browse files Browse the repository at this point in the history
  • Loading branch information
abhinavkrin authored Dec 17, 2024
1 parent e2d286c commit 156da2a
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 4 deletions.
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) {
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

0 comments on commit 156da2a

Please sign in to comment.