1 import { retryTransactionWrapper } from '@server/helpers/database-utils'
2 import { logger } from '@server/helpers/logger'
3 import { JobQueue } from '@server/lib/job-queue'
4 import { ActorLoadByUrlType, loadActorByUrl } from '@server/lib/model-loaders'
5 import { MActor, MActorAccountChannelId, MActorAccountChannelIdActor, MActorAccountId, MActorFullActor } from '@server/types/models'
6 import { ActivityPubActor } from '@shared/models'
7 import { getAPId } from '../activity'
8 import { checkUrlsSameHost } from '../url'
9 import { refreshActorIfNeeded } from './refresh'
10 import { APActorCreator, fetchRemoteActor } from './shared'
12 function getOrCreateAPActor (
13 activityActor: string | ActivityPubActor,
15 recurseIfNeeded?: boolean,
16 updateCollections?: boolean
17 ): Promise<MActorFullActor>
19 function getOrCreateAPActor (
20 activityActor: string | ActivityPubActor,
21 fetchType?: 'association-ids',
22 recurseIfNeeded?: boolean,
23 updateCollections?: boolean
24 ): Promise<MActorAccountChannelId>
26 async function getOrCreateAPActor (
27 activityActor: string | ActivityPubActor,
28 fetchType: ActorLoadByUrlType = 'association-ids',
29 recurseIfNeeded = true,
30 updateCollections = false
31 ): Promise<MActorFullActor | MActorAccountChannelId> {
32 const actorUrl = getAPId(activityActor)
33 let actor = await loadActorFromDB(actorUrl, fetchType)
36 let accountPlaylistsUrl: string
38 // We don't have this actor in our database, fetch it on remote
40 const { actorObject } = await fetchRemoteActor(actorUrl)
41 if (actorObject === undefined) throw new Error('Cannot fetch remote actor ' + actorUrl)
43 // actorUrl is just an alias/rediraction, so process object id instead
44 if (actorObject.id !== actorUrl) return getOrCreateAPActor(actorObject, 'all', recurseIfNeeded, updateCollections)
46 // Create the attributed to actor
47 // In PeerTube a video channel is owned by an account
48 let ownerActor: MActorFullActor
49 if (recurseIfNeeded === true && actorObject.type === 'Group') {
50 ownerActor = await getOrCreateAPOwner(actorObject, actorUrl)
53 const creator = new APActorCreator(actorObject, ownerActor)
54 actor = await retryTransactionWrapper(creator.create.bind(creator))
56 accountPlaylistsUrl = actorObject.playlists
59 if (actor.Account) (actor as MActorAccountChannelIdActor).Account.Actor = actor
60 if (actor.VideoChannel) (actor as MActorAccountChannelIdActor).VideoChannel.Actor = actor
62 const { actor: actorRefreshed, refreshed } = await refreshActorIfNeeded({ actor, fetchedType: fetchType })
63 if (!actorRefreshed) throw new Error('Actor ' + actor.url + ' does not exist anymore.')
65 await scheduleOutboxFetchIfNeeded(actor, created, refreshed, updateCollections)
66 await schedulePlaylistFetchIfNeeded(actor, created, accountPlaylistsUrl)
71 function getOrCreateAPOwner (actorObject: ActivityPubActor, actorUrl: string) {
72 const accountAttributedTo = actorObject.attributedTo.find(a => a.type === 'Person')
73 if (!accountAttributedTo) throw new Error('Cannot find account attributed to video channel ' + actorUrl)
75 if (checkUrlsSameHost(accountAttributedTo.id, actorUrl) !== true) {
76 throw new Error(`Account attributed to ${accountAttributedTo.id} does not have the same host than actor url ${actorUrl}`)
80 // Don't recurse another time
81 const recurseIfNeeded = false
82 return getOrCreateAPActor(accountAttributedTo.id, 'all', recurseIfNeeded)
84 logger.error('Cannot get or create account attributed to video channel ' + actorUrl)
89 // ---------------------------------------------------------------------------
96 // ---------------------------------------------------------------------------
98 async function loadActorFromDB (actorUrl: string, fetchType: ActorLoadByUrlType) {
99 let actor = await loadActorByUrl(actorUrl, fetchType)
101 // Orphan actor (not associated to an account of channel) so recreate it
102 if (actor && (!actor.Account && !actor.VideoChannel)) {
103 await actor.destroy()
110 async function scheduleOutboxFetchIfNeeded (actor: MActor, created: boolean, refreshed: boolean, updateCollections: boolean) {
111 if ((created === true || refreshed === true) && updateCollections === true) {
112 const payload = { uri: actor.outboxUrl, type: 'activity' as 'activity' }
113 await JobQueue.Instance.createJobWithPromise({ type: 'activitypub-http-fetcher', payload })
117 async function schedulePlaylistFetchIfNeeded (actor: MActorAccountId, created: boolean, accountPlaylistsUrl: string) {
118 // We created a new account: fetch the playlists
119 if (created === true && actor.Account && accountPlaylistsUrl) {
120 const payload = { uri: accountPlaylistsUrl, type: 'account-playlists' as 'account-playlists' }
121 await JobQueue.Instance.createJobWithPromise({ type: 'activitypub-http-fetcher', payload })