]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/activitypub.ts
Improve video torrent AP object validator
[github/Chocobozzz/PeerTube.git] / server / helpers / activitypub.ts
CommitLineData
3fd3ab2d 1import { ResultList } from '../../shared/models'
6be84cbc 2import { Activity, ActivityPubActor } from '../../shared/models/activitypub'
3fd3ab2d 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 21 'size': 'http://schema.org/Number',
2422c46b
C
22 'commentsEnabled': 'http://schema.org/Boolean',
23 'support': 'http://schema.org/Text'
2fe86927
C
24 },
25 {
26 likes: {
27 '@id': 'as:likes',
28 '@type': '@id'
29 },
30 dislikes: {
31 '@id': 'as:dislikes',
32 '@type': '@id'
33 },
34 shares: {
35 '@id': 'as:shares',
36 '@type': '@id'
37 },
38 comments: {
39 '@id': 'as:comments',
40 '@type': '@id'
41 }
e4f97bab
C
42 }
43 ]
44 })
45}
46
46531a0a 47function activityPubCollection (url: string, results: any[]) {
16b90975 48 return {
46531a0a 49 id: url,
16b90975
C
50 type: 'OrderedCollection',
51 totalItems: results.length,
52 orderedItems: results
53 }
54}
55
c46edbc2 56function activityPubCollectionPagination (url: string, page: any, result: ResultList<any>) {
e71bcc0f
C
57 let next: string
58 let prev: string
59
c46edbc2
C
60 // Assert page is a number
61 page = parseInt(page, 10)
62
e71bcc0f 63 // There are more results
c46edbc2 64 if (result.total > page * ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) {
e71bcc0f
C
65 next = url + '?page=' + (page + 1)
66 }
67
68 if (page > 1) {
69 prev = url + '?page=' + (page - 1)
70 }
71
72 const orderedCollectionPagination = {
73 id: url + '?page=' + page,
74 type: 'OrderedCollectionPage',
75 prev,
76 next,
77 partOf: url,
78 orderedItems: result.data
79 }
e4f97bab 80
c986175d
C
81 if (page === 1) {
82 return activityPubContextify({
83 id: url,
84 type: 'OrderedCollection',
85 totalItems: result.total,
86 first: orderedCollectionPagination
87 })
c46edbc2
C
88 } else {
89 orderedCollectionPagination['totalItems'] = result.total
e4f97bab
C
90 }
91
c986175d 92 return orderedCollectionPagination
e4f97bab
C
93}
94
50d6de9c 95function buildSignedActivity (byActor: ActorModel, data: Object) {
afffe988
C
96 const activity = activityPubContextify(data)
97
50d6de9c 98 return signObject(byActor, activity) as Promise<Activity>
afffe988
C
99}
100
6be84cbc
C
101function getActorUrl (activityActor: string | ActivityPubActor) {
102 if (typeof activityActor === 'string') return activityActor
103
104 return activityActor.id
105}
106
e4f97bab
C
107// ---------------------------------------------------------------------------
108
109export {
6be84cbc 110 getActorUrl,
e4f97bab 111 activityPubContextify,
0d0e8dd0 112 activityPubCollectionPagination,
16b90975 113 activityPubCollection,
892211e8 114 buildSignedActivity
e4f97bab 115}