Storing avatars in actors avatars table to allow multiple (historical) photos per profile.

This commit is contained in:
DebaucheryLibrarian
2024-10-26 01:04:28 +02:00
parent 8d3f1c13cf
commit e24012f446
4 changed files with 83 additions and 59 deletions

View File

@@ -291,13 +291,13 @@ function curateActorEntries(baseActors, batchId) {
}
function curateProfileEntry(profile) {
if (!profile.id) {
if (!profile.actorId) {
return null;
}
const curatedProfileEntry = {
...(profile.update !== false && { id: profile.update }),
actor_id: profile.id,
actor_id: profile.actorId,
entity_id: profile.entity?.id || null,
date_of_birth: profile.dateOfBirth,
date_of_death: profile.dateOfDeath,
@@ -383,13 +383,15 @@ async function curateProfile(profile, actor) {
try {
const curatedProfile = {
id: profile.id,
// id: profile.id,
update: profile.update,
actorId: profile.actorId,
profileId: profile.profileId,
name: profile.name,
url: profile.url,
avatar: profile.avatar,
scraper: profile.scraper,
entity: profile.entity,
update: profile.update,
};
curatedProfile.description = domPurify.sanitize(profile.description?.replace(/\s+/g, ' '), { ALLOWED_TAGS: [] }).trim() || null;
@@ -550,6 +552,17 @@ async function upsertProfiles(profiles) {
logger.info(`Updated ${updatingProfileEntries.length} new actor profiles`);
}
if (profiles.length > 0) {
await knex('actors_avatars')
.insert(profiles.filter((profile) => !!profile.avatarMediaId).map((profile) => ({
actor_id: profile.actorId,
profile_id: profile.profileId,
media_id: profile.avatarMediaId,
})))
.onConflict()
.ignore();
}
}
async function scrapeProfiles(actor, sources, entitiesBySlug, existingProfilesByActorEntityId) {
@@ -604,6 +617,8 @@ async function scrapeProfiles(actor, sources, entitiesBySlug, existingProfilesBy
...actor,
}), context, include);
console.log('PROFILE', profile);
if (!profile || typeof profile === 'number') { // scraper returns HTTP code on request failure
logger.verbose(`Profile for '${actor.name}' not available on ${label}, scraper returned ${profile}`);
throw Object.assign(new Error(`Profile for '${actor.name}' not available on ${label}`), { code: 'PROFILE_NOT_AVAILABLE' });
@@ -615,6 +630,8 @@ async function scrapeProfiles(actor, sources, entitiesBySlug, existingProfilesBy
...actor,
...profile,
entity,
actorId: actor.id,
profileId: existingProfile?.id,
update: existingProfile?.id || false,
}, actor);
} catch (error) {
@@ -641,7 +658,7 @@ async function scrapeProfiles(actor, sources, entitiesBySlug, existingProfilesBy
}
async function associateSocials(profiles) {
const profileEntries = await knex('actors_profiles').whereIn(['actor_id', 'entity_id'], profiles.map((profile) => [profile.id, profile.entity.id]));
const profileEntries = await knex('actors_profiles').whereIn(['actor_id', 'entity_id'], profiles.map((profile) => [profile.actorId, profile.entity.id]));
const profileEntriesByActorIdAndEntityId = profileEntries.reduce((acc, profileEntry) => {
if (!acc[profileEntry.actor_id]) {
@@ -660,7 +677,7 @@ async function associateSocials(profiles) {
return;
}
const profileId = profileEntriesByActorIdAndEntityId[profile.id]?.[profile.entity.id];
const profileId = profileEntriesByActorIdAndEntityId[profile.actorId]?.[profile.entity.id];
if (!profileId) {
return;
@@ -670,7 +687,7 @@ async function associateSocials(profiles) {
.insert(profile.social.map((url) => ({
url,
platform: new URL(url).hostname.match(/([\w-]+)?\.(\w+)$/)?.[1],
actor_id: profile.id,
actor_id: profile.actorId,
profile_id: profileId,
})))
.onConflict()
@@ -698,8 +715,10 @@ async function getActorNames(actorNames) {
}
async function storeProfiles(profiles) {
console.log('profiles', profiles);
const profilesWithAvatarIds = await associateAvatars(profiles);
const actorIds = Array.from(new Set(profiles.map((profile) => profile.id)));
const actorIds = Array.from(new Set(profiles.map((profile) => profile.actorId)));
await associateSocials(profiles);