From 571389d43b8fc8aaf27e77c06f19b320b08dbbc9 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 10 Nov 2017 17:27:49 +0100 Subject: Make it compile at least --- server/helpers/activitypub.ts | 46 +++++++++++++++++------- server/helpers/requests.ts | 83 ++----------------------------------------- server/helpers/webfinger.ts | 2 +- 3 files changed, 36 insertions(+), 95 deletions(-) (limited to 'server/helpers') 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 @@ +import { join } from 'path' +import * as request from 'request' import * as url from 'url' - -import { database as db } from '../initializers' -import { logger } from './logger' -import { doRequest, doRequestAndSaveToFile } from './requests' -import { isRemoteAccountValid } from './custom-validators' +import { ActivityIconObject } from '../../shared/index' import { ActivityPubActor } from '../../shared/models/activitypub/activitypub-actor' import { ResultList } from '../../shared/models/result-list.model' -import { CONFIG } from '../initializers/constants' +import { database as db, REMOTE_SCHEME } from '../initializers' +import { CONFIG, STATIC_PATHS } from '../initializers/constants' import { VideoInstance } from '../models/video/video-interface' -import { ActivityIconObject } from '../../shared/index' -import { join } from 'path' +import { isRemoteAccountValid } from './custom-validators' +import { logger } from './logger' +import { doRequest, doRequestAndSaveToFile } from './requests' function generateThumbnailFromUrl (video: VideoInstance, icon: ActivityIconObject) { const thumbnailName = video.getThumbnailName() @@ -22,9 +22,10 @@ function generateThumbnailFromUrl (video: VideoInstance, icon: ActivityIconObjec return doRequestAndSaveToFile(options, thumbnailPath) } -function getActivityPubUrl (type: 'video' | 'videoChannel', uuid: string) { - if (type === 'video') return CONFIG.WEBSERVER.URL + '/videos/watch/' + uuid - else if (type === 'videoChannel') return CONFIG.WEBSERVER.URL + '/video-channels/' + uuid +function getActivityPubUrl (type: 'video' | 'videoChannel' | 'account', id: string) { + if (type === 'video') return CONFIG.WEBSERVER.URL + '/videos/watch/' + id + else if (type === 'videoChannel') return CONFIG.WEBSERVER.URL + '/video-channels/' + id + else if (type === 'account') return CONFIG.WEBSERVER.URL + '/account/' + id return '' } @@ -94,7 +95,24 @@ async function fetchRemoteAccountAndCreatePod (accountUrl: string) { return { account, pod } } -function activityPubContextify (data: object) { +function fetchRemoteVideoPreview (video: VideoInstance) { + // FIXME: use url + const host = video.VideoChannel.Account.Pod.host + const path = join(STATIC_PATHS.PREVIEWS, video.getPreviewName()) + + return request.get(REMOTE_SCHEME.HTTP + '://' + host + path) +} + +async function fetchRemoteVideoDescription (video: VideoInstance) { + const options = { + uri: video.url + } + + const { body } = await doRequest(options) + return body.description ? body.description : '' +} + +function activityPubContextify (data: T) { return Object.assign(data,{ '@context': [ 'https://www.w3.org/ns/activitystreams', @@ -141,7 +159,9 @@ export { activityPubCollectionPagination, getActivityPubUrl, generateThumbnailFromUrl, - getOrCreateAccount + getOrCreateAccount, + fetchRemoteVideoPreview, + fetchRemoteVideoDescription } // --------------------------------------------------------------------------- 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 @@ -import * as replay from 'request-replay' -import * as request from 'request' import * as Promise from 'bluebird' - -import { - RETRY_REQUESTS, - REMOTE_SCHEME, - CONFIG -} from '../initializers' -import { PodInstance } from '../models' -import { PodSignature } from '../../shared' -import { signObject } from './peertube-crypto' import { createWriteStream } from 'fs' +import * as request from 'request' function doRequest (requestOptions: request.CoreOptions & request.UriOptions) { return new Promise<{ response: request.RequestResponse, body: any }>((res, rej) => { @@ -27,78 +17,9 @@ function doRequestAndSaveToFile (requestOptions: request.CoreOptions & request.U }) } -type MakeRetryRequestParams = { - url: string, - method: 'GET' | 'POST', - json: Object -} -function makeRetryRequest (params: MakeRetryRequestParams) { - return new Promise<{ response: request.RequestResponse, body: any }>((res, rej) => { - replay( - request(params, (err, response, body) => err ? rej(err) : res({ response, body })), - { - retries: RETRY_REQUESTS, - factor: 3, - maxTimeout: Infinity, - errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ] - } - ) - }) -} - -type MakeSecureRequestParams = { - toPod: PodInstance - path: string - data?: Object -} -function makeSecureRequest (params: MakeSecureRequestParams) { - const requestParams: { - method: 'POST', - uri: string, - json: { - signature: PodSignature, - data: any - } - } = { - method: 'POST', - uri: REMOTE_SCHEME.HTTP + '://' + params.toPod.host + params.path, - json: { - signature: null, - data: null - } - } - - const host = CONFIG.WEBSERVER.HOST - - let dataToSign - if (params.data) { - dataToSign = params.data - } else { - // We do not have data to sign so we just take our host - // It is not ideal but the connection should be in HTTPS - dataToSign = host - } - - sign(dataToSign).then(signature => { - requestParams.json.signature = { - host, // Which host we pretend to be - signature - } - - // If there are data information - if (params.data) { - requestParams.json.data = params.data - } - - return doRequest(requestParams) - }) -} - // --------------------------------------------------------------------------- export { doRequest, - doRequestAndSaveToFile, - makeRetryRequest, - makeSecureRequest + doRequestAndSaveToFile } 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 { function webfingerLookup (url: string) { return new Promise((res, rej) => { - webfinger.lookup('nick@silverbucket.net', (err, p) => { + webfinger.lookup(url, (err, p) => { if (err) return rej(err) return p -- cgit v1.2.3