Support applink update
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user