From 3b781b97b8be9afcc621283e42117cc2062db198 Mon Sep 17 00:00:00 2001 From: Claudio Maggioni Date: Fri, 15 Nov 2019 08:55:37 +0100 Subject: [PATCH] HW7: task 6 is buggy. Trying to fix it --- hw7/Claudio_Maggioni/public/js/views.js | 2 +- hw7/Claudio_Maggioni/routes/root/router.js | 127 ++++++++++++++------- 2 files changed, 87 insertions(+), 42 deletions(-) diff --git a/hw7/Claudio_Maggioni/public/js/views.js b/hw7/Claudio_Maggioni/public/js/views.js index 3ffca32..d3e0af0 100644 --- a/hw7/Claudio_Maggioni/public/js/views.js +++ b/hw7/Claudio_Maggioni/public/js/views.js @@ -1,6 +1,6 @@ (function(dust){dust.register("500",body_0);function body_0(chk,ctx){return chk.w("

Error 500

").f(ctx.get(["err"], false),ctx,"h").w("
");}body_0.__dustBody=!0;return body_0}(dust)); (function(dust){dust.register("favourite",body_0);function body_0(chk,ctx){return chk.w("").f(ctx.get(["name"], false),ctx,"h").w("").p("favourite_partial",ctx,ctx,{}).w("Favourites list");}body_0.__dustBody=!0;return body_0}(dust)); -(function(dust){dust.register("favourite_partial",body_0);function body_0(chk,ctx){return chk.w("

").f(ctx.get(["name"], false),ctx,"h").w("

\"").f(ctx.get(["name"],").x(ctx.get(["b"], false),ctx,{"block":body_1},{}).x(ctx.get(["details"], false),ctx,{"else":body_2,"block":body_5},{});}body_0.__dustBody=!0;function body_1(chk,ctx){return chk.w("

Bookmarked

");}body_1.__dustBody=!0;function body_2(chk,ctx){return chk.w("




").x(ctx.get(["bookmarked"], false),ctx,{"else":body_3,"block":body_4},{}).w("






");}body_2.__dustBody=!0;function body_3(chk,ctx){return chk.w("");}body_3.__dustBody=!0;function body_4(chk,ctx){return chk.w("");}body_4.__dustBody=!0;function body_5(chk,ctx){return chk.w("Details");}body_5.__dustBody=!0;return body_0}(dust)); +(function(dust){dust.register("favourite_partial",body_0);function body_0(chk,ctx){return chk.w("

").f(ctx.get(["name"], false),ctx,"h").w("

\"").f(ctx.get(["name"],").x(ctx.get(["b"], false),ctx,{"block":body_1},{}).x(ctx.get(["details"], false),ctx,{"else":body_2,"block":body_5},{});}body_0.__dustBody=!0;function body_1(chk,ctx){return chk.w("

Bookmarked

");}body_1.__dustBody=!0;function body_2(chk,ctx){return chk.w("




").x(ctx.get(["bookmarked"], false),ctx,{"else":body_3,"block":body_4},{}).w("






");}body_2.__dustBody=!0;function body_3(chk,ctx){return chk.w("");}body_3.__dustBody=!0;function body_4(chk,ctx){return chk.w("");}body_4.__dustBody=!0;function body_5(chk,ctx){return chk.w("Details");}body_5.__dustBody=!0;return body_0}(dust)); (function(dust){dust.register("favourites",body_0);function body_0(chk,ctx){return chk.w("").x(ctx.get(["bookmarked"], false),ctx,{"else":body_1,"block":body_2},{}).w("").x(ctx.get(["bookmarked"], false),ctx,{"else":body_3,"block":body_4},{}).s(ctx.get(["favs"], false),ctx,{"else":body_5,"block":body_6},{}).w("");}body_0.__dustBody=!0;function body_1(chk,ctx){return chk.w("Favourites");}body_1.__dustBody=!0;function body_2(chk,ctx){return chk.w("Bookmarked");}body_2.__dustBody=!0;function body_3(chk,ctx){return chk.w("

Favourites

");}body_3.__dustBody=!0;function body_4(chk,ctx){return chk.w("

Bookmarked

");}body_4.__dustBody=!0;function body_5(chk,ctx){return chk.w("No favourites.");}body_5.__dustBody=!0;function body_6(chk,ctx){return chk.w("
").p("favourite_partial",ctx,ctx,{"name":ctx.get(["name"], false),"dataURL":ctx.get(["dataURL"], false),"_id":ctx.get(["_id"], false),"bookmarked":ctx.get(["bookmarked"], false),"details":"true"}).w("
");}body_6.__dustBody=!0;return body_0}(dust)); (function(dust){dust.register("index",body_0);function body_0(chk,ctx){return chk.w("OO-JS Exercise - Web Atelier 2017

OO-JS Exercise: Canvas



");}body_0.__dustBody=!0;return body_0}(dust)); (function(dust){dust.register("server_side",body_0);function body_0(chk,ctx){return chk.s(ctx.get(["favs"], false),ctx,{"block":body_1},{});}body_0.__dustBody=!0;function body_1(chk,ctx){return chk.w("
").p("favourite_partial",ctx,ctx,{"name":ctx.get(["name"], false),"dataURL":ctx.get(["dataURL"], false),"_id":ctx.get(["_id"], false),"bookmarked":ctx.get(["bookmarked"], false)}).w("
");}body_1.__dustBody=!0;function body_2(chk,ctx){return chk.w("bookmarked");}body_2.__dustBody=!0;return body_0}(dust)); diff --git a/hw7/Claudio_Maggioni/routes/root/router.js b/hw7/Claudio_Maggioni/routes/root/router.js index cd6b7db..5adc4e1 100644 --- a/hw7/Claudio_Maggioni/routes/root/router.js +++ b/hw7/Claudio_Maggioni/routes/root/router.js @@ -53,6 +53,10 @@ async function fetchImgur(req, res, method, url, body) { const json = await response.json(); + if (json.status == 409 && url.match(/follow\/tag/)) { + return {}; + } + if (Math.floor(json.status / 100) != 2) { res.status(json.status).json(json); return false; @@ -61,6 +65,27 @@ async function fetchImgur(req, res, method, url, body) { return json; } +async function tryAllImages(req, res) { + const images = await fetchImgur(req, res, 'GET', '/account/me/images'); + if (!images) { + return false; + } + + console.log('all images list', images.data); + + const image = images.data + .filter(e => e.title.match(req.body.oldName) && e.in_gallery)[0]; + + console.log('all images', image); + + if (image) { + return { album: null, image: image }; + } else { + res.status(404).json({ error: "No image in gallery found" }); + return false; + } +} + router.post('/imgur/ordeal', async (req, res) => { try { const ordealResponse = { ordealSuccess: true }; @@ -111,50 +136,70 @@ router.post('/imgur/ordeal', async (req, res) => { console.log('albums', albums.data.length); - let reject = false; - const promises = []; - for (const album of albums.data) { - promises.push(new Promise(async (res, rej) => { - if (reject) { - return reject; - } - - const images = await fetchImgur(req, res, 'GET', '/album/' + album.id + - '/images'); - - if (!images || reject) { - rej(); - return; - } - - const image = - images.data.filter(e => e.title.match(req.body.oldName))[0]; - - console.log('image', image); - - if (!image || reject) { - rej(); - return; - } - - reject = true; - res({ album: album, image: image }); - })); - } - let result; - try { - result = await promiseAny(promises); - } catch(_) { - res.status(404).json({ error: "No image in gallery found" }); - return; + if (albums.data.length == 0) { + result = await tryAllImages(req, res); + + if (!result) { + return; + } + } else { + let reject = false; + const promises = []; + for (const album of albums.data) { + promises.push(new Promise(async (res, rej) => { + if (reject) { + return reject; + } + + const images = await fetchImgur(req, res, 'GET', '/album/' + + album.id + '/images'); + + if (!images || reject) { + rej(); + return; + } + + const image = + images.data + .filter(e => e.title.match(req.body.oldName))[0]; + + console.log('image', image); + + if (!image || reject) { + rej(); + return; + } + + reject = true; + res({ album: album, image: image }); + })); + } + + try { + result = await promiseAny(promises); + } catch(_) { + result = await tryAllImages(req, res); + if (!result) { + return + } + } } + console.log('result', result); albumJson = { data: result.album }; ordealResponse.views = result.image.views; - ordealResponse.votes = await fetchImgur(req, res, 'GET', '/gallery/' - + result.image.id + '/votes'); + + if (result.image.in_gallery) { + ordealResponse.votes = await fetchImgur(req, res, 'GET', '/gallery/' + + result.image.id + '/votes'); + } else if (result.album.in_gallery) { + ordealResponse.votes = await fetchImgur(req, res, 'GET', '/gallery/' + + result.album.id + '/votes'); + } else { + ordealResponse.votes = { ups: 'not avaliable', downs: 'not avaliable' }; + } if (!ordealResponse.votes) { return; @@ -168,14 +213,14 @@ router.post('/imgur/ordeal', async (req, res) => { return; } - if (!await fetchImgur(req, res, 'PUT', '/album/' + result.album.id + - '/add', { 'ids[]': uploadJson.data.id })) { + if (result.album && !await fetchImgur(req, res, 'PUT', '/album/' + + result.album.id + '/add', { 'ids[]': uploadJson.data.id })) { return; } } if (!await fetchImgur(req, res, 'POST', '/gallery/image/' + - uploadJson.data.id, { title: req.body.name })) { + uploadJson.data.id, { title: req.body.name, tags: req.body.tags })) { return; }