]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/activitypub.ts
Optimize view endpoint
[github/Chocobozzz/PeerTube.git] / server / helpers / activitypub.ts
CommitLineData
8fffe21a 1import * as Bluebird from 'bluebird'
7cde3b9c 2import validator from 'validator'
3fd3ab2d 3import { ResultList } from '../../shared/models'
361805c4 4import { Activity } from '../../shared/models/activitypub'
ca6d3622 5import { ACTIVITY_PUB, REMOTE_SCHEME } from '../initializers/constants'
f7509cbe 6import { signJsonLDObject } from './peertube-crypto'
8fffe21a 7import { pageToStartAndCount } from './core-utils'
a1587156 8import { URL } from 'url'
ca6d3622 9import { MActor, MVideoAccountLight } from '../typings/models'
571389d4 10
598edb8a
C
11export type ContextType = 'All' | 'View' | 'Announce'
12
13function activityPubContextify <T> (data: T, type: ContextType = 'All') {
14 const base = {
15 RsaSignature2017: 'https://w3id.org/security#RsaSignature2017'
16 }
17
18 if (type === 'All') {
19 Object.assign(base, {
20 pt: 'https://joinpeertube.org/ns#',
21 sc: 'http://schema.org#',
22 Hashtag: 'as:Hashtag',
23 uuid: 'sc:identifier',
24 category: 'sc:category',
25 licence: 'sc:license',
26 subtitleLanguage: 'sc:subtitleLanguage',
27 sensitive: 'as:sensitive',
28 language: 'sc:inLanguage',
29 expires: 'sc:expires',
30 CacheFile: 'pt:CacheFile',
31 Infohash: 'pt:Infohash',
32 originallyPublishedAt: 'sc:datePublished',
33 views: {
34 '@type': 'sc:Number',
35 '@id': 'pt:views'
36 },
37 state: {
38 '@type': 'sc:Number',
39 '@id': 'pt:state'
40 },
41 size: {
42 '@type': 'sc:Number',
43 '@id': 'pt:size'
44 },
45 fps: {
46 '@type': 'sc:Number',
47 '@id': 'pt:fps'
48 },
49 startTimestamp: {
50 '@type': 'sc:Number',
51 '@id': 'pt:startTimestamp'
52 },
53 stopTimestamp: {
54 '@type': 'sc:Number',
55 '@id': 'pt:stopTimestamp'
56 },
57 position: {
58 '@type': 'sc:Number',
59 '@id': 'pt:position'
60 },
61 commentsEnabled: {
62 '@type': 'sc:Boolean',
63 '@id': 'pt:commentsEnabled'
64 },
65 downloadEnabled: {
66 '@type': 'sc:Boolean',
67 '@id': 'pt:downloadEnabled'
68 },
69 waitTranscoding: {
70 '@type': 'sc:Boolean',
71 '@id': 'pt:waitTranscoding'
72 },
73 support: {
74 '@type': 'sc:Text',
75 '@id': 'pt:support'
76 },
77 likes: {
78 '@id': 'as:likes',
79 '@type': '@id'
80 },
81 dislikes: {
82 '@id': 'as:dislikes',
83 '@type': '@id'
84 },
85 playlists: {
86 '@id': 'pt:playlists',
87 '@type': '@id'
88 },
89 shares: {
90 '@id': 'as:shares',
91 '@type': '@id'
92 },
93 comments: {
94 '@id': 'as:comments',
95 '@type': '@id'
96 }
97 })
98 }
99
100 return Object.assign({}, data, {
e4f97bab
C
101 '@context': [
102 'https://www.w3.org/ns/activitystreams',
103 'https://w3id.org/security/v1',
598edb8a 104 base
e4f97bab
C
105 ]
106 })
107}
108
8fffe21a 109type ActivityPubCollectionPaginationHandler = (start: number, count: number) => Bluebird<ResultList<any>> | Promise<ResultList<any>>
fbc77eb6
RK
110async function activityPubCollectionPagination (
111 baseUrl: string,
112 handler: ActivityPubCollectionPaginationHandler,
113 page?: any,
114 size = ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE
115) {
8fffe21a
C
116 if (!page || !validator.isInt(page)) {
117 // We just display the first page URL, we only need the total items
118 const result = await handler(0, 1)
119
120 return {
babecc3c 121 id: baseUrl,
418d092a 122 type: 'OrderedCollectionPage',
8fffe21a 123 totalItems: result.total,
babecc3c 124 first: baseUrl + '?page=1'
8fffe21a 125 }
16b90975 126 }
16b90975 127
fbc77eb6 128 const { start, count } = pageToStartAndCount(page, size)
8fffe21a
C
129 const result = await handler(start, count)
130
c1e791ba
RK
131 let next: string | undefined
132 let prev: string | undefined
e71bcc0f 133
c46edbc2
C
134 // Assert page is a number
135 page = parseInt(page, 10)
136
e71bcc0f 137 // There are more results
fbc77eb6 138 if (result.total > page * size) {
babecc3c 139 next = baseUrl + '?page=' + (page + 1)
e71bcc0f
C
140 }
141
142 if (page > 1) {
babecc3c 143 prev = baseUrl + '?page=' + (page - 1)
e71bcc0f
C
144 }
145
8fffe21a 146 return {
babecc3c 147 id: baseUrl + '?page=' + page,
e71bcc0f
C
148 type: 'OrderedCollectionPage',
149 prev,
150 next,
babecc3c 151 partOf: baseUrl,
8fffe21a
C
152 orderedItems: result.data,
153 totalItems: result.total
e4f97bab
C
154 }
155
e4f97bab
C
156}
157
598edb8a
C
158function buildSignedActivity (byActor: MActor, data: Object, contextType?: ContextType) {
159 const activity = activityPubContextify(data, contextType)
afffe988 160
f7509cbe 161 return signJsonLDObject(byActor, activity) as Promise<Activity>
afffe988
C
162}
163
848f499d 164function getAPId (activity: string | { id: string }) {
361805c4 165 if (typeof activity === 'string') return activity
6be84cbc 166
361805c4 167 return activity.id
6be84cbc
C
168}
169
5c6d985f 170function checkUrlsSameHost (url1: string, url2: string) {
a1587156
C
171 const idHost = new URL(url1).host
172 const actorHost = new URL(url2).host
5c6d985f
C
173
174 return idHost && actorHost && idHost.toLowerCase() === actorHost.toLowerCase()
6be84cbc
C
175}
176
ca6d3622
C
177function buildRemoteVideoBaseUrl (video: MVideoAccountLight, path: string) {
178 const host = video.VideoChannel.Account.Actor.Server.host
179
180 return REMOTE_SCHEME.HTTP + '://' + host + path
181}
182
e4f97bab
C
183// ---------------------------------------------------------------------------
184
185export {
5c6d985f 186 checkUrlsSameHost,
848f499d 187 getAPId,
e4f97bab 188 activityPubContextify,
0d0e8dd0 189 activityPubCollectionPagination,
ca6d3622
C
190 buildSignedActivity,
191 buildRemoteVideoBaseUrl
e4f97bab 192}