]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/activitypub.ts
Merge branch 'release/beta-10' 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 { signObject } 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 Hashtag: 'as:Hashtag',
18 uuid: 'http://schema.org/identifier',
19 category: 'http://schema.org/category',
20 licence: 'http://schema.org/license',
21 subtitleLanguage: 'http://schema.org/subtitleLanguage',
22 sensitive: 'as:sensitive',
23 language: 'http://schema.org/inLanguage',
24 views: 'http://schema.org/Number',
25 stats: 'http://schema.org/Number',
26 size: 'http://schema.org/Number',
27 fps: 'http://schema.org/Number',
28 commentsEnabled: 'http://schema.org/Boolean',
29 waitTranscoding: 'http://schema.org/Boolean',
30 support: 'http://schema.org/Text'
31 },
32 {
33 likes: {
34 '@id': 'as:likes',
35 '@type': '@id'
36 },
37 dislikes: {
38 '@id': 'as:dislikes',
39 '@type': '@id'
40 },
41 shares: {
42 '@id': 'as:shares',
43 '@type': '@id'
44 },
45 comments: {
46 '@id': 'as:comments',
47 '@type': '@id'
48 }
49 }
50 ]
51 })
52 }
53
54 type ActivityPubCollectionPaginationHandler = (start: number, count: number) => Bluebird<ResultList<any>> | Promise<ResultList<any>>
55 async function activityPubCollectionPagination (url: string, handler: ActivityPubCollectionPaginationHandler, page?: any) {
56 if (!page || !validator.isInt(page)) {
57 // We just display the first page URL, we only need the total items
58 const result = await handler(0, 1)
59
60 return {
61 id: url,
62 type: 'OrderedCollection',
63 totalItems: result.total,
64 first: url + '?page=1'
65 }
66 }
67
68 const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
69 const result = await handler(start, count)
70
71 let next: string | undefined
72 let prev: string | undefined
73
74 // Assert page is a number
75 page = parseInt(page, 10)
76
77 // There are more results
78 if (result.total > page * ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) {
79 next = url + '?page=' + (page + 1)
80 }
81
82 if (page > 1) {
83 prev = url + '?page=' + (page - 1)
84 }
85
86 return {
87 id: url + '?page=' + page,
88 type: 'OrderedCollectionPage',
89 prev,
90 next,
91 partOf: url,
92 orderedItems: result.data,
93 totalItems: result.total
94 }
95
96 }
97
98 function buildSignedActivity (byActor: ActorModel, data: Object) {
99 const activity = activityPubContextify(data)
100
101 return signObject(byActor, activity) as Promise<Activity>
102 }
103
104 function getActorUrl (activityActor: string | ActivityPubActor) {
105 if (typeof activityActor === 'string') return activityActor
106
107 return activityActor.id
108 }
109
110 // ---------------------------------------------------------------------------
111
112 export {
113 getActorUrl,
114 activityPubContextify,
115 activityPubCollectionPagination,
116 buildSignedActivity
117 }