Link party IDs to users in chat: added getPartyId query, updated EventUser with partyId field, and adjusted message filtering logic in ChatService.

This commit is contained in:
akastijn 2026-08-01 18:47:06 +02:00
parent 3100ac21c3
commit 0d6a4ee8dd
4 changed files with 41 additions and 5 deletions

View File

@ -1,10 +1,13 @@
package com.alttd.altitudeweb.controllers.event; package com.alttd.altitudeweb.controllers.event;
import com.alttd.altitudeweb.controllers.chat.ChatMessageMapper; import com.alttd.altitudeweb.controllers.chat.ChatMessageMapper;
import com.alttd.altitudeweb.database.Databases;
import com.alttd.altitudeweb.database.chat.ChatLogMapper;
import com.alttd.altitudeweb.model.PermissionClaimDto; import com.alttd.altitudeweb.model.PermissionClaimDto;
import com.alttd.altitudeweb.services.chat.ChatService; import com.alttd.altitudeweb.services.chat.ChatService;
import com.alttd.altitudeweb.services.chat.event_publisher.EventPublisher; import com.alttd.altitudeweb.services.chat.event_publisher.EventPublisher;
import com.alttd.altitudeweb.services.chat.event_publisher.EventUser; import com.alttd.altitudeweb.services.chat.event_publisher.EventUser;
import com.alttd.altitudeweb.setup.Connection;
import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -21,7 +24,9 @@ import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import java.time.Duration; import java.time.Duration;
import java.time.Instant; import java.time.Instant;
import java.util.List; import java.util.List;
import java.util.Optional;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@RequiredArgsConstructor @RequiredArgsConstructor
@ -62,7 +67,29 @@ public class EventController {
.map(ChatMessageMapper::toJson) .map(ChatMessageMapper::toJson)
.collect(Collectors.joining(",", "[", "]")); .collect(Collectors.joining(",", "[", "]"));
return eventPublisher.subscribe(new EventUser(subject, authorities), json); Integer partyId = getPartyId(subject);
return eventPublisher.subscribe(new EventUser(subject, authorities, partyId), json);
}
private Integer getPartyId(UUID subject) {
CompletableFuture<Integer> partyIdFuture = new CompletableFuture<>();
Connection.getConnection(Databases.CHAT)
.runQuery(sqlSession -> {
log.debug("Loading party id for uuid {}", subject);
try {
Integer partyId = sqlSession.getMapper(ChatLogMapper.class)
.getPartyId(subject.toString())
.orElse(null);
partyIdFuture.complete(partyId);
} catch (Exception e) {
log.error("Failed to load party id", e);
partyIdFuture.complete(null);
}
});
return partyIdFuture.join();
} }
private static boolean doesNotHavePermission(List<String> authorities) { private static boolean doesNotHavePermission(List<String> authorities) {

View File

@ -113,9 +113,11 @@ public class ChatService {
return switch (chatMessage.getType()) { return switch (chatMessage.getType()) {
case PUBLIC, GLOBAL -> true; case PUBLIC, GLOBAL -> true;
case PARTY -> { case PARTY -> {
// TODO [Stijn] [2026-07-19]: Check if user is in party Integer partyId = eventUser.partyId();
// if (isInParty(eventUser, chatMessage.getChannel())) return true; if (partyId == null) {
yield false; yield false;
}
yield chatMessage.getChannel().equals(String.valueOf(partyId));
} }
case MSG -> { case MSG -> {
if (eventUser.uuid().equals(chatMessage.getUuid())) { if (eventUser.uuid().equals(chatMessage.getUuid())) {

View File

@ -6,7 +6,7 @@ import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.UUID; import java.util.UUID;
public record EventUser(UUID uuid, List<String> authorities) { public record EventUser(UUID uuid, List<String> authorities, Integer partyId) {
public boolean hasPermission(PermissionClaimDto permission) { public boolean hasPermission(PermissionClaimDto permission) {
return authorities.contains(permission.getValue()); return authorities.contains(permission.getValue());

View File

@ -4,6 +4,7 @@ import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Select;
import java.util.List; import java.util.List;
import java.util.Optional;
public interface ChatLogMapper { public interface ChatLogMapper {
@ -15,4 +16,10 @@ public interface ChatLogMapper {
""") """)
List<ChatLogDao> getChatLogs(@Param("since") long since); List<ChatLogDao> getChatLogs(@Param("since") long since);
@Select("""
SELECT party_id FROM chat_users
WHERE uuid = #{uuid}
""")
Optional<Integer> getPartyId(@Param("uuid") String uuid);
} }