Skip to content

Commit

Permalink
Added ban logs to DB, post & story delete on ban where content is log…
Browse files Browse the repository at this point in the history
…ged to DB for unban requests

Fixed clear cookies error, new logs middleware, new DB logs table, mod checks on separate non-blocking thread
  • Loading branch information
nidhish-nayak committed Apr 10, 2024
1 parent b831cd8 commit e9dd284
Show file tree
Hide file tree
Showing 8 changed files with 4,382 additions and 3,183 deletions.
7,388 changes: 4,289 additions & 3,099 deletions client/pnpm-lock.yaml

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion server/src/controllers/post/addPost.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ export const addPost = async (req: Request, res: Response) => {

if (error) return res.status(400).json("Post upload to DB has failed!");

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

return res.status(200).json(data);
} catch (error) {
return res.status(401).json(error);
Expand Down
6 changes: 5 additions & 1 deletion server/src/controllers/story/postStory.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ export const postStory = async (req: Request, res: Response) => {
return res.status(500).json("Story data upload to DB failed!");
}

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

return res.status(200).json(data);
};
100 changes: 50 additions & 50 deletions server/src/db/database.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,86 +465,86 @@ type PublicSchema = Database[Extract<keyof Database, "public">];

export type Tables<
PublicTableNameOrOptions extends
| keyof (PublicSchema["Tables"] & PublicSchema["Views"])
| { schema: keyof Database },
| keyof (PublicSchema["Tables"] & PublicSchema["Views"])
| { schema: keyof Database },
TableName extends PublicTableNameOrOptions extends {
schema: keyof Database;
}
? keyof (Database[PublicTableNameOrOptions["schema"]]["Tables"] &
Database[PublicTableNameOrOptions["schema"]]["Views"])
: never = never,
? keyof (Database[PublicTableNameOrOptions["schema"]]["Tables"] &
Database[PublicTableNameOrOptions["schema"]]["Views"])
: never = never,
> = PublicTableNameOrOptions extends { schema: keyof Database }
? (Database[PublicTableNameOrOptions["schema"]]["Tables"] &
Database[PublicTableNameOrOptions["schema"]]["Views"])[TableName] extends {
Row: infer R;
}
? R
: never
Database[PublicTableNameOrOptions["schema"]]["Views"])[TableName] extends {
Row: infer R;
}
? R
: never
: PublicTableNameOrOptions extends keyof (PublicSchema["Tables"] &
PublicSchema["Views"])
? (PublicSchema["Tables"] &
PublicSchema["Views"])[PublicTableNameOrOptions] extends {
PublicSchema["Views"])
? (PublicSchema["Tables"] &
PublicSchema["Views"])[PublicTableNameOrOptions] extends {
Row: infer R;
}
? R
: never
: never;
? R
: never
: never;

export type TablesInsert<
PublicTableNameOrOptions extends
| keyof PublicSchema["Tables"]
| { schema: keyof Database },
| keyof PublicSchema["Tables"]
| { schema: keyof Database },
TableName extends PublicTableNameOrOptions extends {
schema: keyof Database;
}
? keyof Database[PublicTableNameOrOptions["schema"]]["Tables"]
: never = never,
? keyof Database[PublicTableNameOrOptions["schema"]]["Tables"]
: never = never,
> = PublicTableNameOrOptions extends { schema: keyof Database }
? Database[PublicTableNameOrOptions["schema"]]["Tables"][TableName] extends {
Insert: infer I;
}
? I
: never
Insert: infer I;
}
? I
: never
: PublicTableNameOrOptions extends keyof PublicSchema["Tables"]
? PublicSchema["Tables"][PublicTableNameOrOptions] extends {
Insert: infer I;
}
? I
: never
: never;
? PublicSchema["Tables"][PublicTableNameOrOptions] extends {
Insert: infer I;
}
? I
: never
: never;

export type TablesUpdate<
PublicTableNameOrOptions extends
| keyof PublicSchema["Tables"]
| { schema: keyof Database },
| keyof PublicSchema["Tables"]
| { schema: keyof Database },
TableName extends PublicTableNameOrOptions extends {
schema: keyof Database;
}
? keyof Database[PublicTableNameOrOptions["schema"]]["Tables"]
: never = never,
? keyof Database[PublicTableNameOrOptions["schema"]]["Tables"]
: never = never,
> = PublicTableNameOrOptions extends { schema: keyof Database }
? Database[PublicTableNameOrOptions["schema"]]["Tables"][TableName] extends {
Update: infer U;
}
? U
: never
Update: infer U;
}
? U
: never
: PublicTableNameOrOptions extends keyof PublicSchema["Tables"]
? PublicSchema["Tables"][PublicTableNameOrOptions] extends {
Update: infer U;
}
? U
: never
: never;
? PublicSchema["Tables"][PublicTableNameOrOptions] extends {
Update: infer U;
}
? U
: never
: never;

export type Enums<
PublicEnumNameOrOptions extends
| keyof PublicSchema["Enums"]
| { schema: keyof Database },
| keyof PublicSchema["Enums"]
| { schema: keyof Database },
EnumName extends PublicEnumNameOrOptions extends { schema: keyof Database }
? keyof Database[PublicEnumNameOrOptions["schema"]]["Enums"]
: never = never,
? keyof Database[PublicEnumNameOrOptions["schema"]]["Enums"]
: never = never,
> = PublicEnumNameOrOptions extends { schema: keyof Database }
? Database[PublicEnumNameOrOptions["schema"]]["Enums"][EnumName]
: PublicEnumNameOrOptions extends keyof PublicSchema["Enums"]
? PublicSchema["Enums"][PublicEnumNameOrOptions]
: never;
? PublicSchema["Enums"][PublicEnumNameOrOptions]
: never;
32 changes: 8 additions & 24 deletions server/src/middlewares/moderator.middleware.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import config from "$/config/config";
import { Response } from "express";
import axios from "axios";

import { addBan, getModerationAxiosConfig } from "$/utils/axios.util";
import clearAllCookies from "$/utils/cookie.util";
import { supabase } from "$/db/connect";
import logDetails from "$/utils/log.util";

const moderatorCheck = async (
res: Response,
userId: number,
id: number,
post: boolean,
Expand All @@ -18,54 +15,41 @@ const moderatorCheck = async (
return;
}

// Get axios headers setup
const options = getModerationAxiosConfig(imageUrl);

try {
const options = getModerationAxiosConfig(imageUrl);
const response = await axios.request(options);

if (response.data.unsafe === true) {
const banAdded = await addBan(userId);
if (banAdded === false)
return console.log("User ban failed upon unsafe upload!");

// Delete post or story if user banned
if (post) {
// Add logs of images for unban requests
const { error: logError } = await supabase
.from("logs")
.insert({ user_id: userId, img: imageUrl, type: "post" });
if (logError) return console.log("Error during post log!");
await logDetails(userId, imageUrl, "posts");

// Remove actual post
const { error } = await supabase
.from("posts")
.delete()
.eq("userId", userId)
.eq("id", id);
if (error) return console.log("Error during post delete!");
} else {
const { error: logError } = await supabase
.from("logs")
.insert({ user_id: userId, img: imageUrl, type: "story" });
if (logError) return console.log("Error logging story delete!");
await logDetails(userId, imageUrl, "stories");

// Remove actual story
const { error } = await supabase
.from("stories")
.delete()
.eq("userId", userId)
.eq("id", id);
if (error) return console.log("Error during Story delete!");
if (error) return console.log("Error during story delete!");
}

// Logout user on ban
clearAllCookies(res);
// Log the banned userId on server
console.log(`UserId: ${userId} was banned!`);
}
return;
} catch (error) {
if (error.code === "ERR_HTTP_HEADERS_SENT") {
return console.log("User was banned, cookie was cleared");
}
return console.log("Failed to request ban check from API!");
}
};
Expand Down
6 changes: 3 additions & 3 deletions server/src/middlewares/multer.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ export const uploadMulter = multer({
bucket: config.s3Config.bucketLink,
acl: "private",
contentType: multerS3.AUTO_CONTENT_TYPE,
key: function(req: Request, file, cb) {
key: function (req: Request, file, cb) {
cb(
null,
`${req.body.folder}/` +
Date.now().toString() +
file.originalname
Date.now().toString() +
file.originalname
);
},
}),
Expand Down
14 changes: 9 additions & 5 deletions server/src/utils/cookie.util.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { Response } from "express";

const clearAllCookies = (res: Response) => {
res.clearCookie("accessToken", {
httpOnly: true,
secure: true,
sameSite: "none",
});
try {
return res.clearCookie("accessToken", {
httpOnly: true,
secure: true,
sameSite: "none",
});
} catch (error) {
return console.log("Cookie clear failed!");
}
};

export default clearAllCookies;
11 changes: 11 additions & 0 deletions server/src/utils/log.util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { supabase } from "$/db/connect";

const logDetails = async (userId: number, imageUrl: string, type: string) => {
const { error: logError } = await supabase
.from("logs")
.insert({ user_id: userId, img: imageUrl, type: type });

if (logError) return console.log(`Error during ${type} log!`);
};

export default logDetails;

0 comments on commit e9dd284

Please sign in to comment.