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

41 lines
983 B
TypeScript
Raw Normal View History

import axios from 'axios';
import { z } from 'zod';
2022-10-20 14:29:14 -07:00
import { getSettings } from 'soapbox/actions/settings';
import { cardSchema } from 'soapbox/schemas/card';
import { filteredArray } from 'soapbox/schemas/utils';
2022-10-20 14:29:14 -07:00
import type { AdProvider } from '.';
/** TruthSocial ad API entity. */
const truthAdSchema = z.object({
impression: z.string(),
card: cardSchema,
expires_at: z.string(),
reason: z.string().catch(''),
});
2022-10-20 14:29:14 -07:00
/** Provides ads from the TruthSocial API. */
const TruthAdProvider: AdProvider = {
getAds: async(getState) => {
const state = getState();
const settings = getSettings(state);
try {
const { data } = await axios.get('/api/v2/truth/ads?device=desktop', {
headers: {
'Accept-Language': z.string().catch('*').parse(settings.get('locale')),
},
});
2022-10-20 14:29:14 -07:00
return filteredArray(truthAdSchema).parse(data);
} catch (e) {
// do nothing
2022-10-20 14:29:14 -07:00
}
return [];
},
};
export default TruthAdProvider;