diff --git a/src/controllers/userController.ts b/src/controllers/userController.ts index 75b1e61..d99a0dc 100644 --- a/src/controllers/userController.ts +++ b/src/controllers/userController.ts @@ -14,22 +14,24 @@ interface GetUser extends Request { //TODO: add a user here that would contain the user data. } -export const getCurrentUser = catchAsync(async (req: GetUser, res: Response) => { - const userId = req.user.id; - - const user = await User.findById(userId); - - if (!user) { - throw new AppError('No User exists with this ID', 404); +export const getCurrentUser = catchAsync( + async (req: GetUser, res: Response) => { + const userId = req.user.id; + + const user = await User.findById(userId); + + if (!user) { + throw new AppError('No User exists with this ID', 404); + } + return res.status(200).json({ + status: 'success', + message: 'User retrieved successfuly', + data: { + user, + }, + }); } - return res.status(200).json({ - status: 'success', - message: 'User retrieved successfuly', - data: { - user, - }, - }); -}); +); export const updateCurrentUser = catchAsync(async (req: any, res: Response) => { const userData = req.body; @@ -84,7 +86,14 @@ export const getUser = catchAsync(async (req: GetUser, res: Response) => { throw new AppError('No User exists with this ID', 404); } - const fieldsToGet = ['username', 'screenFirstName', 'screenLastName', 'email', 'status', 'bio']; + const fieldsToGet = [ + 'username', + 'screenFirstName', + 'screenLastName', + 'email', + 'status', + 'bio', + ]; if ( user.picturePrivacy === 'everyone' || @@ -123,7 +132,11 @@ export const updateBio = catchAsync(async (req: any, res: Response) => { const { bio } = req.body; const userId = req.user.id; - const user = await User.findByIdAndUpdate(userId, { bio }, { new: true, runValidators: true }); + const user = await User.findByIdAndUpdate( + userId, + { bio }, + { new: true, runValidators: true } + ); if (!user) { throw new AppError('No User exists with this ID', 404); @@ -161,7 +174,11 @@ export const updateEmail = catchAsync(async (req: any, res: Response) => { const { email } = req.body; const userId = req.user.id; - const user = await User.findByIdAndUpdate(userId, { email }, { new: true, runValidators: true }); + const user = await User.findByIdAndUpdate( + userId, + { email }, + { new: true, runValidators: true } + ); if (!user) { throw new AppError('No User exists with this ID', 404); @@ -263,9 +280,9 @@ export const deletePicture = catchAsync(async (req: any, res: Response) => { data: {}, }); }); + export const getAllGroups = catchAsync(async (req: Request, res: Response) => { - const groupsAndChannels = await GroupChannel.find(); // Use `find()` in Mongoose to retrieve all documents - console.log(groupsAndChannels) + const groupsAndChannels = await GroupChannel.find(); return res.status(200).json({ status: 'success', message: 'Groups and Channels retrieved successfully', @@ -274,8 +291,23 @@ export const getAllGroups = catchAsync(async (req: Request, res: Response) => { }, }); }); + +export const toggleAutomaticDownload = catchAsync( + async (req: any, res: Response) => { + const userId = req.user.id; + const { enabled } = req.body; + + User.findByIdAndUpdate(userId, { automaticDownloadEnable: enabled }); + return res.status(200).json({ + status: 'success', + message: 'Automatic download settings updated successfully', + data: {}, + }); + } +); + export const activateUser = catchAsync(async (req: Request, res: Response) => { - const userId = req.params.userId; + const { userId } = req.params; const user = await User.findById(userId); if (!user) { @@ -284,7 +316,7 @@ export const activateUser = catchAsync(async (req: Request, res: Response) => { message: 'User not found', }); } -if (user.accountStatus === 'banned') { + if (user.accountStatus === 'banned') { return res.status(400).json({ status: 'fail', message: 'User is Banned', @@ -298,27 +330,29 @@ if (user.accountStatus === 'banned') { message: 'User activated successfully', }); }); -export const deactivateUser = catchAsync(async (req: Request, res: Response) => { - const userId = req.params.userId; - console.log(req.params) - const user = await User.findById(userId); - if (!user) { - return res.status(404).json({ - status: 'fail', - message: 'User not found', +export const deactivateUser = catchAsync( + async (req: Request, res: Response) => { + const { userId } = req.params; + console.log(req.params); + const user = await User.findById(userId); + if (!user) { + return res.status(404).json({ + status: 'fail', + message: 'User not found', + }); + } + + user.accountStatus = 'deactivated'; + await user.save(); + + return res.status(200).json({ + status: 'success', + message: 'User deactivated successfully', }); } - - user.accountStatus = 'deactivated'; - await user.save(); - - return res.status(200).json({ - status: 'success', - message: 'User deactivated successfully', - }); -}); +); export const banUser = catchAsync(async (req: Request, res: Response) => { - const userId = req.params.userId; + const { userId } = req.params; const user = await User.findById(userId); if (!user) { diff --git a/src/models/messageModel.ts b/src/models/messageModel.ts index 61e12a0..930c834 100644 --- a/src/models/messageModel.ts +++ b/src/models/messageModel.ts @@ -5,6 +5,8 @@ import Communication from './communicationModel'; const messageSchema = new mongoose.Schema({ content: String, media: String, + mediaName: String, + mediaSize: Number, contentType: { type: String, enum: ['text', 'image', 'GIF', 'sticker', 'audio', 'video', 'file', 'link'], diff --git a/src/sockets/MessagingServices.ts b/src/sockets/MessagingServices.ts index c3301ae..e35c42d 100644 --- a/src/sockets/MessagingServices.ts +++ b/src/sockets/MessagingServices.ts @@ -64,12 +64,8 @@ export const check = async ( chat?.type === 'group' && chat.isFilterd && (await detectInappropriateContent(content)) - ) { - return ack({ - success: false, - message: 'inappropriate content', - }); - } + ) + return 'inappropriate'; if (sender.Role !== 'admin') { if (!groupChannelChat.messagingPermission) return ack({ @@ -83,7 +79,7 @@ export const check = async ( }); } } - return true; + return 'ok'; }; export const informSessions = async ( diff --git a/src/sockets/messages.ts b/src/sockets/messages.ts index 9431798..9ace9fb 100644 --- a/src/sockets/messages.ts +++ b/src/sockets/messages.ts @@ -18,7 +18,8 @@ const handleMessaging = async ( ack: Function, senderId: string ) => { - let { media, content, contentType, parentMessageId } = data; + let { media, mediaName, mediaSize, content, contentType, parentMessageId } = + data; const { chatId, chatType, isReply, isForward, isAnnouncement } = data; if ( @@ -42,12 +43,12 @@ const handleMessaging = async ( }); const chat = await Chat.findById(chatId); - const func = await check(chat, ack, senderId, { + const valid = await check(chat, ack, senderId, { newMessageIsReply: isReply, content, sendMessage: true, }); - if (!func) return; + if (!valid) return; let parentMessage; if (isForward || isReply) { @@ -60,13 +61,15 @@ const handleMessaging = async ( }); if (isForward) { - ({ content, contentType, media } = parentMessage); + ({ content, contentType, media, mediaName, mediaSize } = parentMessage); parentMessageId = undefined; } } const message = new Message({ media, + mediaName, + mediaSize, content, contentType, isForward, @@ -74,6 +77,7 @@ const handleMessaging = async ( chatId, parentMessageId, isAnnouncement, + isAppropriate: valid === 'ok', }); await message.save(); diff --git a/src/types/message.ts b/src/types/message.ts index 0e4b363..470dd96 100644 --- a/src/types/message.ts +++ b/src/types/message.ts @@ -2,8 +2,10 @@ import { Types } from 'mongoose'; import ICommunication from './communication'; interface IMessage extends ICommunication { - media: string; content: string; + media: string; + mediaName: string; + mediaSize: number; contentType: string; isPinned: boolean; isForward: boolean;