From b5e1cd9a3088884e080765e98621faf5ee34d852 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 9 Dec 2021 11:48:29 +0100 Subject: [PATCH] Handle channel owner update of remote server --- server/lib/activitypub/actors/get.ts | 41 +++++++++++----------- server/lib/activitypub/actors/updater.ts | 10 +++++- server/lib/activitypub/send/send-create.ts | 2 +- server/lib/activitypub/send/send-update.ts | 2 +- 4 files changed, 32 insertions(+), 23 deletions(-) diff --git a/server/lib/activitypub/actors/get.ts b/server/lib/activitypub/actors/get.ts index 8681ea02a..4200ddb4d 100644 --- a/server/lib/activitypub/actors/get.ts +++ b/server/lib/activitypub/actors/get.ts @@ -68,26 +68,6 @@ async function getOrCreateAPActor ( return actorRefreshed } -// --------------------------------------------------------------------------- - -export { - getOrCreateAPActor -} - -// --------------------------------------------------------------------------- - -async function loadActorFromDB (actorUrl: string, fetchType: ActorLoadByUrlType) { - let actor = await loadActorByUrl(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) @@ -106,6 +86,27 @@ function getOrCreateAPOwner (actorObject: ActivityPubActor, actorUrl: string) { } } +// --------------------------------------------------------------------------- + +export { + getOrCreateAPOwner, + getOrCreateAPActor +} + +// --------------------------------------------------------------------------- + +async function loadActorFromDB (actorUrl: string, fetchType: ActorLoadByUrlType) { + let actor = await loadActorByUrl(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 +} + 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' } diff --git a/server/lib/activitypub/actors/updater.ts b/server/lib/activitypub/actors/updater.ts index de5e03eee..042438d9c 100644 --- a/server/lib/activitypub/actors/updater.ts +++ b/server/lib/activitypub/actors/updater.ts @@ -1,8 +1,10 @@ import { resetSequelizeInstance, runInReadCommittedTransaction } from '@server/helpers/database-utils' import { logger } from '@server/helpers/logger' +import { AccountModel } from '@server/models/account/account' import { VideoChannelModel } from '@server/models/video/video-channel' import { MAccount, MActor, MActorFull, MChannel } from '@server/types/models' import { ActivityPubActor, ActorImageType } from '@shared/models' +import { getOrCreateAPOwner } from './get' import { updateActorImageInstance } from './image' import { fetchActorFollowsCount } from './shared' import { getImageInfoFromObject } from './shared/object-to-model-attributes' @@ -36,7 +38,13 @@ export class APActorUpdater { 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 + if (this.accountOrChannel instanceof VideoChannelModel) { + const owner = await getOrCreateAPOwner(this.actorObject, this.actorObject.url) + this.accountOrChannel.accountId = owner.Account.id + this.accountOrChannel.Account = owner.Account as AccountModel + + this.accountOrChannel.support = this.actorObject.support + } await runInReadCommittedTransaction(async t => { await updateActorImageInstance(this.actor, ActorImageType.AVATAR, avatarInfo, t) diff --git a/server/lib/activitypub/send/send-create.ts b/server/lib/activitypub/send/send-create.ts index baded642a..f46317163 100644 --- a/server/lib/activitypub/send/send-create.ts +++ b/server/lib/activitypub/send/send-create.ts @@ -116,7 +116,7 @@ function buildCreateActivity (url: string, byActor: MActorLight, object: any, au type: 'Create' as 'Create', id: url + '/activity', actor: byActor.url, - object: audiencify(object, audience) + object }, audience ) diff --git a/server/lib/activitypub/send/send-update.ts b/server/lib/activitypub/send/send-update.ts index bcf6e1569..3c65e19ed 100644 --- a/server/lib/activitypub/send/send-update.ts +++ b/server/lib/activitypub/send/send-update.ts @@ -134,7 +134,7 @@ function buildUpdateActivity (url: string, byActor: MActorLight, object: any, au type: 'Update' as 'Update', id: url, actor: byActor.url, - object: audiencify(object, audience) + object }, audience )