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'
10 import { ContextType } from '@shared/models/activitypub/context'
12 function getContextData (type: ContextType) {
13 const context: any[] = [
14 'https://www.w3.org/ns/activitystreams',
15 'https://w3id.org/security/v1',
17 RsaSignature2017: 'https://w3id.org/security#RsaSignature2017'
21 if (type !== 'View' && type !== 'Announce') {
23 pt: 'https://joinpeertube.org/ns#',
24 sc: 'http://schema.org#'
27 if (type === 'CacheFile') {
28 Object.assign(additional, {
29 expires: 'sc:expires',
30 CacheFile: 'pt:CacheFile'
33 Object.assign(additional, {
34 Hashtag: 'as:Hashtag',
35 uuid: 'sc:identifier',
36 category: 'sc:category',
37 licence: 'sc:license',
38 subtitleLanguage: 'sc:subtitleLanguage',
39 sensitive: 'as:sensitive',
40 language: 'sc:inLanguage',
42 Infohash: 'pt:Infohash',
43 originallyPublishedAt: 'sc:datePublished',
62 '@id': 'pt:startTimestamp'
66 '@id': 'pt:stopTimestamp'
73 '@type': 'sc:Boolean',
74 '@id': 'pt:commentsEnabled'
77 '@type': 'sc:Boolean',
78 '@id': 'pt:downloadEnabled'
81 '@type': 'sc:Boolean',
82 '@id': 'pt:waitTranscoding'
97 '@id': 'pt:playlists',
105 '@id': 'as:comments',
111 context.push(additional)
119 function activityPubContextify <T> (data: T, type: ContextType = 'All') {
120 return Object.assign({}, data, getContextData(type))
123 type ActivityPubCollectionPaginationHandler = (start: number, count: number) => Bluebird<ResultList<any>> | Promise<ResultList<any>>
124 async function activityPubCollectionPagination (
126 handler: ActivityPubCollectionPaginationHandler,
128 size = ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE
130 if (!page || !validator.isInt(page)) {
131 // We just display the first page URL, we only need the total items
132 const result = await handler(0, 1)
136 type: 'OrderedCollectionPage',
137 totalItems: result.total,
138 first: baseUrl + '?page=1'
142 const { start, count } = pageToStartAndCount(page, size)
143 const result = await handler(start, count)
145 let next: string | undefined
146 let prev: string | undefined
148 // Assert page is a number
149 page = parseInt(page, 10)
151 // There are more results
152 if (result.total > page * size) {
153 next = baseUrl + '?page=' + (page + 1)
157 prev = baseUrl + '?page=' + (page - 1)
161 id: baseUrl + '?page=' + page,
162 type: 'OrderedCollectionPage',
166 orderedItems: result.data,
167 totalItems: result.total
172 function buildSignedActivity (byActor: MActor, data: Object, contextType?: ContextType) {
173 const activity = activityPubContextify(data, contextType)
175 return signJsonLDObject(byActor, activity) as Promise<Activity>
178 function getAPId (activity: string | { id: string }) {
179 if (typeof activity === 'string') return activity
184 function checkUrlsSameHost (url1: string, url2: string) {
185 const idHost = new URL(url1).host
186 const actorHost = new URL(url2).host
188 return idHost && actorHost && idHost.toLowerCase() === actorHost.toLowerCase()
191 function buildRemoteVideoBaseUrl (video: MVideoAccountLight, path: string) {
192 const host = video.VideoChannel.Account.Actor.Server.host
194 return REMOTE_SCHEME.HTTP + '://' + host + path
197 // ---------------------------------------------------------------------------
202 activityPubContextify,
203 activityPubCollectionPagination,
205 buildRemoteVideoBaseUrl