Skip to content

Commit

Permalink
TW-1399: New ads providers to replace (#157)
Browse files Browse the repository at this point in the history
* TW-1442 Add API for sites blacklist for replacing ads

* TW-1442 Change response types for URLs blacklist

* TW-1399 Add parentDepth parameter for selectors by ads providers
  • Loading branch information
keshan3262 authored May 8, 2024
1 parent 8f706e6 commit 3a5a915
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/advertising/external-ads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ export interface AdProvidersByDomainRule extends ExtVersionConstraints {

export interface AdProviderSelectorsRule extends ExtVersionConstraints {
selectors: string[];
parentDepth?: number;
}

export interface AdProviderForAllSitesRule extends ExtVersionConstraints {
Expand Down
39 changes: 34 additions & 5 deletions src/routers/slise-ad-rules/providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from '../../advertising/external-ads';
import { basicAuth } from '../../middlewares/basic-auth.middleware';
import { addObjectStorageMethodsToRouter, withBodyValidation, withExceptionHandler } from '../../utils/express-helpers';
import { transformValues } from '../../utils/helpers';
import { isDefined, transformValues } from '../../utils/helpers';
import {
nonEmptyStringsListSchema,
hostnamesListSchema,
Expand Down Expand Up @@ -72,17 +72,36 @@ import {
* type: array
* items:
* type: string
* parentDepth:
* type: integer
* minimum: 0
* default: 0
* AdByProviderSelector:
* oneOf:
* - type: string
* - type: object
* required:
* - selector
* - parentDepth
* properties:
* selector:
* type: string
* parentDepth:
* type: integer
* AdProvidersDictionary:
* type: object
* additionalProperties:
* type: array
* items:
* type: string
* $ref: '#/components/schemas/AdByProviderSelector'
* example:
* google:
* - '#Ads_google_bottom_wide'
* - '.GoogleAdInfo'
* - 'a[href^="https://googleads.g.doubleclick.net/pcs/click"]'
* persona:
* - selector: "a.persona-product"
* parentDepth: 1
* AdProvidersInputsDictionary:
* type: object
* additionalProperties:
Expand Down Expand Up @@ -366,11 +385,13 @@ addObjectStorageMethodsToRouter<AdProvidersByDomainRule[]>(adProvidersRouter, {
* schema:
* type: array
* items:
* type: string
* $ref: '#/components/schemas/AdByProviderSelector'
* example:
* - '#Ads_google_bottom_wide'
* - '.GoogleAdInfo'
* - 'a[href^="https://googleads.g.doubleclick.net/pcs/click"]'
* - selector: "a.persona-product"
* parentDepth: 1
* '500':
* $ref: '#/components/responses/ErrorResponse'
* /api/slise-ad-rules/providers:
Expand Down Expand Up @@ -446,12 +467,20 @@ const transformAdProviderSelectorsRules = (rules: AdProviderSelectorsRule[], req
Array.from(
new Set(
filterByVersion(rules, req.query.extVersion as string | undefined)
.map(({ selectors }) => selectors)
.map(({ selectors, parentDepth }) =>
isDefined(parentDepth) && parentDepth > 0 ? { selector: selectors.join(', '), parentDepth } : selectors
)
.flat()
)
);

addObjectStorageMethodsToRouter<AdProviderSelectorsRule[], Record<string, string[]>, string[]>(adProvidersRouter, {
type AdByProviderSelector = string | { selector: string; parentDepth: number };

addObjectStorageMethodsToRouter<
AdProviderSelectorsRule[],
Record<string, AdByProviderSelector[]>,
AdByProviderSelector[]
>(adProvidersRouter, {
path: '/',
methods: adProvidersMethods,
keyName: 'providerId',
Expand Down
3 changes: 2 additions & 1 deletion src/utils/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,8 @@ export const adProvidersByDomainsRulesDictionarySchema: IObjectSchema<Record<str

const adProvidersSelectorsRuleSchema = objectSchema().shape({
selectors: cssSelectorsListSchema.clone().required(),
extVersion: versionRangeSchema.clone().required()
extVersion: versionRangeSchema.clone().required(),
parentDepth: numberSchema().integer().min(0).default(0)
});

export const adProvidersDictionarySchema: IObjectSchema<Record<string, AdProviderSelectorsRule[]>> =
Expand Down

0 comments on commit 3a5a915

Please sign in to comment.