+ actor = await retryTransactionWrapper(saveActorAndServerAndModelIfNotExist, result, ownerActor)
+ created = true
+ accountPlaylistsUrl = result.playlists
+ }
+
+ if (actor.Account) actor.Account.Actor = actor
+ if (actor.VideoChannel) actor.VideoChannel.Actor = actor
+
+ const { actor: actorRefreshed, refreshed } = await retryTransactionWrapper(refreshActorIfNeeded, actor, fetchType)
+ if (!actorRefreshed) throw new Error('Actor ' + actorRefreshed.url + ' does not exist anymore.')
+
+ if ((created === true || refreshed === true) && updateCollections === true) {
+ const payload = { uri: actor.outboxUrl, type: 'activity' as 'activity' }
+ await JobQueue.Instance.createJob({ type: 'activitypub-http-fetcher', payload })
+ }
+
+ // We created a new account: fetch the playlists
+ if (created === true && actor.Account && accountPlaylistsUrl) {
+ const payload = { uri: accountPlaylistsUrl, accountId: actor.Account.id, type: 'account-playlists' as 'account-playlists' }
+ await JobQueue.Instance.createJob({ type: 'activitypub-http-fetcher', payload })
+ }
+
+ return actorRefreshed
+}
+
+function buildActorInstance (type: ActivityPubActorType, url: string, preferredUsername: string, uuid?: string) {
+ return new ActorModel({
+ type,
+ url,
+ preferredUsername,
+ uuid,
+ publicKey: null,
+ privateKey: null,
+ followersCount: 0,
+ followingCount: 0,
+ inboxUrl: url + '/inbox',
+ outboxUrl: url + '/outbox',
+ sharedInboxUrl: CONFIG.WEBSERVER.URL + '/inbox',
+ followersUrl: url + '/followers',
+ followingUrl: url + '/following'
+ })
+}
+
+async function updateActorInstance (actorInstance: ActorModel, attributes: ActivityPubActor) {
+ const followersCount = await fetchActorTotalItems(attributes.followers)
+ const followingCount = await fetchActorTotalItems(attributes.following)
+
+ actorInstance.set('type', attributes.type)
+ actorInstance.set('uuid', attributes.uuid)
+ actorInstance.set('preferredUsername', attributes.preferredUsername)
+ actorInstance.set('url', attributes.id)
+ actorInstance.set('publicKey', attributes.publicKey.publicKeyPem)
+ actorInstance.set('followersCount', followersCount)
+ actorInstance.set('followingCount', followingCount)
+ actorInstance.set('inboxUrl', attributes.inbox)
+ actorInstance.set('outboxUrl', attributes.outbox)
+ actorInstance.set('sharedInboxUrl', attributes.endpoints.sharedInbox)
+ actorInstance.set('followersUrl', attributes.followers)
+ actorInstance.set('followingUrl', attributes.following)
+}
+
+async function updateActorAvatarInstance (actorInstance: ActorModel, avatarName: string, t: Transaction) {
+ if (avatarName !== undefined) {
+ if (actorInstance.avatarId) {
+ try {
+ await actorInstance.Avatar.destroy({ transaction: t })
+ } catch (err) {
+ logger.error('Cannot remove old avatar of actor %s.', actorInstance.url, { err })
+ }