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 isLiveBroadcast: 'sc:isLiveBroadcast',
44 '@type': 'sc:Boolean',
45 '@id': 'pt:liveSaveReplay'
48 '@type': 'sc:Boolean',
49 '@id': 'pt:permanentLive'
52 Infohash: 'pt:Infohash',
53 Playlist: 'pt:Playlist',
54 PlaylistElement: 'pt:PlaylistElement',
56 originallyPublishedAt: 'sc:datePublished',
75 '@id': 'pt:startTimestamp'
79 '@id': 'pt:stopTimestamp'
86 '@type': 'sc:Boolean',
87 '@id': 'pt:commentsEnabled'
90 '@type': 'sc:Boolean',
91 '@id': 'pt:downloadEnabled'
94 '@type': 'sc:Boolean',
95 '@id': 'pt:waitTranscoding'
106 '@id': 'as:dislikes',
110 '@id': 'pt:playlists',
118 '@id': 'as:comments',
124 context.push(additional)
132 function activityPubContextify <T> (data: T, type: ContextType = 'All') {
133 return Object.assign({}, data, getContextData(type))
136 type ActivityPubCollectionPaginationHandler = (start: number, count: number) => Bluebird<ResultList<any>> | Promise<ResultList<any>>
137 async function activityPubCollectionPagination (
139 handler: ActivityPubCollectionPaginationHandler,
141 size = ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE
143 if (!page || !validator.isInt(page)) {
144 // We just display the first page URL, we only need the total items
145 const result = await handler(0, 1)
149 type: 'OrderedCollectionPage',
150 totalItems: result.total,
151 first: baseUrl + '?page=1'
155 const { start, count } = pageToStartAndCount(page, size)
156 const result = await handler(start, count)
158 let next: string | undefined
159 let prev: string | undefined
161 // Assert page is a number
162 page = parseInt(page, 10)
164 // There are more results
165 if (result.total > page * size) {
166 next = baseUrl + '?page=' + (page + 1)
170 prev = baseUrl + '?page=' + (page - 1)
174 id: baseUrl + '?page=' + page,
175 type: 'OrderedCollectionPage',
179 orderedItems: result.data,
180 totalItems: result.total
185 function buildSignedActivity (byActor: MActor, data: Object, contextType?: ContextType) {
186 const activity = activityPubContextify(data, contextType)
188 return signJsonLDObject(byActor, activity) as Promise<Activity>
191 function getAPId (activity: string | { id: string }) {
192 if (typeof activity === 'string') return activity
197 function checkUrlsSameHost (url1: string, url2: string) {
198 const idHost = new URL(url1).host
199 const actorHost = new URL(url2).host
201 return idHost && actorHost && idHost.toLowerCase() === actorHost.toLowerCase()
204 function buildRemoteVideoBaseUrl (video: MVideoAccountLight, path: string) {
205 const host = video.VideoChannel.Account.Actor.Server.host
207 return REMOTE_SCHEME.HTTP + '://' + host + path
210 // ---------------------------------------------------------------------------
215 activityPubContextify,
216 activityPubCollectionPagination,
218 buildRemoteVideoBaseUrl