From ac00225a75e61b3b5eb85df69a49adf80000eb4e Mon Sep 17 00:00:00 2001 From: Johannes Zellner Date: Thu, 7 Jul 2022 16:41:25 +0200 Subject: [PATCH] Support applink update --- src/applinks.js | 35 +++++++++++++++++++++++------------ src/routes/applinks.js | 2 +- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/applinks.js b/src/applinks.js index 60c02d1b4..357cc8a05 100644 --- a/src/applinks.js +++ b/src/applinks.js @@ -45,18 +45,14 @@ async function list() { return results; } -async function add(applink) { +async function amendIconAndLabel(applink) { assert.strictEqual(typeof applink, 'object'); - assert.strictEqual(typeof applink.upstreamUri, 'string'); - debug(`add: ${applink.upstreamUri}`, applink); + const [error, response] = await safe(superagent.get(applink.upstreamUri)); + if (error || !response.text) throw new BoxError(BoxError.BAD_FIELD, 'cannot fetch applink icon'); - // TODO applink property validation and fetch icon and label if not provided + const dom = new jsdom.JSDOM(response.text); if (!applink.icon) { - const [error, response] = await safe(superagent.get(applink.upstreamUri)); - if (error || !response.text) throw new BoxError(BoxError.BAD_FIELD, 'cannot fetch applink icon'); - - const dom = new jsdom.JSDOM(response.text); if (dom.window.document.querySelector('link[rel="icon"]')) { const favicon = dom.window.document.querySelector('link[rel="icon"]').href ; const [error, response] = await safe(superagent.get(favicon)); @@ -67,9 +63,20 @@ async function add(applink) { } else { debug(`Cannot fetch icon for ${applink.upstreamUri}`); } + } + if (!applink.lable) { if (dom.window.document.title) applink.label = dom.window.document.title; } +} + +async function add(applink) { + assert.strictEqual(typeof applink, 'object'); + assert.strictEqual(typeof applink.upstreamUri, 'string'); + + debug(`add: ${applink.upstreamUri}`, applink); + + await amendIconAndLabel(applink); const data = { id: uuid.v4(), @@ -92,8 +99,6 @@ async function add(applink) { async function get(applinkId) { assert.strictEqual(typeof applinkId, 'string'); - debug(`get: ${applinkId}`); - const result = await database.query(`SELECT ${APPLINKS_FIELDS} FROM applinks WHERE id = ?`, [ applinkId ]); if (result.length === 0) throw new BoxError(BoxError.NOT_FOUND, 'Applink not found'); @@ -108,6 +113,14 @@ async function update(applinkId, applink) { assert.strictEqual(typeof applink.upstreamUri, 'string'); debug(`update: ${applinkId} ${applink.upstreamUri}`, applink); + + await amendIconAndLabel(applink); + + const query = 'UPDATE applinks SET label=?, icon=?, upstreamUri=? WHERE id = ?'; + const args = [ applink.label, applink.icon, applink.upstreamUri, applinkId ]; + + const result = await database.query(query, args); + if (result.affectedRows !== 1) throw new BoxError(BoxError.NOT_FOUND, 'Applink not found'); } async function remove(applinkId) { @@ -122,8 +135,6 @@ async function remove(applinkId) { async function getIcon(applinkId) { assert.strictEqual(typeof applinkId, 'string'); - debug(`getIcon: ${applinkId}`); - const applink = await get(applinkId); return applink.icon; diff --git a/src/routes/applinks.js b/src/routes/applinks.js index 1877f0383..52eb9e389 100644 --- a/src/routes/applinks.js +++ b/src/routes/applinks.js @@ -51,7 +51,7 @@ async function update(req, res, next) { if (!req.body.upstreamUri || typeof req.body.upstreamUri !== 'string') return next(new HttpError(400, 'upstreamUri must be a non-empty string')); - const [error] = await safe(applinks.get(req.params.id, req.body)); + const [error] = await safe(applinks.update(req.params.id, req.body)); if (error) return next(BoxError.toHttpError(error)); next(new HttpSuccess(202, {}));