Added events system

This commit is contained in:
2025-05-29 14:27:15 +02:00
parent 3174f8fa5b
commit f00d569db3
24 changed files with 1186 additions and 114 deletions

View File

@@ -1,8 +1,9 @@
from flask import Blueprint, render_template, redirect, url_for, flash, request, jsonify, send_file
from flask_login import login_required, current_user
from models import db, Conversation, User, Message, MessageAttachment
from models import db, Conversation, User, Message, MessageAttachment, EventType
from forms import ConversationForm
from routes.auth import require_password_change
from utils.event_logger import log_event
import os
from werkzeug.utils import secure_filename
from datetime import datetime
@@ -84,6 +85,17 @@ def create_conversation():
db.session.add(conversation)
db.session.commit()
# Log conversation creation
log_event(
event_type=EventType.CONVERSATION_CREATE,
user_id=current_user.id,
details={
'conversation_id': conversation.id,
'conversation_name': conversation.name,
'member_count': len(conversation.members)
}
)
flash('Conversation created successfully!', 'success')
return redirect(url_for('conversations.conversations'))
return render_template('conversations/create_conversation.html', form=form)
@@ -227,6 +239,18 @@ def delete_conversation(conversation_id):
conversation = Conversation.query.get_or_404(conversation_id)
# Log conversation deletion before deleting
log_event(
event_type=EventType.CONVERSATION_DELETE,
user_id=current_user.id,
details={
'conversation_id': conversation_id,
'conversation_name': conversation.name,
'message_count': len(conversation.messages),
'member_count': len(conversation.members)
}
)
# Delete all messages in the conversation
Message.query.filter_by(conversation_id=conversation_id).delete()
@@ -324,6 +348,18 @@ def send_message(conversation_id):
db.session.commit()
# Log message sent
log_event(
event_type=EventType.MESSAGE_SENT,
user_id=current_user.id,
details={
'conversation_id': conversation_id,
'message_id': message.id,
'has_attachments': len(attachments) > 0,
'attachment_count': len(attachments)
}
)
# Prepare message data for response
message_data = {
'id': message.id,
@@ -358,6 +394,19 @@ def download_attachment(message_id, attachment_index):
try:
attachment = message.attachments[attachment_index]
# Log attachment download
log_event(
event_type=EventType.ATTACHMENT_DOWNLOAD,
user_id=current_user.id,
details={
'conversation_id': conversation.id,
'message_id': message_id,
'attachment_name': attachment.name,
'attachment_size': attachment.size
}
)
return send_file(
attachment.path,
as_attachment=True,