Skip to content

Commit

Permalink
fix: Don't send link previews without metadata, remove synchronous op…
Browse files Browse the repository at this point in the history
…enGraph (WEBAPP-6508) (backport) (#3159)
  • Loading branch information
ffflorian authored Oct 25, 2019
1 parent 25f6071 commit c8b2834
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 33 deletions.
3 changes: 1 addition & 2 deletions electron/renderer/static/webview-preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,11 @@ const checkAvailability = callback => {
const _clearImmediate = clearImmediate;
const _setImmediate = setImmediate;
process.once('loaded', () => {
const {getOpenGraphData, getOpenGraphDataAsync} = require('../../dist/lib/openGraph');
const {getOpenGraphDataAsync} = require('../../dist/lib/openGraph');

global.clearImmediate = _clearImmediate;
global.desktopCapturer = desktopCapturer;
global.environment = environment;
global.openGraph = getOpenGraphData;
global.openGraphAsync = getOpenGraphDataAsync;
global.setImmediate = _setImmediate;
});
Expand Down
35 changes: 4 additions & 31 deletions electron/src/lib/openGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ import {parse as parseUrl} from 'url';
import {getLogger} from '../logging/getLogger';
import {config} from '../settings/config';

type GetDataCallback = (error: Error | null, meta?: OpenGraphResult) => void;

const logger = getLogger(__filename);

axios.defaults.adapter = require('axios/lib/adapters/http'); // always use Node.js adapter
Expand Down Expand Up @@ -184,38 +182,13 @@ const updateMetaDataWithImage = (meta: OpenGraphResult, imageData?: string): Ope
return meta;
};

export const getOpenGraphData = async (url: string, callback: GetDataCallback): Promise<void> => {
try {
let meta = await fetchOpenGraphData(url);

if (Array.isArray(meta.image)) {
meta.image = meta.image[0];
}

if (typeof meta.image === 'object' && !Array.isArray(meta.image) && meta.image.url) {
const [imageUrl] = arrayify(meta.image.url);

const uri = await fetchImageAsBase64(imageUrl);
meta = updateMetaDataWithImage(meta, uri);
} else {
delete meta.image;
}

if (callback) {
callback(null, meta);
}
} catch (error) {
if (callback) {
callback(error);
} else {
logger.info(error);
}
}
};

export const getOpenGraphDataAsync = async (url: string): Promise<OpenGraphResult> => {
const metadata = await fetchOpenGraphData(url);

if (!metadata.description && !metadata.image && !metadata.type && !metadata.url) {
throw new Error('No openGraph data found');
}

if (Array.isArray(metadata.image)) {
metadata.image = metadata.image[0];
}
Expand Down

0 comments on commit c8b2834

Please sign in to comment.