diff options
Diffstat (limited to 'server/lib/activitypub/actor.ts')
-rw-r--r-- | server/lib/activitypub/actor.ts | 21 |
1 files changed, 13 insertions, 8 deletions
diff --git a/server/lib/activitypub/actor.ts b/server/lib/activitypub/actor.ts index 3464add03..0bdb7d12e 100644 --- a/server/lib/activitypub/actor.ts +++ b/server/lib/activitypub/actor.ts | |||
@@ -21,6 +21,7 @@ import { ServerModel } from '../../models/server/server' | |||
21 | import { VideoChannelModel } from '../../models/video/video-channel' | 21 | import { VideoChannelModel } from '../../models/video/video-channel' |
22 | import { JobQueue } from '../job-queue' | 22 | import { JobQueue } from '../job-queue' |
23 | import { getServerActor } from '../../helpers/utils' | 23 | import { getServerActor } from '../../helpers/utils' |
24 | import { ActorFetchByUrlType, fetchActorByUrl } from '../../helpers/actor' | ||
24 | 25 | ||
25 | // Set account keys, this could be long so process after the account creation and do not block the client | 26 | // Set account keys, this could be long so process after the account creation and do not block the client |
26 | function setAsyncActorKeys (actor: ActorModel) { | 27 | function setAsyncActorKeys (actor: ActorModel) { |
@@ -38,13 +39,14 @@ function setAsyncActorKeys (actor: ActorModel) { | |||
38 | 39 | ||
39 | async function getOrCreateActorAndServerAndModel ( | 40 | async function getOrCreateActorAndServerAndModel ( |
40 | activityActor: string | ActivityPubActor, | 41 | activityActor: string | ActivityPubActor, |
42 | fetchType: ActorFetchByUrlType = 'actor-and-association-ids', | ||
41 | recurseIfNeeded = true, | 43 | recurseIfNeeded = true, |
42 | updateCollections = false | 44 | updateCollections = false |
43 | ) { | 45 | ) { |
44 | const actorUrl = getActorUrl(activityActor) | 46 | const actorUrl = getActorUrl(activityActor) |
45 | let created = false | 47 | let created = false |
46 | 48 | ||
47 | let actor = await ActorModel.loadByUrl(actorUrl) | 49 | let actor = await fetchActorByUrl(actorUrl, fetchType) |
48 | // Orphan actor (not associated to an account of channel) so recreate it | 50 | // Orphan actor (not associated to an account of channel) so recreate it |
49 | if (actor && (!actor.Account && !actor.VideoChannel)) { | 51 | if (actor && (!actor.Account && !actor.VideoChannel)) { |
50 | await actor.destroy() | 52 | await actor.destroy() |
@@ -65,7 +67,7 @@ async function getOrCreateActorAndServerAndModel ( | |||
65 | 67 | ||
66 | try { | 68 | try { |
67 | // Assert we don't recurse another time | 69 | // Assert we don't recurse another time |
68 | ownerActor = await getOrCreateActorAndServerAndModel(accountAttributedTo.id, false) | 70 | ownerActor = await getOrCreateActorAndServerAndModel(accountAttributedTo.id, 'all', false) |
69 | } catch (err) { | 71 | } catch (err) { |
70 | logger.error('Cannot get or create account attributed to video channel ' + actor.url) | 72 | logger.error('Cannot get or create account attributed to video channel ' + actor.url) |
71 | throw new Error(err) | 73 | throw new Error(err) |
@@ -76,10 +78,7 @@ async function getOrCreateActorAndServerAndModel ( | |||
76 | created = true | 78 | created = true |
77 | } | 79 | } |
78 | 80 | ||
79 | if (actor.Account) actor.Account.Actor = actor | 81 | const { actor: actorRefreshed, refreshed } = await retryTransactionWrapper(refreshActorIfNeeded, actor, fetchType) |
80 | if (actor.VideoChannel) actor.VideoChannel.Actor = actor | ||
81 | |||
82 | const { actor: actorRefreshed, refreshed } = await retryTransactionWrapper(refreshActorIfNeeded, actor) | ||
83 | if (!actorRefreshed) throw new Error('Actor ' + actorRefreshed.url + ' does not exist anymore.') | 82 | if (!actorRefreshed) throw new Error('Actor ' + actorRefreshed.url + ' does not exist anymore.') |
84 | 83 | ||
85 | if ((created === true || refreshed === true) && updateCollections === true) { | 84 | if ((created === true || refreshed === true) && updateCollections === true) { |
@@ -370,8 +369,14 @@ async function saveVideoChannel (actor: ActorModel, result: FetchRemoteActorResu | |||
370 | return videoChannelCreated | 369 | return videoChannelCreated |
371 | } | 370 | } |
372 | 371 | ||
373 | async function refreshActorIfNeeded (actor: ActorModel): Promise<{ actor: ActorModel, refreshed: boolean }> { | 372 | async function refreshActorIfNeeded ( |
374 | if (!actor.isOutdated()) return { actor, refreshed: false } | 373 | actorArg: ActorModel, |
374 | fetchedType: ActorFetchByUrlType | ||
375 | ): Promise<{ actor: ActorModel, refreshed: boolean }> { | ||
376 | if (!actorArg.isOutdated()) return { actor: actorArg, refreshed: false } | ||
377 | |||
378 | // We need more attributes | ||
379 | const actor = fetchedType === 'all' ? actorArg : await ActorModel.loadByUrlAndPopulateAccountAndChannel(actorArg.url) | ||
375 | 380 | ||
376 | try { | 381 | try { |
377 | const actorUrl = await getUrlFromWebfinger(actor.preferredUsername + '@' + actor.getHost()) | 382 | const actorUrl = await getUrlFromWebfinger(actor.preferredUsername + '@' + actor.getHost()) |