From b5e1cd9a3088884e080765e98621faf5ee34d852 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 9 Dec 2021 11:48:29 +0100 Subject: Handle channel owner update of remote server --- server/lib/activitypub/actors/get.ts | 37 +++++++++++++++--------------- 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, 30 insertions(+), 21 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,9 +68,28 @@ async function getOrCreateAPActor ( return actorRefreshed } +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) + } +} + // --------------------------------------------------------------------------- export { + getOrCreateAPOwner, getOrCreateAPActor } @@ -88,24 +107,6 @@ async function loadActorFromDB (actorUrl: string, fetchType: ActorLoadByUrlType) 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' } 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 ) -- cgit v1.2.3