diff options
Diffstat (limited to 'server/helpers')
-rw-r--r-- | server/helpers/activitypub.ts | 46 | ||||
-rw-r--r-- | server/helpers/requests.ts | 83 | ||||
-rw-r--r-- | server/helpers/webfinger.ts | 2 |
3 files changed, 36 insertions, 95 deletions
diff --git a/server/helpers/activitypub.ts b/server/helpers/activitypub.ts index 75de2278c..a1493e5c1 100644 --- a/server/helpers/activitypub.ts +++ b/server/helpers/activitypub.ts | |||
@@ -1,15 +1,15 @@ | |||
1 | import { join } from 'path' | ||
2 | import * as request from 'request' | ||
1 | import * as url from 'url' | 3 | import * as url from 'url' |
2 | 4 | import { ActivityIconObject } from '../../shared/index' | |
3 | import { database as db } from '../initializers' | ||
4 | import { logger } from './logger' | ||
5 | import { doRequest, doRequestAndSaveToFile } from './requests' | ||
6 | import { isRemoteAccountValid } from './custom-validators' | ||
7 | import { ActivityPubActor } from '../../shared/models/activitypub/activitypub-actor' | 5 | import { ActivityPubActor } from '../../shared/models/activitypub/activitypub-actor' |
8 | import { ResultList } from '../../shared/models/result-list.model' | 6 | import { ResultList } from '../../shared/models/result-list.model' |
9 | import { CONFIG } from '../initializers/constants' | 7 | import { database as db, REMOTE_SCHEME } from '../initializers' |
8 | import { CONFIG, STATIC_PATHS } from '../initializers/constants' | ||
10 | import { VideoInstance } from '../models/video/video-interface' | 9 | import { VideoInstance } from '../models/video/video-interface' |
11 | import { ActivityIconObject } from '../../shared/index' | 10 | import { isRemoteAccountValid } from './custom-validators' |
12 | import { join } from 'path' | 11 | import { logger } from './logger' |
12 | import { doRequest, doRequestAndSaveToFile } from './requests' | ||
13 | 13 | ||
14 | function generateThumbnailFromUrl (video: VideoInstance, icon: ActivityIconObject) { | 14 | function generateThumbnailFromUrl (video: VideoInstance, icon: ActivityIconObject) { |
15 | const thumbnailName = video.getThumbnailName() | 15 | const thumbnailName = video.getThumbnailName() |
@@ -22,9 +22,10 @@ function generateThumbnailFromUrl (video: VideoInstance, icon: ActivityIconObjec | |||
22 | return doRequestAndSaveToFile(options, thumbnailPath) | 22 | return doRequestAndSaveToFile(options, thumbnailPath) |
23 | } | 23 | } |
24 | 24 | ||
25 | function getActivityPubUrl (type: 'video' | 'videoChannel', uuid: string) { | 25 | function getActivityPubUrl (type: 'video' | 'videoChannel' | 'account', id: string) { |
26 | if (type === 'video') return CONFIG.WEBSERVER.URL + '/videos/watch/' + uuid | 26 | if (type === 'video') return CONFIG.WEBSERVER.URL + '/videos/watch/' + id |
27 | else if (type === 'videoChannel') return CONFIG.WEBSERVER.URL + '/video-channels/' + uuid | 27 | else if (type === 'videoChannel') return CONFIG.WEBSERVER.URL + '/video-channels/' + id |
28 | else if (type === 'account') return CONFIG.WEBSERVER.URL + '/account/' + id | ||
28 | 29 | ||
29 | return '' | 30 | return '' |
30 | } | 31 | } |
@@ -94,7 +95,24 @@ async function fetchRemoteAccountAndCreatePod (accountUrl: string) { | |||
94 | return { account, pod } | 95 | return { account, pod } |
95 | } | 96 | } |
96 | 97 | ||
97 | function activityPubContextify (data: object) { | 98 | function fetchRemoteVideoPreview (video: VideoInstance) { |
99 | // FIXME: use url | ||
100 | const host = video.VideoChannel.Account.Pod.host | ||
101 | const path = join(STATIC_PATHS.PREVIEWS, video.getPreviewName()) | ||
102 | |||
103 | return request.get(REMOTE_SCHEME.HTTP + '://' + host + path) | ||
104 | } | ||
105 | |||
106 | async function fetchRemoteVideoDescription (video: VideoInstance) { | ||
107 | const options = { | ||
108 | uri: video.url | ||
109 | } | ||
110 | |||
111 | const { body } = await doRequest(options) | ||
112 | return body.description ? body.description : '' | ||
113 | } | ||
114 | |||
115 | function activityPubContextify <T> (data: T) { | ||
98 | return Object.assign(data,{ | 116 | return Object.assign(data,{ |
99 | '@context': [ | 117 | '@context': [ |
100 | 'https://www.w3.org/ns/activitystreams', | 118 | 'https://www.w3.org/ns/activitystreams', |
@@ -141,7 +159,9 @@ export { | |||
141 | activityPubCollectionPagination, | 159 | activityPubCollectionPagination, |
142 | getActivityPubUrl, | 160 | getActivityPubUrl, |
143 | generateThumbnailFromUrl, | 161 | generateThumbnailFromUrl, |
144 | getOrCreateAccount | 162 | getOrCreateAccount, |
163 | fetchRemoteVideoPreview, | ||
164 | fetchRemoteVideoDescription | ||
145 | } | 165 | } |
146 | 166 | ||
147 | // --------------------------------------------------------------------------- | 167 | // --------------------------------------------------------------------------- |
diff --git a/server/helpers/requests.ts b/server/helpers/requests.ts index 31cedd768..4b1deeadc 100644 --- a/server/helpers/requests.ts +++ b/server/helpers/requests.ts | |||
@@ -1,16 +1,6 @@ | |||
1 | import * as replay from 'request-replay' | ||
2 | import * as request from 'request' | ||
3 | import * as Promise from 'bluebird' | 1 | import * as Promise from 'bluebird' |
4 | |||
5 | import { | ||
6 | RETRY_REQUESTS, | ||
7 | REMOTE_SCHEME, | ||
8 | CONFIG | ||
9 | } from '../initializers' | ||
10 | import { PodInstance } from '../models' | ||
11 | import { PodSignature } from '../../shared' | ||
12 | import { signObject } from './peertube-crypto' | ||
13 | import { createWriteStream } from 'fs' | 2 | import { createWriteStream } from 'fs' |
3 | import * as request from 'request' | ||
14 | 4 | ||
15 | function doRequest (requestOptions: request.CoreOptions & request.UriOptions) { | 5 | function doRequest (requestOptions: request.CoreOptions & request.UriOptions) { |
16 | return new Promise<{ response: request.RequestResponse, body: any }>((res, rej) => { | 6 | return new Promise<{ response: request.RequestResponse, body: any }>((res, rej) => { |
@@ -27,78 +17,9 @@ function doRequestAndSaveToFile (requestOptions: request.CoreOptions & request.U | |||
27 | }) | 17 | }) |
28 | } | 18 | } |
29 | 19 | ||
30 | type MakeRetryRequestParams = { | ||
31 | url: string, | ||
32 | method: 'GET' | 'POST', | ||
33 | json: Object | ||
34 | } | ||
35 | function makeRetryRequest (params: MakeRetryRequestParams) { | ||
36 | return new Promise<{ response: request.RequestResponse, body: any }>((res, rej) => { | ||
37 | replay( | ||
38 | request(params, (err, response, body) => err ? rej(err) : res({ response, body })), | ||
39 | { | ||
40 | retries: RETRY_REQUESTS, | ||
41 | factor: 3, | ||
42 | maxTimeout: Infinity, | ||
43 | errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ] | ||
44 | } | ||
45 | ) | ||
46 | }) | ||
47 | } | ||
48 | |||
49 | type MakeSecureRequestParams = { | ||
50 | toPod: PodInstance | ||
51 | path: string | ||
52 | data?: Object | ||
53 | } | ||
54 | function makeSecureRequest (params: MakeSecureRequestParams) { | ||
55 | const requestParams: { | ||
56 | method: 'POST', | ||
57 | uri: string, | ||
58 | json: { | ||
59 | signature: PodSignature, | ||
60 | data: any | ||
61 | } | ||
62 | } = { | ||
63 | method: 'POST', | ||
64 | uri: REMOTE_SCHEME.HTTP + '://' + params.toPod.host + params.path, | ||
65 | json: { | ||
66 | signature: null, | ||
67 | data: null | ||
68 | } | ||
69 | } | ||
70 | |||
71 | const host = CONFIG.WEBSERVER.HOST | ||
72 | |||
73 | let dataToSign | ||
74 | if (params.data) { | ||
75 | dataToSign = params.data | ||
76 | } else { | ||
77 | // We do not have data to sign so we just take our host | ||
78 | // It is not ideal but the connection should be in HTTPS | ||
79 | dataToSign = host | ||
80 | } | ||
81 | |||
82 | sign(dataToSign).then(signature => { | ||
83 | requestParams.json.signature = { | ||
84 | host, // Which host we pretend to be | ||
85 | signature | ||
86 | } | ||
87 | |||
88 | // If there are data information | ||
89 | if (params.data) { | ||
90 | requestParams.json.data = params.data | ||
91 | } | ||
92 | |||
93 | return doRequest(requestParams) | ||
94 | }) | ||
95 | } | ||
96 | |||
97 | // --------------------------------------------------------------------------- | 20 | // --------------------------------------------------------------------------- |
98 | 21 | ||
99 | export { | 22 | export { |
100 | doRequest, | 23 | doRequest, |
101 | doRequestAndSaveToFile, | 24 | doRequestAndSaveToFile |
102 | makeRetryRequest, | ||
103 | makeSecureRequest | ||
104 | } | 25 | } |
diff --git a/server/helpers/webfinger.ts b/server/helpers/webfinger.ts index 9586fa562..164ae4951 100644 --- a/server/helpers/webfinger.ts +++ b/server/helpers/webfinger.ts | |||
@@ -35,7 +35,7 @@ export { | |||
35 | 35 | ||
36 | function webfingerLookup (url: string) { | 36 | function webfingerLookup (url: string) { |
37 | return new Promise<WebFingerData>((res, rej) => { | 37 | return new Promise<WebFingerData>((res, rej) => { |
38 | webfinger.lookup('nick@silverbucket.net', (err, p) => { | 38 | webfinger.lookup(url, (err, p) => { |
39 | if (err) return rej(err) | 39 | if (err) return rej(err) |
40 | 40 | ||
41 | return p | 41 | return p |