bigbuffet-rw/app/soapbox/features/ads/providers/index.ts

44 lines
1.6 KiB
TypeScript
Raw Normal View History

2022-08-01 20:43:28 -07:00
import { getSoapboxConfig } from 'soapbox/actions/soapbox';
2022-08-01 16:50:46 -07:00
import type { RootState } from 'soapbox/store';
import type { Card } from 'soapbox/types/entities';
2022-08-01 20:43:28 -07:00
/** Map of available provider modules. */
const PROVIDERS: Record<string, () => Promise<AdProvider>> = {
soapbox: async() => (await import(/* webpackChunkName: "features/ads/soapbox" */'./soapbox-config')).default,
2022-08-01 21:03:16 -07:00
rumble: async() => (await import(/* webpackChunkName: "features/ads/rumble" */'./rumble')).default,
2022-10-20 14:29:14 -07:00
truth: async() => (await import(/* webpackChunkName: "features/ads/truth" */'./truth')).default,
2022-08-01 20:43:28 -07:00
};
2022-08-01 16:50:46 -07:00
/** Ad server implementation. */
interface AdProvider {
getAds(getState: () => RootState): Promise<Ad[]>,
}
/** Entity representing an advertisement. */
interface Ad {
/** Ad data in Card (OEmbed-ish) format. */
card: Card,
/** Impression URL to fetch when displaying the ad. */
impression?: string,
2022-08-26 07:48:49 -07:00
/** Time when the ad expires and should no longer be displayed. */
2022-10-20 14:29:14 -07:00
expires_at?: string,
/** Reason the ad is displayed. */
reason?: string,
2022-08-01 16:50:46 -07:00
}
2022-08-01 20:43:28 -07:00
/** Gets the current provider based on config. */
const getProvider = async(getState: () => RootState): Promise<AdProvider | undefined> => {
const state = getState();
const soapboxConfig = getSoapboxConfig(state);
const isEnabled = soapboxConfig.extensions.getIn(['ads', 'enabled'], false) === true;
const providerName = soapboxConfig.extensions.getIn(['ads', 'provider'], 'soapbox') as string;
if (isEnabled && PROVIDERS[providerName]) {
return PROVIDERS[providerName]();
}
};
export { getProvider };
2022-08-01 16:50:46 -07:00
export type { Ad, AdProvider };