Skip to content

Commit e7ac850

Browse files
committed
fixes
1 parent 9da064b commit e7ac850

23 files changed

Lines changed: 591 additions & 188 deletions

backend/models/chat_room.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ def __init__(self, db):
1515
def create_chat_room(self, topic_id: Optional[str], name: str, description: str, owner_id: str,
1616
is_public: bool = True, tags: Optional[List[str]] = None,
1717
picture: Optional[str] = None,
18-
background_picture: Optional[str] = None) -> str:
18+
background_picture: Optional[str] = None,
19+
voip_enabled: bool = True) -> str:
1920
"""Create a new conversation (chat room) in a topic or a group chat."""
2021
chat_room_data = {
2122
'topic_id': ObjectId(topic_id) if topic_id else None, # Conversation belongs to a Topic or None for Group Chat
@@ -27,7 +28,7 @@ def create_chat_room(self, topic_id: Optional[str], name: str, description: str,
2728
'is_public': is_public,
2829
'picture': picture, # Chat room picture (displayed in header/list)
2930
'background_picture': background_picture, # Background picture (faded behind messages)
30-
'voip_enabled': False, # VoIP calls disabled by default
31+
'voip_enabled': voip_enabled,
3132
'member_count': 1, # Owner counts as first member
3233
'members': [ObjectId(owner_id)],
3334
'banned_users': [], # Users banned from this chat

backend/models/friend.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,28 +138,45 @@ def accept_friend_request(self, request_id: str, user_id: str) -> Dict[str, Any]
138138
'updated_at': datetime.utcnow().isoformat()
139139
}
140140

141-
def reject_friend_request(self, request_id: str, user_id: str) -> bool:
141+
def reject_friend_request(self, request_id: str, user_id: str) -> Optional[Dict[str, str]]:
142142
"""Reject a friend request."""
143-
result = self.collection.delete_one({
143+
request = self.collection.find_one({
144144
'_id': ObjectId(request_id),
145145
'to_user_id': ObjectId(user_id),
146146
'status': 'pending'
147147
})
148+
149+
if not request:
150+
return None
148151

149-
return result.deleted_count > 0
152+
from_user_id = str(request['from_user_id'])
153+
result = self.collection.delete_one({'_id': ObjectId(request_id)})
154+
155+
if result.deleted_count > 0:
156+
return {'from_user_id': from_user_id, 'to_user_id': user_id}
157+
return None
150158

151-
def cancel_friend_request(self, request_id: str, user_id: str) -> bool:
159+
def cancel_friend_request(self, request_id: str, user_id: str) -> Optional[Dict[str, str]]:
152160
"""Cancel a sent friend request."""
153-
result = self.collection.delete_one({
161+
request = self.collection.find_one({
154162
'_id': ObjectId(request_id),
155163
'from_user_id': ObjectId(user_id),
156164
'status': 'pending'
157165
})
166+
167+
if not request:
168+
return None
158169

159-
return result.deleted_count > 0
170+
to_user_id = str(request['to_user_id'])
171+
result = self.collection.delete_one({'_id': ObjectId(request_id)})
172+
173+
if result.deleted_count > 0:
174+
return {'from_user_id': user_id, 'to_user_id': to_user_id}
175+
return None
160176

161177
def remove_friend(self, user_id: str, friend_id: str) -> bool:
162178
"""Remove a friend."""
179+
# This one already has the IDs passed in
163180
result = self.collection.delete_one({
164181
'$or': [
165182
{'from_user_id': ObjectId(user_id), 'to_user_id': ObjectId(friend_id), 'status': 'accepted'},

backend/models/voip.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
from datetime import datetime, timedelta
22
from typing import List, Dict, Optional, Any
33
from bson import ObjectId
4+
import logging
5+
6+
logger = logging.getLogger(__name__)
47

58

69
class VoipCall:
@@ -57,6 +60,7 @@ def create_call(self, room_id: str, room_type: str, created_by: str, room_name:
5760
result = self.collection.insert_one(call_doc)
5861
call_doc['id'] = str(result.inserted_id)
5962
call_doc['_id'] = str(result.inserted_id)
63+
logger.info(f"New VOIP call created: {call_doc['id']} in room {room_id} by {created_by}")
6064
return self._process_call(call_doc)
6165

6266
def get_active_call(self, room_id: str) -> Optional[Dict[str, Any]]:
@@ -124,9 +128,10 @@ def join_call(self, call_id: str, user_id: str) -> Optional[Dict[str, Any]]:
124128
)
125129

126130
if result.modified_count > 0:
131+
logger.info(f"User {user_id} joined VOIP call {call_id}")
127132
return self.get_call_by_id(call_id)
128133
except Exception as e:
129-
print(f"Error joining call: {e}")
134+
logger.error(f"Error joining call {call_id} for user {user_id}: {e}")
130135
return None
131136

132137
def leave_call(self, call_id: str, user_id: str) -> Optional[Dict[str, Any]]:
@@ -146,11 +151,12 @@ def leave_call(self, call_id: str, user_id: str) -> Optional[Dict[str, Any]]:
146151
call = self.get_call_by_id(call_id)
147152
if call and len(call.get('participants', [])) == 0:
148153
# End the call
149-
self.end_call(call_id)
154+
logger.info(f"Ending call {call_id} because no participants remain after user {user_id} left")
155+
self.end_call(call_id, 'no_participants')
150156
return {'ended': True, 'call_id': call_id}
151157
return call
152158
except Exception as e:
153-
print(f"Error leaving call: {e}")
159+
logger.error(f"Error leaving call {call_id} for user {user_id}: {e}")
154160
return None
155161

156162
def end_call(self, call_id: str, reason: str = None) -> bool:
@@ -166,9 +172,11 @@ def end_call(self, call_id: str, reason: str = None) -> bool:
166172
}
167173
}
168174
)
175+
if result.modified_count > 0:
176+
logger.info(f"Call {call_id} ended. Reason: {reason or 'Not specified'}")
169177
return result.modified_count > 0
170178
except Exception as e:
171-
print(f"Error ending call: {e}")
179+
logger.error(f"Error ending call {call_id}: {e}")
172180
return False
173181

174182
def set_mute_status(self, call_id: str, user_id: str, is_muted: bool) -> bool:
@@ -282,7 +290,8 @@ def check_and_remove_offline_participants(self) -> List[Dict[str, Any]]:
282290
# Check if call is now empty
283291
updated_call = self.get_call_by_id(call_id)
284292
if updated_call and len(updated_call.get('participants', [])) == 0:
285-
self.end_call(call_id, 'no_participants')
293+
logger.info(f"Ending call {call_id} during offline cleanup (no participants remain)")
294+
self.end_call(call_id, 'no_participants_offline')
286295
affected.append({
287296
'call_id': call_id,
288297
'removed_users': removed_users,

backend/routes/chat_rooms.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ def create_group_chat():
159159
# Process pictures if provided
160160
picture = data.get('picture')
161161
background_picture = data.get('background_picture')
162+
voip_enabled = data.get('voip_enabled', True)
162163

163164
from utils.image_compression import compress_image_base64
164165
from utils.imgbb_upload import process_image_for_storage
@@ -188,7 +189,8 @@ def create_group_chat():
188189
is_public=False, # Group chats are private by default usually, or invite only
189190
tags=[],
190191
picture=picture,
191-
background_picture=background_picture
192+
background_picture=background_picture,
193+
voip_enabled=voip_enabled
192194
)
193195

194196
# Invite users
@@ -291,6 +293,7 @@ def create_conversation(topic_id):
291293
description = data.get('description', '').strip()
292294
is_public = data.get('is_public', True)
293295
tags = data.get('tags', [])
296+
voip_enabled = data.get('voip_enabled', True)
294297

295298
if not name:
296299
return jsonify({'success': False, 'errors': ['Conversation name is required']}), 400
@@ -340,7 +343,8 @@ def create_conversation(topic_id):
340343
is_public=is_public,
341344
tags=tags,
342345
picture=picture,
343-
background_picture=background_picture
346+
background_picture=background_picture,
347+
voip_enabled=voip_enabled
344348
)
345349

346350
# Get created chat room

backend/routes/posts.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,20 @@ def create_post(topic_id):
255255
if use_anonymous and topic.get('settings', {}).get('allow_anonymous', True):
256256
anon_model = AnonymousIdentity(current_app.db)
257257
anonymous_identity = anon_model.get_anonymous_identity(user_id, topic_id)
258+
if not anonymous_identity:
259+
# Create a new one if it doesn't exist
260+
anonymous_identity = anon_model.create_anonymous_identity(user_id, topic_id)
261+
# Emit socket event so Settings page can update
262+
try:
263+
from app import socketio
264+
if socketio:
265+
socketio.emit('anonymous_identity_updated', {
266+
'user_id': user_id,
267+
'topic_id': topic_id,
268+
'anonymous_name': anonymous_identity
269+
}, room=f"user_{user_id}")
270+
except Exception as e:
271+
logger.warning(f"Failed to emit socket event for auto-created identity: {str(e)}")
258272

259273
# Create post
260274
post_model = Post(current_app.db)

backend/routes/topics.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,20 @@ def update_anonymous_identity(topic_id):
812812

813813
# Get the final identity name
814814
final_name = anon_model.get_anonymous_identity(user_id, topic_id)
815+
816+
# Emit socket event
817+
try:
818+
from app import socketio
819+
if socketio:
820+
socketio.emit('anonymous_identity_updated', {
821+
'user_id': user_id,
822+
'topic_id': topic_id,
823+
'anonymous_name': final_name or custom_name
824+
}, room=f"user_{user_id}")
825+
logger.info(f"Emitted anonymous_identity_updated for user {user_id} in topic {topic_id}")
826+
except Exception as e:
827+
logger.warning(f"Failed to emit socket event for identity update: {str(e)}")
828+
815829
return jsonify({
816830
'success': True,
817831
'message': 'Anonymous identity set successfully',

0 commit comments

Comments
 (0)