api schmapi

This commit is contained in:
Flummi
2022-03-24 20:29:20 +01:00
parent 06850f96c1
commit e0c7f3971c
3 changed files with 66 additions and 57 deletions

View File

@ -85,18 +85,55 @@ export default (router, tpl) => {
});
// adminzeugs
group.post(/\/admin\/tags\/add$/, auth, async (req, res) => {
if(!req.post.postid || !req.post.tag) {
group.delete(/\/admin\/(?<postid>\d+)\/tags\/(?<tagname>.*)/, auth, async (req, res) => {
// delete tag
if(!req.params.postid || !req.params.tagname) {
return res.reply({ body: JSON.stringify({
success: false,
msg: "missing postid or tagname"
})});
}
const postid = +req.params.postid;
const tagname = decodeURIComponent(req.params.tagname);
const tags = await lib.getTags(postid);
const tagid = tags.filter(t => t.tag === tagname)[0]?.id ?? null;
if(!tagid) {
return res.reply({ body: JSON.stringify({
success: false,
msg: "tag is not assigned",
tags: await lib.getTags(postid)
})});
}
let q = sql("tags_assign").where("tag_id", tagid).andWhere("item_id", postid).del();
if(req.session.level < 50)
q = q.andWhere("user_id", req.session.id);
const reply = !!(await q);
return res.reply({ body: JSON.stringify({
success: reply,
tagid: tagid,
tags: await lib.getTags(postid)
})});
});
group.post(/\/admin\/(?<postid>\d+)\/tags/, auth, async (req, res) => {
// assign and/or create tag
if(!req.params.postid || !req.post.tagname) {
return res.reply({ body: JSON.stringify({
success: false,
msg: "missing postid or tag"
})});
}
const postid = +req.params.postid;
const tagname = req.post.tagname?.trim();
const postid = +req.post.postid;
const tag = req.post.tag?.trim();
if(tag.length >= 45) {
if(tagname.length >= 45) {
return res.reply({ body: JSON.stringify({
success: false,
msg: "tag is too long!"
@ -105,12 +142,12 @@ export default (router, tpl) => {
try {
let tagid;
const tag_exists = await sql("tags").select("id", "tag").where("tag", tag);
const tag_exists = await sql("tags").select("id", "tag").where("tag", tagname);
if(tag_exists.length === 0) { // create new tag
await sql("tags").insert({
tag: tag
tag: tagname
});
tagid = (await sql("tags").select("id").where("tag", tag))[0].id;
tagid = (await sql("tags").select("id").where("tag", tagname))[0].id;
}
else {
tagid = tag_exists[0].id;
@ -131,42 +168,22 @@ export default (router, tpl) => {
return res.reply({ body: JSON.stringify({
success: true,
postid: postid,
tag: tag,
tag: tagname,
tags: await lib.getTags(postid)
})});
});
group.post(/\/admin\/tags\/delete$/, auth, async (req, res) => {
if(!req.post.postid || !req.post.tagid) {
return res.reply({ body: JSON.stringify({
group.get(/\/admin\/(?<postid>\d+)\/tags$/, auth, async (req, res) => {
// get tags
if(!req.params.postid) {
return res.json({
success: false,
msg: "missing postid or tag"
})});
msg: "missing postid"
});
}
const postid = +req.post.postid;
const tagid = +req.post.tagid;
const tags = await lib.getTags(postid);
const tagcheck = tags.filter(t => t.id === tagid)[0]?.id ?? null;
if(!tagcheck || !tagid || tagid?.length === 0) {
return res.reply({ body: JSON.stringify({
success: false,
msg: "tag is not assigned",
tags: await lib.getTags(postid)
})});
}
let q = sql("tags_assign").where("tag_id", tagid).andWhere("item_id", postid).del();
if(req.session.level < 50)
q = q.andWhere("user_id", req.session.id);
const reply = !!(await q);
return res.reply({ body: JSON.stringify({
success: reply,
tagid: tagid,
tags: await lib.getTags(postid)
tags: await lib.getTags(+req.params.postid)
})});
});
@ -200,12 +217,6 @@ export default (router, tpl) => {
return res.reply({ body: JSON.stringify(reply) });
});
group.get(/\/admin\/tags\/get\/\d+$/, auth, async (req, res) => {
return res.reply({ body: JSON.stringify({
tags: await lib.getTags(+req.url.split[5])
})});
});
group.post(/\/admin\/deletepost$/, auth, async (req, res) => {
if(!req.post.postid) {
return res.reply({ body: JSON.stringify({