1 import { resetSequelizeInstance, runInReadCommittedTransaction } from '@server/helpers/database-utils'
2 import { logger } from '@server/helpers/logger'
3 import { VideoChannelModel } from '@server/models/video/video-channel'
4 import { MAccount, MActor, MActorFull, MChannel } from '@server/types/models'
5 import { ActivityPubActor, ActorImageType } from '@shared/models'
6 import { updateActorImageInstance } from './image'
7 import { fetchActorFollowsCount } from './shared'
8 import { getImageInfoFromObject } from './shared/object-to-model-attributes'
10 export class APActorUpdater {
12 private accountOrChannel: MAccount | MChannel
14 private readonly actorFieldsSave: object
15 private readonly accountOrChannelFieldsSave: object
18 private readonly actorObject: ActivityPubActor,
19 private readonly actor: MActorFull
21 this.actorFieldsSave = this.actor.toJSON()
23 if (this.actorObject.type === 'Group') this.accountOrChannel = this.actor.VideoChannel
24 else this.accountOrChannel = this.actor.Account
26 this.accountOrChannelFieldsSave = this.accountOrChannel.toJSON()
30 const avatarInfo = getImageInfoFromObject(this.actorObject, ActorImageType.AVATAR)
31 const bannerInfo = getImageInfoFromObject(this.actorObject, ActorImageType.BANNER)
34 await this.updateActorInstance(this.actor, this.actorObject)
36 this.accountOrChannel.name = this.actorObject.name || this.actorObject.preferredUsername
37 this.accountOrChannel.description = this.actorObject.summary
39 if (this.accountOrChannel instanceof VideoChannelModel) this.accountOrChannel.support = this.actorObject.support
41 await runInReadCommittedTransaction(async t => {
42 await updateActorImageInstance(this.actor, ActorImageType.AVATAR, avatarInfo, t)
43 await updateActorImageInstance(this.actor, ActorImageType.BANNER, bannerInfo, t)
46 await runInReadCommittedTransaction(async t => {
47 await this.actor.save({ transaction: t })
48 await this.accountOrChannel.save({ transaction: t })
51 logger.info('Remote account %s updated', this.actorObject.url)
53 if (this.actor !== undefined && this.actorFieldsSave !== undefined) {
54 resetSequelizeInstance(this.actor, this.actorFieldsSave)
57 if (this.accountOrChannel !== undefined && this.accountOrChannelFieldsSave !== undefined) {
58 resetSequelizeInstance(this.accountOrChannel, this.accountOrChannelFieldsSave)
61 // This is just a debug because we will retry the insert
62 logger.debug('Cannot update the remote account.', { err })
67 private async updateActorInstance (actorInstance: MActor, actorObject: ActivityPubActor) {
68 const { followersCount, followingCount } = await fetchActorFollowsCount(actorObject)
70 actorInstance.type = actorObject.type
71 actorInstance.preferredUsername = actorObject.preferredUsername
72 actorInstance.url = actorObject.id
73 actorInstance.publicKey = actorObject.publicKey.publicKeyPem
74 actorInstance.followersCount = followersCount
75 actorInstance.followingCount = followingCount
76 actorInstance.inboxUrl = actorObject.inbox
77 actorInstance.outboxUrl = actorObject.outbox
78 actorInstance.followersUrl = actorObject.followers
79 actorInstance.followingUrl = actorObject.following
81 if (actorObject.published) actorInstance.remoteCreatedAt = new Date(actorObject.published)
83 if (actorObject.endpoints?.sharedInbox) {
84 actorInstance.sharedInboxUrl = actorObject.endpoints.sharedInbox
88 actorInstance.changed('updatedAt', true)