1 import 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 // TODO: remove in a few versions, introduced in 4.2
44 isLiveBroadcast: 'sc:isLiveBroadcast',
46 '@type': 'sc:Boolean',
47 '@id': 'pt:liveSaveReplay'
50 '@type': 'sc:Boolean',
51 '@id': 'pt:permanentLive'
54 Infohash: 'pt:Infohash',
55 Playlist: 'pt:Playlist',
56 PlaylistElement: 'pt:PlaylistElement',
58 originallyPublishedAt: 'sc:datePublished',
77 '@id': 'pt:startTimestamp'
81 '@id': 'pt:stopTimestamp'
88 '@type': 'sc:Boolean',
89 '@id': 'pt:commentsEnabled'
92 '@type': 'sc:Boolean',
93 '@id': 'pt:downloadEnabled'
96 '@type': 'sc:Boolean',
97 '@id': 'pt:waitTranscoding'
108 '@id': 'as:dislikes',
112 '@id': 'pt:playlists',
120 '@id': 'as:comments',
126 context.push(additional)
134 function activityPubContextify <T> (data: T, type: ContextType = 'All') {
135 return Object.assign({}, data, getContextData(type))
138 type ActivityPubCollectionPaginationHandler = (start: number, count: number) => Bluebird<ResultList<any>> | Promise<ResultList<any>>
139 async function activityPubCollectionPagination (
141 handler: ActivityPubCollectionPaginationHandler,
143 size = ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE
145 if (!page || !validator.isInt(page)) {
146 // We just display the first page URL, we only need the total items
147 const result = await handler(0, 1)
151 type: 'OrderedCollectionPage',
152 totalItems: result.total,
153 first: baseUrl + '?page=1'
157 const { start, count } = pageToStartAndCount(page, size)
158 const result = await handler(start, count)
160 let next: string | undefined
161 let prev: string | undefined
163 // Assert page is a number
164 page = parseInt(page, 10)
166 // There are more results
167 if (result.total > page * size) {
168 next = baseUrl + '?page=' + (page + 1)
172 prev = baseUrl + '?page=' + (page - 1)
176 id: baseUrl + '?page=' + page,
177 type: 'OrderedCollectionPage',
181 orderedItems: result.data,
182 totalItems: result.total
187 function buildSignedActivity <T> (byActor: MActor, data: T, contextType?: ContextType) {
188 const activity = activityPubContextify(data, contextType)
190 return signJsonLDObject(byActor, activity)
193 function getAPId (object: string | { id: string }) {
194 if (typeof object === 'string') return object
199 function checkUrlsSameHost (url1: string, url2: string) {
200 const idHost = new URL(url1).host
201 const actorHost = new URL(url2).host
203 return idHost && actorHost && idHost.toLowerCase() === actorHost.toLowerCase()
206 function buildRemoteVideoBaseUrl (video: MVideoWithHost, path: string, scheme?: string) {
207 if (!scheme) scheme = REMOTE_SCHEME.HTTP
209 const host = video.VideoChannel.Actor.Server.host
211 return scheme + '://' + host + path
214 // ---------------------------------------------------------------------------
219 activityPubContextify,
220 activityPubCollectionPagination,
222 buildRemoteVideoBaseUrl