mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-02-23 06:30:48 +08:00
feat: add channel tabs and chat support
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
package com.openisle.config;
|
||||
|
||||
import com.openisle.model.Channel;
|
||||
import com.openisle.model.MessageConversation;
|
||||
import com.openisle.repository.ChannelRepository;
|
||||
import com.openisle.repository.MessageConversationRepository;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class ChannelInitializer implements CommandLineRunner {
|
||||
private final ChannelRepository channelRepository;
|
||||
private final MessageConversationRepository conversationRepository;
|
||||
|
||||
@Override
|
||||
public void run(String... args) {
|
||||
if (channelRepository.count() == 0) {
|
||||
createChannel("吹水群", "闲聊讨论", "/default-avatar.svg");
|
||||
createChannel("技术讨论群", "技术交流", "/default-avatar.svg");
|
||||
}
|
||||
}
|
||||
|
||||
private void createChannel(String name, String description, String avatar) {
|
||||
MessageConversation conversation = new MessageConversation();
|
||||
conversation = conversationRepository.save(conversation);
|
||||
Channel channel = new Channel();
|
||||
channel.setName(name);
|
||||
channel.setDescription(description);
|
||||
channel.setAvatar(avatar);
|
||||
channel.setConversation(conversation);
|
||||
channelRepository.save(channel);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package com.openisle.controller;
|
||||
|
||||
import com.openisle.dto.ChannelDto;
|
||||
import com.openisle.model.Channel;
|
||||
import com.openisle.model.MessageParticipant;
|
||||
import com.openisle.model.MessageConversation;
|
||||
import com.openisle.model.User;
|
||||
import com.openisle.repository.ChannelRepository;
|
||||
import com.openisle.repository.MessageParticipantRepository;
|
||||
import com.openisle.repository.UserRepository;
|
||||
import com.openisle.repository.MessageRepository;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/channels")
|
||||
@RequiredArgsConstructor
|
||||
public class ChannelController {
|
||||
private final ChannelRepository channelRepository;
|
||||
private final MessageParticipantRepository participantRepository;
|
||||
private final UserRepository userRepository;
|
||||
private final MessageRepository messageRepository;
|
||||
|
||||
private Long getCurrentUserId(Authentication auth) {
|
||||
User user = userRepository.findByUsername(auth.getName()).orElseThrow(() -> new IllegalArgumentException("User not found"));
|
||||
return user.getId();
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public ResponseEntity<List<ChannelDto>> listChannels(Authentication auth) {
|
||||
Long userId = auth == null ? null : getCurrentUserId(auth);
|
||||
List<ChannelDto> channels = channelRepository.findAll().stream()
|
||||
.map(c -> toDto(c, userId))
|
||||
.collect(Collectors.toList());
|
||||
return ResponseEntity.ok(channels);
|
||||
}
|
||||
|
||||
@PostMapping("/{id}/join")
|
||||
public ResponseEntity<Void> joinChannel(@PathVariable Long id, Authentication auth) {
|
||||
Channel channel = channelRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("Channel not found"));
|
||||
Long userId = getCurrentUserId(auth);
|
||||
boolean exists = channel.getConversation().getParticipants().stream().anyMatch(p -> p.getUser().getId().equals(userId));
|
||||
if (!exists) {
|
||||
MessageParticipant participant = new MessageParticipant();
|
||||
participant.setConversation(channel.getConversation());
|
||||
participant.setUser(userRepository.findById(userId).orElseThrow(() -> new IllegalArgumentException("User not found")));
|
||||
participantRepository.save(participant);
|
||||
}
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
|
||||
private ChannelDto toDto(Channel channel, Long userId) {
|
||||
ChannelDto dto = new ChannelDto();
|
||||
dto.setId(channel.getId());
|
||||
dto.setName(channel.getName());
|
||||
dto.setDescription(channel.getDescription());
|
||||
dto.setAvatar(channel.getAvatar());
|
||||
if (channel.getConversation() != null) {
|
||||
MessageConversation conversation = channel.getConversation();
|
||||
dto.setConversationId(conversation.getId());
|
||||
dto.setMemberCount(conversation.getParticipants().size());
|
||||
if (userId != null) {
|
||||
MessageParticipant self = conversation.getParticipants().stream()
|
||||
.filter(p -> p.getUser().getId().equals(userId))
|
||||
.findFirst().orElse(null);
|
||||
if (self != null) {
|
||||
var lastRead = self.getLastReadAt();
|
||||
dto.setUnreadCount(messageRepository
|
||||
.countByConversationIdAndCreatedAtAfterAndSenderIdNot(conversation.getId(),
|
||||
lastRead == null ? java.time.LocalDateTime.of(1970,1,1,0,0) : lastRead,
|
||||
userId));
|
||||
}
|
||||
}
|
||||
}
|
||||
return dto;
|
||||
}
|
||||
}
|
||||
@@ -59,6 +59,14 @@ public class MessageController {
|
||||
return ResponseEntity.ok(toDto(message));
|
||||
}
|
||||
|
||||
@PostMapping("/conversations/{conversationId}/messages")
|
||||
public ResponseEntity<MessageDto> sendMessageToConversation(@PathVariable Long conversationId,
|
||||
@RequestBody ContentRequest req,
|
||||
Authentication auth) {
|
||||
Message message = messageService.sendMessageToConversation(getCurrentUserId(auth), conversationId, req.getContent());
|
||||
return ResponseEntity.ok(toDto(message));
|
||||
}
|
||||
|
||||
@PostMapping("/conversations/{conversationId}/read")
|
||||
public ResponseEntity<Void> markAsRead(@PathVariable Long conversationId, Authentication auth) {
|
||||
messageService.markConversationAsRead(conversationId, getCurrentUserId(auth));
|
||||
@@ -114,4 +122,16 @@ public class MessageController {
|
||||
this.content = content;
|
||||
}
|
||||
}
|
||||
|
||||
static class ContentRequest {
|
||||
private String content;
|
||||
|
||||
public String getContent() {
|
||||
return content;
|
||||
}
|
||||
|
||||
public void setContent(String content) {
|
||||
this.content = content;
|
||||
}
|
||||
}
|
||||
}
|
||||
16
backend/src/main/java/com/openisle/dto/ChannelDto.java
Normal file
16
backend/src/main/java/com/openisle/dto/ChannelDto.java
Normal file
@@ -0,0 +1,16 @@
|
||||
package com.openisle.dto;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public class ChannelDto {
|
||||
private Long id;
|
||||
private String name;
|
||||
private String description;
|
||||
private String avatar;
|
||||
private Long conversationId;
|
||||
private int memberCount;
|
||||
private long unreadCount;
|
||||
}
|
||||
@@ -10,4 +10,5 @@ public class ConversationDetailDto {
|
||||
private Long id;
|
||||
private List<UserSummaryDto> participants;
|
||||
private Page<MessageDto> messages;
|
||||
private ChannelDto channel;
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package com.openisle.dto;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
@@ -14,4 +15,5 @@ public class ConversationDto {
|
||||
private List<UserSummaryDto> participants;
|
||||
private LocalDateTime createdAt;
|
||||
private long unreadCount;
|
||||
private ChannelDto channel;
|
||||
}
|
||||
27
backend/src/main/java/com/openisle/model/Channel.java
Normal file
27
backend/src/main/java/com/openisle/model/Channel.java
Normal file
@@ -0,0 +1,27 @@
|
||||
package com.openisle.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Entity
|
||||
@Table(name = "channels")
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
public class Channel {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
|
||||
private String description;
|
||||
|
||||
private String avatar;
|
||||
|
||||
@OneToOne
|
||||
@JoinColumn(name = "conversation_id")
|
||||
private MessageConversation conversation;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.openisle.repository;
|
||||
|
||||
import com.openisle.model.Channel;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface ChannelRepository extends JpaRepository<Channel, Long> {
|
||||
Optional<Channel> findByConversationId(Long conversationId);
|
||||
}
|
||||
@@ -8,6 +8,9 @@ import com.openisle.repository.MessageConversationRepository;
|
||||
import com.openisle.repository.MessageParticipantRepository;
|
||||
import com.openisle.repository.MessageRepository;
|
||||
import com.openisle.repository.UserRepository;
|
||||
import com.openisle.repository.ChannelRepository;
|
||||
import com.openisle.model.Channel;
|
||||
import com.openisle.dto.ChannelDto;
|
||||
import com.openisle.dto.ConversationDetailDto;
|
||||
import com.openisle.dto.ConversationDto;
|
||||
import com.openisle.dto.MessageDto;
|
||||
@@ -33,6 +36,7 @@ public class MessageService {
|
||||
private final MessageConversationRepository conversationRepository;
|
||||
private final MessageParticipantRepository participantRepository;
|
||||
private final UserRepository userRepository;
|
||||
private final ChannelRepository channelRepository;
|
||||
private final SimpMessagingTemplate messagingTemplate;
|
||||
|
||||
@Transactional
|
||||
@@ -82,6 +86,39 @@ public class MessageService {
|
||||
return message;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Message sendMessageToConversation(Long senderId, Long conversationId, String content) {
|
||||
User sender = userRepository.findById(senderId)
|
||||
.orElseThrow(() -> new IllegalArgumentException("Sender not found"));
|
||||
MessageConversation conversation = conversationRepository.findById(conversationId)
|
||||
.orElseThrow(() -> new IllegalArgumentException("Conversation not found"));
|
||||
|
||||
Message message = new Message();
|
||||
message.setConversation(conversation);
|
||||
message.setSender(sender);
|
||||
message.setContent(content);
|
||||
message = messageRepository.save(message);
|
||||
|
||||
conversation.setLastMessage(message);
|
||||
conversationRepository.save(conversation);
|
||||
|
||||
MessageDto messageDto = toDto(message);
|
||||
String conversationDestination = "/topic/conversation/" + conversation.getId();
|
||||
messagingTemplate.convertAndSend(conversationDestination, messageDto);
|
||||
|
||||
conversation.getParticipants().forEach(p -> {
|
||||
if (!p.getUser().getId().equals(senderId)) {
|
||||
String userDestination = "/topic/user/" + p.getUser().getId() + "/messages";
|
||||
messagingTemplate.convertAndSend(userDestination, messageDto);
|
||||
long unreadCount = getUnreadMessageCount(p.getUser().getId());
|
||||
String recipientUsername = p.getUser().getUsername();
|
||||
messagingTemplate.convertAndSendToUser(recipientUsername, "/queue/unread-count", unreadCount);
|
||||
}
|
||||
});
|
||||
|
||||
return message;
|
||||
}
|
||||
|
||||
private MessageDto toDto(Message message) {
|
||||
MessageDto dto = new MessageDto();
|
||||
dto.setId(message.getId());
|
||||
@@ -98,6 +135,19 @@ public class MessageService {
|
||||
return dto;
|
||||
}
|
||||
|
||||
private ChannelDto toDto(Channel channel) {
|
||||
ChannelDto dto = new ChannelDto();
|
||||
dto.setId(channel.getId());
|
||||
dto.setName(channel.getName());
|
||||
dto.setDescription(channel.getDescription());
|
||||
dto.setAvatar(channel.getAvatar());
|
||||
if (channel.getConversation() != null) {
|
||||
dto.setConversationId(channel.getConversation().getId());
|
||||
dto.setMemberCount(channel.getConversation().getParticipants().size());
|
||||
}
|
||||
return dto;
|
||||
}
|
||||
|
||||
public MessageConversation findOrCreateConversation(Long user1Id, Long user2Id) {
|
||||
User user1 = userRepository.findById(user1Id)
|
||||
.orElseThrow(() -> new IllegalArgumentException("User1 not found"));
|
||||
@@ -154,6 +204,9 @@ public class MessageService {
|
||||
})
|
||||
.collect(Collectors.toList()));
|
||||
|
||||
channelRepository.findByConversationId(conversation.getId())
|
||||
.ifPresent(channel -> dto.setChannel(toDto(channel)));
|
||||
|
||||
MessageParticipant self = conversation.getParticipants().stream()
|
||||
.filter(p -> p.getUser().getId().equals(userId))
|
||||
.findFirst()
|
||||
@@ -191,6 +244,8 @@ public class MessageService {
|
||||
detailDto.setId(conversation.getId());
|
||||
detailDto.setParticipants(participants);
|
||||
detailDto.setMessages(messageDtoPage);
|
||||
channelRepository.findByConversationId(conversation.getId())
|
||||
.ifPresent(channel -> detailDto.setChannel(toDto(channel)));
|
||||
|
||||
return detailDto;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user