]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/activitypub.ts
Add outbox
[github/Chocobozzz/PeerTube.git] / server / helpers / activitypub.ts
1 import { Activity } from '../../shared/models/activitypub/activity'
2 import { ResultList } from '../../shared/models/result-list.model'
3 import { AccountInstance } from '../models/account/account-interface'
4 import { signObject } from './peertube-crypto'
5 import { ACTIVITY_PUB } from '../initializers/constants'
6
7 function activityPubContextify <T> (data: T) {
8 return Object.assign(data,{
9 '@context': [
10 'https://www.w3.org/ns/activitystreams',
11 'https://w3id.org/security/v1',
12 {
13 'Hashtag': 'as:Hashtag',
14 'uuid': 'http://schema.org/identifier',
15 'category': 'http://schema.org/category',
16 'licence': 'http://schema.org/license',
17 'nsfw': 'as:sensitive',
18 'language': 'http://schema.org/inLanguage',
19 'views': 'http://schema.org/Number',
20 'size': 'http://schema.org/Number',
21 'VideoChannel': 'https://peertu.be/ns/VideoChannel'
22 }
23 ]
24 })
25 }
26
27 function activityPubCollectionPagination (url: string, page: number, result: ResultList<any>) {
28 let next: string
29 let prev: string
30
31 // There are more results
32 if (result.total > ((page + 1) * ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)) {
33 next = url + '?page=' + (page + 1)
34 }
35
36 if (page > 1) {
37 prev = url + '?page=' + (page - 1)
38 }
39
40 const orderedCollectionPagination = {
41 id: url + '?page=' + page,
42 type: 'OrderedCollectionPage',
43 prev,
44 next,
45 partOf: url,
46 orderedItems: result.data
47 }
48
49 const obj = {
50 id: url,
51 type: 'OrderedCollection',
52 totalItems: result.total,
53 orderedItems: orderedCollectionPagination
54 }
55
56 return activityPubContextify(obj)
57 }
58
59 function buildSignedActivity (byAccount: AccountInstance, data: Object) {
60 const activity = activityPubContextify(data)
61
62 return signObject(byAccount, activity) as Promise<Activity>
63 }
64
65 // ---------------------------------------------------------------------------
66
67 export {
68 activityPubContextify,
69 activityPubCollectionPagination,
70 buildSignedActivity
71 }