Removed info channels from overview. Fixed poster gap.
This commit is contained in:
@@ -15,6 +15,7 @@ function curateCampaign(campaign) {
|
||||
return {
|
||||
id: campaign.id,
|
||||
url: campaign.url,
|
||||
entity: campaign.entity && curateEntity({ ...campaign.entity, parent: campaign.parent_entity }),
|
||||
banner: campaign.banner && {
|
||||
id: campaign.banner.id,
|
||||
type: campaign.banner.type,
|
||||
@@ -37,8 +38,8 @@ function curateCampaign(campaign) {
|
||||
}
|
||||
|
||||
export async function getRandomCampaign(options = {}) {
|
||||
const rawCampaigns = await redis.hGetAll('traxxx:campaigns');
|
||||
const campaigns = Object.values(rawCampaigns).map((rawCampaign) => JSON.parse(rawCampaign));
|
||||
const campaigns = options.campaigns
|
||||
|| await redis.hGetAll('traxxx:campaigns').then((rawCampaigns) => Object.values(rawCampaigns).map((rawCampaign) => JSON.parse(rawCampaign)));
|
||||
|
||||
const validCampaigns = campaigns.filter((campaign) => {
|
||||
if (options.minRatio && (!campaign.banner || campaign.banner.ratio < options.minRatio)) {
|
||||
@@ -49,12 +50,34 @@ export async function getRandomCampaign(options = {}) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (options.entityIds && !options.entityIds.some((entityId) => campaign.entity.id === entityId || campaign.entity.parent?.id === entityId)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
|
||||
const randomCampaign = validCampaigns[crypto.randomInt(validCampaigns.length)];
|
||||
const primaryCampaigns = validCampaigns.filter((campaign) => campaign.entity.id === options.entityIds?.[0]);
|
||||
|
||||
return randomCampaign;
|
||||
if (validCampaigns.length > 0) {
|
||||
const randomCampaign = primaryCampaigns.length > 0
|
||||
? primaryCampaigns[crypto.randomInt(primaryCampaigns.length)]
|
||||
: validCampaigns[crypto.randomInt(validCampaigns.length)];
|
||||
|
||||
return randomCampaign;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
export async function getRandomCampaigns(allOptions = []) {
|
||||
const rawCampaigns = await redis.hGetAll('traxxx:campaigns');
|
||||
const campaigns = Object.values(rawCampaigns).map((rawCampaign) => JSON.parse(rawCampaign));
|
||||
|
||||
return Promise.all(allOptions.map(async (options) => getRandomCampaign({
|
||||
...options,
|
||||
campaigns,
|
||||
})));
|
||||
}
|
||||
|
||||
export async function cacheCampaigns() {
|
||||
@@ -64,17 +87,29 @@ export async function cacheCampaigns() {
|
||||
'campaigns.*',
|
||||
knex.raw('row_to_json(affiliates) as affiliate'),
|
||||
knex.raw('row_to_json(banners) as banner'),
|
||||
knex.raw('row_to_json(entities) as banner_entity'),
|
||||
knex.raw('row_to_json(parents) as banner_parent_entity'),
|
||||
knex.raw('row_to_json(entities) as entity'),
|
||||
knex.raw('row_to_json(parents) as parent_entity'),
|
||||
knex.raw('row_to_json(banner_entities) as banner_entity'),
|
||||
knex.raw('row_to_json(banner_parents) as banner_parent_entity'),
|
||||
knex.raw('json_agg(tags.slug) filter (where tags.id is not null) as banner_tags'),
|
||||
)
|
||||
.leftJoin('affiliates', 'affiliates.id', 'campaigns.affiliate_id')
|
||||
.leftJoin('banners', 'banners.id', 'campaigns.banner_id')
|
||||
.leftJoin('banners_tags', 'banners_tags.banner_id', 'banners.id')
|
||||
.leftJoin('tags', 'tags.id', 'banners_tags.tag_id')
|
||||
.leftJoin('entities', 'entities.id', 'banners.entity_id')
|
||||
.leftJoin('entities', 'entities.id', 'campaigns.entity_id')
|
||||
.leftJoin('entities as parents', 'parents.id', 'entities.parent_id')
|
||||
.groupBy('campaigns.id', 'affiliates.id', 'banners.id', 'entities.id', 'parents.id');
|
||||
.leftJoin('entities as banner_entities', 'banner_entities.id', 'banners.entity_id')
|
||||
.leftJoin('entities as banner_parents', 'banner_parents.id', 'banner_entities.parent_id')
|
||||
.groupBy(
|
||||
'campaigns.id',
|
||||
'affiliates.id',
|
||||
'entities.id',
|
||||
'parents.id',
|
||||
'banners.id',
|
||||
'banner_entities.id',
|
||||
'banner_parents.id',
|
||||
);
|
||||
|
||||
await redis.del('traxxx:campaigns');
|
||||
|
||||
|
||||
Reference in New Issue
Block a user