]>
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' | |
12 | ||
13 | function 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, { | |
101 | '@context': [ | |
102 | 'https://www.w3.org/ns/activitystreams', | |
103 | 'https://w3id.org/security/v1', | |
104 | base | |
105 | ] | |
106 | }) | |
107 | } | |
108 | ||
109 | type ActivityPubCollectionPaginationHandler = (start: number, count: number) => Bluebird<ResultList<any>> | Promise<ResultList<any>> | |
110 | async function activityPubCollectionPagination ( | |
111 | baseUrl: string, | |
112 | handler: ActivityPubCollectionPaginationHandler, | |
113 | page?: any, | |
114 | size = ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE | |
115 | ) { | |
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 { | |
121 | id: baseUrl, | |
122 | type: 'OrderedCollectionPage', | |
123 | totalItems: result.total, | |
124 | first: baseUrl + '?page=1' | |
125 | } | |
126 | } | |
127 | ||
128 | const { start, count } = pageToStartAndCount(page, size) | |
129 | const result = await handler(start, count) | |
130 | ||
131 | let next: string | undefined | |
132 | let prev: string | undefined | |
133 | ||
134 | // Assert page is a number | |
135 | page = parseInt(page, 10) | |
136 | ||
137 | // There are more results | |
138 | if (result.total > page * size) { | |
139 | next = baseUrl + '?page=' + (page + 1) | |
140 | } | |
141 | ||
142 | if (page > 1) { | |
143 | prev = baseUrl + '?page=' + (page - 1) | |
144 | } | |
145 | ||
146 | return { | |
147 | id: baseUrl + '?page=' + page, | |
148 | type: 'OrderedCollectionPage', | |
149 | prev, | |
150 | next, | |
151 | partOf: baseUrl, | |
152 | orderedItems: result.data, | |
153 | totalItems: result.total | |
154 | } | |
155 | ||
156 | } | |
157 | ||
158 | function buildSignedActivity (byActor: MActor, data: Object, contextType?: ContextType) { | |
159 | const activity = activityPubContextify(data, contextType) | |
160 | ||
161 | return signJsonLDObject(byActor, activity) as Promise<Activity> | |
162 | } | |
163 | ||
164 | function getAPId (activity: string | { id: string }) { | |
165 | if (typeof activity === 'string') return activity | |
166 | ||
167 | return activity.id | |
168 | } | |
169 | ||
170 | function checkUrlsSameHost (url1: string, url2: string) { | |
171 | const idHost = new URL(url1).host | |
172 | const actorHost = new URL(url2).host | |
173 | ||
174 | return idHost && actorHost && idHost.toLowerCase() === actorHost.toLowerCase() | |
175 | } | |
176 | ||
177 | function buildRemoteVideoBaseUrl (video: MVideoAccountLight, path: string) { | |
178 | const host = video.VideoChannel.Account.Actor.Server.host | |
179 | ||
180 | return REMOTE_SCHEME.HTTP + '://' + host + path | |
181 | } | |
182 | ||
183 | // --------------------------------------------------------------------------- | |
184 | ||
185 | export { | |
186 | checkUrlsSameHost, | |
187 | getAPId, | |
188 | activityPubContextify, | |
189 | activityPubCollectionPagination, | |
190 | buildSignedActivity, | |
191 | buildRemoteVideoBaseUrl | |
192 | } |