Added liking and security measures
This commit is contained in:
@@ -9,6 +9,7 @@ const {
|
||||
removeImageLink,
|
||||
removeUploadedFile,
|
||||
} = require("../../middleware/post_upload");
|
||||
const { likeDataForPosts } = require("./post_graph");
|
||||
|
||||
async function editPost(req, res) {
|
||||
const id = req.query.id;
|
||||
@@ -33,7 +34,7 @@ async function editPost(req, res) {
|
||||
|
||||
try {
|
||||
const existing = await pool.query(
|
||||
"SELECT id, image_link FROM posts WHERE id = $1 AND author_id = $2",
|
||||
"SELECT id, image_link FROM posts WHERE id = $1 AND author_id = $2 AND active = true",
|
||||
[id, req.user.id],
|
||||
);
|
||||
if (existing.rowCount === 0) {
|
||||
@@ -53,12 +54,13 @@ async function editPost(req, res) {
|
||||
if (text !== undefined) addField("text", text);
|
||||
if (req.file) addField("image_link", imageLinkForFile(req.file));
|
||||
if (removeImage) addField("image_link", null);
|
||||
fields.push("edited_last = NOW()");
|
||||
values.push(id, req.user.id);
|
||||
|
||||
let updated;
|
||||
try {
|
||||
updated = await pool.query(
|
||||
`WITH updated AS (UPDATE posts SET ${fields.join(", ")} WHERE id = $${values.length - 1} AND author_id = $${values.length} RETURNING id, title, text, author_id, image_link, created_at) SELECT updated.*, people.username AS author_username FROM updated LEFT JOIN people ON people.id = updated.author_id`,
|
||||
`WITH updated AS (UPDATE posts SET ${fields.join(", ")} WHERE id = $${values.length - 1} AND author_id = $${values.length} RETURNING id, title, text, author_id, image_link, created_at, edited_last) SELECT updated.*, people.username AS author_username FROM updated LEFT JOIN people ON people.id = updated.author_id`,
|
||||
values,
|
||||
);
|
||||
} catch (err) {
|
||||
@@ -71,7 +73,14 @@ async function editPost(req, res) {
|
||||
console.error("old post image could not be removed", { message: err.message });
|
||||
});
|
||||
}
|
||||
return res.status(200).json(updated.rows[0]);
|
||||
const likeData = (await likeDataForPosts(req.user.id, [updated.rows[0].id])).get(
|
||||
Number(updated.rows[0].id),
|
||||
) ?? { like_count: 0, liked_by_me: false };
|
||||
return res.status(200).json({
|
||||
...updated.rows[0],
|
||||
like_count: likeData.like_count,
|
||||
liked_by_me: likeData.liked_by_me,
|
||||
});
|
||||
} catch (err) {
|
||||
await removeUploadedFile(req.file);
|
||||
console.error("database query failed", {
|
||||
|
||||
Reference in New Issue
Block a user