logging auth, conversations, and contacts
This commit is contained in:
@@ -3,6 +3,7 @@ from flask_login import login_required, current_user
|
||||
from models import db, Conversation, User, Message, MessageAttachment
|
||||
from forms import ConversationForm
|
||||
from routes.auth import require_password_change
|
||||
from utils import log_event
|
||||
import os
|
||||
from werkzeug.utils import secure_filename
|
||||
from datetime import datetime
|
||||
@@ -83,6 +84,21 @@ def create_conversation():
|
||||
|
||||
db.session.add(conversation)
|
||||
db.session.commit()
|
||||
|
||||
# Log conversation creation
|
||||
log_event(
|
||||
event_type='conversation_create',
|
||||
details={
|
||||
'conversation_id': conversation.id,
|
||||
'created_by': current_user.id,
|
||||
'created_by_name': f"{current_user.username} {current_user.last_name}",
|
||||
'name': conversation.name,
|
||||
'description': conversation.description,
|
||||
'member_count': len(conversation.members),
|
||||
'member_ids': [member.id for member in conversation.members]
|
||||
}
|
||||
)
|
||||
db.session.commit()
|
||||
|
||||
flash('Conversation created successfully!', 'success')
|
||||
return redirect(url_for('conversations.conversations'))
|
||||
@@ -147,6 +163,22 @@ def add_member(conversation_id):
|
||||
else:
|
||||
conversation.members.append(user)
|
||||
db.session.commit()
|
||||
|
||||
# Log member addition
|
||||
log_event(
|
||||
event_type='conversation_member_add',
|
||||
details={
|
||||
'conversation_id': conversation.id,
|
||||
'conversation_name': conversation.name,
|
||||
'added_by': current_user.id,
|
||||
'added_by_name': f"{current_user.username} {current_user.last_name}",
|
||||
'added_user_id': user.id,
|
||||
'added_by_name': f"{current_user.username} {current_user.last_name}",
|
||||
'added_user_email': user.email
|
||||
}
|
||||
)
|
||||
db.session.commit()
|
||||
|
||||
flash(f'{user.username} has been added to the conversation.', 'success')
|
||||
|
||||
return redirect(url_for('conversations.conversation_members', conversation_id=conversation_id))
|
||||
@@ -169,6 +201,22 @@ def remove_member(conversation_id, user_id):
|
||||
else:
|
||||
conversation.members.remove(user)
|
||||
db.session.commit()
|
||||
|
||||
# Log member removal
|
||||
log_event(
|
||||
event_type='conversation_member_remove',
|
||||
details={
|
||||
'conversation_id': conversation.id,
|
||||
'conversation_name': conversation.name,
|
||||
'removed_by': current_user.id,
|
||||
'removed_by_name': f"{current_user.username} {current_user.last_name}",
|
||||
'removed_user_id': user.id,
|
||||
'removed_user_name': f"{user.username} {user.last_name}",
|
||||
'removed_user_email': user.email
|
||||
}
|
||||
)
|
||||
db.session.commit()
|
||||
|
||||
flash('User has been removed from the conversation.', 'success')
|
||||
|
||||
return redirect(url_for('conversations.conversation_members', conversation_id=conversation_id))
|
||||
@@ -184,6 +232,13 @@ def edit_conversation(conversation_id):
|
||||
form = ConversationForm(obj=conversation)
|
||||
|
||||
if request.method == 'POST':
|
||||
# Store old values for comparison
|
||||
old_values = {
|
||||
'name': conversation.name,
|
||||
'description': conversation.description,
|
||||
'member_ids': [member.id for member in conversation.members]
|
||||
}
|
||||
|
||||
# Get members from the form data
|
||||
member_ids = request.form.getlist('members')
|
||||
|
||||
@@ -205,6 +260,25 @@ def edit_conversation(conversation_id):
|
||||
conversation.members.append(user)
|
||||
|
||||
db.session.commit()
|
||||
|
||||
# Log conversation update
|
||||
log_event(
|
||||
event_type='conversation_update',
|
||||
details={
|
||||
'conversation_id': conversation.id,
|
||||
'updated_by': current_user.id,
|
||||
'updated_by_name': f"{current_user.username} {current_user.last_name}",
|
||||
'old_values': old_values,
|
||||
'new_values': {
|
||||
'name': conversation.name,
|
||||
'description': conversation.description,
|
||||
'member_ids': [member.id for member in conversation.members],
|
||||
'member_names': [f"{member.username} {member.last_name}" for member in conversation.members]
|
||||
}
|
||||
}
|
||||
)
|
||||
db.session.commit()
|
||||
|
||||
flash('Conversation members updated successfully!', 'success')
|
||||
|
||||
# Check if redirect parameter is provided
|
||||
@@ -227,6 +301,20 @@ def delete_conversation(conversation_id):
|
||||
|
||||
conversation = Conversation.query.get_or_404(conversation_id)
|
||||
|
||||
# Log conversation deletion
|
||||
log_event(
|
||||
event_type='conversation_delete',
|
||||
details={
|
||||
'conversation_id': conversation.id,
|
||||
'conversation_name': conversation.name,
|
||||
'deleted_by': current_user.id,
|
||||
'deleted_by_name': f"{current_user.username} {current_user.last_name}",
|
||||
'member_count': len(conversation.members),
|
||||
'message_count': Message.query.filter_by(conversation_id=conversation_id).count()
|
||||
}
|
||||
)
|
||||
db.session.commit()
|
||||
|
||||
# Delete all messages in the conversation
|
||||
Message.query.filter_by(conversation_id=conversation_id).delete()
|
||||
|
||||
@@ -264,7 +352,6 @@ def get_messages(conversation_id):
|
||||
'created_at': message.created_at.strftime('%b %d, %Y %H:%M'),
|
||||
'sender_id': str(message.user_id),
|
||||
'sender_name': f"{message.user.username} {message.user.last_name}",
|
||||
'sender_avatar': url_for('profile_pic', filename=message.user.profile_picture) if message.user.profile_picture else url_for('static', filename='default-avatar.png'),
|
||||
'attachments': [{
|
||||
'name': attachment.name,
|
||||
'size': attachment.size,
|
||||
@@ -323,6 +410,22 @@ def send_message(conversation_id):
|
||||
attachments.append(attachment)
|
||||
|
||||
db.session.commit()
|
||||
|
||||
# Log message creation
|
||||
log_event(
|
||||
event_type='message_create',
|
||||
details={
|
||||
'message_id': message.id,
|
||||
'conversation_id': conversation_id,
|
||||
'conversation_name': conversation.name,
|
||||
'sender_id': current_user.id,
|
||||
'sender_name': f"{current_user.username} {current_user.last_name}",
|
||||
'has_attachments': len(attachments) > 0,
|
||||
'attachment_count': len(attachments),
|
||||
'attachment_types': [get_file_extension(att.name) for att in attachments] if attachments else []
|
||||
}
|
||||
)
|
||||
db.session.commit()
|
||||
|
||||
# Prepare message data for response
|
||||
message_data = {
|
||||
|
||||
Reference in New Issue
Block a user