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