Support applink update
This commit is contained in:
+23
-12
@@ -45,18 +45,14 @@ async function list() {
|
|||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function add(applink) {
|
async function amendIconAndLabel(applink) {
|
||||||
assert.strictEqual(typeof applink, 'object');
|
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) {
|
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"]')) {
|
if (dom.window.document.querySelector('link[rel="icon"]')) {
|
||||||
const favicon = dom.window.document.querySelector('link[rel="icon"]').href ;
|
const favicon = dom.window.document.querySelector('link[rel="icon"]').href ;
|
||||||
const [error, response] = await safe(superagent.get(favicon));
|
const [error, response] = await safe(superagent.get(favicon));
|
||||||
@@ -67,9 +63,20 @@ async function add(applink) {
|
|||||||
} else {
|
} else {
|
||||||
debug(`Cannot fetch icon for ${applink.upstreamUri}`);
|
debug(`Cannot fetch icon for ${applink.upstreamUri}`);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!applink.lable) {
|
||||||
if (dom.window.document.title) applink.label = dom.window.document.title;
|
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 = {
|
const data = {
|
||||||
id: uuid.v4(),
|
id: uuid.v4(),
|
||||||
@@ -92,8 +99,6 @@ async function add(applink) {
|
|||||||
async function get(applinkId) {
|
async function get(applinkId) {
|
||||||
assert.strictEqual(typeof applinkId, 'string');
|
assert.strictEqual(typeof applinkId, 'string');
|
||||||
|
|
||||||
debug(`get: ${applinkId}`);
|
|
||||||
|
|
||||||
const result = await database.query(`SELECT ${APPLINKS_FIELDS} FROM applinks WHERE id = ?`, [ 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');
|
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');
|
assert.strictEqual(typeof applink.upstreamUri, 'string');
|
||||||
|
|
||||||
debug(`update: ${applinkId} ${applink.upstreamUri}`, applink);
|
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) {
|
async function remove(applinkId) {
|
||||||
@@ -122,8 +135,6 @@ async function remove(applinkId) {
|
|||||||
async function getIcon(applinkId) {
|
async function getIcon(applinkId) {
|
||||||
assert.strictEqual(typeof applinkId, 'string');
|
assert.strictEqual(typeof applinkId, 'string');
|
||||||
|
|
||||||
debug(`getIcon: ${applinkId}`);
|
|
||||||
|
|
||||||
const applink = await get(applinkId);
|
const applink = await get(applinkId);
|
||||||
|
|
||||||
return applink.icon;
|
return applink.icon;
|
||||||
|
|||||||
@@ -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'));
|
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));
|
if (error) return next(BoxError.toHttpError(error));
|
||||||
|
|
||||||
next(new HttpSuccess(202, {}));
|
next(new HttpSuccess(202, {}));
|
||||||
|
|||||||
Reference in New Issue
Block a user