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(); }