Add Rumble AdProvider
This commit is contained in:
parent
b02141874e
commit
0eeca2be5c
2 changed files with 46 additions and 0 deletions
|
@ -6,6 +6,7 @@ import type { Card } from 'soapbox/types/entities';
|
||||||
/** Map of available provider modules. */
|
/** Map of available provider modules. */
|
||||||
const PROVIDERS: Record<string, () => Promise<AdProvider>> = {
|
const PROVIDERS: Record<string, () => Promise<AdProvider>> = {
|
||||||
soapbox: async() => (await import(/* webpackChunkName: "features/ads/soapbox" */'./soapbox-config')).default,
|
soapbox: async() => (await import(/* webpackChunkName: "features/ads/soapbox" */'./soapbox-config')).default,
|
||||||
|
rumble: async() => (await import(/* webpackChunkName: "features/ads/rumble" */'./rumble')).default,
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Ad server implementation. */
|
/** Ad server implementation. */
|
||||||
|
|
45
app/soapbox/features/ads/providers/rumble.ts
Normal file
45
app/soapbox/features/ads/providers/rumble.ts
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
import { getSoapboxConfig } from 'soapbox/actions/soapbox';
|
||||||
|
import { normalizeCard } from 'soapbox/normalizers';
|
||||||
|
|
||||||
|
import type { AdProvider } from '.';
|
||||||
|
|
||||||
|
/** Rumble ad API entity. */
|
||||||
|
interface RumbleAd {
|
||||||
|
type: number,
|
||||||
|
impression: string,
|
||||||
|
click: string,
|
||||||
|
asset: string,
|
||||||
|
expires: number,
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Response from Rumble ad server. */
|
||||||
|
interface RumbleApiResponse {
|
||||||
|
count: number,
|
||||||
|
ads: RumbleAd[],
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Provides ads from Soapbox Config. */
|
||||||
|
const RumbleAdProvider: AdProvider = {
|
||||||
|
getAds: async(getState) => {
|
||||||
|
const state = getState();
|
||||||
|
const soapboxConfig = getSoapboxConfig(state);
|
||||||
|
const endpoint = soapboxConfig.extensions.getIn(['ads', 'endpoint']) as string | undefined;
|
||||||
|
|
||||||
|
if (endpoint) {
|
||||||
|
const response = await fetch(endpoint);
|
||||||
|
const data = await response.json() as RumbleApiResponse;
|
||||||
|
return data.ads.map(item => ({
|
||||||
|
impression: item.impression,
|
||||||
|
card: normalizeCard({
|
||||||
|
type: item.type === 1 ? 'Link' : 'Rich',
|
||||||
|
image: item.asset,
|
||||||
|
url: item.click,
|
||||||
|
}),
|
||||||
|
}));
|
||||||
|
} else {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export default RumbleAdProvider;
|
Loading…
Reference in a new issue