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 '../types/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 Playlist: 'pt:Playlist',
44 PlaylistElement: 'pt:PlaylistElement',
46 originallyPublishedAt: 'sc:datePublished',
65 '@id': 'pt:startTimestamp'
69 '@id': 'pt:stopTimestamp'
76 '@type': 'sc:Boolean',
77 '@id': 'pt:commentsEnabled'
80 '@type': 'sc:Boolean',
81 '@id': 'pt:downloadEnabled'
84 '@type': 'sc:Boolean',
85 '@id': 'pt:waitTranscoding'
100 '@id': 'pt:playlists',
108 '@id': 'as:comments',
114 context.push(additional)
122 function activityPubContextify <T> (data: T, type: ContextType = 'All') {
123 return Object.assign({}, data, getContextData(type))
126 type ActivityPubCollectionPaginationHandler = (start: number, count: number) => Bluebird<ResultList<any>> | Promise<ResultList<any>>
127 async function activityPubCollectionPagination (
129 handler: ActivityPubCollectionPaginationHandler,
131 size = ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE
133 if (!page || !validator.isInt(page)) {
134 // We just display the first page URL, we only need the total items
135 const result = await handler(0, 1)
139 type: 'OrderedCollectionPage',
140 totalItems: result.total,
141 first: baseUrl + '?page=1'
145 const { start, count } = pageToStartAndCount(page, size)
146 const result = await handler(start, count)
148 let next: string | undefined
149 let prev: string | undefined
151 // Assert page is a number
152 page = parseInt(page, 10)
154 // There are more results
155 if (result.total > page * size) {
156 next = baseUrl + '?page=' + (page + 1)
160 prev = baseUrl + '?page=' + (page - 1)
164 id: baseUrl + '?page=' + page,
165 type: 'OrderedCollectionPage',
169 orderedItems: result.data,
170 totalItems: result.total
175 function buildSignedActivity (byActor: MActor, data: Object, contextType?: ContextType) {
176 const activity = activityPubContextify(data, contextType)
178 return signJsonLDObject(byActor, activity) as Promise<Activity>
181 function getAPId (activity: string | { id: string }) {
182 if (typeof activity === 'string') return activity
187 function checkUrlsSameHost (url1: string, url2: string) {
188 const idHost = new URL(url1).host
189 const actorHost = new URL(url2).host
191 return idHost && actorHost && idHost.toLowerCase() === actorHost.toLowerCase()
194 function buildRemoteVideoBaseUrl (video: MVideoAccountLight, path: string) {
195 const host = video.VideoChannel.Account.Actor.Server.host
197 return REMOTE_SCHEME.HTTP + '://' + host + path
200 // ---------------------------------------------------------------------------
205 activityPubContextify,
206 activityPubCollectionPagination,
208 buildRemoteVideoBaseUrl