]>
Commit | Line | Data |
---|---|---|
1 | import * as Bluebird from 'bluebird' | |
2 | import validator from 'validator' | |
3 | import { ResultList } from '../../shared/models' | |
4 | import { Activity } from '../../shared/models/activitypub' | |
5 | import { ACTIVITY_PUB, REMOTE_SCHEME } from '../initializers/constants' | |
6 | import { signJsonLDObject } from './peertube-crypto' | |
7 | import { pageToStartAndCount } from './core-utils' | |
8 | import { URL } from 'url' | |
9 | import { MActor, MVideoAccountLight } from '../typings/models' | |
10 | ||
11 | export type ContextType = 'All' | 'View' | 'Announce' | 'CacheFile' | |
12 | ||
13 | function 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 | ] | |
21 | ||
22 | if (type !== 'View' && type !== 'Announce') { | |
23 | const additional = { | |
24 | pt: 'https://joinpeertube.org/ns#', | |
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) | |
113 | } | |
114 | ||
115 | return { | |
116 | '@context': context | |
117 | } | |
118 | } | |
119 | ||
120 | function activityPubContextify <T> (data: T, type: ContextType = 'All') { | |
121 | return Object.assign({}, data, getContextData(type)) | |
122 | } | |
123 | ||
124 | type ActivityPubCollectionPaginationHandler = (start: number, count: number) => Bluebird<ResultList<any>> | Promise<ResultList<any>> | |
125 | async function activityPubCollectionPagination ( | |
126 | baseUrl: string, | |
127 | handler: ActivityPubCollectionPaginationHandler, | |
128 | page?: any, | |
129 | size = ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE | |
130 | ) { | |
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 { | |
136 | id: baseUrl, | |
137 | type: 'OrderedCollectionPage', | |
138 | totalItems: result.total, | |
139 | first: baseUrl + '?page=1' | |
140 | } | |
141 | } | |
142 | ||
143 | const { start, count } = pageToStartAndCount(page, size) | |
144 | const result = await handler(start, count) | |
145 | ||
146 | let next: string | undefined | |
147 | let prev: string | undefined | |
148 | ||
149 | // Assert page is a number | |
150 | page = parseInt(page, 10) | |
151 | ||
152 | // There are more results | |
153 | if (result.total > page * size) { | |
154 | next = baseUrl + '?page=' + (page + 1) | |
155 | } | |
156 | ||
157 | if (page > 1) { | |
158 | prev = baseUrl + '?page=' + (page - 1) | |
159 | } | |
160 | ||
161 | return { | |
162 | id: baseUrl + '?page=' + page, | |
163 | type: 'OrderedCollectionPage', | |
164 | prev, | |
165 | next, | |
166 | partOf: baseUrl, | |
167 | orderedItems: result.data, | |
168 | totalItems: result.total | |
169 | } | |
170 | ||
171 | } | |
172 | ||
173 | function buildSignedActivity (byActor: MActor, data: Object, contextType?: ContextType) { | |
174 | const activity = activityPubContextify(data, contextType) | |
175 | ||
176 | return signJsonLDObject(byActor, activity) as Promise<Activity> | |
177 | } | |
178 | ||
179 | function getAPId (activity: string | { id: string }) { | |
180 | if (typeof activity === 'string') return activity | |
181 | ||
182 | return activity.id | |
183 | } | |
184 | ||
185 | function checkUrlsSameHost (url1: string, url2: string) { | |
186 | const idHost = new URL(url1).host | |
187 | const actorHost = new URL(url2).host | |
188 | ||
189 | return idHost && actorHost && idHost.toLowerCase() === actorHost.toLowerCase() | |
190 | } | |
191 | ||
192 | function buildRemoteVideoBaseUrl (video: MVideoAccountLight, path: string) { | |
193 | const host = video.VideoChannel.Account.Actor.Server.host | |
194 | ||
195 | return REMOTE_SCHEME.HTTP + '://' + host + path | |
196 | } | |
197 | ||
198 | // --------------------------------------------------------------------------- | |
199 | ||
200 | export { | |
201 | checkUrlsSameHost, | |
202 | getAPId, | |
203 | activityPubContextify, | |
204 | activityPubCollectionPagination, | |
205 | buildSignedActivity, | |
206 | buildRemoteVideoBaseUrl | |
207 | } |