]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/activitypub.ts
Redirect to uuid video route after upload
[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',
ce33ee01 21 'size': 'http://schema.org/Number'
e4f97bab
C
22 }
23 ]
24 })
25}
26
16b90975
C
27function activityPubCollection (results: any[]) {
28 return {
29 type: 'OrderedCollection',
30 totalItems: results.length,
31 orderedItems: results
32 }
33}
34
c46edbc2 35function activityPubCollectionPagination (url: string, page: any, result: ResultList<any>) {
e71bcc0f
C
36 let next: string
37 let prev: string
38
c46edbc2
C
39 // Assert page is a number
40 page = parseInt(page, 10)
41
e71bcc0f 42 // There are more results
c46edbc2 43 if (result.total > page * ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) {
e71bcc0f
C
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 }
e4f97bab 59
c986175d
C
60 if (page === 1) {
61 return activityPubContextify({
62 id: url,
63 type: 'OrderedCollection',
64 totalItems: result.total,
65 first: orderedCollectionPagination
66 })
c46edbc2
C
67 } else {
68 orderedCollectionPagination['totalItems'] = result.total
e4f97bab
C
69 }
70
c986175d 71 return orderedCollectionPagination
e4f97bab
C
72}
73
50d6de9c 74function buildSignedActivity (byActor: ActorModel, data: Object) {
afffe988
C
75 const activity = activityPubContextify(data)
76
50d6de9c 77 return signObject(byActor, activity) as Promise<Activity>
afffe988
C
78}
79
e4f97bab
C
80// ---------------------------------------------------------------------------
81
82export {
e4f97bab 83 activityPubContextify,
0d0e8dd0 84 activityPubCollectionPagination,
16b90975 85 activityPubCollection,
892211e8 86 buildSignedActivity
e4f97bab 87}