Interface ChatMessageRepository
- All Superinterfaces:
org.springframework.data.repository.CrudRepository<ChatMessage,,UUID> org.springframework.data.jpa.repository.JpaRepository<ChatMessage,,UUID> org.springframework.data.repository.ListCrudRepository<ChatMessage,,UUID> org.springframework.data.repository.ListPagingAndSortingRepository<ChatMessage,,UUID> org.springframework.data.repository.PagingAndSortingRepository<ChatMessage,,UUID> org.springframework.data.repository.query.QueryByExampleExecutor<ChatMessage>,org.springframework.data.repository.Repository<ChatMessage,UUID>
@Repository
public interface ChatMessageRepository
extends org.springframework.data.jpa.repository.JpaRepository<ChatMessage,UUID>
-
Method Summary
Modifier and TypeMethodDescriptioncountByConversationId(Set<UUID> conversationIds) Find the count of messages by conversation id for the provided ids.countUnreadByConversationIdIn(UUID userId, Set<UUID> conversationIds) Count the unread messages for a provided conversationsorg.springframework.data.domain.Page<ChatMessage> findByContentContainingIgnoreCase(UUID userId, String term, org.springframework.data.domain.Pageable pageable) Search chat messages by search term in the contentfindByConversationIdOrderByCreatedAtAsc(UUID conversationId) Find all messages for a specific conversation, ordered by timestampFind a message by its unique message IDfindByIdInAndConversationId(Collection<UUID> ids, UUID conversationId) Find the messages by conversation id and its unique message IDsfindBySenderIdOrderByCreatedAtDesc(UUID senderId) Find messages by sender IDfindEarliestUnreadByConversationIdIn(UUID userId, Set<UUID> conversationIds) Find the earliest unread message for a provided conversations, ordered by creation timestampfindLatestByConversationIdIn(Set<UUID> conversationIds) Find the latest messages for a specific conversation, ordered by creation timestamp descending orderorg.springframework.data.domain.Page<ChatMessage> findMessagesByConversationId(UUID conversationId, org.springframework.data.domain.Pageable pageable) Find page of messages in a conversation with offset and limit for lazy loadingfindMessagesByConversationIdAndIdIn(UUID conversationId, List<UUID> ids) Find page of messages in a conversation for the provided ids with offset and limit for lazy loadinggetConversationMessagesByIdInToMarkAsRead(UUID conversationId, Set<UUID> ids, UUID receiverId, Instant readAt) Get all unread messages in a conversation that will be marked as read by a specific user for the passed ids.getConversationMessagesToMarkAsRead(UUID conversationId, UUID receiverId, Instant readAt) Get all unread messages in a conversation that will be marked as read by a specific usergetConversationMessagesUptoCreatedAtToMarkAsRead(UUID conversationId, Instant uptoCreatedAt, UUID receiverId, Instant readAt) Get all unread messages in a conversation that will be marked as read by a specific user upto the created at timestamp.intmarkConversationMessagesAsRead(UUID conversationId, UUID receiverId, Instant readAt) Mark all unread messages in a conversation as read by a specific userintmarkConversationMessagesByIdInAsRead(UUID conversationId, Set<UUID> ids, UUID receiverId, Instant readAt) Mark all unread messages in a conversation as read by a specific user for the passed ids.intmarkConversationMessagesUptoCreatedAtAsRead(UUID conversationId, Instant uptoCreatedAt, UUID receiverId, Instant readAt) Mark all unread messages in a conversation as read by a specific user upto the created at timestamp.intmarkMessageAsReadByReceiver(String messageId, UUID receiverId, Instant readAt) Mark a specific message as read by receiverintupdateMessageIndex(UUID conversationId) Sets the message index for all the messages that do not have the message index set and the newly calculated index for the conversationMethods inherited from interface org.springframework.data.repository.CrudRepository
count, delete, deleteAll, deleteAll, deleteAllById, deleteById, existsById, saveMethods inherited from interface org.springframework.data.jpa.repository.JpaRepository
deleteAllByIdInBatch, deleteAllInBatch, deleteAllInBatch, deleteInBatch, findAll, findAll, flush, getById, getOne, getReferenceById, saveAllAndFlush, saveAndFlushMethods inherited from interface org.springframework.data.repository.ListCrudRepository
findAll, findAllById, saveAllMethods inherited from interface org.springframework.data.repository.ListPagingAndSortingRepository
findAllMethods inherited from interface org.springframework.data.repository.PagingAndSortingRepository
findAllMethods inherited from interface org.springframework.data.repository.query.QueryByExampleExecutor
count, exists, findAll, findBy, findOne
-
Method Details
-
findById
Find a message by its unique message ID- Specified by:
findByIdin interfaceorg.springframework.data.repository.CrudRepository<ChatMessage,UUID>
-
findByIdInAndConversationId
@NonNull List<ChatMessage> findByIdInAndConversationId(@NonNull Collection<UUID> ids, @NonNull UUID conversationId) Find the messages by conversation id and its unique message IDs -
findByConversationIdOrderByCreatedAtAsc
Find all messages for a specific conversation, ordered by timestamp -
countByConversationId
@Query("SELECT new com.finconsgroup.itserr.marketplace.usercommunication.dm.dto.OutputConversationMessageSummaryDto(cm.conversationId, count(cm)) FROM ChatMessage cm WHERE cm.conversationId in (:conversationIds) GROUP BY cm.conversationId") List<OutputConversationMessageSummaryDto> countByConversationId(Set<UUID> conversationIds) Find the count of messages by conversation id for the provided ids.- Parameters:
conversationIds- the conversation ids- Returns:
- the list of
OutputConversationMessageSummaryDtocontaining the counts
-
findLatestByConversationIdIn
@Query(value="WITH messages AS (SELECT cm.*, rank() OVER (PARTITION BY cm.conversation_id ORDER by cm.created_at DESC) message_rank FROM chat_messages cm WHERE cm.conversation_id IN (:conversationIds))SELECT messages.* FROM messages WHERE message_rank = 1", nativeQuery=true) List<ChatMessage> findLatestByConversationIdIn(Set<UUID> conversationIds) Find the latest messages for a specific conversation, ordered by creation timestamp descending order -
countUnreadByConversationIdIn
@Query("SELECT new com.finconsgroup.itserr.marketplace.usercommunication.dm.dto.OutputConversationMessageSummaryDto(\ncm.conversationId, COUNT(cm)) FROM ChatMessage cm\nWHERE cm.conversationId IN (:conversationIds)\nAND cm.senderId <> :userId\nAND cm.readAt IS NULL\nGROUP BY cm.conversationId\n") List<OutputConversationMessageSummaryDto> countUnreadByConversationIdIn(@Param("userId") UUID userId, @Param("conversationIds") Set<UUID> conversationIds) Count the unread messages for a provided conversations- Parameters:
conversationIds- the conversation ids
-
findEarliestUnreadByConversationIdIn
@Query(value=" WITH messages AS (SELECT cm.*,\n rank() OVER (PARTITION BY cm.conversation_id ORDER by cm.created_at) message_rank\n FROM chat_messages cm\n WHERE cm.conversation_id IN (:conversationIds)\n AND cm.sender_id <> :userId\n AND cm.read_at IS NULL)\n SELECT messages.* FROM messages WHERE message_rank = 1\n", nativeQuery=true) List<ChatMessage> findEarliestUnreadByConversationIdIn(@Param("userId") UUID userId, @Param("conversationIds") Set<UUID> conversationIds) Find the earliest unread message for a provided conversations, ordered by creation timestamp- Parameters:
conversationIds- the conversation ids
-
findBySenderIdOrderByCreatedAtDesc
Find messages by sender ID -
findMessagesByConversationId
org.springframework.data.domain.Page<ChatMessage> findMessagesByConversationId(UUID conversationId, org.springframework.data.domain.Pageable pageable) Find page of messages in a conversation with offset and limit for lazy loading -
findMessagesByConversationIdAndIdIn
Find page of messages in a conversation for the provided ids with offset and limit for lazy loading -
findByContentContainingIgnoreCase
@Query("SELECT cm FROM ChatMessage cm\nJOIN Conversation c on c.id = cm.conversationId\nWHERE c.active = true\nAND LOWER(cm.content) LIKE LOWER(CONCAT(\'%\', :term, \'%\'))\nAND EXISTS (\n SELECT 1 FROM ConversationParticipant cp\n WHERE cp.conversationId = c.id\n AND cp.userId = :userId\n AND cp.active = true\n)") org.springframework.data.domain.Page<ChatMessage> findByContentContainingIgnoreCase(@Param("userId") UUID userId, @Param("term") String term, org.springframework.data.domain.Pageable pageable) Search chat messages by search term in the content -
markMessageAsReadByReceiver
@Modifying(flushAutomatically=true) @Query("UPDATE ChatMessage cm SET cm.readByReceiver = true, cm.readAt = :readAt WHERE cm.id = :messageId AND cm.receiverId = :receiverId") int markMessageAsReadByReceiver(@Param("messageId") String messageId, @Param("receiverId") UUID receiverId, @Param("readAt") Instant readAt) Mark a specific message as read by receiver -
getConversationMessagesToMarkAsRead
@Query("SELECT cm.id FROM ChatMessage cm\nWHERE cm.conversationId = :conversationId AND cm.receiverId = :receiverId\nAND cm.readByReceiver = false\n") List<UUID> getConversationMessagesToMarkAsRead(@Param("conversationId") UUID conversationId, @Param("receiverId") UUID receiverId, @Param("readAt") Instant readAt) Get all unread messages in a conversation that will be marked as read by a specific user -
markConversationMessagesAsRead
@Modifying(flushAutomatically=true) @Query("UPDATE ChatMessage cm SET cm.readByReceiver = true, cm.readAt = :readAt\nWHERE cm.conversationId = :conversationId AND cm.receiverId = :receiverId\nAND cm.readByReceiver = false\n") int markConversationMessagesAsRead(@Param("conversationId") UUID conversationId, @Param("receiverId") UUID receiverId, @Param("readAt") Instant readAt) Mark all unread messages in a conversation as read by a specific user -
getConversationMessagesUptoCreatedAtToMarkAsRead
@Query("SELECT cm.id FROM ChatMessage cm\nWHERE cm.conversationId = :conversationId AND cm.receiverId = :receiverId\nAND cm.readByReceiver = false AND cm.createdAt <= :uptoCreatedAt\n") List<UUID> getConversationMessagesUptoCreatedAtToMarkAsRead(@Param("conversationId") UUID conversationId, @Param("uptoCreatedAt") Instant uptoCreatedAt, @Param("receiverId") UUID receiverId, @Param("readAt") Instant readAt) Get all unread messages in a conversation that will be marked as read by a specific user upto the created at timestamp. -
markConversationMessagesUptoCreatedAtAsRead
@Modifying(flushAutomatically=true) @Query("UPDATE ChatMessage cm SET cm.readByReceiver = true, cm.readAt = :readAt\nWHERE cm.conversationId = :conversationId AND cm.receiverId = :receiverId\nAND cm.readByReceiver = false AND cm.createdAt <= :uptoCreatedAt\n") int markConversationMessagesUptoCreatedAtAsRead(@Param("conversationId") UUID conversationId, @Param("uptoCreatedAt") Instant uptoCreatedAt, @Param("receiverId") UUID receiverId, @Param("readAt") Instant readAt) Mark all unread messages in a conversation as read by a specific user upto the created at timestamp. -
getConversationMessagesByIdInToMarkAsRead
@Query("SELECT cm.id FROM ChatMessage cm\nWHERE cm.conversationId = :conversationId AND cm.receiverId = :receiverId\nAND cm.readByReceiver = false AND cm.id IN (:ids)\n") List<UUID> getConversationMessagesByIdInToMarkAsRead(@Param("conversationId") UUID conversationId, @Param("ids") Set<UUID> ids, @Param("receiverId") UUID receiverId, @Param("readAt") Instant readAt) Get all unread messages in a conversation that will be marked as read by a specific user for the passed ids. -
markConversationMessagesByIdInAsRead
@Modifying @Query("UPDATE ChatMessage cm SET cm.readByReceiver = true, cm.readAt = :readAt\nWHERE cm.conversationId = :conversationId AND cm.receiverId = :receiverId\nAND cm.readByReceiver = false AND cm.id IN (:ids)\n") int markConversationMessagesByIdInAsRead(@Param("conversationId") UUID conversationId, @Param("ids") Set<UUID> ids, @Param("receiverId") UUID receiverId, @Param("readAt") Instant readAt) Mark all unread messages in a conversation as read by a specific user for the passed ids. -
updateMessageIndex
@Modifying @Query(value=" with max_index as (\n select\n conversation_id,\n coalesce(max(message_index), 0) as max_index\n from\n chat_messages\n where\n conversation_id = :conversationId\n and message_index is not null\n group by\n conversation_id\n ),\n messages_with_index as (\n select\n cm.id,\n cm.conversation_id,\n row_number() over(partition by cm.conversation_id order by cm.created_at, cm.id) as message_index,\n coalesce(max_index.max_index, 0) as max_index\n from\n chat_messages cm\n left join max_index on\n max_index.conversation_id = cm.conversation_id\n where\n cm.conversation_id = :conversationId\n and message_index is null\n )\n update\n chat_messages\n set\n message_index = messages_with_index.max_index + messages_with_index.message_index\n from\n messages_with_index\n where\n chat_messages.conversation_id = :conversationId\n and chat_messages.id = messages_with_index.id\n", nativeQuery=true) int updateMessageIndex(@Param("conversationId") UUID conversationId) Sets the message index for all the messages that do not have the message index set and the newly calculated index for the conversation
-