diff options
author | Chocobozzz <me@florianbigard.com> | 2021-06-03 16:02:29 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-06-03 16:40:32 +0200 |
commit | 136d7efde798d3dc0ec0dd18aac674365f7d162e (patch) | |
tree | 3a0e2a7a5d04dedf0d8ffda99c2787cecb838891 /server/lib/activitypub/actors/shared/url-to-object.ts | |
parent | 49af5ac8c2653cb0ef23479c9d3256c5b724d49d (diff) | |
download | PeerTube-136d7efde798d3dc0ec0dd18aac674365f7d162e.tar.gz PeerTube-136d7efde798d3dc0ec0dd18aac674365f7d162e.tar.zst PeerTube-136d7efde798d3dc0ec0dd18aac674365f7d162e.zip |
Refactor AP actors
Diffstat (limited to 'server/lib/activitypub/actors/shared/url-to-object.ts')
-rw-r--r-- | server/lib/activitypub/actors/shared/url-to-object.ts | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/server/lib/activitypub/actors/shared/url-to-object.ts b/server/lib/activitypub/actors/shared/url-to-object.ts new file mode 100644 index 000000000..f4f16b044 --- /dev/null +++ b/server/lib/activitypub/actors/shared/url-to-object.ts | |||
@@ -0,0 +1,54 @@ | |||
1 | |||
2 | import { checkUrlsSameHost } from '@server/helpers/activitypub' | ||
3 | import { sanitizeAndCheckActorObject } from '@server/helpers/custom-validators/activitypub/actor' | ||
4 | import { logger } from '@server/helpers/logger' | ||
5 | import { doJSONRequest } from '@server/helpers/requests' | ||
6 | import { ActivityPubActor, ActivityPubOrderedCollection } from '@shared/models' | ||
7 | |||
8 | async function fetchRemoteActor (actorUrl: string): Promise<{ statusCode: number, actorObject: ActivityPubActor }> { | ||
9 | logger.info('Fetching remote actor %s.', actorUrl) | ||
10 | |||
11 | const { body, statusCode } = await doJSONRequest<ActivityPubActor>(actorUrl, { activityPub: true }) | ||
12 | |||
13 | if (sanitizeAndCheckActorObject(body) === false) { | ||
14 | logger.debug('Remote actor JSON is not valid.', { actorJSON: body }) | ||
15 | return { actorObject: undefined, statusCode: statusCode } | ||
16 | } | ||
17 | |||
18 | if (checkUrlsSameHost(body.id, actorUrl) !== true) { | ||
19 | logger.warn('Actor url %s has not the same host than its AP id %s', actorUrl, body.id) | ||
20 | return { actorObject: undefined, statusCode: statusCode } | ||
21 | } | ||
22 | |||
23 | return { | ||
24 | statusCode, | ||
25 | |||
26 | actorObject: body | ||
27 | } | ||
28 | } | ||
29 | |||
30 | async function fetchActorFollowsCount (actorObject: ActivityPubActor) { | ||
31 | const followersCount = await fetchActorTotalItems(actorObject.followers) | ||
32 | const followingCount = await fetchActorTotalItems(actorObject.following) | ||
33 | |||
34 | return { followersCount, followingCount } | ||
35 | } | ||
36 | |||
37 | // --------------------------------------------------------------------------- | ||
38 | export { | ||
39 | fetchActorFollowsCount, | ||
40 | fetchRemoteActor | ||
41 | } | ||
42 | |||
43 | // --------------------------------------------------------------------------- | ||
44 | |||
45 | async function fetchActorTotalItems (url: string) { | ||
46 | try { | ||
47 | const { body } = await doJSONRequest<ActivityPubOrderedCollection<unknown>>(url, { activityPub: true }) | ||
48 | |||
49 | return body.totalItems || 0 | ||
50 | } catch (err) { | ||
51 | logger.warn('Cannot fetch remote actor count %s.', url, { err }) | ||
52 | return 0 | ||
53 | } | ||
54 | } | ||