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'
10 import { MActor } from '../typings/models'
12 function activityPubContextify <T> (data: T) {
13 return Object.assign(data, {
15 'https://www.w3.org/ns/activitystreams',
16 'https://w3id.org/security/v1',
18 RsaSignature2017: 'https://w3id.org/security#RsaSignature2017',
19 pt: 'https://joinpeertube.org/ns#',
20 sc: 'http://schema.org#',
21 Hashtag: 'as:Hashtag',
22 uuid: 'sc:identifier',
23 category: 'sc:category',
24 licence: 'sc:license',
25 subtitleLanguage: 'sc:subtitleLanguage',
26 sensitive: 'as:sensitive',
27 language: 'sc:inLanguage',
28 expires: 'sc:expires',
29 CacheFile: 'pt:CacheFile',
30 Infohash: 'pt:Infohash',
31 originallyPublishedAt: 'sc:datePublished',
50 '@id': 'pt:startTimestamp'
54 '@id': 'pt:stopTimestamp'
61 '@type': 'sc:Boolean',
62 '@id': 'pt:commentsEnabled'
65 '@type': 'sc:Boolean',
66 '@id': 'pt:downloadEnabled'
69 '@type': 'sc:Boolean',
70 '@id': 'pt:waitTranscoding'
87 '@id': 'pt:playlists',
103 type ActivityPubCollectionPaginationHandler = (start: number, count: number) => Bluebird<ResultList<any>> | Promise<ResultList<any>>
104 async function activityPubCollectionPagination (baseUrl: string, handler: ActivityPubCollectionPaginationHandler, page?: any) {
105 if (!page || !validator.isInt(page)) {
106 // We just display the first page URL, we only need the total items
107 const result = await handler(0, 1)
111 type: 'OrderedCollectionPage',
112 totalItems: result.total,
113 first: baseUrl + '?page=1'
117 const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
118 const result = await handler(start, count)
120 let next: string | undefined
121 let prev: string | undefined
123 // Assert page is a number
124 page = parseInt(page, 10)
126 // There are more results
127 if (result.total > page * ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) {
128 next = baseUrl + '?page=' + (page + 1)
132 prev = baseUrl + '?page=' + (page - 1)
136 id: baseUrl + '?page=' + page,
137 type: 'OrderedCollectionPage',
141 orderedItems: result.data,
142 totalItems: result.total
147 function buildSignedActivity (byActor: MActor, data: Object) {
148 const activity = activityPubContextify(data)
150 return signJsonLDObject(byActor, activity) as Promise<Activity>
153 function getAPId (activity: string | { id: string }) {
154 if (typeof activity === 'string') return activity
159 function checkUrlsSameHost (url1: string, url2: string) {
160 const idHost = parse(url1).host
161 const actorHost = parse(url2).host
163 return idHost && actorHost && idHost.toLowerCase() === actorHost.toLowerCase()
166 // ---------------------------------------------------------------------------
171 activityPubContextify,
172 activityPubCollectionPagination,