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

@ -53,28 +53,27 @@ const flash = ({ type, msg }) => {
tags = res.tags.map(t => t.tag);
renderTags(res.tags);
};
const post = async (url, data) => fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(data)
const get = async (url, data) => {
let s = [];
for(const [ key, val ] of Object.entries(data))
s.push(encodeURIComponent(key) + "=" + encodeURIComponent(val));
return (await fetch(url + "?" + s.join("&"))).json();
};
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 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", {
const addTag = async (postid, tag) => await get("/api/v2/admin/tags/add", {
postid: postid,
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,
tagid: tagid
})).json();
});
const renderTags = _tags => {
[...document.querySelectorAll("#tags > .badge")].forEach(tag => tag.parentElement.removeChild(tag));
@ -123,7 +122,7 @@ const flash = ({ type, msg }) => {
let tt = null;
let lastInput = '';
const testList = document.querySelector('#testList');
const testList = document.querySelector('#testlist');
input.addEventListener("keyup", async e => {
if(e.key === "Enter") {
@ -161,9 +160,9 @@ const flash = ({ type, msg }) => {
testList.innerText = "";
lastInput = tmptag;
const res = await (await post("/api/v2/admin/tags/suggest", {
searchString: tmptag
})).json();
const res = await get('/api/v2/admin/tags/suggest', {
q: tmptag
});
for(const entry of res.suggestions) {
const option = document.createElement('option');
@ -236,7 +235,9 @@ const flash = ({ type, msg }) => {
};
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) {
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");

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