mirror of
https://github.com/Ocelot-Social-Community/Ocelot-Social.git
synced 2025-12-13 07:46:06 +00:00
Fix resolve function returns undefind
I'm also trying to make sense of the attribute `sources` - probably it's indicating where the data is coming from.
This commit is contained in:
parent
3d7a30d419
commit
657a5ac1f5
10042
backend/src/jest/snapshots/embeds/pr960.html
Normal file
10042
backend/src/jest/snapshots/embeds/pr960.html
Normal file
File diff suppressed because it is too large
Load Diff
@ -52,10 +52,10 @@ export default schema => {
|
||||
if (CONFIG.DISABLED_MIDDLEWARES) {
|
||||
const disabledMiddlewares = CONFIG.DISABLED_MIDDLEWARES.split(',')
|
||||
order = order.filter(key => {
|
||||
if(disabledMiddlewares.includes(key)) console.log(`Warning: Disabled "${disabledMiddlewares}" middleware.`)
|
||||
return !disabledMiddlewares.includes(key)
|
||||
})
|
||||
/* eslint-disable-next-line no-console */
|
||||
console.log(`Warning: "${disabledMiddlewares}" middlewares have been disabled.`)
|
||||
}
|
||||
|
||||
const appliedMiddlewares = order.map(key => middlewares[key])
|
||||
|
||||
@ -1,15 +1,19 @@
|
||||
import fetch from 'node-fetch'
|
||||
|
||||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
import { createTestClient } from 'apollo-server-testing'
|
||||
import createServer from '../../server'
|
||||
import { gql } from '../../jest/helpers'
|
||||
|
||||
jest.mock('node-fetch')
|
||||
const { Response } = jest.requireActual('node-fetch')
|
||||
|
||||
let variables = {}
|
||||
|
||||
const pr960 = fs.readFileSync(
|
||||
path.join(__dirname, '../../jest/snapshots/embeds/pr960.html'),
|
||||
'utf8',
|
||||
)
|
||||
const babyLovesCat = fs.readFileSync(
|
||||
path.join(__dirname, '../../jest/snapshots/embeds/babyLovesCat.html'),
|
||||
'utf8',
|
||||
@ -67,6 +71,39 @@ describe('Query', () => {
|
||||
}
|
||||
})
|
||||
|
||||
describe('given a Github link', () => {
|
||||
beforeEach(() => {
|
||||
fetch
|
||||
.mockReturnValueOnce(Promise.resolve(new Response(pr960)))
|
||||
variables = { url: "https://github.com/Human-Connection/Human-Connection/pull/960" }
|
||||
})
|
||||
|
||||
it('returns meta data even if no embed html can be retrieved', async () => {
|
||||
const expected = expect.objectContaining({
|
||||
data: {
|
||||
embed: {
|
||||
"type": "link",
|
||||
"title": "Editor embeds merge in nitro embed by mattwr18 · Pull Request #960 · Human-Connection/Human-Connection",
|
||||
"author": "Human-Connection",
|
||||
"publisher": "GitHub",
|
||||
"date": "2019-07-21T00:56:00.000Z",
|
||||
"description": "🍰 Pullrequest Issues fixes #256",
|
||||
"url": "https://github.com/Human-Connection/Human-Connection/pull/960",
|
||||
"image": "https://repository-images.githubusercontent.com/112590397/52c9a000-7e11-11e9-899d-aaa55f3a3d72",
|
||||
"audio": null,
|
||||
"video": null,
|
||||
"lang": "en",
|
||||
"sources": [
|
||||
"resource"
|
||||
],
|
||||
"html": null
|
||||
},
|
||||
},
|
||||
})
|
||||
await expect(embedAction(variables)).resolves.toEqual(expected)
|
||||
})
|
||||
})
|
||||
|
||||
describe('given a youtube link', () => {
|
||||
beforeEach(() => {
|
||||
fetch
|
||||
@ -75,7 +112,7 @@ describe('Query', () => {
|
||||
variables = { url: 'https://www.youtube.com/watch?v=qkdXAtO40Fo&t=18s' }
|
||||
})
|
||||
|
||||
it('returns meta data', async () => {
|
||||
it('returns meta data plus youtube iframe html', async () => {
|
||||
const expected = expect.objectContaining({
|
||||
data: {
|
||||
embed: {
|
||||
|
||||
@ -49,26 +49,23 @@ const fetchEmbed = async url => {
|
||||
endpointUrl.searchParams.append('format', 'json')
|
||||
const response = await fetch(endpointUrl)
|
||||
const json = await response.json()
|
||||
const {
|
||||
type = 'link',
|
||||
html,
|
||||
author_name, // eslint-disable-line camelcase
|
||||
upload_date, // eslint-disable-line camelcase
|
||||
sources = ['oembed'],
|
||||
} = json
|
||||
|
||||
return { type, html, author: author_name, date: upload_date, sources }
|
||||
}
|
||||
|
||||
const fetchMeta = async url => {
|
||||
const response = await fetch(url)
|
||||
const html = await response.text()
|
||||
const metadata = await metascraper({ html, url })
|
||||
|
||||
return {
|
||||
type: 'link',
|
||||
type: json.type,
|
||||
html: json.html,
|
||||
author: json.author_name,
|
||||
date: json.upload_date,
|
||||
sources: ['oembed'],
|
||||
}
|
||||
}
|
||||
|
||||
const fetchResource = async url => {
|
||||
const response = await fetch(url)
|
||||
const html = await response.text()
|
||||
const resource = await metascraper({ html, url })
|
||||
return {
|
||||
sources: ['resource'],
|
||||
...metadata,
|
||||
...resource
|
||||
}
|
||||
}
|
||||
|
||||
@ -79,8 +76,7 @@ export default async function scrape(url) {
|
||||
url.hostname = 'youtube.com'
|
||||
}
|
||||
|
||||
const [meta, embed] = await Promise.all([fetchMeta(url), fetchEmbed(url)])
|
||||
|
||||
const [meta, embed] = await Promise.all([fetchResource(url), fetchEmbed(url)])
|
||||
const output = mergeWith(meta, embed, (objValue, srcValue) => {
|
||||
if (isArray(objValue)) {
|
||||
return objValue.concat(srcValue)
|
||||
@ -91,5 +87,23 @@ export default async function scrape(url) {
|
||||
throw new ApolloError('Not found', 'NOT_FOUND')
|
||||
}
|
||||
|
||||
return output
|
||||
const defaults = {
|
||||
type: 'link',
|
||||
title: null,
|
||||
author: null,
|
||||
publisher: null,
|
||||
date: null,
|
||||
description: null,
|
||||
url: null,
|
||||
image: null,
|
||||
audio: null,
|
||||
video: null,
|
||||
lang: null,
|
||||
html: null,
|
||||
}
|
||||
|
||||
return {
|
||||
...defaults,
|
||||
...output
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user