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

View File

@ -85,18 +85,55 @@ export default (router, tpl) => {
}); });
// adminzeugs // adminzeugs
group.post(/\/admin\/tags\/add$/, auth, async (req, res) => { group.delete(/\/admin\/(?<postid>\d+)\/tags\/(?<tagname>.*)/, auth, async (req, res) => {
if(!req.post.postid || !req.post.tag) { // 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({ 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.params.postid;
const tag = req.post.tag?.trim(); const tagname = req.post.tagname?.trim();
if(tag.length >= 45) { if(tagname.length >= 45) {
return res.reply({ body: JSON.stringify({ return res.reply({ body: JSON.stringify({
success: false, success: false,
msg: "tag is too long!" msg: "tag is too long!"
@ -105,12 +142,12 @@ export default (router, tpl) => {
try { try {
let tagid; 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 if(tag_exists.length === 0) { // create new tag
await sql("tags").insert({ 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 { else {
tagid = tag_exists[0].id; tagid = tag_exists[0].id;
@ -131,42 +168,22 @@ export default (router, tpl) => {
return res.reply({ body: JSON.stringify({ return res.reply({ body: JSON.stringify({
success: true, success: true,
postid: postid, postid: postid,
tag: tag, tag: tagname,
tags: await lib.getTags(postid) tags: await lib.getTags(postid)
})}); })});
}); });
group.post(/\/admin\/tags\/delete$/, auth, async (req, res) => { group.get(/\/admin\/(?<postid>\d+)\/tags$/, auth, async (req, res) => {
if(!req.post.postid || !req.post.tagid) { // get tags
return res.reply({ body: JSON.stringify({ if(!req.params.postid) {
return res.json({
success: false, 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({ return res.reply({ body: JSON.stringify({
success: false, tags: await lib.getTags(+req.params.postid)
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)
})}); })});
}); });
@ -200,12 +217,6 @@ export default (router, tpl) => {
return res.reply({ body: JSON.stringify(reply) }); 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) => { group.post(/\/admin\/deletepost$/, auth, async (req, res) => {
if(!req.post.postid) { if(!req.post.postid) {
return res.reply({ body: JSON.stringify({ return res.reply({ body: JSON.stringify({

View File

@ -66,7 +66,7 @@
<span class="badge badge-dark" id="tags"> <span class="badge badge-dark" id="tags">
@if(typeof item.tags !== "undefined") @if(typeof item.tags !== "undefined")
@each(item.tags as tag) @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 <a href="/tag/{{ tag.tag }}">{!! tag.tag !!}</a>@if(session)&nbsp;<a href="#">&#215;</a>@endif
</span> </span>
@endeach @endeach