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 commentsEnabled: 'http://schema.org/Boolean',
28 waitTranscoding: 'http://schema.org/Boolean',
29 support: 'http://schema.org/Text'
53 type ActivityPubCollectionPaginationHandler = (start: number, count: number) => Bluebird<ResultList<any>> | Promise<ResultList<any>>
54 async function activityPubCollectionPagination (url: string, handler: ActivityPubCollectionPaginationHandler, page?: any) {
55 if (!page || !validator.isInt(page)) {
56 // We just display the first page URL, we only need the total items
57 const result = await handler(0, 1)
61 type: 'OrderedCollection',
62 totalItems: result.total,
63 first: url + '?page=1'
67 const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
68 const result = await handler(start, count)
70 let next: string | undefined
71 let prev: string | undefined
73 // Assert page is a number
74 page = parseInt(page, 10)
76 // There are more results
77 if (result.total > page * ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) {
78 next = url + '?page=' + (page + 1)
82 prev = url + '?page=' + (page - 1)
86 id: url + '?page=' + page,
87 type: 'OrderedCollectionPage',
91 orderedItems: result.data,
92 totalItems: result.total
97 function buildSignedActivity (byActor: ActorModel, data: Object) {
98 const activity = activityPubContextify(data)
100 return signObject(byActor, activity) as Promise<Activity>
103 function getActorUrl (activityActor: string | ActivityPubActor) {
104 if (typeof activityActor === 'string') return activityActor
106 return activityActor.id
109 // ---------------------------------------------------------------------------
113 activityPubContextify,
114 activityPubCollectionPagination,