File size: 3,050 Bytes
057576a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
const User = require('../models/User');
exports.searchUsers = async (req, res) => {
try {
const { username, email } = req.query;
if (!username && !email) {
return res.status(400).json({ error: 'Username or email is required for search' });
}
const searchQuery = {};
if (username) {
searchQuery.username = { $regex: username, $options: 'i' };
}
if (email) {
searchQuery.email = { $regex: email, $options: 'i' };
}
// Exclude current user from results
searchQuery._id = { $ne: req.user.userId };
const users = await User.find(searchQuery)
.select('username displayName avatar status lastSeen bio')
.limit(20);
res.json({ users });
} catch (error) {
console.error('User search error:', error);
res.status(500).json({ error: 'Server error during user search' });
}
};
exports.getUserProfile = async (req, res) => {
try {
const { userId } = req.params;
const user = await User.findById(userId)
.select('username displayName avatar status lastSeen bio createdAt');
if (!user) {
return res.status(404).json({ error: 'User not found' });
}
res.json({ user });
} catch (error) {
console.error('Get user profile error:', error);
res.status(500).json({ error: 'Server error' });
}
};
exports.updateSettings = async (req, res) => {
try {
const { theme, notifications } = req.body;
const updateData = {};
if (theme) updateData['settings.theme'] = theme;
if (notifications) {
if (typeof notifications.enabled !== 'undefined') {
updateData['settings.notifications.enabled'] = notifications.enabled;
}
if (typeof notifications.sound !== 'undefined') {
updateData['settings.notifications.sound'] = notifications.sound;
}
if (typeof notifications.preview !== 'undefined') {
updateData['settings.notifications.preview'] = notifications.preview;
}
}
const user = await User.findByIdAndUpdate(
req.user.userId,
{ $set: updateData },
{ new: true, runValidators: true }
);
res.json({
message: 'Settings updated successfully',
settings: user.settings
});
} catch (error) {
console.error('Update settings error:', error);
res.status(500).json({ error: 'Server error during settings update' });
}
};
exports.deleteAccount = async (req, res) => {
try {
const { confirmation } = req.body;
if (confirmation !== 'DELETE MY ACCOUNT') {
return res.status(400).json({
error: 'Please type "DELETE MY ACCOUNT" to confirm account deletion'
});
}
await User.findByIdAndDelete(req.user.userId);
res.clearCookie('token');
res.json({ message: 'Account deleted successfully' });
} catch (error) {
console.error('Delete account error:', error);
res.status(500).json({ error: 'Server error during account deletion' });
}
}; |