]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/activitypub.ts
5850fc19f36bd61c021c981dafdac715cc6b07dd
[github/Chocobozzz/PeerTube.git] / server / helpers / activitypub.ts
1 import { ResultList } from '../../shared/models'
2 import { Activity } from '../../shared/models/activitypub'
3 import { ACTIVITY_PUB } from '../initializers'
4 import { ActorModel } from '../models/activitypub/actor'
5 import { signObject } from './peertube-crypto'
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 activityPubCollection (results: any[]) {
28 return {
29 type: 'OrderedCollection',
30 totalItems: results.length,
31 orderedItems: results
32 }
33 }
34
35 function activityPubCollectionPagination (url: string, page: any, result: ResultList<any>) {
36 let next: string
37 let prev: string
38
39 // Assert page is a number
40 page = parseInt(page, 10)
41
42 // There are more results
43 if (result.total > page * ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) {
44 next = url + '?page=' + (page + 1)
45 }
46
47 if (page > 1) {
48 prev = url + '?page=' + (page - 1)
49 }
50
51 const orderedCollectionPagination = {
52 id: url + '?page=' + page,
53 type: 'OrderedCollectionPage',
54 prev,
55 next,
56 partOf: url,
57 orderedItems: result.data
58 }
59
60 if (page === 1) {
61 return activityPubContextify({
62 id: url,
63 type: 'OrderedCollection',
64 totalItems: result.total,
65 first: orderedCollectionPagination
66 })
67 } else {
68 orderedCollectionPagination['totalItems'] = result.total
69 }
70
71 return orderedCollectionPagination
72 }
73
74 function buildSignedActivity (byActor: ActorModel, data: Object) {
75 const activity = activityPubContextify(data)
76
77 return signObject(byActor, activity) as Promise<Activity>
78 }
79
80 // ---------------------------------------------------------------------------
81
82 export {
83 activityPubContextify,
84 activityPubCollectionPagination,
85 activityPubCollection,
86 buildSignedActivity
87 }