Update app.py
Browse files
app.py
CHANGED
|
@@ -77,7 +77,8 @@ def format_datetime(value):
|
|
| 77 |
return value.strftime("%I:%M %p").replace("AM", "صباحاً").replace("PM", "مساءً") + " " + value.strftime("%Y-%m-%d")
|
| 78 |
|
| 79 |
app.config['SECRET_KEY'] = 'your-secret-key'
|
| 80 |
-
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///users.db?check_same_thread=False'
|
|
|
|
| 81 |
|
| 82 |
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
| 83 |
app.config['SQLALCHEMY_ENGINE_OPTIONS'] = {
|
|
@@ -714,7 +715,7 @@ def send_message():
|
|
| 714 |
@login_required
|
| 715 |
def stream_messages(user_id):
|
| 716 |
def event_stream():
|
| 717 |
-
last_message_id = None
|
| 718 |
|
| 719 |
while True:
|
| 720 |
try:
|
|
@@ -725,37 +726,36 @@ def stream_messages(user_id):
|
|
| 725 |
).order_by(Message.created_at.desc())
|
| 726 |
|
| 727 |
if last_message_id:
|
| 728 |
-
query = query.filter(Message.id > last_message_id)
|
| 729 |
|
| 730 |
messages = query.limit(1).all()
|
| 731 |
|
| 732 |
if messages:
|
| 733 |
message = messages[0]
|
| 734 |
-
last_message_id = message.id
|
| 735 |
|
| 736 |
-
#
|
| 737 |
-
|
| 738 |
'message_id': message.id,
|
| 739 |
'sender_id': message.sender_id,
|
| 740 |
'receiver_id': message.receiver_id,
|
| 741 |
'content': message.content,
|
| 742 |
'file_url': message.file_url,
|
| 743 |
'file_type': message.file_type,
|
| 744 |
-
'created_at': message.created_at.strftime('%Y-%m-%d %H:%M:%S'),
|
| 745 |
-
'watched_by_receiver': message.watched_by_receiver,
|
| 746 |
-
'watched_by_sender': message.watched_by_sender
|
| 747 |
-
}
|
| 748 |
|
| 749 |
-
|
| 750 |
-
yield f"data: {json.dumps(message_data)}\n\n"
|
| 751 |
-
|
| 752 |
-
time.sleep(1)
|
| 753 |
|
| 754 |
except Exception as e:
|
| 755 |
print(f"Error in SSE stream: {e}")
|
| 756 |
-
time.sleep(5)
|
| 757 |
|
| 758 |
return Response(stream_with_context(event_stream()), content_type='text/event-stream')
|
|
|
|
|
|
|
| 759 |
@app.route('/is_user_active/<int:user_id>')
|
| 760 |
@login_required
|
| 761 |
def is_user_active(user_id):
|
|
@@ -867,10 +867,11 @@ def stream_unread_count():
|
|
| 867 |
last_count = unread_count
|
| 868 |
yield f"data: {json.dumps({'unread_count': unread_count})}\n\n"
|
| 869 |
|
| 870 |
-
time.sleep(1)
|
| 871 |
|
| 872 |
return Response(stream_with_context(event_stream()), content_type='text/event-stream')
|
| 873 |
|
|
|
|
| 874 |
@app.route('/mark_message_as_seen/<int:message_id>', methods=['POST'])
|
| 875 |
@login_required
|
| 876 |
def mark_message_as_seen(message_id):
|
|
@@ -927,20 +928,18 @@ def stream_message_updates(user_id):
|
|
| 927 |
last_update_time = messages[-1].last_updated # تحديث وقت التحقق الأخير
|
| 928 |
|
| 929 |
for message in messages:
|
| 930 |
-
|
| 931 |
-
message_data = {
|
| 932 |
'message_id': message.id,
|
| 933 |
'sender_id': message.sender_id,
|
| 934 |
'receiver_id': message.receiver_id,
|
| 935 |
'watched_by_receiver': message.watched_by_receiver,
|
| 936 |
-
'watched_by_sender': message.watched_by_sender
|
| 937 |
-
|
| 938 |
-
}
|
| 939 |
-
yield f"data: {json.dumps(message_data)}\n\n"
|
| 940 |
|
| 941 |
-
time.sleep(0.5)
|
| 942 |
|
| 943 |
return Response(stream_with_context(event_stream()), content_type='text/event-stream')
|
|
|
|
| 944 |
@app.route('/leave_chat', methods=['POST'])
|
| 945 |
@login_required
|
| 946 |
def leave_chat():
|
|
@@ -1584,8 +1583,6 @@ def delete_video_from_playlist():
|
|
| 1584 |
if __name__ == '__main__':
|
| 1585 |
with app.app_context():
|
| 1586 |
db.create_all()
|
| 1587 |
-
|
| 1588 |
-
app.run(debug=True, host="0.0.0.0")
|
| 1589 |
-
|
| 1590 |
-
|
| 1591 |
|
|
|
|
| 77 |
return value.strftime("%I:%M %p").replace("AM", "صباحاً").replace("PM", "مساءً") + " " + value.strftime("%Y-%m-%d")
|
| 78 |
|
| 79 |
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 |
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
| 84 |
app.config['SQLALCHEMY_ENGINE_OPTIONS'] = {
|
|
|
|
| 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 |
+
yield f"data: {json.dumps({
|
| 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 |
+
})}\n\n"
|
| 749 |
|
| 750 |
+
time.sleep(1) # انتظر ثانية قبل التحقق مرة أخرى
|
|
|
|
|
|
|
|
|
|
| 751 |
|
| 752 |
except Exception as e:
|
| 753 |
print(f"Error in SSE stream: {e}")
|
| 754 |
+
time.sleep(5) # انتظر 5 ثوانٍ قبل إعادة المحاولة
|
| 755 |
|
| 756 |
return Response(stream_with_context(event_stream()), content_type='text/event-stream')
|
| 757 |
+
|
| 758 |
+
|
| 759 |
@app.route('/is_user_active/<int:user_id>')
|
| 760 |
@login_required
|
| 761 |
def is_user_active(user_id):
|
|
|
|
| 867 |
last_count = unread_count
|
| 868 |
yield f"data: {json.dumps({'unread_count': unread_count})}\n\n"
|
| 869 |
|
| 870 |
+
time.sleep(1) # انتظر ثانية قبل التحقق مرة أخرى
|
| 871 |
|
| 872 |
return Response(stream_with_context(event_stream()), content_type='text/event-stream')
|
| 873 |
|
| 874 |
+
|
| 875 |
@app.route('/mark_message_as_seen/<int:message_id>', methods=['POST'])
|
| 876 |
@login_required
|
| 877 |
def mark_message_as_seen(message_id):
|
|
|
|
| 928 |
last_update_time = messages[-1].last_updated # تحديث وقت التحقق الأخير
|
| 929 |
|
| 930 |
for message in messages:
|
| 931 |
+
yield f"data: {json.dumps({
|
|
|
|
| 932 |
'message_id': message.id,
|
| 933 |
'sender_id': message.sender_id,
|
| 934 |
'receiver_id': message.receiver_id,
|
| 935 |
'watched_by_receiver': message.watched_by_receiver,
|
| 936 |
+
'watched_by_sender': message.watched_by_sender
|
| 937 |
+
})}\n\n"
|
|
|
|
|
|
|
| 938 |
|
| 939 |
+
time.sleep(0.5) # تحقق كل 0.5 ثانية لتحسين الأداء
|
| 940 |
|
| 941 |
return Response(stream_with_context(event_stream()), content_type='text/event-stream')
|
| 942 |
+
|
| 943 |
@app.route('/leave_chat', methods=['POST'])
|
| 944 |
@login_required
|
| 945 |
def leave_chat():
|
|
|
|
| 1583 |
if __name__ == '__main__':
|
| 1584 |
with app.app_context():
|
| 1585 |
db.create_all()
|
| 1586 |
+
Session = scoped_session(sessionmaker(bind=db.engine))
|
| 1587 |
+
app.run(debug=True, host="0.0.0.0", port=int(os.environ.get("PORT", 8080)))
|
|
|
|
|
|
|
| 1588 |
|