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-1192 Implement a backend for ads replacement management #130

4 changes: 2 additions & 2 deletions .env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ ALICE_BOB_PRIVATE_KEY=
THREE_ROUTE_API_URL=
THREE_ROUTE_API_AUTH_TOKEN=
REDIS_URL=
ADD_NOTIFICATION_USERNAME=
ADD_NOTIFICATION_PASSWORD=
ADMIN_USERNAME=
ADMIN_PASSWORD=
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@
"pino-http": "^5.5.0",
"pino-pretty": "^4.7.1",
"qs": "^6.10.3",
"semaphore": "^1.1.0"
"semaphore": "^1.1.0",
"swagger-jsdoc": "^6.2.8",
"swagger-ui-express": "^5.0.0",
"yup": "^1.3.2"
},
"scripts": {
"start": "cross-env NODE_ENV=development ts-node-dev --files --quiet src/index.ts",
Expand Down
153 changes: 153 additions & 0 deletions src/advertising/slise.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
import { redisClient } from '../redis';
import { isDefined } from '../utils/helpers';

/** Style properties names that are likely to be unnecessary for banners are skipped */
export const stylePropsNames = [
'align-content',
'align-items',
'align-self',
'alignment-baseline',
'aspect-ratio',
'background',
'border-radius',
'bottom',
'box-shadow',
'box-sizing',
'display',
'flex',
'flex-basis',
'flex-direction',
'flex-flow',
'flex-grow',
'flex-shrink',
'flex-wrap',
'float',
'height',
'justify-content',
'justify-items',
'justify-self',
'left',
'margin',
'margin-block',
'margin-block-end',
'margin-block-start',
'margin-bottom',
'margin-inline',
'margin-inline-end',
'margin-inline-start',
'margin-left',
'margin-right',
'margin-top',
'max-block-size',
'max-height',
'max-inline-size',
'max-width',
'min-block-size',
'min-height',
'min-inline-size',
'min-width',
'overflow',
'overflow-anchor',
'overflow-wrap',
'overflow-x',
'overflow-y',
'padding',
'padding-block',
'padding-block-end',
'padding-block-start',
'padding-bottom',
'padding-inline',
'padding-inline-end',
'padding-inline-start',
'padding-left',
'padding-right',
'padding-top',
'position',
'right',
'text-align',
'top',
'visibility',
'width',
'z-index'
];
export type StylePropName = (typeof stylePropsNames)[number];

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

export interface SliseAdPlacesRule {
urlRegexes: string[];
selector: {
isMultiple: boolean;
cssString: string;
parentDepth: number;
shouldUseDivWrapper: boolean;
divWrapperStyle?: Record<StylePropName, string>;
};
stylesOverrides?: SliseAdStylesOverrides[];
}

export interface SliseAdProvidersByDomainRule {
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 objectStorageMethodsFactory = <V>(storageKey: string, fallbackValue: V) => ({
getByKey: async (key: string): Promise<V> => {
const value = await redisClient.hget(storageKey, key);

return isDefined(value) ? JSON.parse(value) : fallbackValue;
},
getAllValues: async (): Promise<Record<string, V>> => {
const values = await redisClient.hgetall(storageKey);

const parsedValues: Record<string, V> = {};
for (const key in values) {
parsedValues[key] = JSON.parse(values[key]);
}

return parsedValues;
},
upsertValues: (newValues: Record<string, V>) =>
redisClient.hmset(
storageKey,
Object.fromEntries(Object.entries(newValues).map(([domain, value]) => [domain, JSON.stringify(value)]))
),
removeValues: (keys: string[]) => redisClient.hdel(storageKey, ...keys)
});

export const {
getByKey: getSliseAdPlacesRulesByDomain,
getAllValues: getAllSliseAdPlacesRules,
upsertValues: upsertSliseAdPlacesRules,
removeValues: removeSliseAdPlacesRules
} = objectStorageMethodsFactory<SliseAdPlacesRule[]>(SLISE_AD_PLACES_RULES_KEY, []);

export const {
getByKey: getSliseAdProvidersByDomain,
getAllValues: getAllSliseAdProvidersBySites,
upsertValues: upsertSliseAdProvidersBySites,
removeValues: removeSliseAdProvidersBySites
} = objectStorageMethodsFactory<SliseAdProvidersByDomainRule[]>(SLISE_AD_PROVIDERS_BY_SITES_KEY, []);

export const {
getByKey: getSelectorsByProviderId,
getAllValues: getAllProviders,
upsertValues: upsertProviders,
removeValues: removeProviders
} = objectStorageMethodsFactory<string[]>(SLISE_AD_PROVIDERS_LIST_KEY, []);

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

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

export const removeSliseAdProvidersForAllSites = async (providers: string[]) =>
redisClient.srem(SLISE_AD_PROVIDERS_ALL_SITES_KEY, ...providers);
4 changes: 2 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export const EnvVars = {
THREE_ROUTE_API_URL: getEnv('THREE_ROUTE_API_URL'),
THREE_ROUTE_API_AUTH_TOKEN: getEnv('THREE_ROUTE_API_AUTH_TOKEN'),
REDIS_URL: getEnv('REDIS_URL'),
ADD_NOTIFICATION_USERNAME: getEnv('ADD_NOTIFICATION_USERNAME'),
ADD_NOTIFICATION_PASSWORD: getEnv('ADD_NOTIFICATION_PASSWORD')
ADMIN_USERNAME: getEnv('ADMIN_USERNAME'),
ADMIN_PASSWORD: getEnv('ADMIN_PASSWORD')
};

for (const name in EnvVars) {
Expand Down
18 changes: 18 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import firebaseAdmin from 'firebase-admin';
import { stdSerializers } from 'pino';
import pinoHttp from 'pino-http';
import swaggerJSDoc from 'swagger-jsdoc';
import swaggerUi from 'swagger-ui-express';

import { getAdvertisingInfo } from './advertising/advertising';
import { MIN_ANDROID_APP_VERSION, MIN_IOS_APP_VERSION } from './config';
Expand All @@ -17,6 +19,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 { 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 @@ -151,7 +154,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 157 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 @@ -322,6 +325,21 @@
}
});

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

const swaggerOptions = {
swaggerDefinition: {
openapi: '3.0.0',
info: {
title: 'Temple Wallet backend',
version: '1.0.0'
}
},
apis: ['./src/index.ts', './src/routers/**/*.ts']
};
const swaggerSpec = swaggerJSDoc(swaggerOptions);
app.use('/docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));

// start the server listening for requests
const port = Boolean(process.env.PORT) ? process.env.PORT : 3000;
app.listen(port, () => console.info(`Server is running on port ${port}...`));
8 changes: 7 additions & 1 deletion src/middlewares/basic-auth.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@ import { Request, Response, NextFunction } from 'express';
import { EnvVars } from '../config';
import { isDefined } from '../utils/helpers';

const credentials = {
username: EnvVars.ADMIN_USERNAME,
password: EnvVars.ADMIN_PASSWORD
};

export const basicAuth = (req: Request, res: Response, next: NextFunction) => {
const base64EncodedCredentials = req.get('Authorization');

if (isDefined(base64EncodedCredentials)) {
const [username, password] = Buffer.from(base64EncodedCredentials.split(' ')[1], 'base64').toString().split(':');
const { username: correctUsername, password: correctPassword } = credentials;

if (!(username === EnvVars.ADD_NOTIFICATION_USERNAME && password === EnvVars.ADD_NOTIFICATION_PASSWORD)) {
if (!(username === correctUsername && password === correctPassword)) {
handleNotAuthenticated(res, next);
}
next();
Expand Down
Loading
Loading