pleroma/app/soapbox/features/ads/providers/rumble.ts

59 lines
1.4 KiB
TypeScript
Raw Normal View History

import axios from 'axios';
import { getSettings } from 'soapbox/actions/settings';
2022-08-01 21:03:16 -07:00
import { getSoapboxConfig } from 'soapbox/actions/soapbox';
2022-10-20 14:29:14 -07:00
import { normalizeAd, normalizeCard } from 'soapbox/normalizers';
2022-08-01 21:03:16 -07:00
import type { AdProvider } from '.';
/** Rumble ad API entity. */
interface RumbleAd {
type: number
impression: string
click: string
asset: string
expires: number
2022-08-01 21:03:16 -07:00
}
/** Response from Rumble ad server. */
interface RumbleApiResponse {
count: number
ads: RumbleAd[]
2022-08-01 21:03:16 -07:00
}
/** Provides ads from Soapbox Config. */
const RumbleAdProvider: AdProvider = {
getAds: async(getState) => {
const state = getState();
const settings = getSettings(state);
2022-08-01 21:03:16 -07:00
const soapboxConfig = getSoapboxConfig(state);
const endpoint = soapboxConfig.extensions.getIn(['ads', 'endpoint']) as string | undefined;
if (endpoint) {
try {
const { data } = await axios.get<RumbleApiResponse>(endpoint, {
headers: {
'Accept-Language': settings.get('locale', '*') as string,
},
});
2022-10-20 14:29:14 -07:00
return data.ads.map(item => normalizeAd({
impression: item.impression,
card: normalizeCard({
type: item.type === 1 ? 'link' : 'rich',
image: item.asset,
url: item.click,
}),
2022-10-20 14:29:14 -07:00
expires_at: new Date(item.expires * 1000),
}));
} catch (e) {
// do nothing
}
2022-08-01 21:03:16 -07:00
}
return [];
2022-08-01 21:03:16 -07:00
},
};
export default RumbleAdProvider;