-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
33 changed files
with
634 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) {} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) {} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.