Skip to content

Commit

Permalink
added support for custom headers for oembed providers
Browse files Browse the repository at this point in the history
Signed-off-by: Abhinav Kumar <[email protected]>
  • Loading branch information
abhinavkrin committed Dec 11, 2024
1 parent e16f595 commit c057863
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
27 changes: 25 additions & 2 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 @@ -73,6 +82,15 @@ providers.registerProvider({
endPoint: 'https://www.dailymotion.com/services/oembed?maxheight=200',
});

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

providers.registerProvider({
urls: [new RegExp('https?://(play|open)\\.spotify\\.com/(track|album|playlist|show)/\\S+')],
endPoint: 'https://open.spotify.com/oembed',
Expand All @@ -99,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 c057863

Please sign in to comment.