Some updates for backend thingies

This commit is contained in:
2026-03-20 15:59:07 -06:00
parent 84e9d3001a
commit 76156955b9
16 changed files with 1027 additions and 100 deletions

View File

@@ -1,12 +1,14 @@
from domain.reports import Report
from application.ports.report_repository import ReportRepository
from application.ports.user_repository import UserRepository
from infrastructure.adapters.rabbitmq.sender import send_to_queue
from infrastructure.adapters.rabbitmq.messages import ReportMessage, ReportEventType
from datetime import datetime
from typing import List, Optional
from typing import List, Optional, Dict, Any
from uuid import uuid4
class CreateReport:
"""Use case para crear un nuevo reporte"""
"""Use case para crear un nuevo reporte - envía mensaje a RabbitMQ"""
def __init__(self, repo: ReportRepository, user_repo: UserRepository):
if not isinstance(repo, ReportRepository):
raise TypeError("repo must implement ReportRepository")
@@ -16,26 +18,51 @@ class CreateReport:
self.user_repo = user_repo
def execute(self, id_usuario: int, tipo_reporte: int, descripcion: str,
ubicacion: Optional[str] = None) -> Report:
# Verificar que el usuario existe
ubicacion: Optional[str] = None) -> Dict[str, Any]:
"""
Sends a create report message to RabbitMQ.
The actual database save will be done by the consumer.
Returns:
Dictionary with status and message
"""
# Verify user exists (we still need to check this before queuing)
user = self.user_repo.find_by_id(id_usuario)
if not user:
raise ValueError(f"Usuario con ID {id_usuario} no existe")
return {
"status": "error",
"message": f"Usuario con ID {id_usuario} no existe"
}
report = Report(
id_reporte=str(uuid4()),
id_reporte = str(uuid4())
fecha_creacion = datetime.now()
# Create message object
message = ReportMessage(
event_type=ReportEventType.CREATE,
id_reporte=id_reporte,
id_usuario=id_usuario,
tipo_reporte=tipo_reporte,
descripcion=descripcion,
ubicacion=ubicacion,
visibilidad=50.0, # Visibilidad inicial neutral
fecha_creacion=datetime.now()
fecha_creacion=fecha_creacion.isoformat()
)
# Incrementar contador de reportes del usuario
self.user_repo.increment_reports(id_usuario)
# Send to RabbitMQ
success = send_to_queue("reports_queue", message.to_dict())
return self.repo.save(report)
if success:
return {
"status": "queued",
"message": "Reporte enviado a cola para procesamiento",
"id_reporte": id_reporte
}
else:
return {
"status": "error",
"message": "Error al enviar reporte a la cola de procesamiento"
}
class GetReportById:
"""Use case para obtener un reporte por ID"""
@@ -68,31 +95,51 @@ class ListAllReports:
return self.repo.find_all()
class UpdateReportVisibility:
"""Use case para actualizar la visibilidad de un reporte basado en votación comunitaria"""
"""Use case para actualizar la visibilidad de un reporte basado en votación comunitaria - envía mensaje a RabbitMQ"""
def __init__(self, repo: ReportRepository, user_repo: UserRepository):
if not isinstance(repo, ReportRepository):
raise TypeError("repo must implement ReportRepository")
self.repo = repo
self.user_repo = user_repo
def execute(self, report_id: str, new_visibility: float, penalize_author: bool = False) -> None:
def execute(self, report_id: str, new_visibility: float, penalize_author: bool = False) -> Dict[str, Any]:
"""
Sends an update report visibility message to RabbitMQ.
The actual database update will be done by the consumer.
Returns:
Dictionary with status and message
"""
# Validar rango de visibilidad
if new_visibility < 0 or new_visibility > 100:
raise ValueError("La visibilidad debe estar entre 0 y 100")
return {
"status": "error",
"message": "La visibilidad debe estar entre 0 y 100"
}
report = self.repo.find_by_id(report_id)
if not report:
raise ValueError(f"Reporte con ID {report_id} no existe")
# Create message object
message = ReportMessage(
event_type=ReportEventType.UPDATE_VISIBILITY,
id_reporte=report_id,
visibilidad=new_visibility,
penalize_author=penalize_author
)
self.repo.update_visibility(report_id, new_visibility)
# Send to RabbitMQ
success = send_to_queue("reports_queue", message.to_dict())
# Si la visibilidad es muy baja (shadowban), penalizar al autor
if penalize_author and new_visibility < 20:
user = self.user_repo.find_by_id(report.id_usuario)
if user:
# Reducir calificación del usuario
new_rating = max(0, user.calificacion - 5)
self.user_repo.update_rating(report.id_usuario, new_rating)
if success:
return {
"status": "queued",
"message": "Actualización de visibilidad enviada a cola para procesamiento",
"report_id": report_id,
"new_visibility": new_visibility
}
else:
return {
"status": "error",
"message": "Error al enviar actualización de visibilidad a la cola de procesamiento"
}
class GetShadowbannedReports:
"""Use case para obtener reportes shadowbaneados (baja visibilidad)"""
@@ -105,11 +152,37 @@ class GetShadowbannedReports:
return self.repo.find_shadowbanned(visibility_threshold)
class DeleteReport:
"""Use case para eliminar un reporte"""
"""Use case para eliminar un reporte - envía mensaje a RabbitMQ"""
def __init__(self, repo: ReportRepository):
if not isinstance(repo, ReportRepository):
raise TypeError("repo must implement ReportRepository")
self.repo = repo
def execute(self, report_id: str) -> bool:
return self.repo.delete(report_id)
def execute(self, report_id: str) -> Dict[str, Any]:
"""
Sends a delete report message to RabbitMQ.
The actual database deletion will be done by the consumer.
Returns:
Dictionary with status and message
"""
# Create message object
message = ReportMessage(
event_type=ReportEventType.DELETE,
id_reporte=report_id
)
# Send to RabbitMQ
success = send_to_queue("reports_queue", message.to_dict())
if success:
return {
"status": "queued",
"message": f"Reporte {report_id} enviado a cola para eliminación",
"id_reporte": report_id
}
else:
return {
"status": "error",
"message": "Error al enviar eliminación del reporte a la cola de procesamiento"
}