From 08e2982f29dff46c536da92f102f64f34a6f84a6 Mon Sep 17 00:00:00 2001 From: Malakyasser8 Date: Wed, 10 Apr 2024 08:35:30 +0200 Subject: [PATCH] Hide/unhide post and follow/unfollow post in user --- src/controller/posts.js | 74 +++++++---------------------------- src/controller/userActions.js | 65 ++++++++++++++++++++++++++++++ src/db/models/Post.js | 2 +- src/routers/posts.js | 13 ------ src/routers/users.js | 30 +++++++++++++- 5 files changed, 110 insertions(+), 74 deletions(-) diff --git a/src/controller/posts.js b/src/controller/posts.js index 2770b67..c3b0b46 100644 --- a/src/controller/posts.js +++ b/src/controller/posts.js @@ -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) { @@ -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, @@ -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 diff --git a/src/controller/userActions.js b/src/controller/userActions.js index de2f89b..2510d59 100644 --- a/src/controller/userActions.js +++ b/src/controller/userActions.js @@ -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 { @@ -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", + }; + } +} diff --git a/src/db/models/Post.js b/src/db/models/Post.js index 224beda..81acb9d 100644 --- a/src/db/models/Post.js +++ b/src/db/models/Post.js @@ -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 }, diff --git a/src/routers/posts.js b/src/routers/posts.js index c61b9c5..f006b23 100644 --- a/src/routers/posts.js +++ b/src/routers/posts.js @@ -7,7 +7,6 @@ import { getViewsCount, getPost, getPostComments, - hideUnhidePost, } from "../controller/posts.js"; dotenv.config(); @@ -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 }); - } -}); diff --git a/src/routers/users.js b/src/routers/users.js index 65d7b86..c055c80 100644 --- a/src/routers/users.js +++ b/src/routers/users.js @@ -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"; @@ -57,6 +57,8 @@ import { favoriteCommunity, clearHistory, deleteAccount, + hidePost, + followPost, } from "../controller/userActions.js"; export const usersRouter = express.Router(); @@ -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 }); + } +});