modified:   app.py
This commit is contained in:
Russell Ballestrini 2023-12-03 08:11:51 -05:00
parent 242a195fbb
commit 27dc7918f3

73
app.py
View file

@ -152,7 +152,7 @@ def on_join(data):
def find_most_recent_code_block(room_name):
with app.app_context():
# Get the room object from the database
room = Room.query.filter_by(name=room_name).first()
room = get_room(room_name)
if not room:
return None # Room not found
@ -221,7 +221,7 @@ def save_code_block_to_s3(room_name, s3_key_path, username):
# Save the message to the database and emit to the frontend
with app.app_context():
# Get the room object from the database
room = Room.query.filter_by(name=room_name).first()
room = get_room(room_name)
if room:
# Create a new message object
new_message = Message(username=username, content=message_content, room_id=room.id)
@ -248,61 +248,42 @@ def load_s3_file(room_name, s3_file_path, username):
# Assuming the bucket name is set in an environment variable
bucket_name = os.environ.get("S3_BUCKET_NAME")
# Initialize message content variable
message_content = ""
try:
# Retrieve the file content from S3
response = s3_client.get_object(Bucket=bucket_name, Key=s3_file_path)
file_content = response["Body"].read().decode("utf-8")
# Format the file content as a code block
formatted_content = f"```\n{file_content}\n```"
# Save the message to the database
with app.app_context():
room = get_room(room_name)
new_message = Message(
username=username, # Use the username who issued the command
content=formatted_content,
room_id=room.id,
)
db.session.add(new_message)
db.session.commit()
# Emit the file content as a message to the chatroom with the message ID
socketio.emit(
"message",
{
"id": new_message.id,
"username": username,
"content": formatted_content,
},
room=room_name,
)
message_content = f"```\n{file_content}\n```"
except Exception as e:
# Handle errors (e.g., file not found, access denied)
error_message = f"Error loading file from S3: {e}"
message_content = f"Error loading file from S3: {e}"
# Save the error message to the database
with app.app_context():
room = get_room(room_name)
new_error_message = Message(
username=username,
content=error_message,
room_id=room.id,
)
db.session.add(new_error_message)
db.session.commit()
# Save the message to the database and emit to the chatroom
with app.app_context():
room = get_room(room_name)
new_message = Message(
username=username,
content=message_content,
room_id=room.id,
)
db.session.add(new_message)
db.session.commit()
# Emit the error message to the chatroom without a message ID
socketio.emit(
"message",
{
"id": new_error_message.id,
"username": username,
"content": error_message,
},
room=room_name,
)
# Emit the message to the chatroom with the message ID
socketio.emit(
"message",
{
"id": new_message.id,
"username": username,
"content": message_content,
},
room=room_name,
)
@socketio.on("message")