Update app.py
Browse files
app.py
CHANGED
|
@@ -80,7 +80,6 @@ app.config['SECRET_KEY'] = 'your-secret-key'
|
|
| 80 |
# app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///users.db?check_same_thread=False'
|
| 81 |
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://sql7760039:[email protected]/sql7760039'
|
| 82 |
|
| 83 |
-
|
| 84 |
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
| 85 |
app.config['SQLALCHEMY_ENGINE_OPTIONS'] = {
|
| 86 |
'charset': 'utf8mb4' # تعيين الترميز UTF-8 لجميع الجداول
|
|
@@ -716,7 +715,7 @@ def send_message():
|
|
| 716 |
@login_required
|
| 717 |
def stream_messages(user_id):
|
| 718 |
def event_stream():
|
| 719 |
-
last_message_id = None
|
| 720 |
|
| 721 |
while True:
|
| 722 |
try:
|
|
@@ -727,36 +726,38 @@ def stream_messages(user_id):
|
|
| 727 |
).order_by(Message.created_at.desc())
|
| 728 |
|
| 729 |
if last_message_id:
|
| 730 |
-
query = query.filter(Message.id > last_message_id)
|
| 731 |
|
| 732 |
messages = query.limit(1).all()
|
| 733 |
|
| 734 |
if messages:
|
| 735 |
message = messages[0]
|
| 736 |
-
last_message_id = message.id
|
| 737 |
|
| 738 |
-
#
|
| 739 |
-
|
| 740 |
'message_id': message.id,
|
| 741 |
'sender_id': message.sender_id,
|
| 742 |
'receiver_id': message.receiver_id,
|
| 743 |
'content': message.content,
|
| 744 |
'file_url': message.file_url,
|
| 745 |
'file_type': message.file_type,
|
| 746 |
-
'created_at': message.created_at.strftime('%Y-%m-%d %H:%M:%S'),
|
| 747 |
-
'watched_by_receiver': message.watched_by_receiver,
|
| 748 |
-
'watched_by_sender': message.watched_by_sender
|
| 749 |
-
}
|
| 750 |
|
| 751 |
-
|
|
|
|
|
|
|
|
|
|
| 752 |
|
| 753 |
except Exception as e:
|
| 754 |
print(f"Error in SSE stream: {e}")
|
| 755 |
-
time.sleep(5)
|
| 756 |
|
| 757 |
return Response(stream_with_context(event_stream()), content_type='text/event-stream')
|
| 758 |
|
| 759 |
-
|
| 760 |
@app.route('/is_user_active/<int:user_id>')
|
| 761 |
@login_required
|
| 762 |
def is_user_active(user_id):
|
|
@@ -868,11 +869,10 @@ def stream_unread_count():
|
|
| 868 |
last_count = unread_count
|
| 869 |
yield f"data: {json.dumps({'unread_count': unread_count})}\n\n"
|
| 870 |
|
| 871 |
-
time.sleep(1)
|
| 872 |
|
| 873 |
return Response(stream_with_context(event_stream()), content_type='text/event-stream')
|
| 874 |
|
| 875 |
-
|
| 876 |
@app.route('/mark_message_as_seen/<int:message_id>', methods=['POST'])
|
| 877 |
@login_required
|
| 878 |
def mark_message_as_seen(message_id):
|
|
@@ -929,15 +929,18 @@ def stream_message_updates(user_id):
|
|
| 929 |
last_update_time = messages[-1].last_updated # تحديث وقت التحقق الأخير
|
| 930 |
|
| 931 |
for message in messages:
|
| 932 |
-
|
|
|
|
| 933 |
'message_id': message.id,
|
| 934 |
'sender_id': message.sender_id,
|
| 935 |
'receiver_id': message.receiver_id,
|
| 936 |
'watched_by_receiver': message.watched_by_receiver,
|
| 937 |
-
'watched_by_sender': message.watched_by_sender
|
| 938 |
-
|
|
|
|
|
|
|
| 939 |
|
| 940 |
-
time.sleep(0.5)
|
| 941 |
|
| 942 |
return Response(stream_with_context(event_stream()), content_type='text/event-stream')
|
| 943 |
|
|
|
|
| 80 |
# app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///users.db?check_same_thread=False'
|
| 81 |
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://sql7760039:[email protected]/sql7760039'
|
| 82 |
|
|
|
|
| 83 |
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
| 84 |
app.config['SQLALCHEMY_ENGINE_OPTIONS'] = {
|
| 85 |
'charset': 'utf8mb4' # تعيين الترميز UTF-8 لجميع الجداول
|
|
|
|
| 715 |
@login_required
|
| 716 |
def stream_messages(user_id):
|
| 717 |
def event_stream():
|
| 718 |
+
last_message_id = None
|
| 719 |
|
| 720 |
while True:
|
| 721 |
try:
|
|
|
|
| 726 |
).order_by(Message.created_at.desc())
|
| 727 |
|
| 728 |
if last_message_id:
|
| 729 |
+
query = query.filter(Message.id > last_message_id)
|
| 730 |
|
| 731 |
messages = query.limit(1).all()
|
| 732 |
|
| 733 |
if messages:
|
| 734 |
message = messages[0]
|
| 735 |
+
last_message_id = message.id
|
| 736 |
|
| 737 |
+
# تحويل التاريخ إلى تنسيق قابل للتسلسل
|
| 738 |
+
message_data = {
|
| 739 |
'message_id': message.id,
|
| 740 |
'sender_id': message.sender_id,
|
| 741 |
'receiver_id': message.receiver_id,
|
| 742 |
'content': message.content,
|
| 743 |
'file_url': message.file_url,
|
| 744 |
'file_type': message.file_type,
|
| 745 |
+
'created_at': message.created_at.strftime('%Y-%m-%d %H:%M:%S'), # تحويل التاريخ
|
| 746 |
+
'watched_by_receiver': message.watched_by_receiver,
|
| 747 |
+
'watched_by_sender': message.watched_by_sender
|
| 748 |
+
}
|
| 749 |
|
| 750 |
+
# إرسال بيانات الرسالة
|
| 751 |
+
yield f"data: {json.dumps(message_data)}\n\n"
|
| 752 |
+
|
| 753 |
+
time.sleep(1)
|
| 754 |
|
| 755 |
except Exception as e:
|
| 756 |
print(f"Error in SSE stream: {e}")
|
| 757 |
+
time.sleep(5)
|
| 758 |
|
| 759 |
return Response(stream_with_context(event_stream()), content_type='text/event-stream')
|
| 760 |
|
|
|
|
| 761 |
@app.route('/is_user_active/<int:user_id>')
|
| 762 |
@login_required
|
| 763 |
def is_user_active(user_id):
|
|
|
|
| 869 |
last_count = unread_count
|
| 870 |
yield f"data: {json.dumps({'unread_count': unread_count})}\n\n"
|
| 871 |
|
| 872 |
+
time.sleep(1)
|
| 873 |
|
| 874 |
return Response(stream_with_context(event_stream()), content_type='text/event-stream')
|
| 875 |
|
|
|
|
| 876 |
@app.route('/mark_message_as_seen/<int:message_id>', methods=['POST'])
|
| 877 |
@login_required
|
| 878 |
def mark_message_as_seen(message_id):
|
|
|
|
| 929 |
last_update_time = messages[-1].last_updated # تحديث وقت التحقق الأخير
|
| 930 |
|
| 931 |
for message in messages:
|
| 932 |
+
# تحويل التاريخ إلى تنسيق قابل للتسلسل
|
| 933 |
+
message_data = {
|
| 934 |
'message_id': message.id,
|
| 935 |
'sender_id': message.sender_id,
|
| 936 |
'receiver_id': message.receiver_id,
|
| 937 |
'watched_by_receiver': message.watched_by_receiver,
|
| 938 |
+
'watched_by_sender': message.watched_by_sender,
|
| 939 |
+
'last_updated': message.last_updated.strftime('%Y-%m-%d %H:%M:%S') # تحويل التاريخ
|
| 940 |
+
}
|
| 941 |
+
yield f"data: {json.dumps(message_data)}\n\n"
|
| 942 |
|
| 943 |
+
time.sleep(0.5)
|
| 944 |
|
| 945 |
return Response(stream_with_context(event_stream()), content_type='text/event-stream')
|
| 946 |
|