]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/activitypub.ts
Merge branch 'master' into develop
[github/Chocobozzz/PeerTube.git] / server / helpers / activitypub.ts
1 import * as Bluebird from 'bluebird'
2 import * as validator from 'validator'
3 import { ResultList } from '../../shared/models'
4 import { Activity, ActivityPubActor } from '../../shared/models/activitypub'
5 import { ACTIVITY_PUB } from '../initializers'
6 import { ActorModel } from '../models/activitypub/actor'
7 import { signJsonLDObject } from './peertube-crypto'
8 import { pageToStartAndCount } from './core-utils'
9
10 function activityPubContextify <T> (data: T) {
11 return Object.assign(data, {
12 '@context': [
13 'https://www.w3.org/ns/activitystreams',
14 'https://w3id.org/security/v1',
15 {
16 RsaSignature2017: 'https://w3id.org/security#RsaSignature2017',
17 pt: 'https://joinpeertube.org/ns',
18 sc: 'http://schema.org#',
19 Hashtag: 'as:Hashtag',
20 uuid: 'sc:identifier',
21 category: 'sc:category',
22 licence: 'sc:license',
23 subtitleLanguage: 'sc:subtitleLanguage',
24 sensitive: 'as:sensitive',
25 language: 'sc:inLanguage',
26 views: 'sc:Number',
27 state: 'sc:Number',
28 size: 'sc:Number',
29 fps: 'sc:Number',
30 commentsEnabled: 'sc:Boolean',
31 waitTranscoding: 'sc:Boolean',
32 expires: 'sc:expires',
33 support: 'sc:Text',
34 CacheFile: 'pt:CacheFile'
35 },
36 {
37 likes: {
38 '@id': 'as:likes',
39 '@type': '@id'
40 },
41 dislikes: {
42 '@id': 'as:dislikes',
43 '@type': '@id'
44 },
45 shares: {
46 '@id': 'as:shares',
47 '@type': '@id'
48 },
49 comments: {
50 '@id': 'as:comments',
51 '@type': '@id'
52 }
53 }
54 ]
55 })
56 }
57
58 type ActivityPubCollectionPaginationHandler = (start: number, count: number) => Bluebird<ResultList<any>> | Promise<ResultList<any>>
59 async function activityPubCollectionPagination (url: string, handler: ActivityPubCollectionPaginationHandler, page?: any) {
60 if (!page || !validator.isInt(page)) {
61 // We just display the first page URL, we only need the total items
62 const result = await handler(0, 1)
63
64 return {
65 id: url,
66 type: 'OrderedCollection',
67 totalItems: result.total,
68 first: url + '?page=1'
69 }
70 }
71
72 const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
73 const result = await handler(start, count)
74
75 let next: string | undefined
76 let prev: string | undefined
77
78 // Assert page is a number
79 page = parseInt(page, 10)
80
81 // There are more results
82 if (result.total > page * ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) {
83 next = url + '?page=' + (page + 1)
84 }
85
86 if (page > 1) {
87 prev = url + '?page=' + (page - 1)
88 }
89
90 return {
91 id: url + '?page=' + page,
92 type: 'OrderedCollectionPage',
93 prev,
94 next,
95 partOf: url,
96 orderedItems: result.data,
97 totalItems: result.total
98 }
99
100 }
101
102 function buildSignedActivity (byActor: ActorModel, data: Object) {
103 const activity = activityPubContextify(data)
104
105 return signJsonLDObject(byActor, activity) as Promise<Activity>
106 }
107
108 function getActorUrl (activityActor: string | ActivityPubActor) {
109 if (typeof activityActor === 'string') return activityActor
110
111 return activityActor.id
112 }
113
114 // ---------------------------------------------------------------------------
115
116 export {
117 getActorUrl,
118 activityPubContextify,
119 activityPubCollectionPagination,
120 buildSignedActivity
121 }