import * as uuidv4 from 'uuid/v4'
import { ActivityPubActor, ActivityPubActorType } from '../../../shared/models/activitypub'
import { ActivityPubAttributedTo } from '../../../shared/models/activitypub/objects'
-import { getActorUrl } from '../../helpers/activitypub'
+import { checkUrlsSameHost, getAPUrl } from '../../helpers/activitypub'
import { isActorObjectValid, normalizeActor } from '../../helpers/custom-validators/activitypub/actor'
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 { doRequest, downloadImage } from '../../helpers/requests'
import { getUrlFromWebfinger } from '../../helpers/webfinger'
-import { CONFIG, IMAGE_MIMETYPE_EXT, sequelizeTypescript } from '../../initializers'
+import { AVATARS_SIZE, CONFIG, IMAGE_MIMETYPE_EXT, sequelizeTypescript } from '../../initializers'
import { AccountModel } from '../../models/account/account'
import { ActorModel } from '../../models/activitypub/actor'
import { AvatarModel } from '../../models/avatar/avatar'
recurseIfNeeded = true,
updateCollections = false
) {
- const actorUrl = getActorUrl(activityActor)
+ const actorUrl = getAPUrl(activityActor)
let created = false
let actor = await fetchActorByUrl(actorUrl, fetchType)
// We don't have this actor in our database, fetch it on remote
if (!actor) {
const { result } = await fetchRemoteActor(actorUrl)
- if (result === undefined) throw new Error('Cannot fetch remote actor.')
+ if (result === undefined) throw new Error('Cannot fetch remote actor ' + actorUrl)
// Create the attributed to actor
// In PeerTube a video channel is owned by an account
const accountAttributedTo = result.attributedTo.find(a => a.type === 'Person')
if (!accountAttributedTo) throw new Error('Cannot find account attributed to video channel ' + actor.url)
+ 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 {
- // Assert we don't recurse another time
+ // Don't recurse another time
ownerActor = await getOrCreateActorAndServerAndModel(accountAttributedTo.id, 'all', false)
} catch (err) {
logger.error('Cannot get or create account attributed to video channel ' + actor.url)
const avatarName = uuidv4() + extension
const destPath = join(CONFIG.STORAGE.AVATARS_DIR, avatarName)
- await doRequestAndSaveToFile({
- method: 'GET',
- uri: actorJSON.icon.url
- }, destPath)
+ await downloadImage(actorJSON.icon.url, destPath, AVATARS_SIZE)
return avatarName
}
normalizeActor(requestResult.body)
const actorJSON: ActivityPubActor = requestResult.body
-
if (isActorObjectValid(actorJSON) === false) {
logger.debug('Remote actor JSON is not valid.', { actorJSON: actorJSON })
return { result: undefined, statusCode: requestResult.response.statusCode }
}
+ if (checkUrlsSameHost(actorJSON.id, actorUrl) !== true) {
+ throw new Error('Actor url ' + actorUrl + ' has not the same host than its AP id ' + actorJSON.id)
+ }
+
const followersCount = await fetchActorTotalItems(actorJSON.followers)
const followingCount = await fetchActorTotalItems(actorJSON.following)