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