diff options
author | Chocobozzz <me@florianbigard.com> | 2021-03-08 14:24:11 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-03-24 18:18:40 +0100 |
commit | db4b15f21fbf4e33434e930ffc7fb768cdcf9d42 (patch) | |
tree | 418117b84a5c2ee578c210288b698d155422d608 /server/lib/activitypub/actor.ts | |
parent | 71926aae0762facb25243f27eaf45933b5a37353 (diff) | |
download | PeerTube-db4b15f21fbf4e33434e930ffc7fb768cdcf9d42.tar.gz PeerTube-db4b15f21fbf4e33434e930ffc7fb768cdcf9d42.tar.zst PeerTube-db4b15f21fbf4e33434e930ffc7fb768cdcf9d42.zip |
Use got instead of request
Diffstat (limited to 'server/lib/activitypub/actor.ts')
-rw-r--r-- | server/lib/activitypub/actor.ts | 41 |
1 files changed, 14 insertions, 27 deletions
diff --git a/server/lib/activitypub/actor.ts b/server/lib/activitypub/actor.ts index a726f9e20..52b6c1f56 100644 --- a/server/lib/activitypub/actor.ts +++ b/server/lib/activitypub/actor.ts | |||
@@ -1,26 +1,28 @@ | |||
1 | import * as Bluebird from 'bluebird' | 1 | import * as Bluebird from 'bluebird' |
2 | import { extname } from 'path' | ||
2 | import { Op, Transaction } from 'sequelize' | 3 | import { Op, Transaction } from 'sequelize' |
3 | import { URL } from 'url' | 4 | import { URL } from 'url' |
4 | import { v4 as uuidv4 } from 'uuid' | 5 | import { v4 as uuidv4 } from 'uuid' |
6 | import { getServerActor } from '@server/models/application/application' | ||
7 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' | ||
5 | import { ActivityPubActor, ActivityPubActorType, ActivityPubOrderedCollection } from '../../../shared/models/activitypub' | 8 | import { ActivityPubActor, ActivityPubActorType, ActivityPubOrderedCollection } from '../../../shared/models/activitypub' |
6 | import { ActivityPubAttributedTo } from '../../../shared/models/activitypub/objects' | 9 | import { ActivityPubAttributedTo } from '../../../shared/models/activitypub/objects' |
7 | import { checkUrlsSameHost, getAPId } from '../../helpers/activitypub' | 10 | import { checkUrlsSameHost, getAPId } from '../../helpers/activitypub' |
11 | import { ActorFetchByUrlType, fetchActorByUrl } from '../../helpers/actor' | ||
8 | import { sanitizeAndCheckActorObject } from '../../helpers/custom-validators/activitypub/actor' | 12 | import { sanitizeAndCheckActorObject } from '../../helpers/custom-validators/activitypub/actor' |
9 | import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' | 13 | import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' |
10 | import { retryTransactionWrapper, updateInstanceWithAnother } from '../../helpers/database-utils' | 14 | import { retryTransactionWrapper, updateInstanceWithAnother } from '../../helpers/database-utils' |
11 | import { logger } from '../../helpers/logger' | 15 | import { logger } from '../../helpers/logger' |
12 | import { createPrivateAndPublicKeys } from '../../helpers/peertube-crypto' | 16 | import { createPrivateAndPublicKeys } from '../../helpers/peertube-crypto' |
13 | import { doRequest } from '../../helpers/requests' | 17 | import { doJSONRequest } from '../../helpers/requests' |
14 | import { getUrlFromWebfinger } from '../../helpers/webfinger' | 18 | import { getUrlFromWebfinger } from '../../helpers/webfinger' |
15 | import { MIMETYPES, WEBSERVER } from '../../initializers/constants' | 19 | import { MIMETYPES, WEBSERVER } from '../../initializers/constants' |
20 | import { sequelizeTypescript } from '../../initializers/database' | ||
16 | import { AccountModel } from '../../models/account/account' | 21 | import { AccountModel } from '../../models/account/account' |
17 | import { ActorModel } from '../../models/activitypub/actor' | 22 | import { ActorModel } from '../../models/activitypub/actor' |
18 | import { AvatarModel } from '../../models/avatar/avatar' | 23 | import { AvatarModel } from '../../models/avatar/avatar' |
19 | import { ServerModel } from '../../models/server/server' | 24 | import { ServerModel } from '../../models/server/server' |
20 | import { VideoChannelModel } from '../../models/video/video-channel' | 25 | import { VideoChannelModel } from '../../models/video/video-channel' |
21 | import { JobQueue } from '../job-queue' | ||
22 | import { ActorFetchByUrlType, fetchActorByUrl } from '../../helpers/actor' | ||
23 | import { sequelizeTypescript } from '../../initializers/database' | ||
24 | import { | 26 | import { |
25 | MAccount, | 27 | MAccount, |
26 | MAccountDefault, | 28 | MAccountDefault, |
@@ -34,9 +36,7 @@ import { | |||
34 | MActorId, | 36 | MActorId, |
35 | MChannel | 37 | MChannel |
36 | } from '../../types/models' | 38 | } from '../../types/models' |
37 | import { extname } from 'path' | 39 | import { JobQueue } from '../job-queue' |
38 | import { getServerActor } from '@server/models/application/application' | ||
39 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' | ||
40 | 40 | ||
41 | // Set account keys, this could be long so process after the account creation and do not block the client | 41 | // Set account keys, this could be long so process after the account creation and do not block the client |
42 | async function generateAndSaveActorKeys <T extends MActor> (actor: T) { | 42 | async function generateAndSaveActorKeys <T extends MActor> (actor: T) { |
@@ -209,16 +209,10 @@ async function deleteActorAvatarInstance (actor: MActorDefault, t: Transaction) | |||
209 | } | 209 | } |
210 | 210 | ||
211 | async function fetchActorTotalItems (url: string) { | 211 | async function fetchActorTotalItems (url: string) { |
212 | const options = { | ||
213 | uri: url, | ||
214 | method: 'GET', | ||
215 | json: true, | ||
216 | activityPub: true | ||
217 | } | ||
218 | |||
219 | try { | 212 | try { |
220 | const { body } = await doRequest<ActivityPubOrderedCollection<unknown>>(options) | 213 | const { body } = await doJSONRequest<ActivityPubOrderedCollection<unknown>>(url, { activityPub: true }) |
221 | return body.totalItems ? body.totalItems : 0 | 214 | |
215 | return body.totalItems || 0 | ||
222 | } catch (err) { | 216 | } catch (err) { |
223 | logger.warn('Cannot fetch remote actor count %s.', url, { err }) | 217 | logger.warn('Cannot fetch remote actor count %s.', url, { err }) |
224 | return 0 | 218 | return 0 |
@@ -449,26 +443,19 @@ type FetchRemoteActorResult = { | |||
449 | attributedTo: ActivityPubAttributedTo[] | 443 | attributedTo: ActivityPubAttributedTo[] |
450 | } | 444 | } |
451 | async function fetchRemoteActor (actorUrl: string): Promise<{ statusCode?: number, result: FetchRemoteActorResult }> { | 445 | async function fetchRemoteActor (actorUrl: string): Promise<{ statusCode?: number, result: FetchRemoteActorResult }> { |
452 | const options = { | ||
453 | uri: actorUrl, | ||
454 | method: 'GET', | ||
455 | json: true, | ||
456 | activityPub: true | ||
457 | } | ||
458 | |||
459 | logger.info('Fetching remote actor %s.', actorUrl) | 446 | logger.info('Fetching remote actor %s.', actorUrl) |
460 | 447 | ||
461 | const requestResult = await doRequest<ActivityPubActor>(options) | 448 | const requestResult = await doJSONRequest<ActivityPubActor>(actorUrl, { activityPub: true }) |
462 | const actorJSON = requestResult.body | 449 | const actorJSON = requestResult.body |
463 | 450 | ||
464 | if (sanitizeAndCheckActorObject(actorJSON) === false) { | 451 | if (sanitizeAndCheckActorObject(actorJSON) === false) { |
465 | logger.debug('Remote actor JSON is not valid.', { actorJSON }) | 452 | logger.debug('Remote actor JSON is not valid.', { actorJSON }) |
466 | return { result: undefined, statusCode: requestResult.response.statusCode } | 453 | return { result: undefined, statusCode: requestResult.statusCode } |
467 | } | 454 | } |
468 | 455 | ||
469 | if (checkUrlsSameHost(actorJSON.id, actorUrl) !== true) { | 456 | if (checkUrlsSameHost(actorJSON.id, actorUrl) !== true) { |
470 | logger.warn('Actor url %s has not the same host than its AP id %s', actorUrl, actorJSON.id) | 457 | logger.warn('Actor url %s has not the same host than its AP id %s', actorUrl, actorJSON.id) |
471 | return { result: undefined, statusCode: requestResult.response.statusCode } | 458 | return { result: undefined, statusCode: requestResult.statusCode } |
472 | } | 459 | } |
473 | 460 | ||
474 | const followersCount = await fetchActorTotalItems(actorJSON.followers) | 461 | const followersCount = await fetchActorTotalItems(actorJSON.followers) |
@@ -496,7 +483,7 @@ async function fetchRemoteActor (actorUrl: string): Promise<{ statusCode?: numbe | |||
496 | 483 | ||
497 | const name = actorJSON.name || actorJSON.preferredUsername | 484 | const name = actorJSON.name || actorJSON.preferredUsername |
498 | return { | 485 | return { |
499 | statusCode: requestResult.response.statusCode, | 486 | statusCode: requestResult.statusCode, |
500 | result: { | 487 | result: { |
501 | actor, | 488 | actor, |
502 | name, | 489 | name, |