From f4af1687bf12543f89dfe37d12bd40061859dc7d Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Fri, 26 Aug 2022 09:48:49 -0500 Subject: [PATCH] Filter out expiring ads --- app/soapbox/features/ads/providers/index.ts | 2 ++ app/soapbox/features/ads/providers/rumble.ts | 1 + app/soapbox/queries/ads.ts | 14 +++++++++++++- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/app/soapbox/features/ads/providers/index.ts b/app/soapbox/features/ads/providers/index.ts index 65e593985..b9c504bff 100644 --- a/app/soapbox/features/ads/providers/index.ts +++ b/app/soapbox/features/ads/providers/index.ts @@ -20,6 +20,8 @@ interface Ad { card: Card, /** Impression URL to fetch when displaying the ad. */ impression?: string, + /** Time when the ad expires and should no longer be displayed. */ + expires?: Date, } /** Gets the current provider based on config. */ diff --git a/app/soapbox/features/ads/providers/rumble.ts b/app/soapbox/features/ads/providers/rumble.ts index 249fc8547..ace4021f0 100644 --- a/app/soapbox/features/ads/providers/rumble.ts +++ b/app/soapbox/features/ads/providers/rumble.ts @@ -43,6 +43,7 @@ const RumbleAdProvider: AdProvider = { image: item.asset, url: item.click, }), + expires: new Date(item.expires * 1000), })); } } diff --git a/app/soapbox/queries/ads.ts b/app/soapbox/queries/ads.ts index 7ad594a94..0b296f677 100644 --- a/app/soapbox/queries/ads.ts +++ b/app/soapbox/queries/ads.ts @@ -17,7 +17,19 @@ export default function useAds() { }); }; - return useQuery(['ads'], getAds, { + const result = useQuery(['ads'], getAds, { placeholderData: [], }); + + // Filter out expired ads. + const data = result.data?.filter(ad => { + const now = new Date(); + const isExpired = ad.expires && (now.getTime() > ad.expires.getTime()); + return !isExpired; + }); + + return { + ...result, + data, + }; }