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