]>
Commit | Line | Data |
---|---|---|
28dfb44b | 1 | import { resetSequelizeInstance, runInReadCommittedTransaction } from '@server/helpers/database-utils' |
136d7efd | 2 | import { logger } from '@server/helpers/logger' |
b5e1cd9a | 3 | import { AccountModel } from '@server/models/account/account' |
136d7efd C |
4 | import { VideoChannelModel } from '@server/models/video/video-channel' |
5 | import { MAccount, MActor, MActorFull, MChannel } from '@server/types/models' | |
6 | import { ActivityPubActor, ActorImageType } from '@shared/models' | |
b5e1cd9a | 7 | import { getOrCreateAPOwner } from './get' |
d0800f76 | 8 | import { updateActorImages } from './image' |
136d7efd | 9 | import { fetchActorFollowsCount } from './shared' |
d0800f76 | 10 | import { getImagesInfoFromObject } from './shared/object-to-model-attributes' |
136d7efd C |
11 | |
12 | export class APActorUpdater { | |
13 | ||
e65ef81c | 14 | private readonly accountOrChannel: MAccount | MChannel |
136d7efd | 15 | |
136d7efd C |
16 | constructor ( |
17 | private readonly actorObject: ActivityPubActor, | |
18 | private readonly actor: MActorFull | |
19 | ) { | |
136d7efd C |
20 | if (this.actorObject.type === 'Group') this.accountOrChannel = this.actor.VideoChannel |
21 | else this.accountOrChannel = this.actor.Account | |
136d7efd C |
22 | } |
23 | ||
24 | async update () { | |
d0800f76 | 25 | const avatarsInfo = getImagesInfoFromObject(this.actorObject, ActorImageType.AVATAR) |
26 | const bannersInfo = getImagesInfoFromObject(this.actorObject, ActorImageType.BANNER) | |
136d7efd C |
27 | |
28 | try { | |
28dfb44b | 29 | await this.updateActorInstance(this.actor, this.actorObject) |
136d7efd | 30 | |
28dfb44b C |
31 | this.accountOrChannel.name = this.actorObject.name || this.actorObject.preferredUsername |
32 | this.accountOrChannel.description = this.actorObject.summary | |
136d7efd | 33 | |
b5e1cd9a C |
34 | if (this.accountOrChannel instanceof VideoChannelModel) { |
35 | const owner = await getOrCreateAPOwner(this.actorObject, this.actorObject.url) | |
36 | this.accountOrChannel.accountId = owner.Account.id | |
37 | this.accountOrChannel.Account = owner.Account as AccountModel | |
38 | ||
39 | this.accountOrChannel.support = this.actorObject.support | |
40 | } | |
136d7efd | 41 | |
28dfb44b | 42 | await runInReadCommittedTransaction(async t => { |
d0800f76 | 43 | await updateActorImages(this.actor, ActorImageType.BANNER, bannersInfo, t) |
44 | await updateActorImages(this.actor, ActorImageType.AVATAR, avatarsInfo, t) | |
136d7efd C |
45 | }) |
46 | ||
28dfb44b | 47 | await runInReadCommittedTransaction(async t => { |
bbfdeb39 C |
48 | await this.actor.save({ transaction: t }) |
49 | await this.accountOrChannel.save({ transaction: t }) | |
28dfb44b C |
50 | }) |
51 | ||
136d7efd C |
52 | logger.info('Remote account %s updated', this.actorObject.url) |
53 | } catch (err) { | |
45657746 | 54 | if (this.actor !== undefined) { |
823c34c0 | 55 | await resetSequelizeInstance(this.actor) |
136d7efd C |
56 | } |
57 | ||
45657746 | 58 | if (this.accountOrChannel !== undefined) { |
823c34c0 | 59 | await resetSequelizeInstance(this.accountOrChannel) |
136d7efd C |
60 | } |
61 | ||
62 | // This is just a debug because we will retry the insert | |
63 | logger.debug('Cannot update the remote account.', { err }) | |
64 | throw err | |
65 | } | |
66 | } | |
67 | ||
68 | private async updateActorInstance (actorInstance: MActor, actorObject: ActivityPubActor) { | |
69 | const { followersCount, followingCount } = await fetchActorFollowsCount(actorObject) | |
70 | ||
71 | actorInstance.type = actorObject.type | |
72 | actorInstance.preferredUsername = actorObject.preferredUsername | |
73 | actorInstance.url = actorObject.id | |
74 | actorInstance.publicKey = actorObject.publicKey.publicKeyPem | |
75 | actorInstance.followersCount = followersCount | |
76 | actorInstance.followingCount = followingCount | |
77 | actorInstance.inboxUrl = actorObject.inbox | |
78 | actorInstance.outboxUrl = actorObject.outbox | |
79 | actorInstance.followersUrl = actorObject.followers | |
80 | actorInstance.followingUrl = actorObject.following | |
81 | ||
82 | if (actorObject.published) actorInstance.remoteCreatedAt = new Date(actorObject.published) | |
83 | ||
84 | if (actorObject.endpoints?.sharedInbox) { | |
85 | actorInstance.sharedInboxUrl = actorObject.endpoints.sharedInbox | |
86 | } | |
87 | ||
88 | // Force actor update | |
89 | actorInstance.changed('updatedAt', true) | |
90 | } | |
91 | } |