feat(backend): implémentation complète du système de messagerie

Ajoute l'infrastructure backend complète pour les conversations et messages :

## Entités
- Conversation : conversations individuelles, groupes, broadcast, annonces
- Message : messages avec statut, priorité, pièces jointes, soft delete

## Repositories
- ConversationRepository : findByParticipant, findByIdAndParticipant (sécurité)
- MessageRepository : findByConversation, countUnread, markAsRead

## Services
- ConversationService : CRUD conversations, archive, mute, pin
- MessageService : send, edit, delete, getMessages

## REST Endpoints (12 total)
- GET /api/conversations - Lister mes conversations
- GET /api/conversations/{id} - Récupérer une conversation
- POST /api/conversations - Créer conversation
- PUT /api/conversations/{id}/archive - Archiver
- PUT /api/conversations/{id}/mark-read - Marquer comme lu
- PUT /api/conversations/{id}/toggle-mute - Activer/désactiver son
- PUT /api/conversations/{id}/toggle-pin - Épingler
- GET /api/messages?conversationId=X - Lister messages
- POST /api/messages - Envoyer message
- PUT /api/messages/{id} - Éditer message
- DELETE /api/messages/{id} - Supprimer message

## Database
- Migration V6 : tables conversations, messages, conversation_participants
- Indexes sur organisation, type, archived, deleted pour performance

## Sécurité
- SecuriteHelper.resolveMembreId() : résolution membre depuis JWT
- Vérification accès conversation avant toute opération
- @RolesAllowed sur tous les endpoints

Débloquer la fonctionnalité Communication mobile (actuellement 100% stubs).

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
dahoud
2026-03-16 06:39:39 +00:00
parent f5271cc29e
commit 3be01e28a7
10 changed files with 1226 additions and 0 deletions

View File

@@ -0,0 +1,208 @@
package dev.lions.unionflow.server.service;
import dev.lions.unionflow.server.api.dto.communication.request.CreateConversationRequest;
import dev.lions.unionflow.server.api.dto.communication.response.ConversationResponse;
import dev.lions.unionflow.server.api.dto.communication.response.MessageResponse;
import dev.lions.unionflow.server.entity.Conversation;
import dev.lions.unionflow.server.entity.Membre;
import dev.lions.unionflow.server.entity.Message;
import dev.lions.unionflow.server.entity.Organisation;
import dev.lions.unionflow.server.repository.ConversationRepository;
import dev.lions.unionflow.server.repository.MembreRepository;
import dev.lions.unionflow.server.repository.MessageRepository;
import dev.lions.unionflow.server.repository.OrganisationRepository;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.transaction.Transactional;
import jakarta.ws.rs.NotFoundException;
import org.jboss.logging.Logger;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
/**
* Service de gestion des conversations
*
* @author UnionFlow Team
* @version 1.0
* @since 2026-03-16
*/
@ApplicationScoped
public class ConversationService {
private static final Logger LOG = Logger.getLogger(ConversationService.class);
@Inject
ConversationRepository conversationRepository;
@Inject
MessageRepository messageRepository;
@Inject
MembreRepository membreRepository;
@Inject
OrganisationRepository organisationRepository;
/**
* Liste les conversations d'un membre
*/
public List<ConversationResponse> getConversations(UUID membreId, UUID organisationId, boolean includeArchived) {
LOG.infof("Récupération conversations pour membre %s", membreId);
List<Conversation> conversations;
if (organisationId != null) {
conversations = conversationRepository.findByOrganisation(organisationId);
} else {
conversations = conversationRepository.findByParticipant(membreId, includeArchived);
}
return conversations.stream()
.map(c -> convertToResponse(c, membreId))
.collect(Collectors.toList());
}
/**
* Récupère une conversation par ID
*/
public ConversationResponse getConversationById(UUID conversationId, UUID membreId) {
Conversation conversation = conversationRepository.findByIdAndParticipant(conversationId, membreId)
.orElseThrow(() -> new NotFoundException("Conversation non trouvée ou accès refusé"));
return convertToResponse(conversation, membreId);
}
/**
* Crée une nouvelle conversation
*/
@Transactional
public ConversationResponse createConversation(CreateConversationRequest request, UUID creatorId) {
LOG.infof("Création conversation: %s (type: %s)", request.name(), request.type());
Conversation conversation = new Conversation();
conversation.setName(request.name());
conversation.setDescription(request.description());
conversation.setType(request.type());
// Ajouter les participants
List<Membre> participants = request.participantIds().stream()
.map(id -> membreRepository.findById(id))
.filter(membre -> membre != null)
.collect(Collectors.toList());
// Ajouter le créateur s'il n'est pas dans la liste
Membre creator = membreRepository.findById(creatorId);
if (creator != null && !participants.contains(creator)) {
participants.add(creator);
}
conversation.setParticipants(participants);
// Organisation
if (request.organisationId() != null) {
Organisation org = organisationRepository.findById(request.organisationId());
conversation.setOrganisation(org);
}
conversation.setUpdatedAt(LocalDateTime.now());
conversationRepository.persist(conversation);
return convertToResponse(conversation, creatorId);
}
/**
* Archive/désarchive une conversation
*/
@Transactional
public void archiveConversation(UUID conversationId, UUID membreId, boolean archive) {
Conversation conversation = conversationRepository.findByIdAndParticipant(conversationId, membreId)
.orElseThrow(() -> new NotFoundException("Conversation non trouvée"));
conversation.setIsArchived(archive);
conversation.setUpdatedAt(LocalDateTime.now());
conversationRepository.persist(conversation);
}
/**
* Marque une conversation comme lue
*/
@Transactional
public void markAsRead(UUID conversationId, UUID membreId) {
// Vérifier accès
conversationRepository.findByIdAndParticipant(conversationId, membreId)
.orElseThrow(() -> new NotFoundException("Conversation non trouvée"));
messageRepository.markAllAsReadByConversationAndMember(conversationId, membreId);
}
/**
* Toggle mute
*/
@Transactional
public void toggleMute(UUID conversationId, UUID membreId) {
Conversation conversation = conversationRepository.findByIdAndParticipant(conversationId, membreId)
.orElseThrow(() -> new NotFoundException("Conversation non trouvée"));
conversation.setIsMuted(!conversation.getIsMuted());
conversation.setUpdatedAt(LocalDateTime.now());
}
/**
* Toggle pin
*/
@Transactional
public void togglePin(UUID conversationId, UUID membreId) {
Conversation conversation = conversationRepository.findByIdAndParticipant(conversationId, membreId)
.orElseThrow(() -> new NotFoundException("Conversation non trouvée"));
conversation.setIsPinned(!conversation.getIsPinned());
conversation.setUpdatedAt(LocalDateTime.now());
}
/**
* Convertit Conversation en DTO
*/
private ConversationResponse convertToResponse(Conversation c, UUID currentUserId) {
Message lastMsg = messageRepository.findLastByConversation(c.getId());
long unreadCount = messageRepository.countUnreadByConversationAndMember(c.getId(), currentUserId);
return ConversationResponse.builder()
.id(c.getId())
.name(c.getName())
.description(c.getDescription())
.type(c.getType())
.participantIds(c.getParticipants().stream().map(Membre::getId).collect(Collectors.toList()))
.organisationId(c.getOrganisation() != null ? c.getOrganisation().getId() : null)
.lastMessage(lastMsg != null ? convertMessageToResponse(lastMsg) : null)
.unreadCount((int) unreadCount)
.isMuted(c.getIsMuted())
.isPinned(c.getIsPinned())
.isArchived(c.getIsArchived())
.createdAt(c.getDateCreation())
.updatedAt(c.getUpdatedAt())
.avatarUrl(c.getAvatarUrl())
.build();
}
/**
* Convertit Message en DTO simple
*/
private MessageResponse convertMessageToResponse(Message m) {
return MessageResponse.builder()
.id(m.getId())
.conversationId(m.getConversation().getId())
.senderId(m.getSender().getId())
.senderName(m.getSenderName())
.senderAvatar(m.getSenderAvatar())
.content(m.getContent())
.type(m.getType())
.status(m.getStatus())
.priority(m.getPriority())
.createdAt(m.getDateCreation())
.isEdited(m.getIsEdited())
.isDeleted(m.getIsDeleted())
.build();
}
}