Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 74 additions & 40 deletions src/controllers/userController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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' ||
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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',
Expand All @@ -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) {
Expand All @@ -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',
Expand All @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions src/models/messageModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import Communication from './communicationModel';
const messageSchema = new mongoose.Schema<IMessage>({
content: String,
media: String,
mediaName: String,
mediaSize: Number,
contentType: {
type: String,
enum: ['text', 'image', 'GIF', 'sticker', 'audio', 'video', 'file', 'link'],
Expand Down
10 changes: 3 additions & 7 deletions src/sockets/MessagingServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -83,7 +79,7 @@ export const check = async (
});
}
}
return true;
return 'ok';
};

export const informSessions = async (
Expand Down
12 changes: 8 additions & 4 deletions src/sockets/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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) {
Expand All @@ -60,20 +61,23 @@ 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,
senderId,
chatId,
parentMessageId,
isAnnouncement,
isAppropriate: valid === 'ok',
});

await message.save();
Expand Down
4 changes: 3 additions & 1 deletion src/types/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down