res.reply -> res.json

This commit is contained in:
Flummi 2022-03-26 20:22:14 +01:00
parent 089a6fc629
commit d6e22a587b

View File

@ -23,11 +23,16 @@ export default (router, tpl) => {
const user = req.url.split[3] === "user" ? req.url.split[4] : "%";
const mime = (allowedMimes.filter(n => req.url.split[3]?.startsWith(n))[0] ? req.url.split[3] : "") + "%";
const rows = await sql("items").orderByRaw("rand()").limit(1).where("mime", "ilike", mime).andWhere("username", "ilike", user);
const rows = await sql("items")
.orderByRaw("rand()")
.limit(1)
.where("mime", "ilike", mime)
.andWhere("username", "ilike", user);
res
.writeHead(200, { "Content-Type": "application/json" })
.end(JSON.stringify(rows.length > 0 ? rows[0] : []), "utf-8");
return res.json({
success: rows.length > 0,
items: rows.length > 0 ? rows[0] : []
});
});
group.get(/\/p\/([0-9]+)/, async (req, res) => { // legacy
@ -41,8 +46,10 @@ export default (router, tpl) => {
last: rows[rows.length - 1].id
};
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify(items), "utf-8");
return res.json({
success: true,
items
});
});
group.get(/\/item\/[0-9]+$/, async (req, res) => {
@ -52,8 +59,12 @@ export default (router, tpl) => {
const next = await sql("items").select("id").where("id", ">", id).orderBy("id").limit(1);
const prev = await sql("items").select("id").where("id", "<", id).orderBy("id", "desc").limit(1);
if(item.length === 0)
return "nope";
if(item.length === 0) {
return res.json({
success: false,
msg: 'no items found'
});
}
const rows = {
...item[0],
@ -62,9 +73,10 @@ export default (router, tpl) => {
prev: prev[0]?.id ?? null
}
};
res.reply({
type: "application/json",
body: JSON.stringify(rows)
return res.json({
success: true,
rows
});
});
@ -78,9 +90,9 @@ export default (router, tpl) => {
.orderBy("stamp", "desc")
.limit(eps);
res.reply({
type: "application/json",
body: JSON.stringify(rows.length > 0 ? rows : [])
return res.json({
success: rows.length > 0,
items: rows.length > 0 ? rows : []
});
});
@ -88,10 +100,10 @@ export default (router, tpl) => {
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({
return res.json({
success: false,
msg: "missing postid or tagname"
})});
msg: 'missing postid or tagname'
});
}
const postid = +req.params.postid;
@ -102,11 +114,11 @@ export default (router, tpl) => {
const tagid = tags.filter(t => t.tag === tagname)[0]?.id ?? null;
if(!tagid) {
return res.reply({ body: JSON.stringify({
return res.json({
success: false,
msg: "tag is not assigned",
msg: 'tag is not assigned',
tags: await lib.getTags(postid)
})});
});
}
let q = sql("tags_assign").where("tag_id", tagid).andWhere("item_id", postid).del();
@ -114,43 +126,43 @@ export default (router, tpl) => {
q = q.andWhere("user_id", req.session.id);
const reply = !!(await q);
return res.reply({ body: JSON.stringify({
return res.json({
success: reply,
tagid: 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.json({
success: false,
msg: "missing postid or tag"
})});
msg: 'missing postid or tag'
});
}
const postid = +req.params.postid;
const tagname = req.post.tagname?.trim();
if(tagname.length >= 45) {
return res.reply({ body: JSON.stringify({
return res.json({
success: false,
msg: "tag is too long!"
})});
msg: 'tag is too long!'
});
}
try {
let tagid;
const tag_exists = await sql("tags").select("id", "tag").where("tag", tagname);
if(tag_exists.length === 0) { // create new tag
await sql("tags").insert({
let tagid = (
await sql("tags")
.select("id")
.whereRaw("normalized = slugify(?)", [ tagname ])
)?.[0]?.id;
if(!tagid) { // create new tag
tagid = (await sql("tags").returning("id").insert({
tag: tagname
});
tagid = (await sql("tags").select("id").where("tag", tagname))[0].id;
}
else {
tagid = tag_exists[0].id;
}))[0];
}
await sql("tags_assign").insert({
tag_id: tagid,
@ -158,19 +170,19 @@ export default (router, tpl) => {
user_id: req.session.id
});
} catch(err) {
return res.reply({ body: JSON.stringify({
return res.json({
success: false,
msg: err.message,
tags: await lib.getTags(postid)
})});
});
}
return res.reply({ body: JSON.stringify({
return res.json({
success: true,
postid: postid,
tag: tagname,
tags: await lib.getTags(postid)
})});
});
});
group.get(/\/admin\/(?<postid>\d+)\/tags$/, auth, async (req, res) => {
@ -178,13 +190,14 @@ export default (router, tpl) => {
if(!req.params.postid) {
return res.json({
success: false,
msg: "missing postid"
msg: 'missing postid'
});
}
return res.reply({ body: JSON.stringify({
return res.json({
success: true,
tags: await lib.getTags(+req.params.postid)
})});
});
});
group.put(/\/admin\/(?<postid>\d+)\/tags\/toggle$/, auth, async (req, res) => {
@ -192,7 +205,7 @@ export default (router, tpl) => {
if(!req.params.postid) {
return res.json({
success: false,
msg: "missing postid"
msg: 'missing postid'
});
}
@ -216,9 +229,10 @@ export default (router, tpl) => {
.andWhere('item_id', postid);
}
return res.reply({ body: JSON.stringify({
return res.json({
success: true,
tags: await lib.getTags(postid)
})});
});
});
group.get(/\/admin\/tags\/suggest$/, auth, async (req, res) => {
@ -231,7 +245,7 @@ export default (router, tpl) => {
if(searchString?.length <= 1) {
reply.error = 'too short lol';
return res.reply({ body: JSON.stringify(reply) });
return res.json(reply);
}
try {
@ -248,22 +262,23 @@ export default (router, tpl) => {
reply.error = err.msg;
}
return res.reply({ body: JSON.stringify(reply) });
return res.json(reply);
});
group.post(/\/admin\/deletepost$/, auth, async (req, res) => {
if(!req.post.postid) {
return res.reply({ body: JSON.stringify({
success: true,
msg: "no postid"
})});
return res.json({
success: false,
msg: 'no postid'
});
}
const postid = +req.post.postid;
await sql("items").where("id", postid).del();
res.reply({ body: JSON.stringify({
res.json({
success: true
})});
});
});
group.post(/\/admin\/togglefav$/, auth, async (req, res) => {
@ -289,11 +304,11 @@ export default (router, tpl) => {
.leftJoin('user_options', 'user_options.user_id', 'favorites.user_id')
.where('favorites.item_id', postid);
res.reply({ body: JSON.stringify({
return res.json({
success: true,
itemid: postid,
favs: favs
})});
favs
});
});
});