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 'sensitive': 'as:sensitive',
22 'language': 'http://schema.org/inLanguage',
23 'views': 'http://schema.org/Number',
24 'size': 'http://schema.org/Number',
25 'commentsEnabled': 'http://schema.org/Boolean',
26 'support': 'http://schema.org/Text'
50 type ActivityPubCollectionPaginationHandler = (start: number, count: number) => Bluebird<ResultList<any>> | Promise<ResultList<any>>
51 async function activityPubCollectionPagination (url: string, handler: ActivityPubCollectionPaginationHandler, page?: any) {
52 if (!page || !validator.isInt(page)) {
53 // We just display the first page URL, we only need the total items
54 const result = await handler(0, 1)
58 type: 'OrderedCollection',
59 totalItems: result.total,
60 first: url + '?page=1'
64 const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
65 const result = await handler(start, count)
70 // Assert page is a number
71 page = parseInt(page, 10)
73 // There are more results
74 if (result.total > page * ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) {
75 next = url + '?page=' + (page + 1)
79 prev = url + '?page=' + (page - 1)
83 id: url + '?page=' + page,
84 type: 'OrderedCollectionPage',
88 orderedItems: result.data,
89 totalItems: result.total
94 function buildSignedActivity (byActor: ActorModel, data: Object) {
95 const activity = activityPubContextify(data)
97 return signObject(byActor, activity) as Promise<Activity>
100 function getActorUrl (activityActor: string | ActivityPubActor) {
101 if (typeof activityActor === 'string') return activityActor
103 return activityActor.id
106 // ---------------------------------------------------------------------------
110 activityPubContextify,
111 activityPubCollectionPagination,