toggle sfw/nsfw
This commit is contained in:
parent
81adcde556
commit
e15c16434c
@ -47,7 +47,11 @@ const flash = ({ type, msg }) => {
|
|||||||
if(!confirm("Do you really want to delete this tag?"))
|
if(!confirm("Do you really want to delete this tag?"))
|
||||||
return;
|
return;
|
||||||
const tagname = e.target.parentElement.querySelector('a:first-child').innerText;
|
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)
|
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 +80,7 @@ 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", {
|
|
||||||
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 => {
|
const renderTags = _tags => {
|
||||||
[...document.querySelectorAll("#tags > .badge")].forEach(tag => tag.parentElement.removeChild(tag));
|
[...document.querySelectorAll("#tags > .badge")].forEach(tag => tag.parentElement.removeChild(tag));
|
||||||
_tags.reverse().forEach(tag => {
|
_tags.reverse().forEach(tag => {
|
||||||
@ -143,7 +134,9 @@ const flash = ({ type, msg }) => {
|
|||||||
const tmptag = input.value?.trim();
|
const tmptag = input.value?.trim();
|
||||||
if(tags.includes(tmptag))
|
if(tags.includes(tmptag))
|
||||||
return alert("tag already exists");
|
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) {
|
if(!res.success) {
|
||||||
return flash({
|
return flash({
|
||||||
type: "error",
|
type: "error",
|
||||||
@ -164,7 +157,7 @@ const flash = ({ type, msg }) => {
|
|||||||
clearTimeout(tt);
|
clearTimeout(tt);
|
||||||
|
|
||||||
tt = setTimeout(async () => {
|
tt = setTimeout(async () => {
|
||||||
globalTimeout = null;
|
tt = null;
|
||||||
|
|
||||||
const tmptag = input.value?.trim();
|
const tmptag = input.value?.trim();
|
||||||
|
|
||||||
@ -201,23 +194,10 @@ const flash = ({ type, msg }) => {
|
|||||||
const toggleEvent = async (e = false) => {
|
const toggleEvent = async (e = false) => {
|
||||||
if(e)
|
if(e)
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
let res;
|
|
||||||
if(tags.includes("sfw")) {
|
const res = await (await fetch('/api/v2/admin/' + encodeURIComponent(postid) + '/tags/toggle', {
|
||||||
await deleteTag(postid, "sfw");
|
method: 'PUT'
|
||||||
res = await addTag(postid, "nsfw");
|
})).json();
|
||||||
}
|
|
||||||
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
|
|
||||||
});
|
|
||||||
|
|
||||||
renderTags(res.tags);
|
renderTags(res.tags);
|
||||||
tags = res.tags.map(t => t.tag);
|
tags = res.tags.map(t => t.tag);
|
||||||
@ -233,7 +213,9 @@ const flash = ({ type, msg }) => {
|
|||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
if(!confirm(`Reason for deleting f0ckpost ${postid} by ${poster} (Weihnachten™)`))
|
if(!confirm(`Reason for deleting f0ckpost ${postid} by ${poster} (Weihnachten™)`))
|
||||||
return;
|
return;
|
||||||
const res = await deletePost(postid);
|
const res = await post("/api/v2/admin/deletepost", {
|
||||||
|
postid: postid
|
||||||
|
});
|
||||||
if(res.success) {
|
if(res.success) {
|
||||||
flash({
|
flash({
|
||||||
type: "success",
|
type: "success",
|
||||||
|
@ -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) => {
|
group.get(/\/admin\/tags\/suggest$/, auth, async (req, res) => {
|
||||||
const reply = {
|
const reply = {
|
||||||
success: false,
|
success: false,
|
||||||
|
Loading…
Reference in New Issue
Block a user