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 } from '../initializers/constants'
6 import { signJsonLDObject } from './peertube-crypto'
7 import { pageToStartAndCount } from './core-utils'
8 import { parse } from 'url'
9 import { MActor } from '../typings/models'
11 function activityPubContextify <T> (data: T) {
12 return Object.assign(data, {
14 'https://www.w3.org/ns/activitystreams',
15 'https://w3id.org/security/v1',
17 RsaSignature2017: 'https://w3id.org/security#RsaSignature2017',
18 pt: 'https://joinpeertube.org/ns#',
19 sc: 'http://schema.org#',
20 Hashtag: 'as:Hashtag',
21 uuid: 'sc:identifier',
22 category: 'sc:category',
23 licence: 'sc:license',
24 subtitleLanguage: 'sc:subtitleLanguage',
25 sensitive: 'as:sensitive',
26 language: 'sc:inLanguage',
27 expires: 'sc:expires',
28 CacheFile: 'pt:CacheFile',
29 Infohash: 'pt:Infohash',
30 originallyPublishedAt: 'sc:datePublished',
49 '@id': 'pt:startTimestamp'
53 '@id': 'pt:stopTimestamp'
60 '@type': 'sc:Boolean',
61 '@id': 'pt:commentsEnabled'
64 '@type': 'sc:Boolean',
65 '@id': 'pt:downloadEnabled'
68 '@type': 'sc:Boolean',
69 '@id': 'pt:waitTranscoding'
86 '@id': 'pt:playlists',
102 type ActivityPubCollectionPaginationHandler = (start: number, count: number) => Bluebird<ResultList<any>> | Promise<ResultList<any>>
103 async function activityPubCollectionPagination (
105 handler: ActivityPubCollectionPaginationHandler,
107 size = ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE
109 if (!page || !validator.isInt(page)) {
110 // We just display the first page URL, we only need the total items
111 const result = await handler(0, 1)
115 type: 'OrderedCollectionPage',
116 totalItems: result.total,
117 first: baseUrl + '?page=1'
121 const { start, count } = pageToStartAndCount(page, size)
122 const result = await handler(start, count)
124 let next: string | undefined
125 let prev: string | undefined
127 // Assert page is a number
128 page = parseInt(page, 10)
130 // There are more results
131 if (result.total > page * size) {
132 next = baseUrl + '?page=' + (page + 1)
136 prev = baseUrl + '?page=' + (page - 1)
140 id: baseUrl + '?page=' + page,
141 type: 'OrderedCollectionPage',
145 orderedItems: result.data,
146 totalItems: result.total
151 function buildSignedActivity (byActor: MActor, data: Object) {
152 const activity = activityPubContextify(data)
154 return signJsonLDObject(byActor, activity) as Promise<Activity>
157 function getAPId (activity: string | { id: string }) {
158 if (typeof activity === 'string') return activity
163 function checkUrlsSameHost (url1: string, url2: string) {
164 const idHost = parse(url1).host
165 const actorHost = parse(url2).host
167 return idHost && actorHost && idHost.toLowerCase() === actorHost.toLowerCase()
170 // ---------------------------------------------------------------------------
175 activityPubContextify,
176 activityPubCollectionPagination,