PeerTube/server/lib/activitypub/context.ts

213 lines
4.4 KiB
TypeScript
Raw Normal View History

import { ContextType } from '@shared/models'
import { Hooks } from '../plugins/hooks'
async function activityPubContextify <T> (data: T, type: ContextType) {
return { ...await getContextData(type), ...data }
2022-03-23 16:14:33 +01:00
}
// ---------------------------------------------------------------------------
export {
getContextData,
activityPubContextify
}
// ---------------------------------------------------------------------------
type ContextValue = { [ id: string ]: (string | { '@type': string, '@id': string }) }
const contextStore: { [ id in ContextType ]: (string | { [ id: string ]: string })[] } = {
2022-03-23 16:14:33 +01:00
Video: buildContext({
Hashtag: 'as:Hashtag',
uuid: 'sc:identifier',
category: 'sc:category',
licence: 'sc:license',
subtitleLanguage: 'sc:subtitleLanguage',
sensitive: 'as:sensitive',
language: 'sc:inLanguage',
2022-09-09 11:11:47 +02:00
identifier: 'sc:identifier',
2022-03-23 16:14:33 +01: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 14:51:16 +02:00
tileWidth: {
'@type': 'sc:Number',
'@id': 'pt:tileWidth'
},
tileHeight: {
'@type': 'sc:Number',
'@id': 'pt:tileHeight'
},
tileDuration: {
'@type': 'sc:Number',
'@id': 'pt:tileDuration'
},
2022-03-23 16:14:33 +01:00
originallyPublishedAt: 'sc:datePublished',
2023-07-19 16:02:49 +02:00
uploadDate: 'sc:uploadDate',
2022-03-23 16:14:33 +01: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 16:34:46 +01:00
}
2022-03-23 16:14:33 +01:00
}),
2022-03-23 16:14:33 +01: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'
},
uuid: 'sc:identifier'
2022-03-23 16:14:33 +01:00
}),
CacheFile: buildContext({
expires: 'sc:expires',
CacheFile: 'pt:CacheFile'
}),
2020-02-04 16:34:46 +01:00
2022-03-23 16:14:33 +01:00
Flag: buildContext({
Hashtag: 'as:Hashtag'
}),
Actor: buildContext({
playlists: {
'@id': 'pt:playlists',
'@type': '@id'
2022-09-09 11:33:06 +02:00
},
support: {
'@type': 'sc:Text',
'@id': 'pt:support'
},
// TODO: remove in a few versions, introduced in 4.2
icons: 'as:icon'
2022-03-23 16:14:33 +01:00
}),
2020-02-04 16:34:46 +01:00
WatchAction: buildContext({
WatchAction: 'sc:WatchAction',
startTimestamp: {
'@type': 'sc:Number',
'@id': 'pt:startTimestamp'
},
stopTimestamp: {
'@type': 'sc:Number',
'@id': 'pt:stopTimestamp'
},
watchSection: {
'@type': 'sc:Number',
'@id': 'pt:stopTimestamp'
},
uuid: 'sc:identifier'
}),
Collection: buildContext(),
2022-03-23 16:14:33 +01:00
Follow: buildContext(),
Reject: buildContext(),
Accept: buildContext(),
View: buildContext(),
Announce: buildContext(),
Comment: buildContext(),
Delete: buildContext(),
Rate: buildContext()
}
async function getContextData (type: ContextType) {
const contextData = await Hooks.wrapObject(
contextStore[type],
'filter:activity-pub.activity.context.build.result'
)
return { '@context': contextData }
2020-02-04 16:34:46 +01:00
}
2022-03-23 16:14:33 +01: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 17:51:58 +01:00
2022-03-23 16:14:33 +01:00
if (!contextValue) return baseContext
return [
...baseContext,
{
pt: 'https://joinpeertube.org/ns#',
2022-06-07 08:42:44 +02:00
sc: 'http://schema.org/',
2022-03-23 16:14:33 +01:00
...contextValue
}
]
2017-11-09 17:51:58 +01:00
}