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

@ -46,8 +46,8 @@ const flash = ({ type, msg }) => {
e.preventDefault();
if(!confirm("Do you really want to delete this tag?"))
return;
const tagid = +e.target.parentElement.dataset.tagid;
const res = await deleteTag(postid, tagid);
const tagname = e.target.parentElement.querySelector('a:first-child').innerText;
const res = await deleteTag(postid, tagname);
if(!res.success)
return alert("uff");
tags = res.tags.map(t => t.tag);
@ -76,20 +76,19 @@ 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, tag) => await post("/api/v2/admin/tags/add", {
postid: postid,
tag: tag
const addTag = async (postid, tagname) => await post("/api/v2/admin/" + postid + "/tags", {
tagname: tagname
});
const deleteTag = async (postid, tagid) => await post("/api/v2/admin/tags/delete", {
postid: postid,
tagid: tagid
});
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));
@ -102,7 +101,6 @@ const flash = ({ type, msg }) => {
const span = document.createElement("span");
span.classList.add("badge", "mr-2");
span.setAttribute('tooltip', tag.user);
span.dataset.tagid = tag.id;
tag.badge.split(" ").forEach(b => span.classList.add(b));
@ -205,11 +203,11 @@ const flash = ({ type, msg }) => {
e.preventDefault();
let res;
if(tags.includes("sfw")) {
await deleteTag(postid, 1);
await deleteTag(postid, "sfw");
res = await addTag(postid, "nsfw");
}
else if(tags.includes("nsfw")) {
await deleteTag(postid, 2);
await deleteTag(postid, "nsfw");
res = await addTag(postid, "sfw");
}
else

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.post.postid;
const tag = req.post.tag?.trim();
const postid = +req.params.postid;
const tagname = req.post.tagname?.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({

View File

@ -66,7 +66,7 @@
<span class="badge badge-dark" id="tags">
@if(typeof item.tags !== "undefined")
@each(item.tags as tag)
<span @if(session)tooltip="{{ tag.user }} ({{ tag.normalized }})" data-tagid="{{ tag.id }}"@endif class="badge {{ tag.badge }} mr-2">
<span @if(session)tooltip="{{ tag.user }} ({{ tag.normalized }})"@endif class="badge {{ tag.badge }} mr-2">
<a href="/tag/{{ tag.tag }}">{!! tag.tag !!}</a>@if(session)&nbsp;<a href="#">&#215;</a>@endif
</span>
@endeach