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