1 import { ResultList } from '../../shared/models'
2 import { Activity, ActivityPubActor } from '../../shared/models/activitypub'
3 import { ACTIVITY_PUB } from '../initializers'
4 import { ActorModel } from '../models/activitypub/actor'
5 import { signObject } from './peertube-crypto'
7 function activityPubContextify <T> (data: T) {
8 return Object.assign(data,{
10 'https://www.w3.org/ns/activitystreams',
11 'https://w3id.org/security/v1',
13 'RsaSignature2017': 'https://w3id.org/security#RsaSignature2017',
14 'Hashtag': 'as:Hashtag',
15 'uuid': 'http://schema.org/identifier',
16 'category': 'http://schema.org/category',
17 'licence': 'http://schema.org/license',
18 'sensitive': 'as:sensitive',
19 'language': 'http://schema.org/inLanguage',
20 'views': 'http://schema.org/Number',
21 'size': 'http://schema.org/Number',
22 'commentsEnabled': 'http://schema.org/Boolean',
23 'support': 'http://schema.org/Text'
47 function activityPubCollection (url: string, results: any[]) {
50 type: 'OrderedCollection',
51 totalItems: results.length,
56 function activityPubCollectionPagination (url: string, page: any, result: ResultList<any>) {
60 // Assert page is a number
61 page = parseInt(page, 10)
63 // There are more results
64 if (result.total > page * ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) {
65 next = url + '?page=' + (page + 1)
69 prev = url + '?page=' + (page - 1)
72 const orderedCollectionPagination = {
73 id: url + '?page=' + page,
74 type: 'OrderedCollectionPage',
78 orderedItems: result.data
82 return activityPubContextify({
84 type: 'OrderedCollection',
85 totalItems: result.total,
86 first: orderedCollectionPagination
89 orderedCollectionPagination['totalItems'] = result.total
92 return orderedCollectionPagination
95 function buildSignedActivity (byActor: ActorModel, data: Object) {
96 const activity = activityPubContextify(data)
98 return signObject(byActor, activity) as Promise<Activity>
101 function getActorUrl (activityActor: string | ActivityPubActor) {
102 if (typeof activityActor === 'string') return activityActor
104 return activityActor.id
107 // ---------------------------------------------------------------------------
111 activityPubContextify,
112 activityPubCollectionPagination,
113 activityPubCollection,