diff options
Diffstat (limited to 'server/lib/activitypub/activity.ts')
-rw-r--r-- | server/lib/activitypub/activity.ts | 74 |
1 files changed, 0 insertions, 74 deletions
diff --git a/server/lib/activitypub/activity.ts b/server/lib/activitypub/activity.ts deleted file mode 100644 index 391bcd9c6..000000000 --- a/server/lib/activitypub/activity.ts +++ /dev/null | |||
@@ -1,74 +0,0 @@ | |||
1 | import { doJSONRequest, PeerTubeRequestOptions } from '@server/helpers/requests' | ||
2 | import { CONFIG } from '@server/initializers/config' | ||
3 | import { ActivityObject, ActivityPubActor, ActivityType, APObjectId } from '@shared/models' | ||
4 | import { buildSignedRequestOptions } from './send' | ||
5 | |||
6 | export function getAPId (object: string | { id: string }) { | ||
7 | if (typeof object === 'string') return object | ||
8 | |||
9 | return object.id | ||
10 | } | ||
11 | |||
12 | export function getActivityStreamDuration (duration: number) { | ||
13 | // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration | ||
14 | return 'PT' + duration + 'S' | ||
15 | } | ||
16 | |||
17 | export function getDurationFromActivityStream (duration: string) { | ||
18 | return parseInt(duration.replace(/[^\d]+/, '')) | ||
19 | } | ||
20 | |||
21 | // --------------------------------------------------------------------------- | ||
22 | |||
23 | export function buildAvailableActivities (): ActivityType[] { | ||
24 | return [ | ||
25 | 'Create', | ||
26 | 'Update', | ||
27 | 'Delete', | ||
28 | 'Follow', | ||
29 | 'Accept', | ||
30 | 'Announce', | ||
31 | 'Undo', | ||
32 | 'Like', | ||
33 | 'Reject', | ||
34 | 'View', | ||
35 | 'Dislike', | ||
36 | 'Flag' | ||
37 | ] | ||
38 | } | ||
39 | |||
40 | // --------------------------------------------------------------------------- | ||
41 | |||
42 | export async function fetchAP <T> (url: string, moreOptions: PeerTubeRequestOptions = {}) { | ||
43 | const options = { | ||
44 | activityPub: true, | ||
45 | |||
46 | httpSignature: CONFIG.FEDERATION.SIGN_FEDERATED_FETCHES | ||
47 | ? await buildSignedRequestOptions({ hasPayload: false }) | ||
48 | : undefined, | ||
49 | |||
50 | ...moreOptions | ||
51 | } | ||
52 | |||
53 | return doJSONRequest<T>(url, options) | ||
54 | } | ||
55 | |||
56 | export async function fetchAPObjectIfNeeded <T extends (ActivityObject | ActivityPubActor)> (object: APObjectId) { | ||
57 | if (typeof object === 'string') { | ||
58 | const { body } = await fetchAP<Exclude<T, string>>(object) | ||
59 | |||
60 | return body | ||
61 | } | ||
62 | |||
63 | return object as Exclude<T, string> | ||
64 | } | ||
65 | |||
66 | export async function findLatestAPRedirection (url: string, iteration = 1) { | ||
67 | if (iteration > 10) throw new Error('Too much iterations to find final URL ' + url) | ||
68 | |||
69 | const { headers } = await fetchAP(url, { followRedirect: false }) | ||
70 | |||
71 | if (headers.location) return findLatestAPRedirection(headers.location, iteration + 1) | ||
72 | |||
73 | return url | ||
74 | } | ||