Showing tag poster and photos on tag page. Improved campaign fallback logic, fixes wrong ratio selected.

This commit is contained in:
2025-12-13 04:00:14 +01:00
parent bc26e07812
commit 84b9bbd1b6
8 changed files with 274 additions and 33 deletions

View File

@@ -33,7 +33,7 @@ function curateCampaign(campaign) {
};
}
function selectRandomCampaign(primaryCampaigns, entityCampaigns, preferredCampaigns, allCampaigns, options) {
function selectRandomCampaign(primaryCampaigns, entityCampaigns, preferredCampaigns) {
if (primaryCampaigns.length > 0) {
return primaryCampaigns[crypto.randomInt(primaryCampaigns.length)];
}
@@ -46,14 +46,10 @@ function selectRandomCampaign(primaryCampaigns, entityCampaigns, preferredCampai
return preferredCampaigns[crypto.randomInt(preferredCampaigns.length)];
}
if (allCampaigns.length > 0 && options.allowRandomFallback !== false) {
return allCampaigns[crypto.randomInt(allCampaigns.length)];
}
return null;
}
export async function getRandomCampaign(options = {}, context = {}) {
export async function getRandomCampaign(options = {}, context = {}, pass = 0) {
const campaigns = options.campaigns
|| await redis.hGetAll('traxxx:campaigns').then((rawCampaigns) => Object.values(rawCampaigns).map((rawCampaign) => JSON.parse(rawCampaign)));
@@ -103,6 +99,18 @@ export async function getRandomCampaign(options = {}, context = {}) {
const randomCampaign = selectRandomCampaign(primaryCampaigns, randomEntityCampaigns, validCampaigns, campaigns, options);
// no campaign found, gradually widen scope
if (!randomCampaign && pass === 0 && options.allowRandomFallback !== false) {
return getRandomCampaign({
minRatio: options.minRatio,
maxRatio: options.maxRatio,
}, context, pass + 1);
}
if (!randomCampaign && pass === 1 && options.allowRandomFallback !== false && options.allowRandomRatio) {
return getRandomCampaign({}, context, pass + 1);
}
return randomCampaign;
}

View File

@@ -1,3 +1,5 @@
import { curateEntity } from './entities.js';
export function curateMedia(media, context = {}) {
if (!media) {
return null;
@@ -21,6 +23,11 @@ export function curateMedia(media, context = {}) {
type: mime[0],
subtype: mime[1],
},
comment: media.comment,
entity: media.entity && curateEntity({
...media.entity,
parent: media.entity_parent,
}),
type: context.type || null,
};
}

View File

@@ -2,7 +2,7 @@ import knex from './knex.js';
import redis from './redis.js';
import initLogger from './logger.js';
import { curateEntity } from './entities.js';
import { curateMedia } from './media.js';
const logger = initLogger();
@@ -13,18 +13,8 @@ function curateTag(tag, context) {
slug: tag.slug,
description: tag.description,
priority: tag.priority,
poster: tag.poster && {
id: tag.poster.id,
path: tag.poster.path,
thumbnail: tag.poster.thumbnail,
lazy: tag.poster.lazy,
isS3: tag.poster.is_s3,
comment: tag.poster.comment,
entity: tag.poster.entity && curateEntity({
...tag.poster.entity,
parent: tag.poster.entity_parent,
}),
},
poster: tag.poster && curateMedia(tag.poster),
photos: tag.photos?.map((photo) => curateMedia(photo)) || [],
alerts: {
only: context?.alerts?.filter((alert) => alert.is_only).flatMap((alert) => alert.alert_ids) || [],
multi: context?.alerts?.filter((alert) => !alert.is_only).flatMap((alert) => alert.alert_ids) || [],
@@ -80,7 +70,7 @@ export async function fetchTags(options = {}) {
}
export async function fetchTagsById(tagIds, options = {}, reqUser) {
const [tags, posters, alerts] = await Promise.all([
const [tags, posters, photos, alerts] = await Promise.all([
knex('tags')
.whereIn('tags.id', tagIds.filter((tagId) => typeof tagId === 'number'))
.orWhereIn('tags.slug', tagIds.filter((tagId) => typeof tagId === 'string'))
@@ -97,6 +87,14 @@ export async function fetchTagsById(tagIds, options = {}, reqUser) {
.leftJoin('entities as parents', 'parents.id', 'entities.parent_id')
.whereIn('tags.id', tagIds.filter((tagId) => typeof tagId === 'number'))
.orWhereIn('tags.slug', tagIds.filter((tagId) => typeof tagId === 'string')),
knex('tags_photos')
.select('tags_photos.tag_id', 'media.*', knex.raw('row_to_json(entities) as entity'), knex.raw('row_to_json(parents) as entity_parent'))
.leftJoin('tags', 'tags.id', 'tags_photos.tag_id')
.leftJoin('media', 'media.id', 'tags_photos.media_id')
.leftJoin('entities', 'entities.id', 'media.entity_id')
.leftJoin('entities as parents', 'parents.id', 'entities.parent_id')
.whereIn('tags.id', tagIds.filter((tagId) => typeof tagId === 'number'))
.orWhereIn('tags.slug', tagIds.filter((tagId) => typeof tagId === 'string')),
reqUser
? knex('alerts_users_tags')
.select('alerts_users_tags.*')
@@ -116,6 +114,7 @@ export async function fetchTagsById(tagIds, options = {}, reqUser) {
return tags.map((tagEntry) => curateTag({
...tagEntry,
poster: postersByTagId[tagEntry.id],
photos: photos.filter((photo) => photo.tag_id === tagEntry.id),
}, {
alerts: alerts.filter((alert) => alert.tag_id === tagEntry.id),
append: options.append,
@@ -133,6 +132,7 @@ export async function fetchTagsById(tagIds, options = {}, reqUser) {
return curateTag({
...tag,
poster: postersByTagId[tag.id],
photos: photos.filter((photo) => photo.tag_id === tag.id),
}, {
alerts: alerts.filter((alert) => alert.tag_id === tag.id),
append: options.append,