1 import * as Bluebird from 'bluebird'
2 import * as validator from 'validator'
3 import { ResultList } from '../../shared/models'
4 import { Activity, ActivityPubActor } from '../../shared/models/activitypub'
5 import { ACTIVITY_PUB } from '../initializers'
6 import { ActorModel } from '../models/activitypub/actor'
7 import { signObject } from './peertube-crypto'
8 import { pageToStartAndCount } from './core-utils'
10 function activityPubContextify <T> (data: T) {
11 return Object.assign(data, {
13 'https://www.w3.org/ns/activitystreams',
14 'https://w3id.org/security/v1',
16 RsaSignature2017: 'https://w3id.org/security#RsaSignature2017',
17 pt: 'https://joinpeertube.org/ns',
18 schema: 'http://schema.org#',
19 Hashtag: 'as:Hashtag',
20 uuid: 'schema:identifier',
21 category: 'schema:category',
22 licence: 'schema:license',
23 subtitleLanguage: 'schema:subtitleLanguage',
24 sensitive: 'as:sensitive',
25 language: 'schema:inLanguage',
26 views: 'schema:Number',
27 stats: 'schema:Number',
28 size: 'schema:Number',
30 commentsEnabled: 'schema:Boolean',
31 waitTranscoding: 'schema:Boolean',
32 expires: 'schema:expires',
33 support: 'schema:Text',
34 CacheFile: 'pt:CacheFile'
58 type ActivityPubCollectionPaginationHandler = (start: number, count: number) => Bluebird<ResultList<any>> | Promise<ResultList<any>>
59 async function activityPubCollectionPagination (url: string, handler: ActivityPubCollectionPaginationHandler, page?: any) {
60 if (!page || !validator.isInt(page)) {
61 // We just display the first page URL, we only need the total items
62 const result = await handler(0, 1)
66 type: 'OrderedCollection',
67 totalItems: result.total,
68 first: url + '?page=1'
72 const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
73 const result = await handler(start, count)
75 let next: string | undefined
76 let prev: string | undefined
78 // Assert page is a number
79 page = parseInt(page, 10)
81 // There are more results
82 if (result.total > page * ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) {
83 next = url + '?page=' + (page + 1)
87 prev = url + '?page=' + (page - 1)
91 id: url + '?page=' + page,
92 type: 'OrderedCollectionPage',
96 orderedItems: result.data,
97 totalItems: result.total
102 function buildSignedActivity (byActor: ActorModel, data: Object) {
103 const activity = activityPubContextify(data)
105 return signObject(byActor, activity) as Promise<Activity>
108 function getActorUrl (activityActor: string | ActivityPubActor) {
109 if (typeof activityActor === 'string') return activityActor
111 return activityActor.id
114 // ---------------------------------------------------------------------------
118 activityPubContextify,
119 activityPubCollectionPagination,