1 import * as Bluebird from 'bluebird'
2 import { URL } from 'url'
3 import validator from 'validator'
4 import { ContextType } from '@shared/models/activitypub/context'
5 import { ResultList } from '../../shared/models'
6 import { ACTIVITY_PUB, REMOTE_SCHEME } from '../initializers/constants'
7 import { MActor, MVideoWithHost } from '../types/models'
8 import { pageToStartAndCount } from './core-utils'
9 import { signJsonLDObject } from './peertube-crypto'
11 function getContextData (type: ContextType) {
12 const context: any[] = [
13 'https://www.w3.org/ns/activitystreams',
14 'https://w3id.org/security/v1',
16 RsaSignature2017: 'https://w3id.org/security#RsaSignature2017'
20 if (type !== 'View' && type !== 'Announce') {
22 pt: 'https://joinpeertube.org/ns#',
23 sc: 'http://schema.org#'
26 if (type === 'CacheFile') {
27 Object.assign(additional, {
28 expires: 'sc:expires',
29 CacheFile: 'pt:CacheFile'
32 Object.assign(additional, {
33 Hashtag: 'as:Hashtag',
34 uuid: 'sc:identifier',
35 category: 'sc:category',
36 licence: 'sc:license',
37 subtitleLanguage: 'sc:subtitleLanguage',
38 sensitive: 'as:sensitive',
39 language: 'sc:inLanguage',
41 isLiveBroadcast: 'sc:isLiveBroadcast',
43 '@type': 'sc:Boolean',
44 '@id': 'pt:liveSaveReplay'
47 '@type': 'sc:Boolean',
48 '@id': 'pt:permanentLive'
51 Infohash: 'pt:Infohash',
52 Playlist: 'pt:Playlist',
53 PlaylistElement: 'pt:PlaylistElement',
55 originallyPublishedAt: 'sc:datePublished',
74 '@id': 'pt:startTimestamp'
78 '@id': 'pt:stopTimestamp'
85 '@type': 'sc:Boolean',
86 '@id': 'pt:commentsEnabled'
89 '@type': 'sc:Boolean',
90 '@id': 'pt:downloadEnabled'
93 '@type': 'sc:Boolean',
94 '@id': 'pt:waitTranscoding'
105 '@id': 'as:dislikes',
109 '@id': 'pt:playlists',
117 '@id': 'as:comments',
123 context.push(additional)
131 function activityPubContextify <T> (data: T, type: ContextType = 'All') {
132 return Object.assign({}, data, getContextData(type))
135 type ActivityPubCollectionPaginationHandler = (start: number, count: number) => Bluebird<ResultList<any>> | Promise<ResultList<any>>
136 async function activityPubCollectionPagination (
138 handler: ActivityPubCollectionPaginationHandler,
140 size = ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE
142 if (!page || !validator.isInt(page)) {
143 // We just display the first page URL, we only need the total items
144 const result = await handler(0, 1)
148 type: 'OrderedCollectionPage',
149 totalItems: result.total,
150 first: baseUrl + '?page=1'
154 const { start, count } = pageToStartAndCount(page, size)
155 const result = await handler(start, count)
157 let next: string | undefined
158 let prev: string | undefined
160 // Assert page is a number
161 page = parseInt(page, 10)
163 // There are more results
164 if (result.total > page * size) {
165 next = baseUrl + '?page=' + (page + 1)
169 prev = baseUrl + '?page=' + (page - 1)
173 id: baseUrl + '?page=' + page,
174 type: 'OrderedCollectionPage',
178 orderedItems: result.data,
179 totalItems: result.total
184 function buildSignedActivity <T> (byActor: MActor, data: T, contextType?: ContextType) {
185 const activity = activityPubContextify(data, contextType)
187 return signJsonLDObject(byActor, activity)
190 function getAPId (activity: string | { id: string }) {
191 if (typeof activity === 'string') return activity
196 function checkUrlsSameHost (url1: string, url2: string) {
197 const idHost = new URL(url1).host
198 const actorHost = new URL(url2).host
200 return idHost && actorHost && idHost.toLowerCase() === actorHost.toLowerCase()
203 function buildRemoteVideoBaseUrl (video: MVideoWithHost, path: string, scheme?: string) {
204 if (!scheme) scheme = REMOTE_SCHEME.HTTP
206 const host = video.VideoChannel.Actor.Server.host
208 return scheme + '://' + host + path
211 // ---------------------------------------------------------------------------
216 activityPubContextify,
217 activityPubCollectionPagination,
219 buildRemoteVideoBaseUrl