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