2023-07-27 17:01:15 +02:00
|
|
|
import { doJSONRequest, PeerTubeRequestOptions } from '@server/helpers/requests'
|
|
|
|
import { CONFIG } from '@server/initializers/config'
|
|
|
|
import { ActivityObject, ActivityPubActor, ActivityType, APObjectId } from '@shared/models'
|
|
|
|
import { buildSignedRequestOptions } from './send'
|
2022-07-05 15:43:21 +02:00
|
|
|
|
2023-07-27 17:01:15 +02:00
|
|
|
export function getAPId (object: string | { id: string }) {
|
2022-03-23 14:24:50 +01:00
|
|
|
if (typeof object === 'string') return object
|
|
|
|
|
|
|
|
return object.id
|
|
|
|
}
|
|
|
|
|
2023-07-27 17:01:15 +02:00
|
|
|
export function getActivityStreamDuration (duration: number) {
|
2022-03-24 13:36:47 +01:00
|
|
|
// https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration
|
|
|
|
return 'PT' + duration + 'S'
|
|
|
|
}
|
|
|
|
|
2023-07-27 17:01:15 +02:00
|
|
|
export function getDurationFromActivityStream (duration: string) {
|
2022-03-24 13:36:47 +01:00
|
|
|
return parseInt(duration.replace(/[^\d]+/, ''))
|
|
|
|
}
|
|
|
|
|
2023-07-27 17:01:15 +02:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export function buildAvailableActivities (): ActivityType[] {
|
2022-07-05 15:43:21 +02:00
|
|
|
return [
|
|
|
|
'Create',
|
|
|
|
'Update',
|
|
|
|
'Delete',
|
|
|
|
'Follow',
|
|
|
|
'Accept',
|
|
|
|
'Announce',
|
|
|
|
'Undo',
|
|
|
|
'Like',
|
|
|
|
'Reject',
|
|
|
|
'View',
|
|
|
|
'Dislike',
|
|
|
|
'Flag'
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2023-07-27 17:01:15 +02:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export async function fetchAP <T> (url: string, moreOptions: PeerTubeRequestOptions = {}) {
|
|
|
|
const options = {
|
|
|
|
activityPub: true,
|
|
|
|
|
|
|
|
httpSignature: CONFIG.FEDERATION.SIGN_FEDERATED_FETCHES
|
|
|
|
? await buildSignedRequestOptions({ hasPayload: false })
|
|
|
|
: undefined,
|
|
|
|
|
|
|
|
...moreOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
return doJSONRequest<T>(url, options)
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function fetchAPObjectIfNeeded <T extends (ActivityObject | ActivityPubActor)> (object: APObjectId) {
|
2023-06-05 15:51:16 +02:00
|
|
|
if (typeof object === 'string') {
|
2023-07-27 17:01:15 +02:00
|
|
|
const { body } = await fetchAP<Exclude<T, string>>(object)
|
2023-06-05 15:51:16 +02:00
|
|
|
|
|
|
|
return body
|
|
|
|
}
|
|
|
|
|
|
|
|
return object as Exclude<T, string>
|
|
|
|
}
|
|
|
|
|
2023-07-27 17:01:15 +02:00
|
|
|
export async function findLatestAPRedirection (url: string, iteration = 1) {
|
|
|
|
if (iteration > 10) throw new Error('Too much iterations to find final URL ' + url)
|
|
|
|
|
|
|
|
const { headers } = await fetchAP(url, { followRedirect: false })
|
|
|
|
|
|
|
|
if (headers.location) return findLatestAPRedirection(headers.location, iteration + 1)
|
|
|
|
|
|
|
|
return url
|
2022-03-23 14:24:50 +01:00
|
|
|
}
|