From 17fcbac663734c937dc9d632e25cfdc9aca0fc5d Mon Sep 17 00:00:00 2001 From: herrmannplatz Date: Tue, 11 Jul 2017 20:35:45 +0200 Subject: [PATCH 1/6] refactor open graph module --- electron/js/lib/arrayify.js | 24 ---------- electron/js/lib/base64-image.js | 52 --------------------- electron/js/lib/datauri.js | 27 ----------- electron/js/lib/download.js | 2 - electron/js/lib/googleAuth.js | 2 - electron/js/lib/openGraph.js | 83 +++++++++++++++++++++++---------- electron/main.js | 8 ++++ tests/arrayifyTest.js | 40 ---------------- 8 files changed, 66 insertions(+), 172 deletions(-) delete mode 100644 electron/js/lib/arrayify.js delete mode 100644 electron/js/lib/base64-image.js delete mode 100644 electron/js/lib/datauri.js delete mode 100644 tests/arrayifyTest.js diff --git a/electron/js/lib/arrayify.js b/electron/js/lib/arrayify.js deleted file mode 100644 index 90c3a15440c..00000000000 --- a/electron/js/lib/arrayify.js +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Wire - * Copyright (C) 2017 Wire Swiss GmbH - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see http://www.gnu.org/licenses/. - * - */ - - - -module.exports = function (obj = []) { - return Array.isArray(obj) ? obj : [obj]; -}; diff --git a/electron/js/lib/base64-image.js b/electron/js/lib/base64-image.js deleted file mode 100644 index db8ff0927da..00000000000 --- a/electron/js/lib/base64-image.js +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Wire - * Copyright (C) 2017 Wire Swiss GmbH - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see http://www.gnu.org/licenses/. - * - */ - - - -const request = require('request'); - -const arrayify = require('./arrayify'); -const datauri = require('./datauri'); - -function fetch(url, callback) { - request({ url: url, encoding: null }, function(error, response, body) { - if (error) return callback(error); - let mimetype = response.headers['content-type']; - callback(null, datauri.fromBuffer(mimetype, body)); - }); -} - -module.exports = function(urls, limit = 1, callback) { - let imagesToFetch = arrayify(urls).slice(0, limit); - let completedRequests = 0; - let images = []; - - if (imagesToFetch.length === 0) return callback(); - - imagesToFetch.forEach(function(url) { - fetch(url, function(err, dataURI) { - completedRequests++; - if (err) console.log('unable to fetch image '); - if (dataURI) images.push(dataURI); - if (completedRequests === imagesToFetch.length) { - callback(images.length > 1 ? images : images[0]); - } - }); - }); -}; diff --git a/electron/js/lib/datauri.js b/electron/js/lib/datauri.js deleted file mode 100644 index 0fc8519b932..00000000000 --- a/electron/js/lib/datauri.js +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Wire - * Copyright (C) 2017 Wire Swiss GmbH - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see http://www.gnu.org/licenses/. - * - */ - - - -module.exports = { - fromBuffer: function (mimetype, buffer) { - let bufferBase64encoded = Buffer.from(buffer).toString('base64'); - return 'data:' + mimetype + ';base64,' + bufferBase64encoded; - }, -}; diff --git a/electron/js/lib/download.js b/electron/js/lib/download.js index 2c34abb365d..39095d6466f 100644 --- a/electron/js/lib/download.js +++ b/electron/js/lib/download.js @@ -17,8 +17,6 @@ * */ - - const fs = require('fs'); const imageType = require('image-type'); diff --git a/electron/js/lib/googleAuth.js b/electron/js/lib/googleAuth.js index 11791d9eb9f..fbee829088e 100644 --- a/electron/js/lib/googleAuth.js +++ b/electron/js/lib/googleAuth.js @@ -17,8 +17,6 @@ * */ - - const {BrowserWindow} = require('electron'); const qs = require('querystring'); diff --git a/electron/js/lib/openGraph.js b/electron/js/lib/openGraph.js index cbf1af8e4b0..9c420e994dc 100644 --- a/electron/js/lib/openGraph.js +++ b/electron/js/lib/openGraph.js @@ -17,40 +17,73 @@ * */ - - const og = require('open-graph'); +const request = require('request'); -const arrayify = require('./arrayify'); -const base64Images = require('./base64-image'); -function updateMetaDataWithImages(meta, images) { - images = arrayify(images); - if (images.length > 1) { - meta.image.url = images; - } else if (images.length > 0) { - meta.image.url = meta.image.data = images[0]; // old version uses data attr +function updateMetaDataWithImage(meta, image) { + if (image) { + meta.image.data = image; } else { delete meta.image; } return meta; } -module.exports = function(url, limit = 1, callback) { - // older version excepts 2 paramaters (url, callback) - if (typeof(limit) === 'function') { - callback = limit; - limit = 1; - } - og(url, function(error, meta) { - if (error) return callback(error); - if (meta.image && meta.image.url) { - base64Images(meta.image.url, limit, function(dataURIs) { - callback(null, updateMetaDataWithImages(meta, dataURIs)); - }); - } else { - callback(null, meta); - } +function bufferToBase64(buffer, mimetype) { + const bufferBase64encoded = Buffer.from(buffer).toString('base64'); + return 'data:' + mimetype + ';base64,' + bufferBase64encoded; +} + + +function arrayify(obj = []) { + return Array.isArray(obj) ? obj : [obj]; +}; + + +function fetchImageAsBase64(url) { + return new Promise((resolve) => { + request({url: encodeURI(url), encoding: null}, (error, response, body) => { + if (!error && response.statusCode === 200) { + resolve(bufferToBase64(body, response.headers['content-type'])); + } else { + // we just skip images that failed to download + resolve(); + } + }); + }); +} + + +function fetchOpenGraphData(url) { + return new Promise((resolve, reject) => { + og(url, function(error, meta) { + if (error) { + reject(error); + } else { + resolve(meta); + } + }); }); +} + + +module.exports = function(url, callback) { + fetchOpenGraphData(url) + .then((meta) => { + if (meta.image && meta.image.url) { + const imageUrl = arrayify(meta.image.url)[0] + return fetchImageAsBase64(imageUrl) + .then((uri) => updateMetaDataWithImage(meta, uri)) + .catch(() => meta); + } + return meta; + }) + .then((meta) => { + callback(null, meta); + }) + .catch((error) => { + callback(error); + }); }; diff --git a/electron/main.js b/electron/main.js index 35238371553..b7857ae8825 100644 --- a/electron/main.js +++ b/electron/main.js @@ -466,6 +466,14 @@ class ElectronWrapperInit { contents.on('new-window', (e, _url) => { openLinkInNewWindow(e, _url); }); contents.on('will-navigate', (e, _url) => { willNavigateInWebview(e, _url); }); + contents.on('destroyed', (e) => { + debugger + console.log(e) + }); + + // Load css to overwrite specific webapp styles + contents.insertCSS(fs.readFileSync(WRAPPER_CSS, 'utf8')) + contents.session.setCertificateVerifyProc((request, cb) => { const {hostname = '', certificate = {}, error} = request; diff --git a/tests/arrayifyTest.js b/tests/arrayifyTest.js deleted file mode 100644 index afae2db05c6..00000000000 --- a/tests/arrayifyTest.js +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Wire - * Copyright (C) 2017 Wire Swiss GmbH - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see http://www.gnu.org/licenses/. - * - */ - -'use strict'; - -const assert = require('assert'); - -const arrayify = require('../electron/js/lib/arrayify'); - -describe('arrayify', () => { - - it('should return empty array if no arguments', () => { - assert.deepEqual(arrayify(), []); - }); - - it('should turn single argument into array', () => { - assert.deepEqual(arrayify('foo'), ['foo']); - }); - - it('should keep array untouched', () => { - assert.deepEqual(arrayify(['foo']), ['foo']); - }); - -}); From 56caa27391092ccd7ce6d710b24a5f5f1f105c68 Mon Sep 17 00:00:00 2001 From: herrmannplatz Date: Wed, 19 Jul 2017 08:17:33 +0200 Subject: [PATCH 2/6] add test folders --- electron/js/lib/__mocks__/og.js | 22 +++++++ electron/js/lib/__tests__/openGraph.spec.js | 65 +++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 electron/js/lib/__mocks__/og.js create mode 100644 electron/js/lib/__tests__/openGraph.spec.js diff --git a/electron/js/lib/__mocks__/og.js b/electron/js/lib/__mocks__/og.js new file mode 100644 index 00000000000..838da707ea2 --- /dev/null +++ b/electron/js/lib/__mocks__/og.js @@ -0,0 +1,22 @@ +/* + * Wire + * Copyright (C) 2017 Wire Swiss GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://www.gnu.org/licenses/. + * + */ + +module.exports = function(url, callback) { + +} diff --git a/electron/js/lib/__tests__/openGraph.spec.js b/electron/js/lib/__tests__/openGraph.spec.js new file mode 100644 index 00000000000..5851383009c --- /dev/null +++ b/electron/js/lib/__tests__/openGraph.spec.js @@ -0,0 +1,65 @@ +/* + * Wire + * Copyright (C) 2017 Wire Swiss GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://www.gnu.org/licenses/. + * + */ + +import nock from 'nock' + +import openGraph from '../openGraph'; + +describe('openGraph', () => { + + it('should return open graph data with image data', (done) => { + const openGraphData = { + "title": "Wire · Modern communication, full privacy. For iOS, Android, macOS, Windows, Linux and web.", + "type": "website", + "url": "https://wire.com/", + "image": { + "url": "https://lh3.ggpht.com/ElqTCcY1N0c3EAX27MRFoXynZlbTaJD2KEqYNXAPn5YQPZa6Bvsux4NCgEMoUhazdIWWelAU__Kzmr55j55EsgM=s1024" + }, + "description": "HD quality calls, private and group chats with inline photos, music and video. Secure and perfectly synced across your devices." + }; + + openGraph('https://wire.com/', (error, meta) => { + expect(error).toBeNull(); + expect(meta.title).toBe(openGraphData.title); + expect(meta.image.data).toBeDefined(); + done(); + }); + + }); + + it('should return open graph data without image data', (done) => { + const openGraphData = { + "title": "Wire · Modern communication, full privacy. For iOS, Android, macOS, Windows, Linux and web.", + "type": "website", + "url": "https://wire.com/", + "description": "HD quality calls, private and group chats with inline photos, music and video. Secure and perfectly synced across your devices." + }; + + openGraph('https://wire.com/', (error, meta) => { + expect(error).toBeNull(); + expect(meta.title).toBe(openGraphData.title); + expect(meta.image).not.toBeDefined(); + done(); + }); + + }); + +}); + + From 3e387e208002c14441906620a56d6d804a91f2e4 Mon Sep 17 00:00:00 2001 From: herrmannplatz Date: Wed, 19 Jul 2017 08:22:30 +0200 Subject: [PATCH 3/6] chore: linting + remove unused code --- electron/js/lib/__tests__/openGraph.spec.js | 2 -- electron/main.js | 8 -------- 2 files changed, 10 deletions(-) diff --git a/electron/js/lib/__tests__/openGraph.spec.js b/electron/js/lib/__tests__/openGraph.spec.js index 5851383009c..8ddf4bbbd70 100644 --- a/electron/js/lib/__tests__/openGraph.spec.js +++ b/electron/js/lib/__tests__/openGraph.spec.js @@ -17,8 +17,6 @@ * */ -import nock from 'nock' - import openGraph from '../openGraph'; describe('openGraph', () => { diff --git a/electron/main.js b/electron/main.js index b7857ae8825..35238371553 100644 --- a/electron/main.js +++ b/electron/main.js @@ -466,14 +466,6 @@ class ElectronWrapperInit { contents.on('new-window', (e, _url) => { openLinkInNewWindow(e, _url); }); contents.on('will-navigate', (e, _url) => { willNavigateInWebview(e, _url); }); - contents.on('destroyed', (e) => { - debugger - console.log(e) - }); - - // Load css to overwrite specific webapp styles - contents.insertCSS(fs.readFileSync(WRAPPER_CSS, 'utf8')) - contents.session.setCertificateVerifyProc((request, cb) => { const {hostname = '', certificate = {}, error} = request; From 4cc4d4de824d48561aa5e58f312a5fc82cf7a47f Mon Sep 17 00:00:00 2001 From: Gregor Herdmann Date: Tue, 10 Oct 2017 19:58:32 +0200 Subject: [PATCH 4/6] fix: Adjust unit tests --- electron/js/lib/__tests__/openGraph.spec.js | 38 +++++++++++++++------ 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/electron/js/lib/__tests__/openGraph.spec.js b/electron/js/lib/__tests__/openGraph.spec.js index 8ddf4bbbd70..0e7caea0d5a 100644 --- a/electron/js/lib/__tests__/openGraph.spec.js +++ b/electron/js/lib/__tests__/openGraph.spec.js @@ -17,11 +17,31 @@ * */ + import openGraph from '../openGraph'; describe('openGraph', () => { + it('should resolve open graph data with image data', (done) => { + const openGraphData = { + "title": "Wire · Modern communication, full privacy. For iOS, Android, macOS, Windows, Linux and web.", + "type": "website", + "url": "https://wire.com/", + "image": { + "url": "https://lh3.ggpht.com/ElqTCcY1N0c3EAX27MRFoXynZlbTaJD2KEqYNXAPn5YQPZa6Bvsux4NCgEMoUhazdIWWelAU__Kzmr55j55EsgM=s1024" + }, + "description": "HD quality calls, private and group chats with inline photos, music and video. Secure and perfectly synced across your devices." + }; - it('should return open graph data with image data', (done) => { + openGraph('https://wire.com/') + .then((meta) => { + expect(meta.title).toBe(openGraphData.title); + expect(meta.image.data).toBeDefined(); + done(); + }) + .catch(done.fail); + }); + + it('should excecute callback with open graph data containing image data', (done) => { const openGraphData = { "title": "Wire · Modern communication, full privacy. For iOS, Android, macOS, Windows, Linux and web.", "type": "website", @@ -38,7 +58,6 @@ describe('openGraph', () => { expect(meta.image.data).toBeDefined(); done(); }); - }); it('should return open graph data without image data', (done) => { @@ -49,15 +68,14 @@ describe('openGraph', () => { "description": "HD quality calls, private and group chats with inline photos, music and video. Secure and perfectly synced across your devices." }; - openGraph('https://wire.com/', (error, meta) => { - expect(error).toBeNull(); - expect(meta.title).toBe(openGraphData.title); - expect(meta.image).not.toBeDefined(); - done(); - }); - + openGraph('https://wire.com/') + .then((meta) => { + expect(meta.title).toBe(openGraphData.title); + expect(meta.image).not.toBeDefined(); + done(); + }) + .catch(done.fail); }); - }); From 4fe34fa79ef27348fb54e3ff2524f734220be3ac Mon Sep 17 00:00:00 2001 From: Gregor Herdmann Date: Tue, 10 Oct 2017 20:03:15 +0200 Subject: [PATCH 5/6] fix: Linting --- electron/js/lib/__tests__/openGraph.spec.js | 39 ++++++++------------- 1 file changed, 14 insertions(+), 25 deletions(-) diff --git a/electron/js/lib/__tests__/openGraph.spec.js b/electron/js/lib/__tests__/openGraph.spec.js index 0e7caea0d5a..b6eeeee0b65 100644 --- a/electron/js/lib/__tests__/openGraph.spec.js +++ b/electron/js/lib/__tests__/openGraph.spec.js @@ -21,17 +21,21 @@ import openGraph from '../openGraph'; describe('openGraph', () => { - it('should resolve open graph data with image data', (done) => { - const openGraphData = { - "title": "Wire · Modern communication, full privacy. For iOS, Android, macOS, Windows, Linux and web.", - "type": "website", - "url": "https://wire.com/", - "image": { - "url": "https://lh3.ggpht.com/ElqTCcY1N0c3EAX27MRFoXynZlbTaJD2KEqYNXAPn5YQPZa6Bvsux4NCgEMoUhazdIWWelAU__Kzmr55j55EsgM=s1024" + let openGraphData = undefined; + + beforeEach(() => { + openGraphData = { + title: 'Wire · Modern communication, full privacy. For iOS, Android, macOS, Windows, Linux and web.', + type: 'website', + url: 'https://wire.com/', + image: { + url: 'https://lh3.ggpht.com/ElqTCcY1N0c3EAX27MRFoXynZlbTaJD2KEqYNXAPn5YQPZa6Bvsux4NCgEMoUhazdIWWelAU__Kzmr55j55EsgM=s1024', }, - "description": "HD quality calls, private and group chats with inline photos, music and video. Secure and perfectly synced across your devices." + description: 'HD quality calls, private and group chats with inline photos, music and video. Secure and perfectly synced across your devices.', }; + }); + it('should resolve open graph data with image data', (done) => { openGraph('https://wire.com/') .then((meta) => { expect(meta.title).toBe(openGraphData.title); @@ -41,17 +45,7 @@ describe('openGraph', () => { .catch(done.fail); }); - it('should excecute callback with open graph data containing image data', (done) => { - const openGraphData = { - "title": "Wire · Modern communication, full privacy. For iOS, Android, macOS, Windows, Linux and web.", - "type": "website", - "url": "https://wire.com/", - "image": { - "url": "https://lh3.ggpht.com/ElqTCcY1N0c3EAX27MRFoXynZlbTaJD2KEqYNXAPn5YQPZa6Bvsux4NCgEMoUhazdIWWelAU__Kzmr55j55EsgM=s1024" - }, - "description": "HD quality calls, private and group chats with inline photos, music and video. Secure and perfectly synced across your devices." - }; - + it('should execute callback with open graph data containing image data', (done) => { openGraph('https://wire.com/', (error, meta) => { expect(error).toBeNull(); expect(meta.title).toBe(openGraphData.title); @@ -61,12 +55,7 @@ describe('openGraph', () => { }); it('should return open graph data without image data', (done) => { - const openGraphData = { - "title": "Wire · Modern communication, full privacy. For iOS, Android, macOS, Windows, Linux and web.", - "type": "website", - "url": "https://wire.com/", - "description": "HD quality calls, private and group chats with inline photos, music and video. Secure and perfectly synced across your devices." - }; + delete openGraphData.image; openGraph('https://wire.com/') .then((meta) => { From 4fd6bf3ab8178daa3964cbb9a752b59740cb3ffe Mon Sep 17 00:00:00 2001 From: Gregor Herdmann Date: Tue, 10 Oct 2017 20:08:26 +0200 Subject: [PATCH 6/6] fix: Remove tests --- electron/js/lib/__mocks__/og.js | 22 ------- electron/js/lib/__tests__/openGraph.spec.js | 70 --------------------- electron/js/lib/openGraph.js | 7 ++- tests/datauriTest.js | 38 ----------- 4 files changed, 6 insertions(+), 131 deletions(-) delete mode 100644 electron/js/lib/__mocks__/og.js delete mode 100644 electron/js/lib/__tests__/openGraph.spec.js delete mode 100644 tests/datauriTest.js diff --git a/electron/js/lib/__mocks__/og.js b/electron/js/lib/__mocks__/og.js deleted file mode 100644 index 838da707ea2..00000000000 --- a/electron/js/lib/__mocks__/og.js +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Wire - * Copyright (C) 2017 Wire Swiss GmbH - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see http://www.gnu.org/licenses/. - * - */ - -module.exports = function(url, callback) { - -} diff --git a/electron/js/lib/__tests__/openGraph.spec.js b/electron/js/lib/__tests__/openGraph.spec.js deleted file mode 100644 index b6eeeee0b65..00000000000 --- a/electron/js/lib/__tests__/openGraph.spec.js +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Wire - * Copyright (C) 2017 Wire Swiss GmbH - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see http://www.gnu.org/licenses/. - * - */ - - -import openGraph from '../openGraph'; - -describe('openGraph', () => { - let openGraphData = undefined; - - beforeEach(() => { - openGraphData = { - title: 'Wire · Modern communication, full privacy. For iOS, Android, macOS, Windows, Linux and web.', - type: 'website', - url: 'https://wire.com/', - image: { - url: 'https://lh3.ggpht.com/ElqTCcY1N0c3EAX27MRFoXynZlbTaJD2KEqYNXAPn5YQPZa6Bvsux4NCgEMoUhazdIWWelAU__Kzmr55j55EsgM=s1024', - }, - description: 'HD quality calls, private and group chats with inline photos, music and video. Secure and perfectly synced across your devices.', - }; - }); - - it('should resolve open graph data with image data', (done) => { - openGraph('https://wire.com/') - .then((meta) => { - expect(meta.title).toBe(openGraphData.title); - expect(meta.image.data).toBeDefined(); - done(); - }) - .catch(done.fail); - }); - - it('should execute callback with open graph data containing image data', (done) => { - openGraph('https://wire.com/', (error, meta) => { - expect(error).toBeNull(); - expect(meta.title).toBe(openGraphData.title); - expect(meta.image.data).toBeDefined(); - done(); - }); - }); - - it('should return open graph data without image data', (done) => { - delete openGraphData.image; - - openGraph('https://wire.com/') - .then((meta) => { - expect(meta.title).toBe(openGraphData.title); - expect(meta.image).not.toBeDefined(); - done(); - }) - .catch(done.fail); - }); -}); - - diff --git a/electron/js/lib/openGraph.js b/electron/js/lib/openGraph.js index 3cd7ccdaeae..b6fcad38a1c 100644 --- a/electron/js/lib/openGraph.js +++ b/electron/js/lib/openGraph.js @@ -48,7 +48,12 @@ const _fetchOpenGraphData = (url) => { }; const _updateMetaDataWithImage = (meta, image) => { - image ? meta.image.data = image : meta.image; + if (image) { + meta.image.data = image; + } else { + delete meta.image; + } + return meta; }; diff --git a/tests/datauriTest.js b/tests/datauriTest.js deleted file mode 100644 index a3767e969e6..00000000000 --- a/tests/datauriTest.js +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Wire - * Copyright (C) 2017 Wire Swiss GmbH - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see http://www.gnu.org/licenses/. - * - */ - -'use strict'; - -const assert = require('assert'); - -const datauri = require('../electron/js/lib/datauri'); - -describe('datauri', () => { - - describe('fromBuffer', () => { - - it('should return datauri', () => { - let buffer = new Buffer('foo', 'utf-8'); - let mimetype = 'text/plain'; - assert.equal(datauri.fromBuffer(mimetype, buffer), 'data:text/plain;base64,Zm9v'); - }); - - }); - -});