implements Grupp chat room
parent
e14f94dfff
commit
9482632faf
|
@ -33,6 +33,13 @@ public class ChatRoom {
|
|||
}
|
||||
|
||||
|
||||
|
||||
public User getUser1() {
|
||||
return user1;
|
||||
}
|
||||
public User getUser2() {
|
||||
return user2;
|
||||
}
|
||||
public List<Message> getMessages() {
|
||||
return messages;
|
||||
}
|
||||
|
|
|
@ -3,35 +3,161 @@ package domain;
|
|||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public class GruppenRoom {
|
||||
private User creater;
|
||||
private List<User> roots;
|
||||
private static int nextGroupId = 1000;
|
||||
private final int groupId;
|
||||
private final User creator;
|
||||
private List<User> admins;
|
||||
private List<User> participants;
|
||||
private String description;
|
||||
private String name;
|
||||
private List<Message> messages;
|
||||
private final LocalDateTime createdAt;
|
||||
|
||||
public GruppenRoom(User creater, String description, String name) {
|
||||
super();
|
||||
this.creater = creater;
|
||||
this.description = description;
|
||||
public GruppenRoom(User creator, String name, String description) {
|
||||
Objects.requireNonNull(creator, "Creator darf nicht null sein");
|
||||
Objects.requireNonNull(name, "Name darf nicht null sein");
|
||||
|
||||
this.groupId = nextGroupId++;
|
||||
this.creator = creator;
|
||||
this.name = name;
|
||||
this.description = description != null ? description : "";
|
||||
this.createdAt = LocalDateTime.now();
|
||||
|
||||
this.roots = new ArrayList<>();
|
||||
this.admins = new ArrayList<>();
|
||||
this.participants = new ArrayList<>();
|
||||
this.messages = new ArrayList<>();
|
||||
|
||||
this.roots.add(creater);
|
||||
this.participants.add(creater);
|
||||
this.admins.add(creator);
|
||||
this.participants.add(creator);
|
||||
|
||||
// Gruppe beim Creator registrieren
|
||||
creator.getUserGruppenRoom().addGruppenRoom(this);
|
||||
}
|
||||
|
||||
public boolean addParticipant(User user) {
|
||||
Objects.requireNonNull(user, "User darf nicht null sein");
|
||||
|
||||
if (participants.contains(user)) {
|
||||
throw new IllegalArgumentException("User ist bereits Teil der Gruppe: " + user.getUsername());
|
||||
}
|
||||
|
||||
// Gruppe beim User registrieren
|
||||
boolean added = user.getUserGruppenRoom().addGruppenRoom(this);
|
||||
if (!added) {
|
||||
throw new IllegalStateException("Gruppe konnte nicht beim User registriert werden");
|
||||
}
|
||||
|
||||
public boolean addparticipants(User user) {
|
||||
return participants.add(user);
|
||||
}
|
||||
|
||||
public boolean removeParticipant(User remover, User userToRemove) {
|
||||
Objects.requireNonNull(remover, "Remover darf nicht null sein");
|
||||
Objects.requireNonNull(userToRemove, "UserToRemove darf nicht null sein");
|
||||
|
||||
if (!admins.contains(remover) && !remover.equals(userToRemove)) {
|
||||
throw new IllegalArgumentException("Nur Admins können andere User entfernen");
|
||||
}
|
||||
|
||||
if (userToRemove.equals(creator)) {
|
||||
throw new IllegalArgumentException("Der Creator kann nicht entfernt werden");
|
||||
}
|
||||
|
||||
// Gruppe beim User deregistrieren
|
||||
userToRemove.getUserGruppenRoom().removeGruppenRoom(this);
|
||||
|
||||
// Admin-Rechte entfernen falls nötig
|
||||
if (admins.contains(userToRemove)) {
|
||||
admins.remove(userToRemove);
|
||||
}
|
||||
|
||||
return participants.remove(userToRemove);
|
||||
}
|
||||
|
||||
public boolean addAdmin(User promoter, User userToPromote) {
|
||||
Objects.requireNonNull(promoter, "Promoter darf nicht null sein");
|
||||
Objects.requireNonNull(userToPromote, "UserToPromote darf nicht null sein");
|
||||
|
||||
if (!admins.contains(promoter)) {
|
||||
throw new IllegalArgumentException("Nur Admins können andere zu Admins befördern");
|
||||
}
|
||||
|
||||
if (!participants.contains(userToPromote)) {
|
||||
throw new IllegalArgumentException("User ist nicht Teil der Gruppe");
|
||||
}
|
||||
|
||||
if (admins.contains(userToPromote)) {
|
||||
throw new IllegalArgumentException("User ist bereits Admin");
|
||||
}
|
||||
|
||||
return admins.add(userToPromote);
|
||||
}
|
||||
|
||||
public boolean removeAdmin(User demoter, User userToDemote) {
|
||||
Objects.requireNonNull(demoter, "Demoter darf nicht null sein");
|
||||
Objects.requireNonNull(userToDemote, "UserToDemote darf nicht null sein");
|
||||
|
||||
if (!admins.contains(demoter)) {
|
||||
throw new IllegalArgumentException("Nur Admins können Admin-Rechte entziehen");
|
||||
}
|
||||
|
||||
if (userToDemote.equals(creator)) {
|
||||
throw new IllegalArgumentException("Der Creator kann nicht seiner Admin-Rechte entzogen werden");
|
||||
}
|
||||
|
||||
if (!admins.contains(userToDemote)) {
|
||||
throw new IllegalArgumentException("User ist kein Admin");
|
||||
}
|
||||
|
||||
return admins.remove(userToDemote);
|
||||
}
|
||||
|
||||
public void addMessage(User sender, String content) {
|
||||
Objects.requireNonNull(sender, "Sender darf nicht null sein");
|
||||
Objects.requireNonNull(content, "Content darf nicht null sein");
|
||||
|
||||
if (!participants.contains(sender)) {
|
||||
throw new IllegalArgumentException("Nur Gruppenmitglieder können Nachrichten senden");
|
||||
}
|
||||
|
||||
Message message = new Message(sender, content);
|
||||
messages.add(message);
|
||||
}
|
||||
|
||||
public List<String> showMessages() {
|
||||
if (messages.isEmpty()) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
return messages.stream()
|
||||
.map(Message::toString)
|
||||
.toList();
|
||||
}
|
||||
|
||||
public boolean isAdmin(User user) {
|
||||
return admins.contains(user);
|
||||
}
|
||||
|
||||
public boolean isParticipant(User user) {
|
||||
return participants.contains(user);
|
||||
}
|
||||
|
||||
// Getter-Methoden
|
||||
public int getGroupId() { return groupId; }
|
||||
public User getCreator() { return creator; }
|
||||
public String getName() { return name; }
|
||||
public String getDescription() { return description; }
|
||||
public LocalDateTime getCreatedAt() { return createdAt; }
|
||||
public List<User> getAdmins() { return new ArrayList<>(admins); }
|
||||
public List<User> getParticipants() { return new ArrayList<>(participants); }
|
||||
public List<Message> getMessages() { return new ArrayList<>(messages); }
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("GruppenRoom{name='%s', id=%d, participants=%d, messages=%d}",
|
||||
name, groupId, participants.size(), messages.size());
|
||||
}
|
||||
|
||||
}
|
|
@ -2,37 +2,68 @@ package domain;
|
|||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public class UserChatRoom {
|
||||
|
||||
private List<ChatRoom> chatsRooms;
|
||||
private List<ChatRoom> chatRooms; // Bessere Namensgebung: chatRooms statt chatsRooms
|
||||
|
||||
public UserChatRoom() {
|
||||
this.chatsRooms = new ArrayList<>();
|
||||
this.chatRooms = new ArrayList<>();
|
||||
}
|
||||
|
||||
public boolean addChat(ChatRoom chatRoom) {
|
||||
if (chatRoom == null)
|
||||
throw new IllegalArgumentException("ChatRoom soll nicht null sein");
|
||||
Objects.requireNonNull(chatRoom, "ChatRoom darf nicht null sein");
|
||||
|
||||
if (chatsRooms.contains(chatRoom))
|
||||
throw new IllegalArgumentException("chatRoom ist vorhanden");
|
||||
if (chatRooms.contains(chatRoom)) {
|
||||
throw new IllegalArgumentException("ChatRoom bereits vorhanden: " + chatRoom.getRoomId());
|
||||
}
|
||||
|
||||
return chatsRooms.add(chatRoom);
|
||||
return chatRooms.add(chatRoom);
|
||||
}
|
||||
|
||||
public boolean removeChat(ChatRoom chatRoom) {
|
||||
if (chatRoom == null)
|
||||
throw new IllegalArgumentException("ChatRoom soll nicht null sein");
|
||||
Objects.requireNonNull(chatRoom, "ChatRoom darf nicht null sein");
|
||||
|
||||
if (!chatsRooms.contains(chatRoom))
|
||||
throw new IllegalArgumentException("chatRoom nicht gefunden");
|
||||
|
||||
return chatsRooms.remove(chatRoom);
|
||||
if (!chatRooms.contains(chatRoom)) {
|
||||
throw new IllegalArgumentException("ChatRoom nicht gefunden: " + chatRoom.getRoomId());
|
||||
}
|
||||
|
||||
public List<ChatRoom> getAllChatsRooms() {
|
||||
return new ArrayList<>(chatsRooms);
|
||||
return chatRooms.remove(chatRoom);
|
||||
}
|
||||
|
||||
public List<ChatRoom> getAllChatRooms() {
|
||||
return new ArrayList<>(chatRooms); // Rückgabe einer Kopie für Encapsulation
|
||||
}
|
||||
|
||||
public List<Integer> getChatRoomIds() {
|
||||
List<Integer> ids = new ArrayList<>();
|
||||
for (ChatRoom room : chatRooms) {
|
||||
ids.add(room.getRoomId());
|
||||
}
|
||||
return ids;
|
||||
}
|
||||
|
||||
public boolean contains(ChatRoom chatRoom) {
|
||||
return chatRooms.contains(chatRoom);
|
||||
}
|
||||
|
||||
public int getChatRoomCount() {
|
||||
return chatRooms.size();
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return chatRooms.isEmpty();
|
||||
}
|
||||
|
||||
public void clearChatRooms() {
|
||||
chatRooms.clear();
|
||||
}
|
||||
|
||||
public List<String> getChatRoomInfos() {
|
||||
List<String> infos = new ArrayList<>();
|
||||
for (ChatRoom room : chatRooms) {
|
||||
infos.add(room.toString());
|
||||
}
|
||||
return infos;
|
||||
}
|
||||
}
|
|
@ -10,8 +10,38 @@ public class UserGruppenRoom {
|
|||
this.gruppenRooms = new ArrayList<>();
|
||||
}
|
||||
|
||||
public boolean addGroupproom(GruppenRoom gruppenRoom) {
|
||||
public boolean addGruppenRoom(GruppenRoom gruppenRoom) {
|
||||
if (gruppenRooms.contains(gruppenRoom))
|
||||
return false;
|
||||
|
||||
return gruppenRooms.add(gruppenRoom);
|
||||
}
|
||||
|
||||
public boolean removeGruppenRoom(GruppenRoom gruppenRoom) {
|
||||
return gruppenRooms.remove(gruppenRoom);
|
||||
}
|
||||
|
||||
public List<GruppenRoom> getGruppenRooms() {
|
||||
return new ArrayList<>(gruppenRooms); // Rückgabe einer Kopie
|
||||
}
|
||||
|
||||
public boolean contains(GruppenRoom gruppenRoom) {
|
||||
return gruppenRooms.contains(gruppenRoom);
|
||||
}
|
||||
|
||||
public int getSize() {
|
||||
return gruppenRooms.size();
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return gruppenRooms.isEmpty();
|
||||
}
|
||||
|
||||
public List<String> getGruppenRoomNames() {
|
||||
List<String> names = new ArrayList<>();
|
||||
for (GruppenRoom room : gruppenRooms) {
|
||||
names.add(room.getName());
|
||||
}
|
||||
return names;
|
||||
}
|
||||
}
|
|
@ -7,11 +7,15 @@ import domain.*;
|
|||
|
||||
public class ChatService {
|
||||
private Map<Integer, ChatRoom> chatrooms;
|
||||
private Map<Integer, GruppenRoom> gruppenRooms;
|
||||
private Map<String, User> users;
|
||||
private Map<Integer, User> usersById;
|
||||
|
||||
public ChatService() {
|
||||
chatrooms = new HashMap<>();
|
||||
gruppenRooms = new HashMap<>();
|
||||
users = new HashMap<>();
|
||||
usersById = new HashMap<>();
|
||||
}
|
||||
|
||||
public boolean createUser(String userName) {
|
||||
|
@ -20,14 +24,17 @@ public class ChatService {
|
|||
|
||||
User tempUser = new User(userName);
|
||||
users.put(userName, tempUser);
|
||||
usersById.put(tempUser.getUserId(), tempUser);
|
||||
return true;
|
||||
}
|
||||
|
||||
private User getUser(String userName) {
|
||||
public User getUser(String userName) {
|
||||
return users.get(userName);
|
||||
}
|
||||
|
||||
|
||||
public User getUser(int userId) {
|
||||
return usersById.get(userId);
|
||||
}
|
||||
|
||||
public int createChatRoom(String user1Name, String user2Name) {
|
||||
User user1 = getUser(user1Name);
|
||||
|
@ -36,6 +43,14 @@ public class ChatService {
|
|||
if (user1 == null || user2 == null)
|
||||
throw new IllegalArgumentException("User existiert nicht");
|
||||
|
||||
// Prüfen ob Chat bereits existiert
|
||||
for (ChatRoom room : chatrooms.values()) {
|
||||
if ((room.getUser1().equals(user1) && room.getUser2().equals(user2)) ||
|
||||
(room.getUser1().equals(user2) && room.getUser2().equals(user1))) {
|
||||
return room.getRoomId(); // Existierenden Chat zurückgeben
|
||||
}
|
||||
}
|
||||
|
||||
ChatRoom tempChatRoom = new ChatRoom(user1, user2);
|
||||
chatrooms.put(tempChatRoom.getRoomId(), tempChatRoom);
|
||||
user1.getUserChatRoom().addChat(tempChatRoom);
|
||||
|
@ -44,8 +59,6 @@ public class ChatService {
|
|||
return tempChatRoom.getRoomId();
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void sendMessage(int roomId, String senderName, String content) {
|
||||
ChatRoom room = chatrooms.get(roomId);
|
||||
if (room == null)
|
||||
|
@ -56,7 +69,10 @@ public class ChatService {
|
|||
throw new IllegalArgumentException("Sender existiert nicht: " + senderName);
|
||||
}
|
||||
|
||||
|
||||
// Prüfen ob Sender im Chatroom ist
|
||||
if (!room.getUser1().equals(sender) && !room.getUser2().equals(sender)) {
|
||||
throw new IllegalArgumentException("Sender ist nicht Teil des Chatrooms");
|
||||
}
|
||||
|
||||
Message message = new Message(sender, content);
|
||||
room.addMessage(message);
|
||||
|
@ -80,6 +96,11 @@ public class ChatService {
|
|||
User contactUser = users.get(contactUserName);
|
||||
User currentUser = users.get(currentUserName);
|
||||
|
||||
// Verhindere Selbsthinzufügung
|
||||
if (contactUser.equals(currentUser)) {
|
||||
throw new IllegalArgumentException("Kann sich nicht selbst als Kontakt hinzufügen");
|
||||
}
|
||||
|
||||
return currentUser.getUserContacts().addContact(contactUser);
|
||||
}
|
||||
|
||||
|
@ -91,7 +112,13 @@ public class ChatService {
|
|||
return user.getUserContacts().showAllContacts();
|
||||
}
|
||||
|
||||
public List<Integer> getUserChatRoomIds(String userName) {
|
||||
if (!users.containsKey(userName))
|
||||
throw new IllegalArgumentException("User existiert nicht: " + userName);
|
||||
|
||||
User user = users.get(userName);
|
||||
return user.getUserChatRoom().getChatRoomIds();
|
||||
}
|
||||
|
||||
public String getChatRoomInfo(int roomId) {
|
||||
ChatRoom room = chatrooms.get(roomId);
|
||||
|
@ -100,4 +127,69 @@ public class ChatService {
|
|||
|
||||
return room.toString();
|
||||
}
|
||||
|
||||
// Gruppen-Funktionen
|
||||
public int createGruppenRoom(String creatorName, String groupName, String description) {
|
||||
User creator = getUser(creatorName);
|
||||
if (creator == null) {
|
||||
throw new IllegalArgumentException("Creator existiert nicht: " + creatorName);
|
||||
}
|
||||
|
||||
GruppenRoom gruppenRoom = new GruppenRoom(creator, groupName, description);
|
||||
gruppenRooms.put(gruppenRoom.getGroupId(), gruppenRoom);
|
||||
return gruppenRoom.getGroupId();
|
||||
}
|
||||
|
||||
public boolean addParticipantToGroup(int groupId, String adderName, String userToAddName) {
|
||||
GruppenRoom group = gruppenRooms.get(groupId);
|
||||
User adder = getUser(adderName);
|
||||
User userToAdd = getUser(userToAddName);
|
||||
|
||||
if (group == null) throw new IllegalArgumentException("Gruppe existiert nicht: " + groupId);
|
||||
if (adder == null) throw new IllegalArgumentException("Adder existiert nicht: " + adderName);
|
||||
if (userToAdd == null) throw new IllegalArgumentException("User existiert nicht: " + userToAddName);
|
||||
|
||||
if (!group.isAdmin(adder)) {
|
||||
throw new IllegalArgumentException("Nur Admins können Teilnehmer hinzufügen");
|
||||
}
|
||||
|
||||
return group.addParticipant(userToAdd);
|
||||
}
|
||||
|
||||
public void sendGroupMessage(int groupId, String senderName, String content) {
|
||||
GruppenRoom group = gruppenRooms.get(groupId);
|
||||
User sender = getUser(senderName);
|
||||
|
||||
if (group == null) throw new IllegalArgumentException("Gruppe existiert nicht: " + groupId);
|
||||
if (sender == null) throw new IllegalArgumentException("Sender existiert nicht: " + senderName);
|
||||
|
||||
group.addMessage(sender, content);
|
||||
}
|
||||
|
||||
public List<String> getGroupMessages(int groupId) {
|
||||
GruppenRoom group = gruppenRooms.get(groupId);
|
||||
if (group == null) {
|
||||
throw new IllegalArgumentException("Gruppe existiert nicht: " + groupId);
|
||||
}
|
||||
|
||||
return group.showMessages();
|
||||
}
|
||||
|
||||
public List<String> getUserGruppenRooms(String userName) {
|
||||
User user = getUser(userName);
|
||||
if (user == null) {
|
||||
throw new IllegalArgumentException("User existiert nicht: " + userName);
|
||||
}
|
||||
|
||||
return user.getUserGruppenRoom().getGruppenRoomNames();
|
||||
}
|
||||
|
||||
public String getGruppenRoomInfo(int groupId) {
|
||||
GruppenRoom room = gruppenRooms.get(groupId);
|
||||
if (room == null) {
|
||||
throw new IllegalArgumentException("Gruppenraum existiert nicht: " + groupId);
|
||||
}
|
||||
|
||||
return room.toString();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue