toggle sfw/nsfw

This commit is contained in:
Flummi 2022-03-25 15:24:12 +01:00
parent 81adcde556
commit e15c16434c
2 changed files with 50 additions and 34 deletions

View File

@ -47,7 +47,11 @@ const flash = ({ type, msg }) => {
if(!confirm("Do you really want to delete this tag?"))
return;
const tagname = e.target.parentElement.querySelector('a:first-child').innerText;
const res = await deleteTag(postid, tagname);
const res = await (await fetch("/api/v2/admin/" + postid + "/tags/" + encodeURIComponent(tagname), {
method: 'DELETE'
})).json();
if(!res.success)
return alert("uff");
tags = res.tags.map(t => t.tag);
@ -76,20 +80,7 @@ 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, tagname) => await post("/api/v2/admin/" + postid + "/tags", {
tagname: tagname
});
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));
_tags.reverse().forEach(tag => {
@ -143,7 +134,9 @@ const flash = ({ type, msg }) => {
const tmptag = input.value?.trim();
if(tags.includes(tmptag))
return alert("tag already exists");
const res = await addTag(postid, tmptag);
const res = await post("/api/v2/admin/" + postid + "/tags", {
tagname: tmptag
});
if(!res.success) {
return flash({
type: "error",
@ -164,7 +157,7 @@ const flash = ({ type, msg }) => {
clearTimeout(tt);
tt = setTimeout(async () => {
globalTimeout = null;
tt = null;
const tmptag = input.value?.trim();
@ -201,23 +194,10 @@ const flash = ({ type, msg }) => {
const toggleEvent = async (e = false) => {
if(e)
e.preventDefault();
let res;
if(tags.includes("sfw")) {
await deleteTag(postid, "sfw");
res = await addTag(postid, "nsfw");
}
else if(tags.includes("nsfw")) {
await deleteTag(postid, "nsfw");
res = await addTag(postid, "sfw");
}
else
res = await addTag(postid, "sfw");
if(!res.success)
return flash({
type: "error",
msg: res.msg
});
const res = await (await fetch('/api/v2/admin/' + encodeURIComponent(postid) + '/tags/toggle', {
method: 'PUT'
})).json();
renderTags(res.tags);
tags = res.tags.map(t => t.tag);
@ -233,7 +213,9 @@ const flash = ({ type, msg }) => {
e.preventDefault();
if(!confirm(`Reason for deleting f0ckpost ${postid} by ${poster} (Weihnachten™)`))
return;
const res = await deletePost(postid);
const res = await post("/api/v2/admin/deletepost", {
postid: postid
});
if(res.success) {
flash({
type: "success",

View File

@ -187,6 +187,40 @@ export default (router, tpl) => {
})});
});
group.put(/\/admin\/(?<postid>\d+)\/tags\/toggle$/, auth, async (req, res) => {
// xD
if(!req.params.postid) {
return res.json({
success: false,
msg: "missing postid"
});
}
const postid = +req.params.postid;
if(!(await lib.getTags(postid)).filter(tag => [1,2].includes(tag.id)).length) {
// insert
await sql('tags_assign').insert({
item_id: postid,
tag_id: 1,
user_id: req.session.id
});
}
else {
// update
await sql('tags_assign')
.update({
tag_id: sql.raw('(array[2,1])[tag_id]')
})
.whereRaw('tag_id = any(array[1,2])')
.andWhere('item_id', postid);
}
return res.reply({ body: JSON.stringify({
tags: await lib.getTags(postid)
})});
});
group.get(/\/admin\/tags\/suggest$/, auth, async (req, res) => {
const reply = {
success: false,