From df820e6e71c7fc650aebaf93234ef381752b9174 Mon Sep 17 00:00:00 2001 From: DebaucheryLibrarian Date: Wed, 30 Oct 2024 00:42:27 +0100 Subject: [PATCH] Fixed new profiles not being assigned avatars appropriately. --- src/actors.js | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/src/actors.js b/src/actors.js index 5aa7196e..fffed8fa 100755 --- a/src/actors.js +++ b/src/actors.js @@ -528,15 +528,23 @@ async function curateProfile(profile, actor) { } } +async function insertProfiles(newProfiles) { + if (newProfiles.length > 0) { + const entries = await bulkInsert('actors_profiles', newProfiles); + + logger.info(`Saved ${newProfiles.length} actor profiles`); + + return entries; + } + + return []; +} + async function upsertProfiles(profiles) { const newProfileEntries = profiles.filter((profile) => !profile.update).map((profile) => curateProfileEntry(profile)).filter(Boolean); const updatingProfileEntries = profiles.filter((profile) => profile.update).map((profile) => curateProfileEntry(profile)).filter(Boolean); - if (newProfileEntries.length > 0) { - await bulkInsert('actors_profiles', newProfileEntries); - - logger.info(`Saved ${newProfileEntries.length} actor profiles`); - } + const newProfiles = await insertProfiles(newProfileEntries); if (argv.force && updatingProfileEntries.length > 0) { const transaction = await knex.transaction(); @@ -554,12 +562,24 @@ async function upsertProfiles(profiles) { } if (profiles.length > 0) { + const newProfileIdMap = newProfiles.reduce((acc, profile) => { + if (!acc[profile.actor_id]) { + acc[profile.actor_id] = {}; + } + + acc[profile.actor_id][profile.entity_id] = profile.id; + + return acc; + }, {}); + + const avatars = profiles.filter((profile) => !!profile.avatarMediaId).map((profile) => ({ + actor_id: profile.actorId, + profile_id: profile.profileId || newProfileIdMap[profile.actorId]?.[profile.entity?.id], + media_id: profile.avatarMediaId, + })); + await knex('actors_avatars') - .insert(profiles.filter((profile) => !!profile.avatarMediaId).map((profile) => ({ - actor_id: profile.actorId, - profile_id: profile.profileId, - media_id: profile.avatarMediaId, - }))) + .insert(avatars) .onConflict() .ignore(); }