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 Hashtag: 'as:Hashtag',
18 uuid: 'http://schema.org/identifier',
19 category: 'http://schema.org/category',
20 licence: 'http://schema.org/license',
21 subtitleLanguage: 'http://schema.org/subtitleLanguage',
22 sensitive: 'as:sensitive',
23 language: 'http://schema.org/inLanguage',
24 views: 'http://schema.org/Number',
25 stats: 'http://schema.org/Number',
26 size: 'http://schema.org/Number',
27 fps: 'http://schema.org/Number',
28 commentsEnabled: 'http://schema.org/Boolean',
29 waitTranscoding: 'http://schema.org/Boolean',
30 support: 'http://schema.org/Text'
54 type ActivityPubCollectionPaginationHandler = (start: number, count: number) => Bluebird<ResultList<any>> | Promise<ResultList<any>>
55 async function activityPubCollectionPagination (url: string, handler: ActivityPubCollectionPaginationHandler, page?: any) {
56 if (!page || !validator.isInt(page)) {
57 // We just display the first page URL, we only need the total items
58 const result = await handler(0, 1)
62 type: 'OrderedCollection',
63 totalItems: result.total,
64 first: url + '?page=1'
68 const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
69 const result = await handler(start, count)
71 let next: string | undefined
72 let prev: string | undefined
74 // Assert page is a number
75 page = parseInt(page, 10)
77 // There are more results
78 if (result.total > page * ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) {
79 next = url + '?page=' + (page + 1)
83 prev = url + '?page=' + (page - 1)
87 id: url + '?page=' + page,
88 type: 'OrderedCollectionPage',
92 orderedItems: result.data,
93 totalItems: result.total
98 function buildSignedActivity (byActor: ActorModel, data: Object) {
99 const activity = activityPubContextify(data)
101 return signObject(byActor, activity) as Promise<Activity>
104 function getActorUrl (activityActor: string | ActivityPubActor) {
105 if (typeof activityActor === 'string') return activityActor
107 return activityActor.id
110 // ---------------------------------------------------------------------------
114 activityPubContextify,
115 activityPubCollectionPagination,