From b369658bb919b3f5e2d6b63ee6ccdc8589cfb82e Mon Sep 17 00:00:00 2001 From: Inokentii Mazhara Date: Tue, 27 Aug 2024 13:15:32 +0300 Subject: [PATCH] TW-1489: Smarty Ads integration (#170) * TW-1464 Prepare backend for new native ads * TW-1464 Update API documentation * TW-1464 Enable adding native ads in replace-only mode * TW-1492 Add some properties for ads definitions * TW-1492 Prepare the backend to additional fixtures for Mises * TW-1492 Add an entrypoint for elements to hide or remove * TW-1489 Add an entrypoints for categories of original ads providers --- src/advertising/external-ads.ts | 5 ++ src/routers/slise-ad-rules/providers.ts | 84 ++++++++++++++++++++++++- src/utils/schemas.ts | 5 ++ 3 files changed, 92 insertions(+), 2 deletions(-) diff --git a/src/advertising/external-ads.ts b/src/advertising/external-ads.ts index 597ec44..f59a5cf 100644 --- a/src/advertising/external-ads.ts +++ b/src/advertising/external-ads.ts @@ -175,6 +175,7 @@ const PERMANENT_AD_PLACES_RULES_KEY = 'permanent_ad_places_rules'; const PERMANENT_NATIVE_AD_PLACES_RULES_KEY = 'permanent_native_ad_places_rules'; const REPLACE_ADS_URLS_BLACKLIST_KEY = 'replace_ads_urls_blacklist'; const ELEMENTS_TO_HIDE_OR_REMOVE_KEY = 'elements_to_hide_or_remove'; +const AD_PROVIDERS_CATEGORIES_KEY = 'ad_providers_categories'; export const adPlacesRulesMethods = objectStorageMethodsFactory(AD_PLACES_RULES_KEY, []); @@ -205,6 +206,10 @@ export const elementsToHideOrRemoveMethods = objectStorageMethodsFactory(AD_PROVIDERS_CATEGORIES_KEY, [ + 'crypto' +]); + export const getAdProvidersForAllSites = async () => redisClient.smembers(AD_PROVIDERS_ALL_SITES_KEY); export const addAdProvidersForAllSites = async (providers: string[]) => diff --git a/src/routers/slise-ad-rules/providers.ts b/src/routers/slise-ad-rules/providers.ts index 253df9d..5567565 100644 --- a/src/routers/slise-ad-rules/providers.ts +++ b/src/routers/slise-ad-rules/providers.ts @@ -9,7 +9,8 @@ import { adProvidersByDomainRulesMethods, AdProviderSelectorsRule, filterRules, - AdProvidersByDomainRule + AdProvidersByDomainRule, + adProvidersCategoriesMethods } from '../../advertising/external-ads'; import { basicAuth } from '../../middlewares/basic-auth.middleware'; import { addObjectStorageMethodsToRouter, withBodyValidation, withExceptionHandler } from '../../utils/express-helpers'; @@ -18,7 +19,8 @@ import { nonEmptyStringsListSchema, hostnamesListSchema, adProvidersByDomainsRulesDictionarySchema, - adProvidersDictionarySchema + adProvidersDictionarySchema, + adProvidersCategoriesDictionarySchema } from '../../utils/schemas'; /** @@ -116,6 +118,12 @@ import { * type: array * items: * $ref: '#/components/schemas/AdProvidersInputValue' + * AdProvidersCategoriesDictionary: + * type: object + * additionalProperties: + * type: array + * items: + * type: string */ export const adProvidersRouter = Router(); @@ -328,6 +336,78 @@ addObjectStorageMethodsToRouter(adProvidersRouter, { objectTransformFn: identity }); +/** + * @swagger + * /api/slise-ad-rules/providers/categories: + * get: + * summary: Get categories for providers + * tags: + * - Known ads providers + * responses: + * '200': + * description: Provider - categories dictionary + * content: + * application/json: + * schema: + * $ref: '#/components/schemas/AdProvidersCategoriesDictionary' + * '500': + * $ref: '#/components/responses/ErrorResponse' + * post: + * summary: Upsert categories for providers + * tags: + * - Known ads providers + * security: + * - basicAuth: [] + * requestBody: + * description: Provider - categories dictionary + * content: + * application/json: + * schema: + * $ref: '#/components/schemas/AdProvidersCategoriesDictionary' + * responses: + * '200': + * $ref: '#/components/responses/SuccessResponse' + * '400': + * $ref: '#/components/responses/ErrorResponse' + * '401': + * $ref: '#/components/responses/UnauthorizedError' + * '500': + * $ref: '#/components/responses/ErrorResponse' + * delete: + * summary: Delete categories for providers + * tags: + * - Known ads providers + * security: + * - basicAuth: [] + * requestBody: + * description: List of provider IDs for which categories should be deleted + * content: + * application/json: + * schema: + * type: array + * items: + * type: string + * responses: + * '200': + * $ref: '#/components/responses/SuccessResponse' + * '400': + * $ref: '#/components/responses/ErrorResponse' + * '401': + * $ref: '#/components/responses/UnauthorizedError' + * '500': + * $ref: '#/components/responses/ErrorResponse' + */ +addObjectStorageMethodsToRouter(adProvidersRouter, { + path: '/categories', + methods: adProvidersCategoriesMethods, + keyName: 'providerId', + objectValidationSchema: adProvidersCategoriesDictionarySchema, + keysArrayValidationSchema: nonEmptyStringsListSchema, + successfulRemovalMessage: entriesCount => `${entriesCount} entries have been removed`, + valueTransformFn: identity, + objectTransformFn: identity +}); + /** * @swagger * /api/slise-ad-rules/providers/negative-selectors: diff --git a/src/utils/schemas.ts b/src/utils/schemas.ts index be62bcc..a5a1241 100644 --- a/src/utils/schemas.ts +++ b/src/utils/schemas.ts @@ -278,3 +278,8 @@ export const elementsToHideOrRemoveDictionarySchema = makeDictionarySchema( + nonEmptyStringSchema.clone().required(), + arraySchema().of(nonEmptyStringSchema.clone().required()).required() +).required();