From 136d7efde798d3dc0ec0dd18aac674365f7d162e Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 3 Jun 2021 16:02:29 +0200 Subject: Refactor AP actors --- server/lib/activitypub/actors/get.ts | 119 ++++++++++++++++ server/lib/activitypub/actors/image.ts | 94 +++++++++++++ server/lib/activitypub/actors/index.ts | 5 + server/lib/activitypub/actors/keys.ts | 16 +++ server/lib/activitypub/actors/refresh.ts | 63 +++++++++ server/lib/activitypub/actors/shared/creator.ts | 149 +++++++++++++++++++++ server/lib/activitypub/actors/shared/index.ts | 3 + .../actors/shared/object-to-model-attributes.ts | 70 ++++++++++ .../lib/activitypub/actors/shared/url-to-object.ts | 54 ++++++++ server/lib/activitypub/actors/updater.ts | 90 +++++++++++++ 10 files changed, 663 insertions(+) create mode 100644 server/lib/activitypub/actors/get.ts create mode 100644 server/lib/activitypub/actors/image.ts create mode 100644 server/lib/activitypub/actors/index.ts create mode 100644 server/lib/activitypub/actors/keys.ts create mode 100644 server/lib/activitypub/actors/refresh.ts create mode 100644 server/lib/activitypub/actors/shared/creator.ts create mode 100644 server/lib/activitypub/actors/shared/index.ts create mode 100644 server/lib/activitypub/actors/shared/object-to-model-attributes.ts create mode 100644 server/lib/activitypub/actors/shared/url-to-object.ts create mode 100644 server/lib/activitypub/actors/updater.ts (limited to 'server/lib/activitypub/actors') diff --git a/server/lib/activitypub/actors/get.ts b/server/lib/activitypub/actors/get.ts new file mode 100644 index 000000000..0d5bea789 --- /dev/null +++ b/server/lib/activitypub/actors/get.ts @@ -0,0 +1,119 @@ + +import { checkUrlsSameHost, getAPId } from '@server/helpers/activitypub' +import { ActorFetchByUrlType, fetchActorByUrl } from '@server/helpers/actor' +import { retryTransactionWrapper } from '@server/helpers/database-utils' +import { logger } from '@server/helpers/logger' +import { JobQueue } from '@server/lib/job-queue' +import { MActor, MActorAccountChannelId, MActorAccountChannelIdActor, MActorAccountId, MActorFullActor } from '@server/types/models' +import { ActivityPubActor } from '@shared/models' +import { refreshActorIfNeeded } from './refresh' +import { APActorCreator, fetchRemoteActor } from './shared' + +function getOrCreateAPActor ( + activityActor: string | ActivityPubActor, + fetchType: 'all', + recurseIfNeeded?: boolean, + updateCollections?: boolean +): Promise + +function getOrCreateAPActor ( + activityActor: string | ActivityPubActor, + fetchType?: 'association-ids', + recurseIfNeeded?: boolean, + updateCollections?: boolean +): Promise + +async function getOrCreateAPActor ( + activityActor: string | ActivityPubActor, + fetchType: ActorFetchByUrlType = 'association-ids', + recurseIfNeeded = true, + updateCollections = false +): Promise { + const actorUrl = getAPId(activityActor) + let actor = await loadActorFromDB(actorUrl, fetchType) + + let created = false + let accountPlaylistsUrl: string + + // We don't have this actor in our database, fetch it on remote + if (!actor) { + const { actorObject } = await fetchRemoteActor(actorUrl) + if (actorObject === undefined) throw new Error('Cannot fetch remote actor ' + actorUrl) + + // Create the attributed to actor + // In PeerTube a video channel is owned by an account + let ownerActor: MActorFullActor + if (recurseIfNeeded === true && actorObject.type === 'Group') { + ownerActor = await getOrCreateAPOwner(actorObject, actorUrl) + } + + const creator = new APActorCreator(actorObject, ownerActor) + actor = await retryTransactionWrapper(creator.create.bind(creator)) + created = true + accountPlaylistsUrl = actorObject.playlists + } + + if (actor.Account) (actor as MActorAccountChannelIdActor).Account.Actor = actor + if (actor.VideoChannel) (actor as MActorAccountChannelIdActor).VideoChannel.Actor = actor + + const { actor: actorRefreshed, refreshed } = await retryTransactionWrapper(refreshActorIfNeeded, actor, fetchType) + if (!actorRefreshed) throw new Error('Actor ' + actor.url + ' does not exist anymore.') + + await scheduleOutboxFetchIfNeeded(actor, created, refreshed, updateCollections) + await schedulePlaylistFetchIfNeeded(actor, created, accountPlaylistsUrl) + + return actorRefreshed +} + +// --------------------------------------------------------------------------- + +export { + getOrCreateAPActor +} + +// --------------------------------------------------------------------------- + +async function loadActorFromDB (actorUrl: string, fetchType: ActorFetchByUrlType) { + let actor = await fetchActorByUrl(actorUrl, fetchType) + + // Orphan actor (not associated to an account of channel) so recreate it + if (actor && (!actor.Account && !actor.VideoChannel)) { + await actor.destroy() + actor = null + } + + return actor +} + +function getOrCreateAPOwner (actorObject: ActivityPubActor, actorUrl: string) { + const accountAttributedTo = actorObject.attributedTo.find(a => a.type === 'Person') + if (!accountAttributedTo) throw new Error('Cannot find account attributed to video channel ' + actorUrl) + + if (checkUrlsSameHost(accountAttributedTo.id, actorUrl) !== true) { + throw new Error(`Account attributed to ${accountAttributedTo.id} does not have the same host than actor url ${actorUrl}`) + } + + try { + // Don't recurse another time + const recurseIfNeeded = false + return getOrCreateAPActor(accountAttributedTo.id, 'all', recurseIfNeeded) + } catch (err) { + logger.error('Cannot get or create account attributed to video channel ' + actorUrl) + throw new Error(err) + } +} + +async function scheduleOutboxFetchIfNeeded (actor: MActor, created: boolean, refreshed: boolean, updateCollections: boolean) { + if ((created === true || refreshed === true) && updateCollections === true) { + const payload = { uri: actor.outboxUrl, type: 'activity' as 'activity' } + await JobQueue.Instance.createJobWithPromise({ type: 'activitypub-http-fetcher', payload }) + } +} + +async function schedulePlaylistFetchIfNeeded (actor: MActorAccountId, created: boolean, accountPlaylistsUrl: string) { + // 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.createJobWithPromise({ type: 'activitypub-http-fetcher', payload }) + } +} diff --git a/server/lib/activitypub/actors/image.ts b/server/lib/activitypub/actors/image.ts new file mode 100644 index 000000000..443ad0a63 --- /dev/null +++ b/server/lib/activitypub/actors/image.ts @@ -0,0 +1,94 @@ +import { Transaction } from 'sequelize/types' +import { logger } from '@server/helpers/logger' +import { ActorImageModel } from '@server/models/actor/actor-image' +import { MActorImage, MActorImages } from '@server/types/models' +import { ActorImageType } from '@shared/models' + +type ImageInfo = { + name: string + fileUrl: string + height: number + width: number + onDisk?: boolean +} + +async function updateActorImageInstance (actor: MActorImages, type: ActorImageType, imageInfo: ImageInfo | null, t: Transaction) { + const oldImageModel = type === ActorImageType.AVATAR + ? actor.Avatar + : actor.Banner + + if (oldImageModel) { + // Don't update the avatar if the file URL did not change + if (imageInfo?.fileUrl && oldImageModel.fileUrl === imageInfo.fileUrl) return actor + + try { + await oldImageModel.destroy({ transaction: t }) + + setActorImage(actor, type, null) + } catch (err) { + logger.error('Cannot remove old actor image of actor %s.', actor.url, { err }) + } + } + + if (imageInfo) { + const imageModel = await ActorImageModel.create({ + filename: imageInfo.name, + onDisk: imageInfo.onDisk ?? false, + fileUrl: imageInfo.fileUrl, + height: imageInfo.height, + width: imageInfo.width, + type + }, { transaction: t }) + + setActorImage(actor, type, imageModel) + } + + return actor +} + +async function deleteActorImageInstance (actor: MActorImages, type: ActorImageType, t: Transaction) { + try { + if (type === ActorImageType.AVATAR) { + await actor.Avatar.destroy({ transaction: t }) + + actor.avatarId = null + actor.Avatar = null + } else { + await actor.Banner.destroy({ transaction: t }) + + actor.bannerId = null + actor.Banner = null + } + } catch (err) { + logger.error('Cannot remove old image of actor %s.', actor.url, { err }) + } + + return actor +} + +// --------------------------------------------------------------------------- + +export { + ImageInfo, + + updateActorImageInstance, + deleteActorImageInstance +} + +// --------------------------------------------------------------------------- + +function setActorImage (actorModel: MActorImages, type: ActorImageType, imageModel: MActorImage) { + const id = imageModel + ? imageModel.id + : null + + if (type === ActorImageType.AVATAR) { + actorModel.avatarId = id + actorModel.Avatar = imageModel + } else { + actorModel.bannerId = id + actorModel.Banner = imageModel + } + + return actorModel +} diff --git a/server/lib/activitypub/actors/index.ts b/server/lib/activitypub/actors/index.ts new file mode 100644 index 000000000..a54da6798 --- /dev/null +++ b/server/lib/activitypub/actors/index.ts @@ -0,0 +1,5 @@ +export * from './get' +export * from './image' +export * from './keys' +export * from './refresh' +export * from './updater' diff --git a/server/lib/activitypub/actors/keys.ts b/server/lib/activitypub/actors/keys.ts new file mode 100644 index 000000000..c3d18abd8 --- /dev/null +++ b/server/lib/activitypub/actors/keys.ts @@ -0,0 +1,16 @@ +import { createPrivateAndPublicKeys } from '@server/helpers/peertube-crypto' +import { MActor } from '@server/types/models' + +// Set account keys, this could be long so process after the account creation and do not block the client +async function generateAndSaveActorKeys (actor: T) { + const { publicKey, privateKey } = await createPrivateAndPublicKeys() + + actor.publicKey = publicKey + actor.privateKey = privateKey + + return actor.save() +} + +export { + generateAndSaveActorKeys +} diff --git a/server/lib/activitypub/actors/refresh.ts b/server/lib/activitypub/actors/refresh.ts new file mode 100644 index 000000000..ff3b249d0 --- /dev/null +++ b/server/lib/activitypub/actors/refresh.ts @@ -0,0 +1,63 @@ +import { ActorFetchByUrlType } from '@server/helpers/actor' +import { logger } from '@server/helpers/logger' +import { PeerTubeRequestError } from '@server/helpers/requests' +import { getUrlFromWebfinger } from '@server/helpers/webfinger' +import { ActorModel } from '@server/models/actor/actor' +import { MActorAccountChannelId, MActorFull } from '@server/types/models' +import { HttpStatusCode } from '@shared/core-utils' +import { fetchRemoteActor } from './shared' +import { APActorUpdater } from './updater' + +async function refreshActorIfNeeded ( + actorArg: T, + fetchedType: ActorFetchByUrlType +): Promise<{ actor: T | MActorFull, refreshed: boolean }> { + if (!actorArg.isOutdated()) return { actor: actorArg, refreshed: false } + + // We need more attributes + const actor = fetchedType === 'all' + ? actorArg as MActorFull + : await ActorModel.loadByUrlAndPopulateAccountAndChannel(actorArg.url) + + try { + const actorUrl = await getActorUrl(actor) + const { actorObject } = await fetchRemoteActor(actorUrl) + + if (actorObject === undefined) { + logger.warn('Cannot fetch remote actor in refresh actor.') + return { actor, refreshed: false } + } + + const updater = new APActorUpdater(actorObject, actor) + await updater.update() + + return { refreshed: true, actor } + } catch (err) { + if ((err as PeerTubeRequestError).statusCode === HttpStatusCode.NOT_FOUND_404) { + logger.info('Deleting actor %s because there is a 404 in refresh actor.', actor.url) + + actor.Account + ? await actor.Account.destroy() + : await actor.VideoChannel.destroy() + + return { actor: undefined, refreshed: false } + } + + logger.warn('Cannot refresh actor %s.', actor.url, { err }) + return { actor, refreshed: false } + } +} + +export { + refreshActorIfNeeded +} + +// --------------------------------------------------------------------------- + +function getActorUrl (actor: MActorFull) { + return getUrlFromWebfinger(actor.preferredUsername + '@' + actor.getHost()) + .catch(err => { + logger.warn('Cannot get actor URL from webfinger, keeping the old one.', err) + return actor.url + }) +} diff --git a/server/lib/activitypub/actors/shared/creator.ts b/server/lib/activitypub/actors/shared/creator.ts new file mode 100644 index 000000000..999aed97d --- /dev/null +++ b/server/lib/activitypub/actors/shared/creator.ts @@ -0,0 +1,149 @@ +import { Op, Transaction } from 'sequelize' +import { sequelizeTypescript } from '@server/initializers/database' +import { AccountModel } from '@server/models/account/account' +import { ActorModel } from '@server/models/actor/actor' +import { ServerModel } from '@server/models/server/server' +import { VideoChannelModel } from '@server/models/video/video-channel' +import { MAccount, MAccountDefault, MActor, MActorFullActor, MActorId, MActorImages, MChannel, MServer } from '@server/types/models' +import { ActivityPubActor, ActorImageType } from '@shared/models' +import { updateActorImageInstance } from '../image' +import { getActorAttributesFromObject, getActorDisplayNameFromObject, getImageInfoFromObject } from './object-to-model-attributes' +import { fetchActorFollowsCount } from './url-to-object' + +export class APActorCreator { + + constructor ( + private readonly actorObject: ActivityPubActor, + private readonly ownerActor?: MActorFullActor + ) { + + } + + async create (): Promise { + const { followersCount, followingCount } = await fetchActorFollowsCount(this.actorObject) + + const actorInstance = new ActorModel(getActorAttributesFromObject(this.actorObject, followersCount, followingCount)) + + return sequelizeTypescript.transaction(async t => { + const server = await this.setServer(actorInstance, t) + + await this.setImageIfNeeded(actorInstance, ActorImageType.AVATAR, t) + await this.setImageIfNeeded(actorInstance, ActorImageType.BANNER, t) + + const { actorCreated, created } = await this.saveActor(actorInstance, t) + + await this.tryToFixActorUrlIfNeeded(actorCreated, actorInstance, created, t) + + if (actorCreated.type === 'Person' || actorCreated.type === 'Application') { // Account or PeerTube instance + actorCreated.Account = await this.saveAccount(actorCreated, t) as MAccountDefault + actorCreated.Account.Actor = actorCreated + } + + if (actorCreated.type === 'Group') { // Video channel + const channel = await this.saveVideoChannel(actorCreated, t) + actorCreated.VideoChannel = Object.assign(channel, { Actor: actorCreated, Account: this.ownerActor.Account }) + } + + actorCreated.Server = server + + return actorCreated + }) + } + + private async setServer (actor: MActor, t: Transaction) { + const actorHost = new URL(actor.url).host + + const serverOptions = { + where: { + host: actorHost + }, + defaults: { + host: actorHost + }, + transaction: t + } + const [ server ] = await ServerModel.findOrCreate(serverOptions) + + // Save our new account in database + actor.serverId = server.id + + return server as MServer + } + + private async setImageIfNeeded (actor: MActor, type: ActorImageType, t: Transaction) { + const imageInfo = getImageInfoFromObject(this.actorObject, type) + if (!imageInfo) return + + return updateActorImageInstance(actor as MActorImages, type, imageInfo, t) + } + + private async saveActor (actor: MActor, t: Transaction) { + // Force the actor creation using findOrCreate() instead of save() + // Sometimes Sequelize skips the save() when it thinks the instance already exists + // (which could be false in a retried query) + const [ actorCreated, created ] = await ActorModel.findOrCreate({ + defaults: actor.toJSON(), + where: { + [Op.or]: [ + { + url: actor.url + }, + { + serverId: actor.serverId, + preferredUsername: actor.preferredUsername + } + ] + }, + transaction: t + }) + + return { actorCreated, created } + } + + private async tryToFixActorUrlIfNeeded (actorCreated: MActor, newActor: MActor, created: boolean, t: Transaction) { + // Try to fix non HTTPS accounts of remote instances that fixed their URL afterwards + if (created !== true && actorCreated.url !== newActor.url) { + // Only fix http://example.com/account/djidane to https://example.com/account/djidane + if (actorCreated.url.replace(/^http:\/\//, '') !== newActor.url.replace(/^https:\/\//, '')) { + throw new Error(`Actor from DB with URL ${actorCreated.url} does not correspond to actor ${newActor.url}`) + } + + actorCreated.url = newActor.url + await actorCreated.save({ transaction: t }) + } + } + + private async saveAccount (actor: MActorId, t: Transaction) { + const [ accountCreated ] = await AccountModel.findOrCreate({ + defaults: { + name: getActorDisplayNameFromObject(this.actorObject), + description: this.actorObject.summary, + actorId: actor.id + }, + where: { + actorId: actor.id + }, + transaction: t + }) + + return accountCreated as MAccount + } + + private async saveVideoChannel (actor: MActorId, t: Transaction) { + const [ videoChannelCreated ] = await VideoChannelModel.findOrCreate({ + defaults: { + name: getActorDisplayNameFromObject(this.actorObject), + description: this.actorObject.summary, + support: this.actorObject.support, + actorId: actor.id, + accountId: this.ownerActor.Account.id + }, + where: { + actorId: actor.id + }, + transaction: t + }) + + return videoChannelCreated as MChannel + } +} diff --git a/server/lib/activitypub/actors/shared/index.ts b/server/lib/activitypub/actors/shared/index.ts new file mode 100644 index 000000000..a2ff468cf --- /dev/null +++ b/server/lib/activitypub/actors/shared/index.ts @@ -0,0 +1,3 @@ +export * from './creator' +export * from './url-to-object' +export * from './object-to-model-attributes' diff --git a/server/lib/activitypub/actors/shared/object-to-model-attributes.ts b/server/lib/activitypub/actors/shared/object-to-model-attributes.ts new file mode 100644 index 000000000..66b22c952 --- /dev/null +++ b/server/lib/activitypub/actors/shared/object-to-model-attributes.ts @@ -0,0 +1,70 @@ +import { extname } from 'path' +import { v4 as uuidv4 } from 'uuid' +import { isActivityPubUrlValid } from '@server/helpers/custom-validators/activitypub/misc' +import { MIMETYPES } from '@server/initializers/constants' +import { ActorModel } from '@server/models/actor/actor' +import { FilteredModelAttributes } from '@server/types' +import { ActivityPubActor, ActorImageType } from '@shared/models' + +function getActorAttributesFromObject ( + actorObject: ActivityPubActor, + followersCount: number, + followingCount: number +): FilteredModelAttributes { + return { + type: actorObject.type, + preferredUsername: actorObject.preferredUsername, + url: actorObject.id, + publicKey: actorObject.publicKey.publicKeyPem, + privateKey: null, + followersCount, + followingCount, + inboxUrl: actorObject.inbox, + outboxUrl: actorObject.outbox, + followersUrl: actorObject.followers, + followingUrl: actorObject.following, + + sharedInboxUrl: actorObject.endpoints?.sharedInbox + ? actorObject.endpoints.sharedInbox + : null + } +} + +function getImageInfoFromObject (actorObject: ActivityPubActor, type: ActorImageType) { + const mimetypes = MIMETYPES.IMAGE + const icon = type === ActorImageType.AVATAR + ? actorObject.icon + : actorObject.image + + if (!icon || icon.type !== 'Image' || !isActivityPubUrlValid(icon.url)) return undefined + + let extension: string + + if (icon.mediaType) { + extension = mimetypes.MIMETYPE_EXT[icon.mediaType] + } else { + const tmp = extname(icon.url) + + if (mimetypes.EXT_MIMETYPE[tmp] !== undefined) extension = tmp + } + + if (!extension) return undefined + + return { + name: uuidv4() + extension, + fileUrl: icon.url, + height: icon.height, + width: icon.width, + type + } +} + +function getActorDisplayNameFromObject (actorObject: ActivityPubActor) { + return actorObject.name || actorObject.preferredUsername +} + +export { + getActorAttributesFromObject, + getImageInfoFromObject, + getActorDisplayNameFromObject +} diff --git a/server/lib/activitypub/actors/shared/url-to-object.ts b/server/lib/activitypub/actors/shared/url-to-object.ts new file mode 100644 index 000000000..f4f16b044 --- /dev/null +++ b/server/lib/activitypub/actors/shared/url-to-object.ts @@ -0,0 +1,54 @@ + +import { checkUrlsSameHost } from '@server/helpers/activitypub' +import { sanitizeAndCheckActorObject } from '@server/helpers/custom-validators/activitypub/actor' +import { logger } from '@server/helpers/logger' +import { doJSONRequest } from '@server/helpers/requests' +import { ActivityPubActor, ActivityPubOrderedCollection } from '@shared/models' + +async function fetchRemoteActor (actorUrl: string): Promise<{ statusCode: number, actorObject: ActivityPubActor }> { + logger.info('Fetching remote actor %s.', actorUrl) + + const { body, statusCode } = await doJSONRequest(actorUrl, { activityPub: true }) + + if (sanitizeAndCheckActorObject(body) === false) { + logger.debug('Remote actor JSON is not valid.', { actorJSON: body }) + return { actorObject: undefined, statusCode: statusCode } + } + + if (checkUrlsSameHost(body.id, actorUrl) !== true) { + logger.warn('Actor url %s has not the same host than its AP id %s', actorUrl, body.id) + return { actorObject: undefined, statusCode: statusCode } + } + + return { + statusCode, + + actorObject: body + } +} + +async function fetchActorFollowsCount (actorObject: ActivityPubActor) { + const followersCount = await fetchActorTotalItems(actorObject.followers) + const followingCount = await fetchActorTotalItems(actorObject.following) + + return { followersCount, followingCount } +} + +// --------------------------------------------------------------------------- +export { + fetchActorFollowsCount, + fetchRemoteActor +} + +// --------------------------------------------------------------------------- + +async function fetchActorTotalItems (url: string) { + try { + const { body } = await doJSONRequest>(url, { activityPub: true }) + + return body.totalItems || 0 + } catch (err) { + logger.warn('Cannot fetch remote actor count %s.', url, { err }) + return 0 + } +} diff --git a/server/lib/activitypub/actors/updater.ts b/server/lib/activitypub/actors/updater.ts new file mode 100644 index 000000000..471688f11 --- /dev/null +++ b/server/lib/activitypub/actors/updater.ts @@ -0,0 +1,90 @@ +import { resetSequelizeInstance } from '@server/helpers/database-utils' +import { logger } from '@server/helpers/logger' +import { sequelizeTypescript } from '@server/initializers/database' +import { VideoChannelModel } from '@server/models/video/video-channel' +import { MAccount, MActor, MActorFull, MChannel } from '@server/types/models' +import { ActivityPubActor, ActorImageType } from '@shared/models' +import { updateActorImageInstance } from './image' +import { fetchActorFollowsCount } from './shared' +import { getImageInfoFromObject } from './shared/object-to-model-attributes' + +export class APActorUpdater { + + private accountOrChannel: MAccount | MChannel + + private readonly actorFieldsSave: object + private readonly accountOrChannelFieldsSave: object + + constructor ( + private readonly actorObject: ActivityPubActor, + private readonly actor: MActorFull + ) { + this.actorFieldsSave = this.actor.toJSON() + + if (this.actorObject.type === 'Group') this.accountOrChannel = this.actor.VideoChannel + else this.accountOrChannel = this.actor.Account + + this.accountOrChannelFieldsSave = this.accountOrChannel.toJSON() + } + + async update () { + const avatarInfo = getImageInfoFromObject(this.actorObject, ActorImageType.AVATAR) + const bannerInfo = getImageInfoFromObject(this.actorObject, ActorImageType.BANNER) + + try { + await sequelizeTypescript.transaction(async t => { + await this.updateActorInstance(this.actor, this.actorObject) + + await updateActorImageInstance(this.actor, ActorImageType.AVATAR, avatarInfo, t) + await updateActorImageInstance(this.actor, ActorImageType.BANNER, bannerInfo, t) + + await this.actor.save({ transaction: t }) + + this.accountOrChannel.name = this.actorObject.name || this.actorObject.preferredUsername + this.accountOrChannel.description = this.actorObject.summary + + if (this.accountOrChannel instanceof VideoChannelModel) this.accountOrChannel.support = this.actorObject.support + + await this.accountOrChannel.save({ transaction: t }) + }) + + logger.info('Remote account %s updated', this.actorObject.url) + } catch (err) { + if (this.actor !== undefined && this.actorFieldsSave !== undefined) { + resetSequelizeInstance(this.actor, this.actorFieldsSave) + } + + if (this.accountOrChannel !== undefined && this.accountOrChannelFieldsSave !== undefined) { + resetSequelizeInstance(this.accountOrChannel, this.accountOrChannelFieldsSave) + } + + // This is just a debug because we will retry the insert + logger.debug('Cannot update the remote account.', { err }) + throw err + } + } + + private async updateActorInstance (actorInstance: MActor, actorObject: ActivityPubActor) { + const { followersCount, followingCount } = await fetchActorFollowsCount(actorObject) + + actorInstance.type = actorObject.type + actorInstance.preferredUsername = actorObject.preferredUsername + actorInstance.url = actorObject.id + actorInstance.publicKey = actorObject.publicKey.publicKeyPem + actorInstance.followersCount = followersCount + actorInstance.followingCount = followingCount + actorInstance.inboxUrl = actorObject.inbox + actorInstance.outboxUrl = actorObject.outbox + actorInstance.followersUrl = actorObject.followers + actorInstance.followingUrl = actorObject.following + + if (actorObject.published) actorInstance.remoteCreatedAt = new Date(actorObject.published) + + if (actorObject.endpoints?.sharedInbox) { + actorInstance.sharedInboxUrl = actorObject.endpoints.sharedInbox + } + + // Force actor update + actorInstance.changed('updatedAt', true) + } +} -- cgit v1.2.3