From e15c16434c8095543e92b8a0bf822741bbaf6295 Mon Sep 17 00:00:00 2001 From: Flummi Date: Fri, 25 Mar 2022 15:24:12 +0100 Subject: [PATCH] toggle sfw/nsfw --- public/s/js/admin.js | 50 +++++++++++++--------------------------- src/inc/routes/apiv2.mjs | 34 +++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 34 deletions(-) diff --git a/public/s/js/admin.js b/public/s/js/admin.js index ea57193..093ae47 100644 --- a/public/s/js/admin.js +++ b/public/s/js/admin.js @@ -47,7 +47,11 @@ const flash = ({ type, msg }) => { if(!confirm("Do you really want to delete this tag?")) return; const tagname = e.target.parentElement.querySelector('a:first-child').innerText; - const res = await deleteTag(postid, tagname); + + const res = await (await fetch("/api/v2/admin/" + postid + "/tags/" + encodeURIComponent(tagname), { + method: 'DELETE' + })).json(); + if(!res.success) return alert("uff"); tags = res.tags.map(t => t.tag); @@ -76,20 +80,7 @@ const flash = ({ type, msg }) => { const get = async (url, data) => queryapi(url, data, 'GET'); const post = async (url, data) => queryapi(url, data, 'POST'); - const del = async (url, data) => queryapi(url, data, 'DELETE'); - const deletePost = async postid => await post("/api/v2/admin/deletepost", { - postid: postid - }); - - const addTag = async (postid, tagname) => await post("/api/v2/admin/" + postid + "/tags", { - tagname: tagname - }); - - const deleteTag = async (postid, tagname) => await (await fetch("/api/v2/admin/" + postid + "/tags/" + encodeURIComponent(tagname), { - method: 'DELETE' - })).json(); - const renderTags = _tags => { [...document.querySelectorAll("#tags > .badge")].forEach(tag => tag.parentElement.removeChild(tag)); _tags.reverse().forEach(tag => { @@ -143,7 +134,9 @@ const flash = ({ type, msg }) => { const tmptag = input.value?.trim(); if(tags.includes(tmptag)) return alert("tag already exists"); - const res = await addTag(postid, tmptag); + const res = await post("/api/v2/admin/" + postid + "/tags", { + tagname: tmptag + }); if(!res.success) { return flash({ type: "error", @@ -164,7 +157,7 @@ const flash = ({ type, msg }) => { clearTimeout(tt); tt = setTimeout(async () => { - globalTimeout = null; + tt = null; const tmptag = input.value?.trim(); @@ -201,23 +194,10 @@ const flash = ({ type, msg }) => { const toggleEvent = async (e = false) => { if(e) e.preventDefault(); - let res; - if(tags.includes("sfw")) { - await deleteTag(postid, "sfw"); - res = await addTag(postid, "nsfw"); - } - else if(tags.includes("nsfw")) { - await deleteTag(postid, "nsfw"); - res = await addTag(postid, "sfw"); - } - else - res = await addTag(postid, "sfw"); - - if(!res.success) - return flash({ - type: "error", - msg: res.msg - }); + + const res = await (await fetch('/api/v2/admin/' + encodeURIComponent(postid) + '/tags/toggle', { + method: 'PUT' + })).json(); renderTags(res.tags); tags = res.tags.map(t => t.tag); @@ -233,7 +213,9 @@ const flash = ({ type, msg }) => { e.preventDefault(); if(!confirm(`Reason for deleting f0ckpost ${postid} by ${poster} (Weihnachten™)`)) return; - const res = await deletePost(postid); + const res = await post("/api/v2/admin/deletepost", { + postid: postid + }); if(res.success) { flash({ type: "success", diff --git a/src/inc/routes/apiv2.mjs b/src/inc/routes/apiv2.mjs index e5ab730..953d331 100644 --- a/src/inc/routes/apiv2.mjs +++ b/src/inc/routes/apiv2.mjs @@ -187,6 +187,40 @@ export default (router, tpl) => { })}); }); + group.put(/\/admin\/(?\d+)\/tags\/toggle$/, auth, async (req, res) => { + // xD + if(!req.params.postid) { + return res.json({ + success: false, + msg: "missing postid" + }); + } + + const postid = +req.params.postid; + + if(!(await lib.getTags(postid)).filter(tag => [1,2].includes(tag.id)).length) { + // insert + await sql('tags_assign').insert({ + item_id: postid, + tag_id: 1, + user_id: req.session.id + }); + } + else { + // update + await sql('tags_assign') + .update({ + tag_id: sql.raw('(array[2,1])[tag_id]') + }) + .whereRaw('tag_id = any(array[1,2])') + .andWhere('item_id', postid); + } + + return res.reply({ body: JSON.stringify({ + tags: await lib.getTags(postid) + })}); + }); + group.get(/\/admin\/tags\/suggest$/, auth, async (req, res) => { const reply = { success: false,