Delete profiles and posts

This commit is contained in:
aeroreyna
2022-02-16 20:33:20 -08:00
parent 294cacf963
commit 117bc5833a
5 changed files with 63 additions and 2 deletions

View File

@@ -9,7 +9,12 @@ DB.getDB.then((DB) => {
const getProfileId = (req) => {
return DB.ObjectID(req.cookies.profile_id || req.query.profile_id || req.body.profile_id);
}
};
const postBelongToProfile = (post, profileid) => {
if(!post) return false;
return post.profileid === profileid;
};
router.get("/", async (req, res) => {
const profileid = getProfileId(req);
@@ -144,6 +149,20 @@ DB.getDB.then((DB) => {
});
});
router.delete("/:id", async (req, res) => {
const profileid = getProfileId(req);
const postId = req.params.id;
const post = await DB.getPost(postId);
if(!postBelongToProfile(post, profileid))
return res.json({
status: "This post is not yours."
});
await DB.removePost(postId);
return res.json({
status: "ok"
});
});
});
module.exports = router

View File

@@ -16,6 +16,12 @@ DB.getDB.then((DB)=>{
return DB.ObjectID(req.cookies.profile_id || req.query.profile_id || req.body.profile_id);
}
const profileBelongsToUser = async (profileid, userid) => {
const profile = await DB.getProfileCache(profileid);
if(!profile) return false;
return profile.userid == (userid + '');
}
router.get("/mine", async (req, res) => {
let userid = req.cookies.user_sid;
let profiles = await DB.getUserProfiles(userid);
@@ -239,6 +245,19 @@ DB.getDB.then((DB)=>{
});
});
router.delete("/:id", async (req, res) => {
const profileId = req.params.id;
const userid = getUserId(req);
if(!await profileBelongsToUser(profileId, userid))
return res.json({
status: "This profile is not yours."
});
await DB.removeProfile(profileId);
return res.json({
status: "ok"
});
});
router.get("/:id/follow", async (req, res) => {
let followProfileId = req.params.id;
const profileid = getProfileId(req);