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:
parent
3100ac21c3
commit
0d6a4ee8dd
|
|
@ -1,10 +1,13 @@
|
|||
package com.alttd.altitudeweb.controllers.event;
|
||||
|
||||
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.services.chat.ChatService;
|
||||
import com.alttd.altitudeweb.services.chat.event_publisher.EventPublisher;
|
||||
import com.alttd.altitudeweb.services.chat.event_publisher.EventUser;
|
||||
import com.alttd.altitudeweb.setup.Connection;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
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.Instant;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
|
|
@ -62,7 +67,29 @@ public class EventController {
|
|||
.map(ChatMessageMapper::toJson)
|
||||
.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) {
|
||||
|
|
|
|||
|
|
@ -113,10 +113,12 @@ public class ChatService {
|
|||
return switch (chatMessage.getType()) {
|
||||
case PUBLIC, GLOBAL -> true;
|
||||
case PARTY -> {
|
||||
// TODO [Stijn] [2026-07-19]: Check if user is in party
|
||||
// if (isInParty(eventUser, chatMessage.getChannel())) return true;
|
||||
Integer partyId = eventUser.partyId();
|
||||
if (partyId == null) {
|
||||
yield false;
|
||||
}
|
||||
yield chatMessage.getChannel().equals(String.valueOf(partyId));
|
||||
}
|
||||
case MSG -> {
|
||||
if (eventUser.uuid().equals(chatMessage.getUuid())) {
|
||||
yield true;
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ import java.util.List;
|
|||
import java.util.Objects;
|
||||
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) {
|
||||
return authorities.contains(permission.getValue());
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import org.apache.ibatis.annotations.Param;
|
|||
import org.apache.ibatis.annotations.Select;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface ChatLogMapper {
|
||||
|
||||
|
|
@ -15,4 +16,10 @@ public interface ChatLogMapper {
|
|||
""")
|
||||
List<ChatLogDao> getChatLogs(@Param("since") long since);
|
||||
|
||||
@Select("""
|
||||
SELECT party_id FROM chat_users
|
||||
WHERE uuid = #{uuid}
|
||||
""")
|
||||
Optional<Integer> getPartyId(@Param("uuid") String uuid);
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user