Skip to content

Commit

Permalink
feat: 댓글, 게시글 신고 및 admin 페이지 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
Hellol77 committed Jan 25, 2024
1 parent c08fdd0 commit 15c5920
Show file tree
Hide file tree
Showing 33 changed files with 634 additions and 32 deletions.
29 changes: 29 additions & 0 deletions backend/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@ provider:
environment:
NODE_ENV: production
functions:
reportComment:
handler: src/index.handler
events:
- httpApi:
method: POST
path: /post/comment/report
cors: true
reportPost:
handler: src/index.handler
events:
- httpApi:
method: POST
path: /post/report
cors: true
addComment:
handler: src/index.handler
events:
Expand Down Expand Up @@ -109,6 +123,21 @@ functions:
method: POST
path: /post/dislike
cors: true
admin:
handler: src/index.handler
events:
- httpApi:
method: GET
path: /admin
cors: true
- httpApi:
method: GET
path: /admin/post/{proxy+}
cors: true
- httpApi:
method: GET
path: /admin/comment/{proxy+}
cors: true
plugins:
- serverless-webpack
# - serverless-dotenv-plugin
Expand Down
24 changes: 24 additions & 0 deletions backend/src/controllers/admin/checkAdmin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Request, Response } from "express";
import { getAccessTokenToheader } from "../../utils/getAccessTokenToHeader";
import { getUserObjectId } from "../../utils/getUserObjectId";
import { User } from "../../models/user";
export const checkAdmin = async (req: Request, res: Response) => {
const accessToken = getAccessTokenToheader(req);
if (!accessToken) {
return res.status(401).send("Unauthorized. Fail to get access token");
}
const _id = await getUserObjectId(req, res, accessToken);

if (!_id) {
return res.status(401).send("Unauthorized. Fail to get user object id");
}
try {
const user = await User.findOne({ _id }).lean();
if (user?.type === "admin") {
return res.status(200).send("admin");
}
return res.status(401).send("user");
} catch (err) {
return res.status(400).send("Fail check admin");
}
};
34 changes: 34 additions & 0 deletions backend/src/controllers/admin/getReportedComments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Request, Response } from "express";
import { getAccessTokenToheader } from "../../utils/getAccessTokenToHeader";
import { getUserObjectId } from "../../utils/getUserObjectId";
import { ReportedComment } from "../../models/repostComment";
import { User } from "../../models/user";

const limit = 24;

export const getReportedComments = async (req: Request, res: Response) => {
try {
const page = Number(req.params.page);
const accessToken = getAccessTokenToheader(req);
const _id = await getUserObjectId(req, res, accessToken);
const adminUser = await User.findOne({ _id }).lean();
if (adminUser?.type !== "admin") {
return res.status(401).send("Unauthorized. Fail to get access token");
}
const repostedComments = await ReportedComment.find({})
.populate({
path: "comment",
populate: { path: "user" },
})
.sort({ reportCount: -1 })
.limit(limit)
.skip((page - 1) * limit)
.lean();
const comments = repostedComments.map((post: any) => post.comment);

return res.status(200).send(comments);
} catch (err) {
console.log("getCommentReport", err);
return res.status(400).send("Fail get comment report");
}
};
34 changes: 34 additions & 0 deletions backend/src/controllers/admin/getReportedPosts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Request, Response } from "express";
import { getAccessTokenToheader } from "../../utils/getAccessTokenToHeader";
import { getUserObjectId } from "../../utils/getUserObjectId";
import { User } from "../../models/user";
import { ReportedPost } from "../../models/reportPost";

const limit = 24;

export const getReportedPosts = async (req: Request, res: Response) => {
try {
const page = Number(req.params.page);
const accessToken = getAccessTokenToheader(req);
const _id = await getUserObjectId(req, res, accessToken);
const adminUser = await User.findOne({ _id }).lean();
if (adminUser?.type !== "admin") {
return res.status(401).send("Unauthorized. Fail to get access token");
}
const repostedPosts = await ReportedPost.find({}, { post: 1, _id: 0 })
.populate({
path: "post",
populate: { path: "user" },
})
.sort({ reportCount: -1 })
.limit(limit)
.skip((page - 1) * limit);
console.log("repostedPosts", repostedPosts);
const posts = repostedPosts.map((post: any) => post.post);
console.log("repostedComments", posts);
return res.status(200).send(posts);
} catch (err) {
console.log("getCommentReport", err);
return res.status(400).send("Fail get comment report");
}
};
10 changes: 5 additions & 5 deletions backend/src/controllers/auth/kakaoAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const getKakaoLogin = async (req: Request, res: Response) => {
const newUserInfo = {
...userInfo,
nickname: getUserInfo.data.properties.nickname,
userId: userInfo?._id,
_id: userInfo?._id,
};
const userData = {
user: newUserInfo,
Expand All @@ -60,7 +60,7 @@ export const getKakaoLogin = async (req: Request, res: Response) => {

const newUserInfo = {
...userInfo,
userId: userInfo?._id,
_id: userInfo?._id,
};
const userData = {
user: newUserInfo,
Expand Down Expand Up @@ -113,7 +113,7 @@ export const refreshKakaoAccessToken = async (req: Request, res: Response) => {
}).lean();
const refreshUserInfo = {
...userInfo,
userId: userInfo?._id,
_id: userInfo?._id,
};
const userData = {
user: refreshUserInfo,
Expand Down Expand Up @@ -185,11 +185,11 @@ export const validateAccessToken = async (req: Request, res: Response) => {
}
const refreshedAccessToken = refreshInfo.data.access_token;

const data = { userId: userInfo?._id, accessToken: refreshedAccessToken };
const data = { _id: userInfo?._id, accessToken: refreshedAccessToken };
return res.status(200).send(data);
}

const data = { userId: userInfo?._id, accessToken };
const data = { _id: userInfo?._id, accessToken };
return res.status(200).send(data);
} catch (err) {
console.log(err);
Expand Down
8 changes: 7 additions & 1 deletion backend/src/controllers/post/deletePost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { Post } from "../../models/post";
import { User } from "../../models/user";
import { Comment } from "../../models/comment";
import s3 from "../../../config/s3Config";
import { ReportedPost } from "../../models/reportPost";
import { ReportedComment } from "../../models/repostComment";

export const deletePost = async (req: Request, res: Response) => {
const accessToken = getAccessTokenToheader(req);
Expand All @@ -18,7 +20,11 @@ export const deletePost = async (req: Request, res: Response) => {
try {
const postId = req.body.postId;
const deletedPost = await Post.findOneAndDelete({ _id: postId });

const user = await User.findOne({ _id }).lean();
if (user?.type === "admin") {
await ReportedPost.deleteMany({ post: postId });
await ReportedComment.deleteMany({ post: postId });
}
await s3.deleteObject({
Bucket: process.env.AWS_S3_POST_BUCKET,
Key: deletedPost?.imageUrl.split(process.env.AWS_S3_URL as string)[1],
Expand Down
48 changes: 48 additions & 0 deletions backend/src/controllers/post/reportComment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Request, Response } from "express";
import { ReportedComment } from "../../models/repostComment";

import { getUserObjectId } from "../../utils/getUserObjectId";
import { getAccessTokenToheader } from "../../utils/getAccessTokenToHeader";

export const reportComment = async (req: Request, res: Response) => {
const accessToken = getAccessTokenToheader(req);

if (!accessToken) {
return res.status(401).send("accessToken이 없습니다.");
}
const _id = await getUserObjectId(req, res, accessToken);
if (!_id) {
return res.status(401).send("Unauthorized. Fail to get user object id");
}

try {
const commentId = req.body.commentId;
const report = new ReportedComment({
reportUser: [_id],
comment: commentId,
});
const isExistReportedComment = await ReportedComment.exists({
comment: commentId,
});
if (isExistReportedComment) {
const alreadyReporteComment = await ReportedComment.findOneAndUpdate(
{
comment: commentId,
},
{ $addToSet: { reportUser: _id } },
{ new: true }
);
await ReportedComment?.updateOne(
{
comment: commentId,
},
{ $set: { reportCount: alreadyReporteComment?.reportUser.length } },
{ new: true }
);
return res.status(200).send("Success report post");
}

await report.save();
return res.status(200).send("Success report post");
} catch (err) {}
};
46 changes: 46 additions & 0 deletions backend/src/controllers/post/reportPost.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Request, Response } from "express";
import { ReportedPost } from "./../../models/reportPost";

import { getUserObjectId } from "../../utils/getUserObjectId";
import { getAccessTokenToheader } from "../../utils/getAccessTokenToHeader";

export const reportPost = async (req: Request, res: Response) => {
const accessToken = getAccessTokenToheader(req);

if (!accessToken) {
return res.status(401).send("accessToken이 없습니다.");
}
const _id = await getUserObjectId(req, res, accessToken);
if (!_id) {
return res.status(401).send("Unauthorized. Fail to get user object id");
}

try {
const postId = req.body.postId;
const report = new ReportedPost({
reportUser: [_id],
post: postId,
});
const isExistReportedPost = await ReportedPost.exists({
post: postId,
});
if (isExistReportedPost) {
const alreadyReportedPost = await ReportedPost.findOneAndUpdate(
{
post: postId,
},
{ $addToSet: { reportUser: _id } },
{ new: true }
);
await ReportedPost?.updateOne(
{ post: postId },
{ $set: { reportCount: alreadyReportedPost?.reportUser.length } },
{ new: true }
);
return res.status(200).send("Success report post");
}

await report.save();
return res.status(200).send("Success report post");
} catch (err) {}
};
2 changes: 2 additions & 0 deletions backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const mongoose = require("mongoose");
import { postRouter } from "./routes/post";
import { kakaoAuthRouter } from "./routes/kakaoAuth";
import { userRouter } from "./routes/user";
import { adminRouter } from "./routes/admin";

const app = express();

Expand All @@ -27,4 +28,5 @@ app.use(express.json());
app.use("/post", postRouter);
app.use("/auth/kakao", kakaoAuthRouter);
app.use("/user", userRouter);
app.use("/admin", adminRouter);
export const handler = serverless(app);
19 changes: 19 additions & 0 deletions backend/src/models/reportPost.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import mongoose from "mongoose";
const Schema = mongoose.Schema;

const reportedPostSchema = new Schema(
{
reportUser: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "user",
required: true,
},
],
post: { type: mongoose.Schema.Types.ObjectId, ref: "post", required: true },
reportCount: { type: Number, default: 1 },
},
{ versionKey: false }
);

export const ReportedPost = mongoose.model("reportedPost", reportedPostSchema);
26 changes: 26 additions & 0 deletions backend/src/models/repostComment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import mongoose from "mongoose";
const Schema = mongoose.Schema;

const reportedCommentSchema = new Schema(
{
reportUser: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "user",
required: true,
},
],
comment: {
type: mongoose.Schema.Types.ObjectId,
ref: "comment",
required: true,
},
reportCount: { type: Number, default: 1 },
},
{ versionKey: false }
);

export const ReportedComment = mongoose.model(
"reportedComment",
reportedCommentSchema
);
5 changes: 5 additions & 0 deletions backend/src/models/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ const userSchema = new Schema(
},
myPosts: [{ type: mongoose.Schema.Types.ObjectId, ref: "post" }], // 추가된 부분
likedPosts: [{ type: mongoose.Schema.Types.ObjectId, ref: "post" }],
type: {
type: String,
enum: ["admin", "user"],
default: "user",
},
},
{ versionKey: false }
);
Expand Down
11 changes: 11 additions & 0 deletions backend/src/routes/admin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Router } from "express";
import { checkAdmin } from "../controllers/admin/checkAdmin";
import { getReportedPosts } from "../controllers/admin/getReportedPosts";
import { getReportedComments } from "../controllers/admin/getReportedComments";

const adminRouter = Router();

adminRouter.get("/", checkAdmin);
adminRouter.get("/post/:page", getReportedPosts);
adminRouter.get("/comment/:page", getReportedComments);
export { adminRouter };
4 changes: 4 additions & 0 deletions backend/src/routes/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { addComment } from "../controllers/post/addComment";
import { deletePost } from "../controllers/post/deletePost";
import { deleteComment } from "../controllers/post/deleteComment";
import { editPost } from "../controllers/post/editPost";
import { reportPost } from "../controllers/post/reportPost";
import { reportComment } from "../controllers/post/reportComment";

const postRouter = Router();
postRouter.post("/", awsUpload.single("imageUrl"), uploadPost);
Expand All @@ -25,4 +27,6 @@ postRouter.post("/comment", addComment);
postRouter.delete("/", deletePost);
postRouter.delete("/comment", deleteComment);
postRouter.patch("/", editPost);
postRouter.post("/report", reportPost);
postRouter.post("/comment/report", reportComment);
export { postRouter };
Loading

0 comments on commit 15c5920

Please sign in to comment.