1 import * as Bluebird from 'bluebird'
2 import * as 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 { ActorModel } from '../models/activitypub/actor'
7 import { signJsonLDObject } from './peertube-crypto'
8 import { pageToStartAndCount } from './core-utils'
9 import { parse } from 'url'
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 (baseUrl: string, handler: ActivityPubCollectionPaginationHandler, page?: any) {
104 if (!page || !validator.isInt(page)) {
105 // We just display the first page URL, we only need the total items
106 const result = await handler(0, 1)
110 type: 'OrderedCollectionPage',
111 totalItems: result.total,
112 first: baseUrl + '?page=1'
116 const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
117 const result = await handler(start, count)
119 let next: string | undefined
120 let prev: string | undefined
122 // Assert page is a number
123 page = parseInt(page, 10)
125 // There are more results
126 if (result.total > page * ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) {
127 next = baseUrl + '?page=' + (page + 1)
131 prev = baseUrl + '?page=' + (page - 1)
135 id: baseUrl + '?page=' + page,
136 type: 'OrderedCollectionPage',
140 orderedItems: result.data,
141 totalItems: result.total
146 function buildSignedActivity (byActor: ActorModel, data: Object) {
147 const activity = activityPubContextify(data)
149 return signJsonLDObject(byActor, activity) as Promise<Activity>
152 function getAPId (activity: string | { id: string }) {
153 if (typeof activity === 'string') return activity
158 function checkUrlsSameHost (url1: string, url2: string) {
159 const idHost = parse(url1).host
160 const actorHost = parse(url2).host
162 return idHost && actorHost && idHost.toLowerCase() === actorHost.toLowerCase()
165 // ---------------------------------------------------------------------------
170 activityPubContextify,
171 activityPubCollectionPagination,