Skip to content

Commit

Permalink
Hide/unhide post and follow/unfollow post in user
Browse files Browse the repository at this point in the history
  • Loading branch information
Malakyasser8 committed Apr 10, 2024
1 parent 843064b commit 08e2982
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 74 deletions.
74 changes: 15 additions & 59 deletions src/controller/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@ import { toggler } from "../utils/toggler.js";
import { getPostCommentsHelper } from "../services/posts.js";

export async function getPost(request, verifyUser) {
let user;
if (verifyUser) {
const { success, err, status, user, msg } = await verifyAuthToken(request);
if (!user) {
const {
success,
err,
status,
user: verifiedUser,
msg,
} = await verifyAuthToken(request);
if (!verifiedUser) {
return { success, error: { status, message: err } };
}
user = verifiedUser;
}
const postId = request?.body?.id || request?.query?.id;
if (postId == undefined || postId == null) {
Expand All @@ -29,28 +37,18 @@ export async function getPost(request, verifyUser) {
return {
success: true,
post,
user,
message: "Post Retrieved sucessfully",
};
}

export async function getPostComments(request) {
const postId = request?.query?.id;

if (postId == undefined || postId == null) {
return {
success: false,
error: { status: 400, message: "Post id is required" },
};
}
const post = await Post.findById(postId);
if (!post) {
return {
success: false,
error: { status: 404, message: "Post Not found" },
};
const { success, error, post, message } = await getPost(request, false);
if (!success) {
return { success, error };
}

const comments = await getPostCommentsHelper(postId);
const comments = await getPostCommentsHelper(post._id);
return {
success: true,
comments,
Expand Down Expand Up @@ -166,48 +164,6 @@ export async function setSuggestedSort(request) {
}
}

export async function hideUnhidePost(request) {
const { success, err, status, user, msg } = await verifyAuthToken(request);
if (!user) {
return { success, error: { status, message: err } };
}

const postId = request?.body?.id;
if (postId == undefined || postId == null) {
return {
success: false,
error: { status: 400, message: "Post id is required" },
};
}
const post = await Post.findById(postId);
if (!post) {
return {
success: false,
error: { status: 404, message: "Post Not found" },
};
}
if (!user.hidden_and_reported_posts_ids.includes(post.id)) {
user.hidden_and_reported_posts_ids.push(post.id);
await user.save();
return {
success: true,
error: {},
message: "Post hidden sucessfully",
};
} else {
const indexToRemove = user.hidden_and_reported_posts_ids.indexOf(
post._id.toString()
);
user.hidden_and_reported_posts_ids.splice(indexToRemove, 1);
await user.save();
return {
success: true,
post,
message: "Post unhidden sucessfully",
};
}
}

export async function postToggler(request, toToggle) {
try {
// Verify the auth token
Expand Down
65 changes: 65 additions & 0 deletions src/controller/userActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { getSafetySettingsFormat } from "../utils/userSettings.js";
import { encodeXText } from "nodemailer/lib/shared/index.js";
import { followUserHelper } from "../services/users.js";
import bcrypt from "bcryptjs";
import { Post } from "../db/models/Post.js";
import { getPost } from "./posts.js";

export async function blockUser(request) {
try {
Expand Down Expand Up @@ -480,3 +482,66 @@ export async function deleteAccount(request) {
};
}
}

export async function followPost(request) {
try {
const { success, error, post, user, message } = await getPost(
request,
true
);
if (!success) {
return { success, error };
}

if (!user.followed_posts_ids.includes(post._id)) {
user.followed_posts_ids.push(post._id);
await user.save();
return {
success: true,
error: {},
message: "User has followed post sucessfully",
};
} else {
const indexToRemove = user.followed_posts_ids.indexOf(post._id);
console.log(indexToRemove);
user.followed_posts_ids.splice(indexToRemove, 1);
await user.save();
return {
success: true,
post,
message: "User has unfollowed post sucessfully",
};
}
} catch (e) {
return {
success: false,
error: { status: 500, message: e },
};
}
}

export async function hidePost(request) {
const { success, error, post, user, message } = await getPost(request, true);
if (!success) {
return { success, error };
}

if (!user.hidden_and_reported_posts_ids.includes(post._id)) {
user.hidden_and_reported_posts_ids.push(post._id);
await user.save();
return {
success: true,
error: {},
message: "Post hidden sucessfully",
};
} else {
const indexToRemove = user.hidden_and_reported_posts_ids.indexOf(post._id);
user.hidden_and_reported_posts_ids.splice(indexToRemove, 1);
await user.save();
return {
success: true,
post,
message: "Post unhidden sucessfully",
};
}
}
2 changes: 1 addition & 1 deletion src/db/models/Post.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export const postSchema = new mongoose.Schema({
community_name: {
type: String,
},
followers_ids: [{ type: mongoose.Schema.Types.ObjectId, ref: "User" }],
//removed followers users id as already each user has his followed posts
comments_count: { type: Number, default: 0, min: 0 },
views_count: { type: Number, default: 0, min: 0 },
shares_count: { type: Number, default: 0, min: 0 },
Expand Down
13 changes: 0 additions & 13 deletions src/routers/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
getViewsCount,
getPost,
getPostComments,
hideUnhidePost,
} from "../controller/posts.js";

dotenv.config();
Expand Down Expand Up @@ -92,15 +91,3 @@ postsRouter.patch("/posts/set-suggested-sort", async (req, res) => {
}
});

postsRouter.patch("/posts/hide-unhide", async (req, res) => {
try {
const { success, error, message } = await hideUnhidePost(req);
if (!success) {
res.status(error.status).send({ error });
return;
}
res.status(200).send({ message });
} catch (e) {
res.status(500).send({ error: e });
}
});
30 changes: 29 additions & 1 deletion src/routers/users.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import express from "express";
import { User } from "../db/models/User.js";
import { User } from "../db/models/User.js";
import dotenv from "dotenv";
import axios from "axios";
import jwt from "jsonwebtoken";
Expand Down Expand Up @@ -57,6 +57,8 @@ import {
favoriteCommunity,
clearHistory,
deleteAccount,
hidePost,
followPost,
} from "../controller/userActions.js";

export const usersRouter = express.Router();
Expand Down Expand Up @@ -985,3 +987,29 @@ usersRouter.post("/users/delete-account", async (req, res) => {
});
}
});

usersRouter.post("/users/follow-unfollow-post", async (req, res) => {
try {
const { success, error, message } = await followPost(req);
if (!success) {
res.status(error.status).send({ error });
return;
}
res.status(200).send({ message });
} catch (e) {
res.status(500).send({ error: e });
}
});

usersRouter.post("/users/hide-unhide-post", async (req, res) => {
try {
const { success, error, message } = await hidePost(req);
if (!success) {
res.status(error.status).send({ error });
return;
}
res.status(200).send({ message });
} catch (e) {
res.status(500).send({ error: e });
}
});

0 comments on commit 08e2982

Please sign in to comment.