]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/activitypub.ts
Add id to likes/dislikes/comments/shares collections
[github/Chocobozzz/PeerTube.git] / server / helpers / activitypub.ts
CommitLineData
3fd3ab2d
C
1import { ResultList } from '../../shared/models'
2import { Activity } from '../../shared/models/activitypub'
3import { ACTIVITY_PUB } from '../initializers'
50d6de9c 4import { ActorModel } from '../models/activitypub/actor'
afffe988 5import { signObject } from './peertube-crypto'
571389d4
C
6
7function activityPubContextify <T> (data: T) {
e4f97bab
C
8 return Object.assign(data,{
9 '@context': [
10 'https://www.w3.org/ns/activitystreams',
11 'https://w3id.org/security/v1',
12 {
ce33ee01 13 'RsaSignature2017': 'https://w3id.org/security#RsaSignature2017',
e4f97bab
C
14 'Hashtag': 'as:Hashtag',
15 'uuid': 'http://schema.org/identifier',
16 'category': 'http://schema.org/category',
17 'licence': 'http://schema.org/license',
0a67e28b 18 'sensitive': 'as:sensitive',
e4f97bab
C
19 'language': 'http://schema.org/inLanguage',
20 'views': 'http://schema.org/Number',
0af3182b
C
21 'size': 'http://schema.org/Number',
22 'commentsEnabled': 'http://schema.org/Boolean'
e4f97bab
C
23 }
24 ]
25 })
26}
27
46531a0a 28function activityPubCollection (url: string, results: any[]) {
16b90975 29 return {
46531a0a 30 id: url,
16b90975
C
31 type: 'OrderedCollection',
32 totalItems: results.length,
33 orderedItems: results
34 }
35}
36
c46edbc2 37function activityPubCollectionPagination (url: string, page: any, result: ResultList<any>) {
e71bcc0f
C
38 let next: string
39 let prev: string
40
c46edbc2
C
41 // Assert page is a number
42 page = parseInt(page, 10)
43
e71bcc0f 44 // There are more results
c46edbc2 45 if (result.total > page * ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) {
e71bcc0f
C
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 }
e4f97bab 61
c986175d
C
62 if (page === 1) {
63 return activityPubContextify({
64 id: url,
65 type: 'OrderedCollection',
66 totalItems: result.total,
67 first: orderedCollectionPagination
68 })
c46edbc2
C
69 } else {
70 orderedCollectionPagination['totalItems'] = result.total
e4f97bab
C
71 }
72
c986175d 73 return orderedCollectionPagination
e4f97bab
C
74}
75
50d6de9c 76function buildSignedActivity (byActor: ActorModel, data: Object) {
afffe988
C
77 const activity = activityPubContextify(data)
78
50d6de9c 79 return signObject(byActor, activity) as Promise<Activity>
afffe988
C
80}
81
e4f97bab
C
82// ---------------------------------------------------------------------------
83
84export {
e4f97bab 85 activityPubContextify,
0d0e8dd0 86 activityPubCollectionPagination,
16b90975 87 activityPubCollection,
892211e8 88 buildSignedActivity
e4f97bab 89}