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' export function getAPId (object: string | { id: string }) { if (typeof object === 'string') return object return object.id } export function getActivityStreamDuration (duration: number) { // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration return 'PT' + duration + 'S' } export function getDurationFromActivityStream (duration: string) { return parseInt(duration.replace(/[^\d]+/, '')) } // --------------------------------------------------------------------------- export function buildAvailableActivities (): ActivityType[] { return [ 'Create', 'Update', 'Delete', 'Follow', 'Accept', 'Announce', 'Undo', 'Like', 'Reject', 'View', 'Dislike', 'Flag' ] } // --------------------------------------------------------------------------- export async function fetchAP (url: string, moreOptions: PeerTubeRequestOptions = {}) { const options = { activityPub: true, httpSignature: CONFIG.FEDERATION.SIGN_FEDERATED_FETCHES ? await buildSignedRequestOptions({ hasPayload: false }) : undefined, ...moreOptions } return doJSONRequest(url, options) } export async function fetchAPObjectIfNeeded (object: APObjectId) { if (typeof object === 'string') { const { body } = await fetchAP>(object) return body } return object as Exclude } 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 }