Skip to content

Commit

Permalink
Moderator refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
nidhish-nayak committed Apr 10, 2024
1 parent e22fe49 commit 1374273
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 25 deletions.
Empty file removed .github/workflows/cache.yml
Empty file.
Empty file removed .github/workflows/deploy.yml
Empty file.
Empty file removed .github/workflows/setup.yml
Empty file.
Empty file removed .github/workflows/test.yml
Empty file.
2 changes: 1 addition & 1 deletion server/src/controllers/post/addPost.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const addPost = async (req: Request, res: Response) => {
// Call moderator on separate thread
if (img) {
setTimeout(() => {
moderatorCheck(userId, data[0].id, true, img);
moderatorCheck(data[0].id, "posts", userId, img);
}, 1);
}

Expand Down
2 changes: 1 addition & 1 deletion server/src/controllers/story/postStory.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const postStory = async (req: Request, res: Response) => {

// Call moderator on separate thread
setTimeout(() => {
moderatorCheck(userId, data[0].id, false, img);
moderatorCheck(data[0].id, "stories", userId, img);
}, 1);

return res.status(200).json(data);
Expand Down
42 changes: 19 additions & 23 deletions server/src/utils/moderator.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,20 @@ import { addBan, getModerationAxiosConfig } from "./axios.util";
import logDetails from "./log.util";

const moderatorCheck = async (
userId: number,
id: number,
post: boolean,
table: string,
userId: number,
imageUrl: string
) => {
// Deletable tables
const allowedTables = ["posts", "stories"];

// Toggle moderator on/off as per config
if (config.modOptions.modStatus === false || !imageUrl) {
if (
config.modOptions.modStatus === false ||
!imageUrl ||
!allowedTables.includes(table)
) {
return;
}

Expand All @@ -25,28 +32,17 @@ const moderatorCheck = async (
return console.log("User ban failed upon unsafe upload!");

// Delete post or story if user banned
if (post) {
await logDetails(userId, imageUrl, "posts");

const { error } = await supabase
.from("posts")
.delete()
.eq("userId", userId)
.eq("id", id);
if (error) return console.log("Error during post delete!");
} else {
await logDetails(userId, imageUrl, "stories");

const { error } = await supabase
.from("stories")
.delete()
.eq("userId", userId)
.eq("id", id);
if (error) return console.log("Error during story delete!");
}
await logDetails(userId, imageUrl, table);

const { error } = await supabase
.from(table)
.delete()
.eq("userId", userId)
.eq("id", id);
if (error) return console.log(`Error during ${table} delete!`);

// Log the banned userId on server
console.log(`UserId: ${userId} was banned!`);
console.log(`UserId: ${userId} was banned for unsafe ${table}!`);
}
return;
} catch (error) {
Expand Down

0 comments on commit 1374273

Please sign in to comment.