Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TW-1364 Implement returning different ads data depending on web exten… #153

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"pino-pretty": "^4.7.1",
"qs": "^6.10.3",
"semaphore": "^1.1.0",
"semver": "^7.6.0",
"swagger-jsdoc": "^6.2.8",
"swagger-ui-express": "^5.0.0",
"yup": "^1.3.2"
Expand Down
68 changes: 42 additions & 26 deletions src/advertising/slise.ts → src/advertising/external-ads.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { satisfies as versionSatisfiesRange } from 'semver';

import { objectStorageMethodsFactory, redisClient } from '../redis';

/** Style properties names that are likely to be unnecessary for banners are skipped */
Expand Down Expand Up @@ -77,12 +79,16 @@ export const stylePropsNames = [
];
export type StylePropName = (typeof stylePropsNames)[number];

interface SliseAdStylesOverrides {
interface AdStylesOverrides {
parentDepth: number;
style: Record<StylePropName, string>;
}

export interface SliseAdPlacesRule {
interface ExtVersionConstraints {
extVersion: string;
}

export interface AdPlacesRule extends ExtVersionConstraints {
urlRegexes: string[];
selector: {
isMultiple: boolean;
Expand All @@ -91,11 +97,11 @@ export interface SliseAdPlacesRule {
shouldUseDivWrapper: boolean;
divWrapperStyle?: Record<StylePropName, string>;
};
stylesOverrides?: SliseAdStylesOverrides[];
stylesOverrides?: AdStylesOverrides[];
shouldHideOriginal?: boolean;
}

export interface PermanentSliseAdPlacesRule {
export interface PermanentAdPlacesRule extends ExtVersionConstraints {
urlRegexes: string[];
adSelector: {
isMultiple: boolean;
Expand All @@ -115,48 +121,58 @@ export interface PermanentSliseAdPlacesRule {
elementStyle?: Record<StylePropName, string>;
divWrapperStyle?: Record<StylePropName, string>;
elementToMeasureSelector?: string;
stylesOverrides?: SliseAdStylesOverrides[];
stylesOverrides?: AdStylesOverrides[];
shouldHideOriginal?: boolean;
}

export interface SliseAdProvidersByDomainRule {
export interface AdProvidersByDomainRule extends ExtVersionConstraints {
urlRegexes: string[];
providers: string[];
}

const SLISE_AD_PLACES_RULES_KEY = 'slise_ad_places_rules';
const SLISE_AD_PROVIDERS_BY_SITES_KEY = 'slise_ad_providers_by_sites';
const SLISE_AD_PROVIDERS_ALL_SITES_KEY = 'slise_ad_providers_all_sites';
const SLISE_AD_PROVIDERS_LIST_KEY = 'slise_ad_providers_list';
const PERMANENT_SLISE_AD_PLACES_RULES_KEY = 'permanent_slise_ad_places_rules';
export interface AdProviderSelectorsRule extends ExtVersionConstraints {
selectors: string[];
}

export interface AdProviderForAllSitesRule extends ExtVersionConstraints {
providers: string[];
}

const AD_PLACES_RULES_KEY = 'ad_places_rules';
const AD_PROVIDERS_BY_SITES_KEY = 'ad_providers_by_sites';
const AD_PROVIDERS_ALL_SITES_KEY = 'ad_providers_all_sites';
const AD_PROVIDERS_LIST_KEY = 'ad_providers_list';
const PERMANENT_AD_PLACES_RULES_KEY = 'permanent_ad_places_rules';
const PERMANENT_NATIVE_AD_PLACES_RULES_KEY = 'permanent_native_ad_places_rules';

export const sliseAdPlacesRulesMethods = objectStorageMethodsFactory<SliseAdPlacesRule[]>(
SLISE_AD_PLACES_RULES_KEY,
[]
);
export const adPlacesRulesMethods = objectStorageMethodsFactory<AdPlacesRule[]>(AD_PLACES_RULES_KEY, []);

export const sliseAdProvidersByDomainRulesMethods = objectStorageMethodsFactory<SliseAdProvidersByDomainRule[]>(
SLISE_AD_PROVIDERS_BY_SITES_KEY,
export const adProvidersByDomainRulesMethods = objectStorageMethodsFactory<AdProvidersByDomainRule[]>(
AD_PROVIDERS_BY_SITES_KEY,
[]
);

export const sliseAdProvidersMethods = objectStorageMethodsFactory<string[]>(SLISE_AD_PROVIDERS_LIST_KEY, []);
export const adProvidersMethods = objectStorageMethodsFactory<AdProviderSelectorsRule[]>(AD_PROVIDERS_LIST_KEY, []);

export const permanentSliseAdPlacesMethods = objectStorageMethodsFactory<PermanentSliseAdPlacesRule[]>(
PERMANENT_SLISE_AD_PLACES_RULES_KEY,
export const permanentAdPlacesMethods = objectStorageMethodsFactory<PermanentAdPlacesRule[]>(
PERMANENT_AD_PLACES_RULES_KEY,
[]
);

export const permanentNativeAdPlacesMethods = objectStorageMethodsFactory<PermanentSliseAdPlacesRule[]>(
export const permanentNativeAdPlacesMethods = objectStorageMethodsFactory<PermanentAdPlacesRule[]>(
PERMANENT_NATIVE_AD_PLACES_RULES_KEY,
[]
);

export const getSliseAdProvidersForAllSites = async () => redisClient.smembers(SLISE_AD_PROVIDERS_ALL_SITES_KEY);
export const getAdProvidersForAllSites = async () => redisClient.smembers(AD_PROVIDERS_ALL_SITES_KEY);

export const addAdProvidersForAllSites = async (providers: string[]) =>
redisClient.sadd(AD_PROVIDERS_ALL_SITES_KEY, ...providers);

export const removeAdProvidersForAllSites = async (providers: string[]) =>
redisClient.srem(AD_PROVIDERS_ALL_SITES_KEY, ...providers);

export const addSliseAdProvidersForAllSites = async (providers: string[]) =>
redisClient.sadd(SLISE_AD_PROVIDERS_ALL_SITES_KEY, ...providers);
const FALLBACK_VERSION = '0.0.0';

export const removeSliseAdProvidersForAllSites = async (providers: string[]) =>
redisClient.srem(SLISE_AD_PROVIDERS_ALL_SITES_KEY, ...providers);
export const filterByVersion = <T extends ExtVersionConstraints>(rules: T[], version?: string) =>
rules.filter(({ extVersion }) => versionSatisfiesRange(version ?? FALLBACK_VERSION, extVersion));
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import { getParsedContent } from './notifications/utils/get-parsed-content.util';
import { getPlatforms } from './notifications/utils/get-platforms.util';
import { redisClient } from './redis';
import { sliseRulesRouter } from './routers/slise-ad-rules';
import { adRulesRouter } from './routers/slise-ad-rules';
import { getABData } from './utils/ab-test';
import { cancelAliceBobOrder } from './utils/alice-bob/cancel-alice-bob-order';
import { createAliceBobOrder } from './utils/alice-bob/create-alice-bob-order';
Expand Down Expand Up @@ -157,7 +157,7 @@
await redisClient.lpush('notifications', JSON.stringify(newNotification));

res.status(200).send({ message: 'Notification added successfully', notification: newNotification });
} catch (error: any) {

Check warning on line 160 in src/index.ts

View workflow job for this annotation

GitHub Actions / Checks if ts and lint works

Unexpected any. Specify a different type
res.status(500).send({ error: error.message });
}
});
Expand Down Expand Up @@ -328,14 +328,14 @@
}
});

app.use('/api/slise-ad-rules', sliseRulesRouter);
app.use('/api/slise-ad-rules', adRulesRouter);

app.post('/api/magic-square-quest/start', async (req, res) => {
try {
await startMagicSquareQuest(req.body);

res.status(200).send({ message: 'Quest successfully started' });
} catch (error: any) {

Check warning on line 338 in src/index.ts

View workflow job for this annotation

GitHub Actions / Checks if ts and lint works

Unexpected any. Specify a different type
console.error(error);

if (error instanceof CodedError) {
Expand All @@ -349,7 +349,7 @@
app.get('/api/magic-square-quest/participants', basicAuth, async (req, res) => {
try {
res.status(200).send(await getMagicSquareQuestParticipants());
} catch (error: any) {

Check warning on line 352 in src/index.ts

View workflow job for this annotation

GitHub Actions / Checks if ts and lint works

Unexpected any. Specify a different type
console.error(error);

if (error instanceof CodedError) {
Expand All @@ -363,10 +363,10 @@
app.get('/api/signing-nonce', (req, res) => {
try {
const pkh = req.query.pkh;
if (!pkh || typeof pkh !== 'string') throw new Error('PKH is not a string');

Check warning on line 366 in src/index.ts

View workflow job for this annotation

GitHub Actions / Checks if ts and lint works

Unexpected value in conditional. A boolean expression is required

res.status(200).send(getSigningNonce(pkh));
} catch (error: any) {

Check warning on line 369 in src/index.ts

View workflow job for this annotation

GitHub Actions / Checks if ts and lint works

Unexpected any. Specify a different type
console.error(error);

if (error instanceof CodedError) {
Expand Down
Loading
Loading