import * as uuidv4 from 'uuid/v4'
import { ActivityPubActor, ActivityPubActorType } from '../../../shared/models/activitypub'
import { ActivityPubAttributedTo } from '../../../shared/models/activitypub/objects'
import * as uuidv4 from 'uuid/v4'
import { ActivityPubActor, ActivityPubActorType } from '../../../shared/models/activitypub'
import { ActivityPubAttributedTo } from '../../../shared/models/activitypub/objects'
import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
import { retryTransactionWrapper, updateInstanceWithAnother } from '../../helpers/database-utils'
import { logger } from '../../helpers/logger'
import { createPrivateAndPublicKeys } from '../../helpers/peertube-crypto'
import { doRequest, doRequestAndSaveToFile } from '../../helpers/requests'
import { getUrlFromWebfinger } from '../../helpers/webfinger'
import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
import { retryTransactionWrapper, updateInstanceWithAnother } from '../../helpers/database-utils'
import { logger } from '../../helpers/logger'
import { createPrivateAndPublicKeys } from '../../helpers/peertube-crypto'
import { doRequest, doRequestAndSaveToFile } from '../../helpers/requests'
import { getUrlFromWebfinger } from '../../helpers/webfinger'
import { AccountModel } from '../../models/account/account'
import { ActorModel } from '../../models/activitypub/actor'
import { AvatarModel } from '../../models/avatar/avatar'
import { ServerModel } from '../../models/server/server'
import { VideoChannelModel } from '../../models/video/video-channel'
import { AccountModel } from '../../models/account/account'
import { ActorModel } from '../../models/activitypub/actor'
import { AvatarModel } from '../../models/avatar/avatar'
import { ServerModel } from '../../models/server/server'
import { VideoChannelModel } from '../../models/video/video-channel'
// Set account keys, this could be long so process after the account creation and do not block the client
function setAsyncActorKeys (actor: ActorModel) {
// Set account keys, this could be long so process after the account creation and do not block the client
function setAsyncActorKeys (actor: ActorModel) {
-async function getOrCreateActorAndServerAndModel (actorUrl: string, recurseIfNeeded = true) {
+async function getOrCreateActorAndServerAndModel (activityActor: string | ActivityPubActor, recurseIfNeeded = true) {
+ const actorUrl = getActorUrl(activityActor)
+
let actor = await ActorModel.loadByUrl(actorUrl)
// We don't have this actor in our database, fetch it on remote
let actor = await ActorModel.loadByUrl(actorUrl)
// We don't have this actor in our database, fetch it on remote
+async function addFetchOutboxJob (actor: ActorModel) {
+ // Don't fetch ourselves
+ const serverActor = await getServerActor()
+ if (serverActor.id === actor.id) {
+ logger.error('Cannot fetch our own outbox!')
+ return undefined
+ }
+
+ const payload = {
+ uris: [ actor.outboxUrl ]
+ }
+
+ return JobQueue.Instance.createJob({ type: 'activitypub-http-fetcher', payload })
+}
+
- const account = await saveAccount(actorCreated, result, t)
- actorCreated.Account = account
+ actorCreated.Account = await saveAccount(actorCreated, result, t)
actorCreated.Account.Actor = actorCreated
} else if (actorCreated.type === 'Group') { // Video channel
actorCreated.Account.Actor = actorCreated
} else if (actorCreated.type === 'Group') { // Video channel
- const videoChannel = await saveVideoChannel(actorCreated, result, ownerActor, t)
- actorCreated.VideoChannel = videoChannel
+ actorCreated.VideoChannel = await saveVideoChannel(actorCreated, result, ownerActor, t)
logger.info('Fetching remote actor %s.', actorUrl)
const requestResult = await doRequest(options)
logger.info('Fetching remote actor %s.', actorUrl)
const requestResult = await doRequest(options)
if (isActorObjectValid(actorJSON) === false) {
logger.debug('Remote actor JSON is not valid.', { actorJSON: actorJSON })
if (isActorObjectValid(actorJSON) === false) {
logger.debug('Remote actor JSON is not valid.', { actorJSON: actorJSON })
await actor.save({ transaction: t })
actor.Account.set('name', result.name)
await actor.save({ transaction: t })
actor.Account.set('name', result.name)
await actor.Account.save({ transaction: t })
} else if (actor.VideoChannel) {
await actor.save({ transaction: t })
actor.VideoChannel.set('name', result.name)
await actor.Account.save({ transaction: t })
} else if (actor.VideoChannel) {
await actor.save({ transaction: t })
actor.VideoChannel.set('name', result.name)