querystrings, get, blah

This commit is contained in:
Flummi 2022-03-24 15:35:40 +01:00
parent 74ac4c33e7
commit abcea99fe0
2 changed files with 47 additions and 45 deletions

View File

@ -54,27 +54,26 @@ const flash = ({ type, msg }) => {
renderTags(res.tags); renderTags(res.tags);
}; };
const post = async (url, data) => fetch(url, { const get = async (url, data) => {
method: "POST", let s = [];
headers: { for(const [ key, val ] of Object.entries(data))
"Content-Type": "application/json" s.push(encodeURIComponent(key) + "=" + encodeURIComponent(val));
}, return (await fetch(url + "?" + s.join("&"))).json();
body: JSON.stringify(data) };
const deletePost = async postid => await get("/api/v2/admin/deletepost", {
postid: postid
}); });
const getTags = async postid => await (await fetch("/api/v2/admin/tags/get/" + postid)).json(); const addTag = async (postid, tag) => await get("/api/v2/admin/tags/add", {
const deletePost = async postid => await (await fetch("/api/v2/admin/deletepost/" + postid)).json();
const addTag = async (postid, tag) => await (await post("/api/v2/admin/tags/add", {
postid: postid, postid: postid,
tag: tag tag: tag
})).json(); });
const deleteTag = async (postid, tagid) => await (await post("/api/v2/admin/tags/delete", { const deleteTag = async (postid, tagid) => await get("/api/v2/admin/tags/delete", {
postid: postid, postid: postid,
tagid: tagid tagid: tagid
})).json(); });
const renderTags = _tags => { const renderTags = _tags => {
[...document.querySelectorAll("#tags > .badge")].forEach(tag => tag.parentElement.removeChild(tag)); [...document.querySelectorAll("#tags > .badge")].forEach(tag => tag.parentElement.removeChild(tag));
@ -123,7 +122,7 @@ const flash = ({ type, msg }) => {
let tt = null; let tt = null;
let lastInput = ''; let lastInput = '';
const testList = document.querySelector('#testList'); const testList = document.querySelector('#testlist');
input.addEventListener("keyup", async e => { input.addEventListener("keyup", async e => {
if(e.key === "Enter") { if(e.key === "Enter") {
@ -161,9 +160,9 @@ const flash = ({ type, msg }) => {
testList.innerText = ""; testList.innerText = "";
lastInput = tmptag; lastInput = tmptag;
const res = await (await post("/api/v2/admin/tags/suggest", { const res = await get('/api/v2/admin/tags/suggest', {
searchString: tmptag q: tmptag
})).json(); });
for(const entry of res.suggestions) { for(const entry of res.suggestions) {
const option = document.createElement('option'); const option = document.createElement('option');
@ -236,7 +235,9 @@ const flash = ({ type, msg }) => {
}; };
const toggleFavEvent = async e => { const toggleFavEvent = async e => {
const res = await (await fetch(`/api/v2/admin/togglefav/${postid}`)).json(); const res = await get('/api/v2/admin/togglefav', {
postid: postid
});
if(res.success) { if(res.success) {
const fav = document.querySelector("svg#a_favo > use").href; const fav = document.querySelector("svg#a_favo > use").href;
fav.baseVal = '/s/img/iconset.svg#heart_' + (fav.baseVal.match(/heart_(regular|solid)$/)[1] == "solid" ? "regular" : "solid"); fav.baseVal = '/s/img/iconset.svg#heart_' + (fav.baseVal.match(/heart_(regular|solid)$/)[1] == "solid" ? "regular" : "solid");

View File

@ -68,7 +68,7 @@ export default (router, tpl) => {
}); });
}); });
group.get(/\/user\/.*(\/\d+)?$/, async (req, res) => { // auf qs umstellen group.get(/\/user\/.*(\/\d+)?$/, async (req, res) => {
const user = req.url.split[3]; const user = req.url.split[3];
const eps = +req.url.split[4] || 50; const eps = +req.url.split[4] || 50;
@ -85,16 +85,16 @@ export default (router, tpl) => {
}); });
// adminzeugs // adminzeugs
group.post(/\/admin\/tags\/add$/, auth, async (req, res) => { group.get(/\/admin\/tags\/add$/, auth, async (req, res) => {
if(!req.post.postid || !req.post.tag) { if(!req.url.qs.postid || !req.url.qs.tag) {
return res.reply({ body: JSON.stringify({ return res.reply({ body: JSON.stringify({
success: false, success: false,
msg: "missing postid or tag" msg: "missing postid or tag"
})}); })});
} }
const postid = +req.post.postid; const postid = +req.url.qs.postid;
const tag = req.post.tag?.trim(); const tag = req.url.qs.tag?.trim();
if(tag.length >= 45) { if(tag.length >= 45) {
return res.reply({ body: JSON.stringify({ return res.reply({ body: JSON.stringify({
@ -130,22 +130,22 @@ export default (router, tpl) => {
return res.reply({ body: JSON.stringify({ return res.reply({ body: JSON.stringify({
success: true, success: true,
postid: req.post.postid, postid: postid,
tag: req.post.tag, tag: tag,
tags: await lib.getTags(postid) tags: await lib.getTags(postid)
})}); })});
}); });
group.post(/\/admin\/tags\/delete$/, auth, async (req, res) => { group.get(/\/admin\/tags\/delete$/, auth, async (req, res) => {
if(!req.post.postid || !req.post.tagid) { if(!req.url.qs.postid || !req.url.qs.tagid) {
return res.reply({ body: JSON.stringify({ return res.reply({ body: JSON.stringify({
success: false, success: false,
msg: "missing postid or tag" msg: "missing postid or tag"
})}); })});
} }
const postid = +req.post.postid; const postid = +req.url.qs.postid;
const tagid = +req.post.tagid; const tagid = +req.url.qs.tagid;
const tags = await lib.getTags(postid); const tags = await lib.getTags(postid);
@ -170,13 +170,15 @@ export default (router, tpl) => {
})}); })});
}); });
group.post(/\/admin\/tags\/suggest$/, auth, async (req, res) => { group.get(/\/admin\/tags\/suggest$/, auth, async (req, res) => {
const reply = { const reply = {
success: false, success: false,
suggestions: {} suggestions: {}
}; };
if(req.post?.searchString.length <= 1) { const searchString = req.url.qs.q;
if(searchString?.length <= 1) {
reply.error = 'too short lol'; reply.error = 'too short lol';
return res.reply({ body: JSON.stringify(reply) }); return res.reply({ body: JSON.stringify(reply) });
} }
@ -185,14 +187,13 @@ export default (router, tpl) => {
const q = await sql('tags') const q = await sql('tags')
.select('tag', sql.raw('count(tags_assign.tag_id) as tagged')) .select('tag', sql.raw('count(tags_assign.tag_id) as tagged'))
.leftJoin('tags_assign', 'tags_assign.tag_id', 'tags.id') .leftJoin('tags_assign', 'tags_assign.tag_id', 'tags.id')
.whereRaw("normalized like '%' || slugify(?) || '%'", [ req.post.searchString ]) .whereRaw("normalized like '%' || slugify(?) || '%'", [ searchString ])
.groupBy('tags.id') .groupBy('tags.id')
.orderBy('tagged', 'desc') .orderBy('tagged', 'desc')
.limit(15); .limit(15);
reply.success = true; reply.success = true;
reply.suggestions = q; reply.suggestions = q;
} catch(err) { } catch(err) {
reply.success = false;
reply.error = err.msg; reply.error = err.msg;
} }
@ -205,14 +206,14 @@ export default (router, tpl) => {
})}); })});
}); });
group.get(/\/admin\/deletepost\/\d+$/, auth, async (req, res) => { group.get(/\/admin\/deletepost$/, auth, async (req, res) => {
if(!req.url.split[4]) { if(!req.url.qs.postid) {
return res.reply({ body: JSON.stringify({ return res.reply({ body: JSON.stringify({
success: true, success: true,
msg: "no postid" msg: "no postid"
})}); })});
} }
const postid = +req.url.split[4]; const postid = +req.url.qs.postid;
await sql("items").where("id", postid).del(); await sql("items").where("id", postid).del();
res.reply({ body: JSON.stringify({ res.reply({ body: JSON.stringify({
@ -220,19 +221,19 @@ export default (router, tpl) => {
})}); })});
}); });
group.get(/\/admin\/togglefav\/\d+$/, auth, async (req, res) => { group.get(/\/admin\/togglefav$/, auth, async (req, res) => {
const itemid = +req.url.split[4]; const postid = +req.url.qs.postid;
let favs = await sql('favorites').select('user_id').where('item_id', itemid); let favs = await sql('favorites').select('user_id').where('item_id', postid);
if(Object.values(favs).filter(u => u.user_id === req.session.id)[0]) { if(Object.values(favs).filter(u => u.user_id === req.session.id)[0]) {
// del fav // del fav
await sql('favorites').where('user_id', req.session.id).andWhere('item_id', itemid).del(); await sql('favorites').where('user_id', req.session.id).andWhere('item_id', postid).del();
} }
else { else {
// add fav // add fav
await sql('favorites').insert({ await sql('favorites').insert({
item_id: itemid, item_id: postid,
user_id: req.session.id user_id: req.session.id
}); });
} }
@ -241,11 +242,11 @@ export default (router, tpl) => {
.select('user.user', 'user_options.avatar') .select('user.user', 'user_options.avatar')
.leftJoin('user', 'user.id', 'favorites.user_id') .leftJoin('user', 'user.id', 'favorites.user_id')
.leftJoin('user_options', 'user_options.user_id', 'favorites.user_id') .leftJoin('user_options', 'user_options.user_id', 'favorites.user_id')
.where('favorites.item_id', itemid); .where('favorites.item_id', postid);
res.reply({ body: JSON.stringify({ res.reply({ body: JSON.stringify({
success: true, success: true,
itemid: itemid, itemid: postid,
favs: favs favs: favs
})}); })});
}); });