]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/activitypub.ts
Don't rehost announced video activities
[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',
18 'nsfw': 'as:sensitive',
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
16b90975
C
28function activityPubCollection (results: any[]) {
29 return {
30 type: 'OrderedCollection',
31 totalItems: results.length,
32 orderedItems: results
33 }
34}
35
c46edbc2 36function activityPubCollectionPagination (url: string, page: any, result: ResultList<any>) {
e71bcc0f
C
37 let next: string
38 let prev: string
39
c46edbc2
C
40 // Assert page is a number
41 page = parseInt(page, 10)
42
e71bcc0f 43 // There are more results
c46edbc2 44 if (result.total > page * ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) {
e71bcc0f
C
45 next = url + '?page=' + (page + 1)
46 }
47
48 if (page > 1) {
49 prev = url + '?page=' + (page - 1)
50 }
51
52 const orderedCollectionPagination = {
53 id: url + '?page=' + page,
54 type: 'OrderedCollectionPage',
55 prev,
56 next,
57 partOf: url,
58 orderedItems: result.data
59 }
e4f97bab 60
c986175d
C
61 if (page === 1) {
62 return activityPubContextify({
63 id: url,
64 type: 'OrderedCollection',
65 totalItems: result.total,
66 first: orderedCollectionPagination
67 })
c46edbc2
C
68 } else {
69 orderedCollectionPagination['totalItems'] = result.total
e4f97bab
C
70 }
71
c986175d 72 return orderedCollectionPagination
e4f97bab
C
73}
74
50d6de9c 75function buildSignedActivity (byActor: ActorModel, data: Object) {
afffe988
C
76 const activity = activityPubContextify(data)
77
50d6de9c 78 return signObject(byActor, activity) as Promise<Activity>
afffe988
C
79}
80
e4f97bab
C
81// ---------------------------------------------------------------------------
82
83export {
e4f97bab 84 activityPubContextify,
0d0e8dd0 85 activityPubCollectionPagination,
16b90975 86 activityPubCollection,
892211e8 87 buildSignedActivity
e4f97bab 88}