]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/activitypub.ts
Add subscriptions endpoints to REST API
[github/Chocobozzz/PeerTube.git] / server / helpers / activitypub.ts
CommitLineData
8fffe21a
C
1import * as Bluebird from 'bluebird'
2import * as validator from 'validator'
3fd3ab2d 3import { ResultList } from '../../shared/models'
6be84cbc 4import { Activity, ActivityPubActor } from '../../shared/models/activitypub'
3fd3ab2d 5import { ACTIVITY_PUB } from '../initializers'
50d6de9c 6import { ActorModel } from '../models/activitypub/actor'
afffe988 7import { signObject } from './peertube-crypto'
8fffe21a 8import { pageToStartAndCount } from './core-utils'
571389d4
C
9
10function activityPubContextify <T> (data: T) {
2186386c 11 return Object.assign(data, {
e4f97bab
C
12 '@context': [
13 'https://www.w3.org/ns/activitystreams',
14 'https://w3id.org/security/v1',
15 {
2186386c
C
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',
40e87e9e 21 subtitleLanguage: 'http://schema.org/subtitleLanguage',
2186386c
C
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',
b2977eec 27 fps: 'http://schema.org/Number',
2186386c
C
28 commentsEnabled: 'http://schema.org/Boolean',
29 waitTranscoding: 'http://schema.org/Boolean',
30 support: 'http://schema.org/Text'
2fe86927
C
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 }
e4f97bab
C
49 }
50 ]
51 })
52}
53
8fffe21a
C
54type ActivityPubCollectionPaginationHandler = (start: number, count: number) => Bluebird<ResultList<any>> | Promise<ResultList<any>>
55async 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 }
16b90975 66 }
16b90975 67
8fffe21a
C
68 const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
69 const result = await handler(start, count)
70
c1e791ba
RK
71 let next: string | undefined
72 let prev: string | undefined
e71bcc0f 73
c46edbc2
C
74 // Assert page is a number
75 page = parseInt(page, 10)
76
e71bcc0f 77 // There are more results
c46edbc2 78 if (result.total > page * ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) {
e71bcc0f
C
79 next = url + '?page=' + (page + 1)
80 }
81
82 if (page > 1) {
83 prev = url + '?page=' + (page - 1)
84 }
85
8fffe21a 86 return {
e71bcc0f
C
87 id: url + '?page=' + page,
88 type: 'OrderedCollectionPage',
89 prev,
90 next,
91 partOf: url,
8fffe21a
C
92 orderedItems: result.data,
93 totalItems: result.total
e4f97bab
C
94 }
95
e4f97bab
C
96}
97
50d6de9c 98function buildSignedActivity (byActor: ActorModel, data: Object) {
afffe988
C
99 const activity = activityPubContextify(data)
100
50d6de9c 101 return signObject(byActor, activity) as Promise<Activity>
afffe988
C
102}
103
6be84cbc
C
104function getActorUrl (activityActor: string | ActivityPubActor) {
105 if (typeof activityActor === 'string') return activityActor
106
107 return activityActor.id
108}
109
e4f97bab
C
110// ---------------------------------------------------------------------------
111
112export {
6be84cbc 113 getActorUrl,
e4f97bab 114 activityPubContextify,
0d0e8dd0 115 activityPubCollectionPagination,
892211e8 116 buildSignedActivity
e4f97bab 117}