PeerTube/server/core/helpers/activity-pub-utils.ts

339 lines
7.8 KiB
TypeScript
Raw Normal View History

import { ContextType } from '@peertube/peertube-models'
import { ACTIVITY_PUB, REMOTE_SCHEME } from '@server/initializers/constants.js'
2024-04-25 04:21:55 -05:00
import { isArray } from './custom-validators/misc.js'
import { buildDigest } from './peertube-crypto.js'
import type { signJsonLDObject } from './peertube-jsonld.js'
import { doJSONRequest } from './requests.js'
2023-10-24 03:45:17 -05:00
export type ContextFilter = <T> (arg: T) => Promise<T>
2023-10-24 03:45:17 -05:00
export function buildGlobalHTTPHeaders (
body: any,
digestBuilder: typeof buildDigest
) {
return {
2023-10-24 03:45:17 -05:00
'digest': digestBuilder(body),
'content-type': 'application/activity+json',
'accept': ACTIVITY_PUB.ACCEPT_HEADER
}
2022-03-23 10:14:33 -05:00
}
export async function activityPubContextify <T> (data: T, type: ContextType, contextFilter: ContextFilter) {
return { ...await getContextData(type, contextFilter), ...data }
}
2022-03-23 10:14:33 -05:00
2023-10-24 03:45:17 -05:00
export async function signAndContextify <T> (options: {
byActor: { url: string, privateKey: string }
data: T
contextType: ContextType | null
contextFilter: ContextFilter
2023-10-24 03:45:17 -05:00
signerFunction: typeof signJsonLDObject<T>
}) {
const { byActor, data, contextType, contextFilter, signerFunction } = options
const activity = contextType
? await activityPubContextify(data, contextType, contextFilter)
: data
2023-10-24 03:45:17 -05:00
return signerFunction({ byActor, data: activity })
2022-03-23 10:14:33 -05:00
}
export async function getApplicationActorOfHost (host: string) {
const url = REMOTE_SCHEME.HTTP + '://' + host + '/.well-known/nodeinfo'
const { body } = await doJSONRequest<{ links: { rel: string, href: string }[] }>(url)
if (!isArray(body.links)) return undefined
const found = body.links.find(l => l.rel === 'https://www.w3.org/ns/activitystreams#Application')
return found?.href || undefined
}
2024-04-25 04:21:55 -05:00
export function getAPPublicValue () {
return 'https://www.w3.org/ns/activitystreams#Public'
}
export function hasAPPublic (toOrCC: string[]) {
if (!isArray(toOrCC)) return false
const publicValue = getAPPublicValue()
return toOrCC.some(f => f === 'as:Public' || publicValue)
}
// ---------------------------------------------------------------------------
// Private
2022-03-23 10:14:33 -05:00
// ---------------------------------------------------------------------------
type ContextValue = { [ id: string ]: (string | { '@type': string, '@id': string }) }
const contextStore: { [ id in ContextType ]: (string | { [ id: string ]: string })[] } = {
2022-03-23 10:14:33 -05:00
Video: buildContext({
Hashtag: 'as:Hashtag',
category: 'sc:category',
licence: 'sc:license',
subtitleLanguage: 'sc:subtitleLanguage',
sensitive: 'as:sensitive',
language: 'sc:inLanguage',
2022-09-09 04:11:47 -05:00
identifier: 'sc:identifier',
2022-03-23 10:14:33 -05:00
isLiveBroadcast: 'sc:isLiveBroadcast',
liveSaveReplay: {
'@type': 'sc:Boolean',
'@id': 'pt:liveSaveReplay'
},
permanentLive: {
'@type': 'sc:Boolean',
'@id': 'pt:permanentLive'
},
latencyMode: {
'@type': 'sc:Number',
'@id': 'pt:latencyMode'
},
Infohash: 'pt:Infohash',
2023-06-01 07:51:16 -05:00
tileWidth: {
'@type': 'sc:Number',
'@id': 'pt:tileWidth'
},
tileHeight: {
'@type': 'sc:Number',
'@id': 'pt:tileHeight'
},
tileDuration: {
'@type': 'sc:Number',
'@id': 'pt:tileDuration'
},
2024-02-27 04:18:56 -06:00
aspectRatio: {
'@type': 'sc:Float',
'@id': 'pt:aspectRatio'
},
2023-06-01 07:51:16 -05:00
2024-04-25 04:21:55 -05:00
uuid: {
'@type': 'sc:identifier',
'@id': 'pt:uuid'
},
2022-03-23 10:14:33 -05:00
originallyPublishedAt: 'sc:datePublished',
2023-07-19 09:02:49 -05:00
uploadDate: 'sc:uploadDate',
2023-08-28 03:55:04 -05:00
hasParts: 'sc:hasParts',
2022-03-23 10:14:33 -05:00
views: {
'@type': 'sc:Number',
'@id': 'pt:views'
},
state: {
'@type': 'sc:Number',
'@id': 'pt:state'
},
size: {
'@type': 'sc:Number',
'@id': 'pt:size'
},
fps: {
'@type': 'sc:Number',
'@id': 'pt:fps'
},
commentsEnabled: {
'@type': 'sc:Boolean',
'@id': 'pt:commentsEnabled'
},
downloadEnabled: {
'@type': 'sc:Boolean',
'@id': 'pt:downloadEnabled'
},
waitTranscoding: {
'@type': 'sc:Boolean',
'@id': 'pt:waitTranscoding'
},
support: {
'@type': 'sc:Text',
'@id': 'pt:support'
},
likes: {
'@id': 'as:likes',
'@type': '@id'
},
dislikes: {
'@id': 'as:dislikes',
'@type': '@id'
},
shares: {
'@id': 'as:shares',
'@type': '@id'
},
comments: {
'@id': 'as:comments',
'@type': '@id'
2020-02-04 09:34:46 -06:00
}
2022-03-23 10:14:33 -05:00
}),
2022-03-23 10:14:33 -05:00
Playlist: buildContext({
Playlist: 'pt:Playlist',
PlaylistElement: 'pt:PlaylistElement',
position: {
'@type': 'sc:Number',
'@id': 'pt:position'
},
startTimestamp: {
'@type': 'sc:Number',
'@id': 'pt:startTimestamp'
},
stopTimestamp: {
'@type': 'sc:Number',
'@id': 'pt:stopTimestamp'
},
2024-04-25 04:21:55 -05:00
uuid: {
'@type': 'sc:identifier',
'@id': 'pt:uuid'
}
2022-03-23 10:14:33 -05:00
}),
CacheFile: buildContext({
expires: 'sc:expires',
2024-04-25 04:21:55 -05:00
CacheFile: 'pt:CacheFile',
size: {
'@type': 'sc:Number',
'@id': 'pt:size'
},
fps: {
'@type': 'sc:Number',
'@id': 'pt:fps'
}
2022-03-23 10:14:33 -05:00
}),
2020-02-04 09:34:46 -06:00
2022-03-23 10:14:33 -05:00
Flag: buildContext({
Hashtag: 'as:Hashtag'
}),
Actor: buildContext({
playlists: {
'@id': 'pt:playlists',
'@type': '@id'
2022-09-09 04:33:06 -05:00
},
support: {
'@type': 'sc:Text',
'@id': 'pt:support'
},
lemmy: 'https://join-lemmy.org/ns#',
postingRestrictedToMods: 'lemmy:postingRestrictedToMods',
2022-09-09 04:33:06 -05:00
// TODO: remove in a few versions, introduced in 4.2
icons: 'as:icon'
2022-03-23 10:14:33 -05:00
}),
2020-02-04 09:34:46 -06:00
WatchAction: buildContext({
WatchAction: 'sc:WatchAction',
startTimestamp: {
'@type': 'sc:Number',
'@id': 'pt:startTimestamp'
},
2024-04-25 04:21:55 -05:00
endTimestamp: {
'@type': 'sc:Number',
2024-04-25 04:21:55 -05:00
'@id': 'pt:endTimestamp'
},
2024-04-25 04:21:55 -05:00
uuid: {
'@type': 'sc:identifier',
'@id': 'pt:uuid'
},
2024-04-25 04:21:55 -05:00
actionStatus: 'sc:actionStatus',
watchSections: {
'@type': '@id',
'@id': 'pt:watchSections'
},
addressRegion: 'sc:addressRegion',
addressCountry: 'sc:addressCountry'
}),
View: buildContext({
WatchAction: 'sc:WatchAction',
InteractionCounter: 'sc:InteractionCounter',
interactionType: 'sc:interactionType',
userInteractionCount: 'sc:userInteractionCount'
}),
Collection: buildContext(),
2022-03-23 10:14:33 -05:00
Follow: buildContext(),
Reject: buildContext(),
Accept: buildContext(),
Announce: buildContext(),
Comment: buildContext(),
Delete: buildContext(),
2023-08-28 03:55:04 -05:00
Rate: buildContext(),
Chapters: buildContext({
hasPart: 'sc:hasPart',
endOffset: 'sc:endOffset',
startOffset: 'sc:startOffset'
})
2022-03-23 10:14:33 -05:00
}
2024-04-25 04:21:55 -05:00
let allContext: (string | ContextValue)[]
export function getAllContext () {
if (allContext) return allContext
const processed = new Set<string>()
allContext = []
let staticContext: ContextValue = {}
for (const v of Object.values(contextStore)) {
for (const item of v) {
if (typeof item === 'string') {
if (!processed.has(item)) {
allContext.push(item)
}
processed.add(item)
} else {
for (const subKey of Object.keys(item)) {
if (!processed.has(subKey)) {
staticContext = { ...staticContext, [subKey]: item[subKey] }
}
processed.add(subKey)
}
}
}
}
allContext = [ ...allContext, staticContext ]
return allContext
}
async function getContextData (type: ContextType, contextFilter: ContextFilter) {
const contextData = contextFilter
? await contextFilter(contextStore[type])
: contextStore[type]
return { '@context': contextData }
2020-02-04 09:34:46 -06:00
}
2022-03-23 10:14:33 -05:00
function buildContext (contextValue?: ContextValue) {
const baseContext = [
'https://www.w3.org/ns/activitystreams',
'https://w3id.org/security/v1',
{
RsaSignature2017: 'https://w3id.org/security#RsaSignature2017'
}
]
2017-11-09 10:51:58 -06:00
2022-03-23 10:14:33 -05:00
if (!contextValue) return baseContext
return [
...baseContext,
{
pt: 'https://joinpeertube.org/ns#',
2022-06-07 01:42:44 -05:00
sc: 'http://schema.org/',
2022-03-23 10:14:33 -05:00
...contextValue
}
]
2017-11-09 10:51:58 -06:00
}