1 import * as Bluebird from 'bluebird'
2 import validator from 'validator'
3 import { ResultList } from '../../shared/models'
4 import { Activity } from '../../shared/models/activitypub'
5 import { ACTIVITY_PUB, REMOTE_SCHEME } from '../initializers/constants'
6 import { signJsonLDObject } from './peertube-crypto'
7 import { pageToStartAndCount } from './core-utils'
8 import { URL } from 'url'
9 import { MActor, MVideoAccountLight } from '../typings/models'
11 export type ContextType = 'All' | 'View' | 'Announce' | 'CacheFile'
13 function getContextData (type: ContextType) {
14 const context: any[] = [
15 'https://www.w3.org/ns/activitystreams',
16 'https://w3id.org/security/v1',
18 RsaSignature2017: 'https://w3id.org/security#RsaSignature2017'
22 if (type !== 'View' && type !== 'Announce') {
24 pt: 'https://joinpeertube.org/ns#',
25 sc: 'http://schema.org#'
28 if (type === 'CacheFile') {
29 Object.assign(additional, {
30 expires: 'sc:expires',
31 CacheFile: 'pt:CacheFile'
34 Object.assign(additional, {
35 Hashtag: 'as:Hashtag',
36 uuid: 'sc:identifier',
37 category: 'sc:category',
38 licence: 'sc:license',
39 subtitleLanguage: 'sc:subtitleLanguage',
40 sensitive: 'as:sensitive',
41 language: 'sc:inLanguage',
43 Infohash: 'pt:Infohash',
44 originallyPublishedAt: 'sc:datePublished',
63 '@id': 'pt:startTimestamp'
67 '@id': 'pt:stopTimestamp'
74 '@type': 'sc:Boolean',
75 '@id': 'pt:commentsEnabled'
78 '@type': 'sc:Boolean',
79 '@id': 'pt:downloadEnabled'
82 '@type': 'sc:Boolean',
83 '@id': 'pt:waitTranscoding'
98 '@id': 'pt:playlists',
106 '@id': 'as:comments',
112 context.push(additional)
120 function activityPubContextify <T> (data: T, type: ContextType = 'All') {
121 return Object.assign({}, data, getContextData(type))
124 type ActivityPubCollectionPaginationHandler = (start: number, count: number) => Bluebird<ResultList<any>> | Promise<ResultList<any>>
125 async function activityPubCollectionPagination (
127 handler: ActivityPubCollectionPaginationHandler,
129 size = ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE
131 if (!page || !validator.isInt(page)) {
132 // We just display the first page URL, we only need the total items
133 const result = await handler(0, 1)
137 type: 'OrderedCollectionPage',
138 totalItems: result.total,
139 first: baseUrl + '?page=1'
143 const { start, count } = pageToStartAndCount(page, size)
144 const result = await handler(start, count)
146 let next: string | undefined
147 let prev: string | undefined
149 // Assert page is a number
150 page = parseInt(page, 10)
152 // There are more results
153 if (result.total > page * size) {
154 next = baseUrl + '?page=' + (page + 1)
158 prev = baseUrl + '?page=' + (page - 1)
162 id: baseUrl + '?page=' + page,
163 type: 'OrderedCollectionPage',
167 orderedItems: result.data,
168 totalItems: result.total
173 function buildSignedActivity (byActor: MActor, data: Object, contextType?: ContextType) {
174 const activity = activityPubContextify(data, contextType)
176 return signJsonLDObject(byActor, activity) as Promise<Activity>
179 function getAPId (activity: string | { id: string }) {
180 if (typeof activity === 'string') return activity
185 function checkUrlsSameHost (url1: string, url2: string) {
186 const idHost = new URL(url1).host
187 const actorHost = new URL(url2).host
189 return idHost && actorHost && idHost.toLowerCase() === actorHost.toLowerCase()
192 function buildRemoteVideoBaseUrl (video: MVideoAccountLight, path: string) {
193 const host = video.VideoChannel.Account.Actor.Server.host
195 return REMOTE_SCHEME.HTTP + '://' + host + path
198 // ---------------------------------------------------------------------------
203 activityPubContextify,
204 activityPubCollectionPagination,
206 buildRemoteVideoBaseUrl