java.lang.Object
fr.utc.miage.sporttrack.service.user.communication.MessageService

@Service public class MessageService extends Object
Service layer component responsible for managing Message entities within the SportTrack application.

Provides business logic for sending messages, retrieving conversations, and tracking unread message counts between athlete friends.

Author:
SportTrack Team
  • Constructor Details

    • MessageService

      public MessageService(MessageRepository messageRepository, AthleteRepository athleteRepository, FriendshipRepository friendshipRepository)
      Constructs a MessageService without notification support.
      Parameters:
      messageRepository - the message repository
      athleteRepository - the athlete repository
      friendshipRepository - the friendship repository
    • MessageService

      @Autowired public MessageService(MessageRepository messageRepository, AthleteRepository athleteRepository, FriendshipRepository friendshipRepository, NotificationService notificationService)
      Constructs a MessageService with full notification support.
      Parameters:
      messageRepository - the message repository
      athleteRepository - the athlete repository
      friendshipRepository - the friendship repository
      notificationService - the notification service for message events
  • Method Details

    • sendMessage

      @Transactional public Message sendMessage(Integer senderId, Integer recipientId, String content)
      Sends a message from one athlete to another. Both athletes must be friends.
      Parameters:
      senderId - the identifier of the sending athlete
      recipientId - the identifier of the receiving athlete
      content - the textual content of the message
      Returns:
      the persisted Message
      Throws:
      IllegalArgumentException - if sender equals recipient, content is empty, or a user is not found
      IllegalStateException - if the two athletes are not friends
    • getConversation

      @Transactional public List<Message> getConversation(Integer currentUserId, Integer friendId)
      Retrieves the full conversation between two friends and marks all messages as seen.
      Parameters:
      currentUserId - the identifier of the current user
      friendId - the identifier of the friend
      Returns:
      a list of messages in the conversation, ordered by send time ascending
      Throws:
      IllegalStateException - if the two athletes are not friends
    • getLatestMessageByFriend

      public Map<Integer,Message> getLatestMessageByFriend(Integer athleteId)
      Returns a map of friend identifiers to their most recent message with the given athlete.
      Parameters:
      athleteId - the identifier of the athlete
      Returns:
      a map where each key is a friend's identifier and the value is the latest message
    • getUnreadCountByFriend

      public Map<Integer,Integer> getUnreadCountByFriend(Integer athleteId)
      Returns a map of friend identifiers to the count of unread messages from that friend.
      Parameters:
      athleteId - the identifier of the athlete
      Returns:
      a map where each key is a friend's identifier and the value is the unread count
    • countUnreadMessages

      public long countUnreadMessages(Integer athleteId)
      Counts the total number of unread messages for the specified athlete.
      Parameters:
      athleteId - the identifier of the athlete
      Returns:
      the total count of unread messages