mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-06-09 11:39:31 +08:00
Merge pull request #719 from nagisa77/codex/add-reply-and-reaction-support-to-messages
feat: support message replies and reactions
This commit is contained in:
@@ -5,7 +5,6 @@ import com.openisle.dto.ConversationDto;
|
|||||||
import com.openisle.dto.CreateConversationRequest;
|
import com.openisle.dto.CreateConversationRequest;
|
||||||
import com.openisle.dto.CreateConversationResponse;
|
import com.openisle.dto.CreateConversationResponse;
|
||||||
import com.openisle.dto.MessageDto;
|
import com.openisle.dto.MessageDto;
|
||||||
import com.openisle.dto.UserSummaryDto;
|
|
||||||
import com.openisle.model.Message;
|
import com.openisle.model.Message;
|
||||||
import com.openisle.model.MessageConversation;
|
import com.openisle.model.MessageConversation;
|
||||||
import com.openisle.model.User;
|
import com.openisle.model.User;
|
||||||
@@ -55,16 +54,16 @@ public class MessageController {
|
|||||||
|
|
||||||
@PostMapping
|
@PostMapping
|
||||||
public ResponseEntity<MessageDto> sendMessage(@RequestBody MessageRequest req, Authentication auth) {
|
public ResponseEntity<MessageDto> sendMessage(@RequestBody MessageRequest req, Authentication auth) {
|
||||||
Message message = messageService.sendMessage(getCurrentUserId(auth), req.getRecipientId(), req.getContent());
|
Message message = messageService.sendMessage(getCurrentUserId(auth), req.getRecipientId(), req.getContent(), req.getReplyToId());
|
||||||
return ResponseEntity.ok(toDto(message));
|
return ResponseEntity.ok(messageService.toDto(message));
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/conversations/{conversationId}/messages")
|
@PostMapping("/conversations/{conversationId}/messages")
|
||||||
public ResponseEntity<MessageDto> sendMessageToConversation(@PathVariable Long conversationId,
|
public ResponseEntity<MessageDto> sendMessageToConversation(@PathVariable Long conversationId,
|
||||||
@RequestBody ChannelMessageRequest req,
|
@RequestBody ChannelMessageRequest req,
|
||||||
Authentication auth) {
|
Authentication auth) {
|
||||||
Message message = messageService.sendMessageToConversation(getCurrentUserId(auth), conversationId, req.getContent());
|
Message message = messageService.sendMessageToConversation(getCurrentUserId(auth), conversationId, req.getContent(), req.getReplyToId());
|
||||||
return ResponseEntity.ok(toDto(message));
|
return ResponseEntity.ok(messageService.toDto(message));
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/conversations/{conversationId}/read")
|
@PostMapping("/conversations/{conversationId}/read")
|
||||||
@@ -79,23 +78,6 @@ public class MessageController {
|
|||||||
return ResponseEntity.ok(new CreateConversationResponse(conversation.getId()));
|
return ResponseEntity.ok(new CreateConversationResponse(conversation.getId()));
|
||||||
}
|
}
|
||||||
|
|
||||||
private MessageDto toDto(Message message) {
|
|
||||||
MessageDto dto = new MessageDto();
|
|
||||||
dto.setId(message.getId());
|
|
||||||
dto.setContent(message.getContent());
|
|
||||||
dto.setCreatedAt(message.getCreatedAt());
|
|
||||||
|
|
||||||
dto.setConversationId(message.getConversation().getId());
|
|
||||||
|
|
||||||
UserSummaryDto senderDto = new UserSummaryDto();
|
|
||||||
senderDto.setId(message.getSender().getId());
|
|
||||||
senderDto.setUsername(message.getSender().getUsername());
|
|
||||||
senderDto.setAvatar(message.getSender().getAvatar());
|
|
||||||
dto.setSender(senderDto);
|
|
||||||
|
|
||||||
return dto;
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping("/unread-count")
|
@GetMapping("/unread-count")
|
||||||
public ResponseEntity<Long> getUnreadCount(Authentication auth) {
|
public ResponseEntity<Long> getUnreadCount(Authentication auth) {
|
||||||
return ResponseEntity.ok(messageService.getUnreadMessageCount(getCurrentUserId(auth)));
|
return ResponseEntity.ok(messageService.getUnreadMessageCount(getCurrentUserId(auth)));
|
||||||
@@ -105,6 +87,7 @@ public class MessageController {
|
|||||||
static class MessageRequest {
|
static class MessageRequest {
|
||||||
private Long recipientId;
|
private Long recipientId;
|
||||||
private String content;
|
private String content;
|
||||||
|
private Long replyToId;
|
||||||
|
|
||||||
public Long getRecipientId() {
|
public Long getRecipientId() {
|
||||||
return recipientId;
|
return recipientId;
|
||||||
@@ -121,10 +104,19 @@ public class MessageController {
|
|||||||
public void setContent(String content) {
|
public void setContent(String content) {
|
||||||
this.content = content;
|
this.content = content;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Long getReplyToId() {
|
||||||
|
return replyToId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setReplyToId(Long replyToId) {
|
||||||
|
this.replyToId = replyToId;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static class ChannelMessageRequest {
|
static class ChannelMessageRequest {
|
||||||
private String content;
|
private String content;
|
||||||
|
private Long replyToId;
|
||||||
|
|
||||||
public String getContent() {
|
public String getContent() {
|
||||||
return content;
|
return content;
|
||||||
@@ -133,5 +125,13 @@ public class MessageController {
|
|||||||
public void setContent(String content) {
|
public void setContent(String content) {
|
||||||
this.content = content;
|
this.content = content;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Long getReplyToId() {
|
||||||
|
return replyToId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setReplyToId(Long replyToId) {
|
||||||
|
this.replyToId = replyToId;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -57,4 +57,17 @@ public class ReactionController {
|
|||||||
pointService.awardForReactionOfComment(auth.getName(), commentId);
|
pointService.awardForReactionOfComment(auth.getName(), commentId);
|
||||||
return ResponseEntity.ok(dto);
|
return ResponseEntity.ok(dto);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostMapping("/messages/{messageId}/reactions")
|
||||||
|
public ResponseEntity<ReactionDto> reactToMessage(@PathVariable Long messageId,
|
||||||
|
@RequestBody ReactionRequest req,
|
||||||
|
Authentication auth) {
|
||||||
|
Reaction reaction = reactionService.reactToMessage(auth.getName(), messageId, req.getType());
|
||||||
|
if (reaction == null) {
|
||||||
|
return ResponseEntity.noContent().build();
|
||||||
|
}
|
||||||
|
ReactionDto dto = reactionMapper.toDto(reaction);
|
||||||
|
dto.setReward(levelService.awardForReaction(auth.getName()));
|
||||||
|
return ResponseEntity.ok(dto);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package com.openisle.dto;
|
|||||||
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
public class MessageDto {
|
public class MessageDto {
|
||||||
@@ -10,4 +11,6 @@ public class MessageDto {
|
|||||||
private UserSummaryDto sender;
|
private UserSummaryDto sender;
|
||||||
private Long conversationId;
|
private Long conversationId;
|
||||||
private LocalDateTime createdAt;
|
private LocalDateTime createdAt;
|
||||||
|
private MessageDto replyTo;
|
||||||
|
private List<ReactionDto> reactions;
|
||||||
}
|
}
|
||||||
@@ -4,7 +4,7 @@ import com.openisle.model.ReactionType;
|
|||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* DTO representing a reaction on a post or comment.
|
* DTO representing a reaction on a post, comment or message.
|
||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
public class ReactionDto {
|
public class ReactionDto {
|
||||||
@@ -13,6 +13,7 @@ public class ReactionDto {
|
|||||||
private String user;
|
private String user;
|
||||||
private Long postId;
|
private Long postId;
|
||||||
private Long commentId;
|
private Long commentId;
|
||||||
|
private Long messageId;
|
||||||
private int reward;
|
private int reward;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -19,6 +19,9 @@ public class ReactionMapper {
|
|||||||
if (reaction.getComment() != null) {
|
if (reaction.getComment() != null) {
|
||||||
dto.setCommentId(reaction.getComment().getId());
|
dto.setCommentId(reaction.getComment().getId());
|
||||||
}
|
}
|
||||||
|
if (reaction.getMessage() != null) {
|
||||||
|
dto.setMessageId(reaction.getMessage().getId());
|
||||||
|
}
|
||||||
dto.setReward(0);
|
dto.setReward(0);
|
||||||
return dto;
|
return dto;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,6 +29,10 @@ public class Message {
|
|||||||
@Column(nullable = false, columnDefinition = "TEXT")
|
@Column(nullable = false, columnDefinition = "TEXT")
|
||||||
private String content;
|
private String content;
|
||||||
|
|
||||||
|
@ManyToOne(fetch = FetchType.LAZY)
|
||||||
|
@JoinColumn(name = "reply_to_id")
|
||||||
|
private Message replyTo;
|
||||||
|
|
||||||
@CreationTimestamp
|
@CreationTimestamp
|
||||||
@Column(nullable = false, updatable = false)
|
@Column(nullable = false, updatable = false)
|
||||||
private LocalDateTime createdAt;
|
private LocalDateTime createdAt;
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import lombok.Setter;
|
|||||||
import org.hibernate.annotations.CreationTimestamp;
|
import org.hibernate.annotations.CreationTimestamp;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reaction entity representing a user's reaction to a post or comment.
|
* Reaction entity representing a user's reaction to a post, comment or message.
|
||||||
*/
|
*/
|
||||||
@Entity
|
@Entity
|
||||||
@Getter
|
@Getter
|
||||||
@@ -16,7 +16,8 @@ import org.hibernate.annotations.CreationTimestamp;
|
|||||||
@Table(name = "reactions",
|
@Table(name = "reactions",
|
||||||
uniqueConstraints = {
|
uniqueConstraints = {
|
||||||
@UniqueConstraint(columnNames = {"user_id", "post_id", "type"}),
|
@UniqueConstraint(columnNames = {"user_id", "post_id", "type"}),
|
||||||
@UniqueConstraint(columnNames = {"user_id", "comment_id", "type"})
|
@UniqueConstraint(columnNames = {"user_id", "comment_id", "type"}),
|
||||||
|
@UniqueConstraint(columnNames = {"user_id", "message_id", "type"})
|
||||||
})
|
})
|
||||||
public class Reaction {
|
public class Reaction {
|
||||||
@Id
|
@Id
|
||||||
@@ -39,6 +40,10 @@ public class Reaction {
|
|||||||
@JoinColumn(name = "comment_id")
|
@JoinColumn(name = "comment_id")
|
||||||
private Comment comment;
|
private Comment comment;
|
||||||
|
|
||||||
|
@ManyToOne(fetch = FetchType.LAZY)
|
||||||
|
@JoinColumn(name = "message_id")
|
||||||
|
private Message message;
|
||||||
|
|
||||||
@CreationTimestamp
|
@CreationTimestamp
|
||||||
@Column(nullable = false, updatable = false,
|
@Column(nullable = false, updatable = false,
|
||||||
columnDefinition = "DATETIME(6) DEFAULT CURRENT_TIMESTAMP(6)")
|
columnDefinition = "DATETIME(6) DEFAULT CURRENT_TIMESTAMP(6)")
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.openisle.repository;
|
package com.openisle.repository;
|
||||||
|
|
||||||
import com.openisle.model.Comment;
|
import com.openisle.model.Comment;
|
||||||
|
import com.openisle.model.Message;
|
||||||
import com.openisle.model.Post;
|
import com.openisle.model.Post;
|
||||||
import com.openisle.model.Reaction;
|
import com.openisle.model.Reaction;
|
||||||
import com.openisle.model.User;
|
import com.openisle.model.User;
|
||||||
@@ -15,8 +16,10 @@ import java.util.Optional;
|
|||||||
public interface ReactionRepository extends JpaRepository<Reaction, Long> {
|
public interface ReactionRepository extends JpaRepository<Reaction, Long> {
|
||||||
Optional<Reaction> findByUserAndPostAndType(User user, Post post, com.openisle.model.ReactionType type);
|
Optional<Reaction> findByUserAndPostAndType(User user, Post post, com.openisle.model.ReactionType type);
|
||||||
Optional<Reaction> findByUserAndCommentAndType(User user, Comment comment, com.openisle.model.ReactionType type);
|
Optional<Reaction> findByUserAndCommentAndType(User user, Comment comment, com.openisle.model.ReactionType type);
|
||||||
|
Optional<Reaction> findByUserAndMessageAndType(User user, Message message, com.openisle.model.ReactionType type);
|
||||||
List<Reaction> findByPost(Post post);
|
List<Reaction> findByPost(Post post);
|
||||||
List<Reaction> findByComment(Comment comment);
|
List<Reaction> findByComment(Comment comment);
|
||||||
|
List<Reaction> findByMessage(Message message);
|
||||||
|
|
||||||
@Query("SELECT r.post.id FROM Reaction r WHERE r.post IS NOT NULL AND r.post.author.username = :username AND r.type = com.openisle.model.ReactionType.LIKE GROUP BY r.post.id ORDER BY COUNT(r.id) DESC")
|
@Query("SELECT r.post.id FROM Reaction r WHERE r.post IS NOT NULL AND r.post.author.username = :username AND r.type = com.openisle.model.ReactionType.LIKE GROUP BY r.post.id ORDER BY COUNT(r.id) DESC")
|
||||||
List<Long> findTopPostIds(@Param("username") String username, Pageable pageable);
|
List<Long> findTopPostIds(@Param("username") String username, Pageable pageable);
|
||||||
|
|||||||
@@ -4,14 +4,18 @@ import com.openisle.model.Message;
|
|||||||
import com.openisle.model.MessageConversation;
|
import com.openisle.model.MessageConversation;
|
||||||
import com.openisle.model.MessageParticipant;
|
import com.openisle.model.MessageParticipant;
|
||||||
import com.openisle.model.User;
|
import com.openisle.model.User;
|
||||||
|
import com.openisle.model.Reaction;
|
||||||
import com.openisle.repository.MessageConversationRepository;
|
import com.openisle.repository.MessageConversationRepository;
|
||||||
import com.openisle.repository.MessageParticipantRepository;
|
import com.openisle.repository.MessageParticipantRepository;
|
||||||
import com.openisle.repository.MessageRepository;
|
import com.openisle.repository.MessageRepository;
|
||||||
import com.openisle.repository.UserRepository;
|
import com.openisle.repository.UserRepository;
|
||||||
|
import com.openisle.repository.ReactionRepository;
|
||||||
import com.openisle.dto.ConversationDetailDto;
|
import com.openisle.dto.ConversationDetailDto;
|
||||||
import com.openisle.dto.ConversationDto;
|
import com.openisle.dto.ConversationDto;
|
||||||
import com.openisle.dto.MessageDto;
|
import com.openisle.dto.MessageDto;
|
||||||
|
import com.openisle.dto.ReactionDto;
|
||||||
import com.openisle.dto.UserSummaryDto;
|
import com.openisle.dto.UserSummaryDto;
|
||||||
|
import com.openisle.mapper.ReactionMapper;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
@@ -34,9 +38,11 @@ public class MessageService {
|
|||||||
private final MessageParticipantRepository participantRepository;
|
private final MessageParticipantRepository participantRepository;
|
||||||
private final UserRepository userRepository;
|
private final UserRepository userRepository;
|
||||||
private final SimpMessagingTemplate messagingTemplate;
|
private final SimpMessagingTemplate messagingTemplate;
|
||||||
|
private final ReactionRepository reactionRepository;
|
||||||
|
private final ReactionMapper reactionMapper;
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Message sendMessage(Long senderId, Long recipientId, String content) {
|
public Message sendMessage(Long senderId, Long recipientId, String content, Long replyToId) {
|
||||||
log.info("Attempting to send message from user {} to user {}", senderId, recipientId);
|
log.info("Attempting to send message from user {} to user {}", senderId, recipientId);
|
||||||
User sender = userRepository.findById(senderId)
|
User sender = userRepository.findById(senderId)
|
||||||
.orElseThrow(() -> new IllegalArgumentException("Sender not found"));
|
.orElseThrow(() -> new IllegalArgumentException("Sender not found"));
|
||||||
@@ -51,6 +57,11 @@ public class MessageService {
|
|||||||
message.setConversation(conversation);
|
message.setConversation(conversation);
|
||||||
message.setSender(sender);
|
message.setSender(sender);
|
||||||
message.setContent(content);
|
message.setContent(content);
|
||||||
|
if (replyToId != null) {
|
||||||
|
Message replyTo = messageRepository.findById(replyToId)
|
||||||
|
.orElseThrow(() -> new IllegalArgumentException("Message not found"));
|
||||||
|
message.setReplyTo(replyTo);
|
||||||
|
}
|
||||||
message = messageRepository.save(message);
|
message = messageRepository.save(message);
|
||||||
log.info("Message saved with ID: {}", message.getId());
|
log.info("Message saved with ID: {}", message.getId());
|
||||||
|
|
||||||
@@ -83,7 +94,7 @@ public class MessageService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Message sendMessageToConversation(Long senderId, Long conversationId, String content) {
|
public Message sendMessageToConversation(Long senderId, Long conversationId, String content, Long replyToId) {
|
||||||
User sender = userRepository.findById(senderId)
|
User sender = userRepository.findById(senderId)
|
||||||
.orElseThrow(() -> new IllegalArgumentException("Sender not found"));
|
.orElseThrow(() -> new IllegalArgumentException("Sender not found"));
|
||||||
MessageConversation conversation = conversationRepository.findById(conversationId)
|
MessageConversation conversation = conversationRepository.findById(conversationId)
|
||||||
@@ -102,6 +113,11 @@ public class MessageService {
|
|||||||
message.setConversation(conversation);
|
message.setConversation(conversation);
|
||||||
message.setSender(sender);
|
message.setSender(sender);
|
||||||
message.setContent(content);
|
message.setContent(content);
|
||||||
|
if (replyToId != null) {
|
||||||
|
Message replyTo = messageRepository.findById(replyToId)
|
||||||
|
.orElseThrow(() -> new IllegalArgumentException("Message not found"));
|
||||||
|
message.setReplyTo(replyTo);
|
||||||
|
}
|
||||||
message = messageRepository.save(message);
|
message = messageRepository.save(message);
|
||||||
|
|
||||||
conversation.setLastMessage(message);
|
conversation.setLastMessage(message);
|
||||||
@@ -128,7 +144,7 @@ public class MessageService {
|
|||||||
return message;
|
return message;
|
||||||
}
|
}
|
||||||
|
|
||||||
private MessageDto toDto(Message message) {
|
public MessageDto toDto(Message message) {
|
||||||
MessageDto dto = new MessageDto();
|
MessageDto dto = new MessageDto();
|
||||||
dto.setId(message.getId());
|
dto.setId(message.getId());
|
||||||
dto.setContent(message.getContent());
|
dto.setContent(message.getContent());
|
||||||
@@ -141,6 +157,25 @@ public class MessageService {
|
|||||||
userSummaryDto.setAvatar(message.getSender().getAvatar());
|
userSummaryDto.setAvatar(message.getSender().getAvatar());
|
||||||
dto.setSender(userSummaryDto);
|
dto.setSender(userSummaryDto);
|
||||||
|
|
||||||
|
if (message.getReplyTo() != null) {
|
||||||
|
Message reply = message.getReplyTo();
|
||||||
|
MessageDto replyDto = new MessageDto();
|
||||||
|
replyDto.setId(reply.getId());
|
||||||
|
replyDto.setContent(reply.getContent());
|
||||||
|
UserSummaryDto replySender = new UserSummaryDto();
|
||||||
|
replySender.setId(reply.getSender().getId());
|
||||||
|
replySender.setUsername(reply.getSender().getUsername());
|
||||||
|
replySender.setAvatar(reply.getSender().getAvatar());
|
||||||
|
replyDto.setSender(replySender);
|
||||||
|
dto.setReplyTo(replyDto);
|
||||||
|
}
|
||||||
|
|
||||||
|
java.util.List<Reaction> reactions = reactionRepository.findByMessage(message);
|
||||||
|
java.util.List<ReactionDto> reactionDtos = reactions.stream()
|
||||||
|
.map(reactionMapper::toDto)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
dto.setReactions(reactionDtos);
|
||||||
|
|
||||||
return dto;
|
return dto;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,10 +6,12 @@ import com.openisle.model.Reaction;
|
|||||||
import com.openisle.model.ReactionType;
|
import com.openisle.model.ReactionType;
|
||||||
import com.openisle.model.User;
|
import com.openisle.model.User;
|
||||||
import com.openisle.model.NotificationType;
|
import com.openisle.model.NotificationType;
|
||||||
|
import com.openisle.model.Message;
|
||||||
import com.openisle.repository.CommentRepository;
|
import com.openisle.repository.CommentRepository;
|
||||||
import com.openisle.repository.PostRepository;
|
import com.openisle.repository.PostRepository;
|
||||||
import com.openisle.repository.ReactionRepository;
|
import com.openisle.repository.ReactionRepository;
|
||||||
import com.openisle.repository.UserRepository;
|
import com.openisle.repository.UserRepository;
|
||||||
|
import com.openisle.repository.MessageRepository;
|
||||||
import com.openisle.service.NotificationService;
|
import com.openisle.service.NotificationService;
|
||||||
import com.openisle.service.EmailSender;
|
import com.openisle.service.EmailSender;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
@@ -24,6 +26,7 @@ public class ReactionService {
|
|||||||
private final UserRepository userRepository;
|
private final UserRepository userRepository;
|
||||||
private final PostRepository postRepository;
|
private final PostRepository postRepository;
|
||||||
private final CommentRepository commentRepository;
|
private final CommentRepository commentRepository;
|
||||||
|
private final MessageRepository messageRepository;
|
||||||
private final NotificationService notificationService;
|
private final NotificationService notificationService;
|
||||||
private final EmailSender emailSender;
|
private final EmailSender emailSender;
|
||||||
|
|
||||||
@@ -77,6 +80,26 @@ public class ReactionService {
|
|||||||
return reaction;
|
return reaction;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Reaction reactToMessage(String username, Long messageId, ReactionType type) {
|
||||||
|
User user = userRepository.findByUsername(username)
|
||||||
|
.orElseThrow(() -> new com.openisle.exception.NotFoundException("User not found"));
|
||||||
|
Message message = messageRepository.findById(messageId)
|
||||||
|
.orElseThrow(() -> new IllegalArgumentException("Message not found"));
|
||||||
|
java.util.Optional<Reaction> existing =
|
||||||
|
reactionRepository.findByUserAndMessageAndType(user, message, type);
|
||||||
|
if (existing.isPresent()) {
|
||||||
|
reactionRepository.delete(existing.get());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
Reaction reaction = new Reaction();
|
||||||
|
reaction.setUser(user);
|
||||||
|
reaction.setMessage(message);
|
||||||
|
reaction.setType(type);
|
||||||
|
reaction = reactionRepository.save(reaction);
|
||||||
|
return reaction;
|
||||||
|
}
|
||||||
|
|
||||||
public java.util.List<Reaction> getReactionsForPost(Long postId) {
|
public java.util.List<Reaction> getReactionsForPost(Long postId) {
|
||||||
Post post = postRepository.findById(postId)
|
Post post = postRepository.findById(postId)
|
||||||
.orElseThrow(() -> new com.openisle.exception.NotFoundException("Post not found"));
|
.orElseThrow(() -> new com.openisle.exception.NotFoundException("Post not found"));
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import com.openisle.model.Post;
|
|||||||
import com.openisle.model.Reaction;
|
import com.openisle.model.Reaction;
|
||||||
import com.openisle.model.ReactionType;
|
import com.openisle.model.ReactionType;
|
||||||
import com.openisle.model.User;
|
import com.openisle.model.User;
|
||||||
|
import com.openisle.model.Message;
|
||||||
import com.openisle.service.ReactionService;
|
import com.openisle.service.ReactionService;
|
||||||
import com.openisle.service.LevelService;
|
import com.openisle.service.LevelService;
|
||||||
import com.openisle.mapper.ReactionMapper;
|
import com.openisle.mapper.ReactionMapper;
|
||||||
@@ -78,6 +79,27 @@ class ReactionControllerTest {
|
|||||||
.andExpect(jsonPath("$.commentId").value(2));
|
.andExpect(jsonPath("$.commentId").value(2));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void reactToMessage() throws Exception {
|
||||||
|
User user = new User();
|
||||||
|
user.setUsername("u3");
|
||||||
|
Message message = new Message();
|
||||||
|
message.setId(3L);
|
||||||
|
Reaction reaction = new Reaction();
|
||||||
|
reaction.setId(3L);
|
||||||
|
reaction.setUser(user);
|
||||||
|
reaction.setMessage(message);
|
||||||
|
reaction.setType(ReactionType.LIKE);
|
||||||
|
Mockito.when(reactionService.reactToMessage(eq("u3"), eq(3L), eq(ReactionType.LIKE))).thenReturn(reaction);
|
||||||
|
|
||||||
|
mockMvc.perform(post("/api/messages/3/reactions")
|
||||||
|
.contentType("application/json")
|
||||||
|
.content("{\"type\":\"LIKE\"}")
|
||||||
|
.principal(new UsernamePasswordAuthenticationToken("u3", "p")))
|
||||||
|
.andExpect(status().isOk())
|
||||||
|
.andExpect(jsonPath("$.messageId").value(3));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void listReactionTypes() throws Exception {
|
void listReactionTypes() throws Exception {
|
||||||
mockMvc.perform(get("/api/reaction-types"))
|
mockMvc.perform(get("/api/reaction-types"))
|
||||||
|
|||||||
@@ -15,9 +15,10 @@ class ReactionServiceTest {
|
|||||||
UserRepository userRepo = mock(UserRepository.class);
|
UserRepository userRepo = mock(UserRepository.class);
|
||||||
PostRepository postRepo = mock(PostRepository.class);
|
PostRepository postRepo = mock(PostRepository.class);
|
||||||
CommentRepository commentRepo = mock(CommentRepository.class);
|
CommentRepository commentRepo = mock(CommentRepository.class);
|
||||||
|
MessageRepository messageRepo = mock(MessageRepository.class);
|
||||||
NotificationService notif = mock(NotificationService.class);
|
NotificationService notif = mock(NotificationService.class);
|
||||||
EmailSender email = mock(EmailSender.class);
|
EmailSender email = mock(EmailSender.class);
|
||||||
ReactionService service = new ReactionService(reactionRepo, userRepo, postRepo, commentRepo, notif, email);
|
ReactionService service = new ReactionService(reactionRepo, userRepo, postRepo, commentRepo, messageRepo, notif, email);
|
||||||
org.springframework.test.util.ReflectionTestUtils.setField(service, "websiteUrl", "https://ex.com");
|
org.springframework.test.util.ReflectionTestUtils.setField(service, "websiteUrl", "https://ex.com");
|
||||||
|
|
||||||
User user = new User();
|
User user = new User();
|
||||||
|
|||||||
@@ -138,7 +138,9 @@ const toggleReaction = async (type) => {
|
|||||||
const url =
|
const url =
|
||||||
props.contentType === 'post'
|
props.contentType === 'post'
|
||||||
? `${API_BASE_URL}/api/posts/${props.contentId}/reactions`
|
? `${API_BASE_URL}/api/posts/${props.contentId}/reactions`
|
||||||
: `${API_BASE_URL}/api/comments/${props.contentId}/reactions`
|
: props.contentType === 'comment'
|
||||||
|
? `${API_BASE_URL}/api/comments/${props.contentId}/reactions`
|
||||||
|
: `${API_BASE_URL}/api/messages/${props.contentId}/reactions`
|
||||||
|
|
||||||
// optimistic update
|
// optimistic update
|
||||||
const existingIdx = reactions.value.findIndex(
|
const existingIdx = reactions.value.findIndex(
|
||||||
|
|||||||
@@ -30,9 +30,21 @@
|
|||||||
{{ TimeManager.format(item.createdAt) }}
|
{{ TimeManager.format(item.createdAt) }}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<div v-if="item.replyTo" class="reply-preview">
|
||||||
|
<div class="reply-author">{{ item.replyTo.sender.username }}</div>
|
||||||
|
<div class="reply-content" v-html="renderMarkdown(item.replyTo.content)"></div>
|
||||||
|
</div>
|
||||||
<div class="message-content">
|
<div class="message-content">
|
||||||
<div class="info-content-text" v-html="renderMarkdown(item.content)"></div>
|
<div class="info-content-text" v-html="renderMarkdown(item.content)"></div>
|
||||||
</div>
|
</div>
|
||||||
|
<ReactionsGroup
|
||||||
|
:model-value="item.reactions"
|
||||||
|
content-type="message"
|
||||||
|
:content-id="item.id"
|
||||||
|
@update:modelValue="(v) => (item.reactions = v)"
|
||||||
|
>
|
||||||
|
<div class="reply-btn" @click="setReply(item)">回复</div>
|
||||||
|
</ReactionsGroup>
|
||||||
</template>
|
</template>
|
||||||
</BaseTimeline>
|
</BaseTimeline>
|
||||||
<div class="empty-container">
|
<div class="empty-container">
|
||||||
@@ -46,6 +58,11 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="message-input-area">
|
<div class="message-input-area">
|
||||||
|
<div v-if="replyTo" class="active-reply">
|
||||||
|
正在回复 {{ replyTo.sender.username }}:
|
||||||
|
{{ stripMarkdownLength(replyTo.content, 50) }}
|
||||||
|
<i class="fas fa-times close-reply" @click="replyTo = null"></i>
|
||||||
|
</div>
|
||||||
<MessageEditor :loading="sending" @submit="sendMessage" />
|
<MessageEditor :loading="sending" @submit="sendMessage" />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -65,8 +82,9 @@ import {
|
|||||||
import { useRoute } from 'vue-router'
|
import { useRoute } from 'vue-router'
|
||||||
import { getToken, fetchCurrentUser } from '~/utils/auth'
|
import { getToken, fetchCurrentUser } from '~/utils/auth'
|
||||||
import { toast } from '~/main'
|
import { toast } from '~/main'
|
||||||
import { renderMarkdown } from '~/utils/markdown'
|
import { renderMarkdown, stripMarkdownLength } from '~/utils/markdown'
|
||||||
import MessageEditor from '~/components/MessageEditor.vue'
|
import MessageEditor from '~/components/MessageEditor.vue'
|
||||||
|
import ReactionsGroup from '~/components/ReactionsGroup.vue'
|
||||||
import { useWebSocket } from '~/composables/useWebSocket'
|
import { useWebSocket } from '~/composables/useWebSocket'
|
||||||
import { useUnreadCount } from '~/composables/useUnreadCount'
|
import { useUnreadCount } from '~/composables/useUnreadCount'
|
||||||
import { useChannelsUnreadCount } from '~/composables/useChannelsUnreadCount'
|
import { useChannelsUnreadCount } from '~/composables/useChannelsUnreadCount'
|
||||||
@@ -97,6 +115,7 @@ const loadingMore = ref(false)
|
|||||||
let scrollInterval = null
|
let scrollInterval = null
|
||||||
const conversationName = ref('')
|
const conversationName = ref('')
|
||||||
const isChannel = ref(false)
|
const isChannel = ref(false)
|
||||||
|
const replyTo = ref(null)
|
||||||
|
|
||||||
const hasMoreMessages = computed(() => currentPage.value < totalPages.value - 1)
|
const hasMoreMessages = computed(() => currentPage.value < totalPages.value - 1)
|
||||||
|
|
||||||
@@ -115,6 +134,10 @@ function handleAvatarError(event) {
|
|||||||
event.target.src = '/default-avatar.svg'
|
event.target.src = '/default-avatar.svg'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function setReply(message) {
|
||||||
|
replyTo.value = message
|
||||||
|
}
|
||||||
|
|
||||||
// No changes needed here, as renderMarkdown is now imported.
|
// No changes needed here, as renderMarkdown is now imported.
|
||||||
// The old function is removed.
|
// The old function is removed.
|
||||||
|
|
||||||
@@ -208,7 +231,7 @@ async function sendMessage(content, clearInput) {
|
|||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
Authorization: `Bearer ${token}`,
|
Authorization: `Bearer ${token}`,
|
||||||
},
|
},
|
||||||
body: JSON.stringify({ content }),
|
body: JSON.stringify({ content, replyToId: replyTo.value?.id }),
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
@@ -226,6 +249,7 @@ async function sendMessage(content, clearInput) {
|
|||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
recipientId: recipient.id,
|
recipientId: recipient.id,
|
||||||
content: content,
|
content: content,
|
||||||
|
replyToId: replyTo.value?.id,
|
||||||
}),
|
}),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -240,6 +264,7 @@ async function sendMessage(content, clearInput) {
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
clearInput()
|
clearInput()
|
||||||
|
replyTo.value = null
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
scrollToBottom()
|
scrollToBottom()
|
||||||
}, 100)
|
}, 100)
|
||||||
@@ -524,4 +549,39 @@ onUnmounted(() => {
|
|||||||
margin-left: 10px;
|
margin-left: 10px;
|
||||||
margin-right: 10px;
|
margin-right: 10px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.reply-preview {
|
||||||
|
padding: 5px 10px;
|
||||||
|
border-left: 2px solid var(--primary-color);
|
||||||
|
margin-bottom: 5px;
|
||||||
|
font-size: 13px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.reply-author {
|
||||||
|
font-weight: bold;
|
||||||
|
margin-bottom: 2px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.reply-btn {
|
||||||
|
cursor: pointer;
|
||||||
|
padding: 4px;
|
||||||
|
opacity: 0.6;
|
||||||
|
}
|
||||||
|
|
||||||
|
.reply-btn:hover {
|
||||||
|
opacity: 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
.active-reply {
|
||||||
|
background-color: var(--bg-color-soft);
|
||||||
|
padding: 5px 10px;
|
||||||
|
border-left: 3px solid var(--primary-color);
|
||||||
|
margin-bottom: 5px;
|
||||||
|
font-size: 13px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.close-reply {
|
||||||
|
margin-left: 8px;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|||||||
Reference in New Issue
Block a user