-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TW-1192 Implement a backend for ads replacement management
- Loading branch information
1 parent
1fa0f20
commit 51fc09b
Showing
8 changed files
with
816 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { redisClient } from '../redis'; | ||
import { isDefined } from '../utils/helpers'; | ||
|
||
export interface SliseAdContainerRule { | ||
urlRegexes: string[]; | ||
selector: { | ||
isMultiple: boolean; | ||
cssString: string; | ||
shouldUseResultParent: boolean; | ||
}; | ||
} | ||
|
||
const SLISE_AD_CONTAINERS_RULES_KEY = 'slise_ad_containers_rules'; | ||
const SLISE_HEURISTIC_URL_REGEXES_KEY = 'slise_heuristic_url_regexes_key'; | ||
const SLISE_HEURISTIC_SELECTORS_KEY = 'slise_heuristic_selectors_key'; | ||
|
||
export const getSliseAdContainerRulesByDomain = async (domain: string) => { | ||
const rule = await redisClient.hget(SLISE_AD_CONTAINERS_RULES_KEY, domain); | ||
|
||
return isDefined(rule) | ||
? (JSON.parse(rule) as SliseAdContainerRule[]) | ||
: [ | ||
{ | ||
urlRegexes: [], | ||
selector: { | ||
isMultiple: false, | ||
cssString: '*', | ||
shouldUseResultParent: false | ||
} | ||
} | ||
]; | ||
}; | ||
|
||
export const getAllSliseAdContainerRules = async () => { | ||
const rules = await redisClient.hgetall(SLISE_AD_CONTAINERS_RULES_KEY); | ||
|
||
const parsedRules: { [domain: string]: SliseAdContainerRule[] } = {}; | ||
for (const domainName in rules) { | ||
parsedRules[domainName] = JSON.parse(rules[domainName]); | ||
} | ||
|
||
return parsedRules; | ||
}; | ||
|
||
export const upsertSliseAdContainerRules = async (rules: Record<string, SliseAdContainerRule[]>) => { | ||
// Failed to set the arrays as values in Redis, so stringifying them | ||
await redisClient.hmset( | ||
SLISE_AD_CONTAINERS_RULES_KEY, | ||
Object.fromEntries(Object.entries(rules).map(([domain, rules]) => [domain, JSON.stringify(rules)])) | ||
); | ||
}; | ||
|
||
export const removeSliseAdContainerRules = async (domains: string[]) => | ||
redisClient.hdel(SLISE_AD_CONTAINERS_RULES_KEY, ...domains); | ||
|
||
export const getSliseHeuristicUrlRegexes = async () => redisClient.smembers(SLISE_HEURISTIC_URL_REGEXES_KEY); | ||
|
||
export const addSliseHeuristicUrlRegexes = async (regexes: string[]) => | ||
redisClient.sadd(SLISE_HEURISTIC_URL_REGEXES_KEY, ...regexes); | ||
|
||
export const removeSliseHeuristicUrlRegexes = async (regexes: string[]) => | ||
redisClient.srem(SLISE_HEURISTIC_URL_REGEXES_KEY, ...regexes); | ||
|
||
export const getSliseHeuristicSelectors = async () => redisClient.smembers(SLISE_HEURISTIC_SELECTORS_KEY); | ||
|
||
export const addSliseHeuristicSelectors = async (selectors: string[]) => | ||
redisClient.sadd(SLISE_HEURISTIC_SELECTORS_KEY, ...selectors); | ||
|
||
export const removeSliseHeuristicSelectors = async (selectors: string[]) => | ||
redisClient.srem(SLISE_HEURISTIC_SELECTORS_KEY, ...selectors); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.