Skip to content

Commit

Permalink
TW-1492 Add some properties for ads definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
keshan3262 committed Jul 16, 2024
1 parent 591436c commit 92f5059
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/advertising/external-ads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,10 @@ export interface PermanentAdPlacesRule extends ExtVersionConstraints {
divWrapperStyle?: Record<StylePropName, string>;
wrapperStyle?: Record<StylePropName, string>;
elementToMeasureSelector?: string;
elementsToMeasureSelectors?: Record<'width' | 'height', string>;
stylesOverrides?: AdStylesOverrides[];
shouldHideOriginal?: boolean;
displayWidth?: string;
}

export interface AdProvidersByDomainRule extends ExtVersionConstraints {
Expand All @@ -138,6 +140,7 @@ export interface AdProvidersByDomainRule extends ExtVersionConstraints {

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

Expand Down
18 changes: 18 additions & 0 deletions src/routers/slise-ad-rules/ad-places.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,18 @@ const transformAdPlacesDictionary = <T extends ExtVersionConstraints>(rules: Rec
* elementToMeasureSelector:
* type: string
* description: A selector of the element which should be measured to define banner size
* elementsToMeasureSelectors:
* type: object
* required:
* - width
* - height
* properties:
* width:
* type: string
* description: A selector of the element which should be measured to define banner width
* height:
* type: string
* description: A selector of the element which should be measured to define banner height
* stylesOverrides:
* type: array
* items:
Expand All @@ -288,6 +300,12 @@ const transformAdPlacesDictionary = <T extends ExtVersionConstraints>(rules: Rec
* type: boolean
* description: Whether original ads banners should be hidden but not removed
* default: false
* displayWidth:
* type: string
* description: >
* A range of display widths in a semver-like format where the rule is applicable. Numbers can be only
* integers. If not specified, the rule is applicable for all display widths.
* example: '>=1024 <1280'
* example:
* urlRegexes:
* - '^https://etherscan\.io/tx/'
Expand Down
45 changes: 45 additions & 0 deletions src/routers/slise-ad-rules/providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ import {
* type: array
* items:
* type: string
* negativeSelectors:
* descriptions: Selectors for the elements that should not be touched even if they match `selectors`
* type: array
* items:
* type: string
* parentDepth:
* type: integer
* minimum: 0
Expand Down Expand Up @@ -320,6 +325,46 @@ addObjectStorageMethodsToRouter<AdProvidersByDomainRule[]>(adProvidersRouter, {
objectTransformFn: identity
});

/**
* @swagger
* /api/slise-ad-rules/providers/negative-selectors:
* get:
* summary: Get negative selectors for all providers filtered by extension version
* tags:
* - Known ads providers
* parameters:
* - in: query
* name: extVersion
* schema:
* type: string
* default: '0.0.0'
* description: The extension version for which the rules should be returned
* responses:
* '200':
* description: Provider - selectors dictionary
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/AdProvidersDictionary'
* '500':
* $ref: '#/components/responses/ErrorResponse'
*/
adProvidersRouter.get(
'/negative-selectors',
withExceptionHandler(async (req, res) => {
const allRules = await adProvidersMethods.getAllValues();

const entries = Object.entries(allRules).map(([providerId, providerRules]): [string, string[]] => [
providerId,
filterByVersion(providerRules, req.query.extVersion as string | undefined)
.map(({ negativeSelectors }) => negativeSelectors ?? [])
.flat()
]);

res.status(200).send(Object.fromEntries(entries));
})
);

/**
* @swagger
* /api/slise-ad-rules/providers/raw/all:
Expand Down
29 changes: 28 additions & 1 deletion src/utils/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,35 @@ const permanentAdPlacesRulesSchema = arraySchema()
divWrapperStyle: styleSchema,
wrapperStyle: styleSchema,
elementToMeasureSelector: cssSelectorSchema,
elementsToMeasureSelectors: objectSchema()
.shape({ width: cssSelectorSchema.clone(), height: cssSelectorSchema.clone() })
.test('all-fields-present', function (value: unknown) {
if (!value || typeof value !== 'object') {

Check warning on line 179 in src/utils/schemas.ts

View workflow job for this annotation

GitHub Actions / Checks if ts and lint works

Unexpected any value in conditional. An explicit comparison or type cast is required
return true;
}

if (typeof (value as any).width === 'string' && typeof (value as any).height === 'string') {

Check warning on line 183 in src/utils/schemas.ts

View workflow job for this annotation

GitHub Actions / Checks if ts and lint works

Unexpected any. Specify a different type
return true;
}

throw this.createError({ path: this.path, message: 'Both `width` and `height` fields must be specified' });
})
.default(undefined) as unknown as IObjectSchema<{ width: string; height: string } | undefined>,
stylesOverrides: arraySchema().of(adStylesOverridesSchema.clone().required()),
shouldHideOriginal: booleanSchema(),
extVersion: versionRangeSchema.clone().required()
extVersion: versionRangeSchema.clone().required(),
displayWidth: versionRangeSchema.clone().test('valid-boundary-values', (value: string | undefined) => {
if (!isDefined(value) || value.length === 0) {
return true;
}

const nonIntegerNumberMatches = value.match(/\d+\.\d+/g);
if (isDefined(nonIntegerNumberMatches)) {
throw new Error('Display width must be an integer');
}

return true;
})
})
.test('insertion-place-specified', (value: PermanentAdPlacesRule | undefined) => {
if (!value) {
Expand Down Expand Up @@ -217,6 +243,7 @@ export const adProvidersByDomainsRulesDictionarySchema: IObjectSchema<Record<str

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

0 comments on commit 92f5059

Please sign in to comment.