Skip to content

Commit d130b99

Browse files
committed
maximum 100 characters for user
1 parent d8740ab commit d130b99

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

src/main/java/sh/mob/timer/web/Room.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ final class Room {
2222
/** The only definition of what constitutes a valid room name. */
2323
public static final String NAME_REGEX = "[A-Za-z0-9-_]{1,64}";
2424

25+
/** User names are stored per timer request and broadcast to every SSE client. */
26+
public static final int MAX_USER_LENGTH = 100;
27+
2528
public static final TimerRequest NULL_TIMER_REQUEST =
2629
new TimerRequest(0L, null, null, null, null);
2730
private final String name;
@@ -46,6 +49,7 @@ final class Room {
4649
}
4750

4851
public void addTimer(Long timer, String user, Instant requested) {
52+
user = truncateTooLongUser(user);
4953
var nextUser = findNextUser(user);
5054
var timerRequest = new TimerRequest(timer, requested, user, nextUser, TimerType.TIMER);
5155
timerRequests.add(timerRequest);
@@ -55,13 +59,14 @@ public void addTimer(Long timer, String user, Instant requested) {
5559
}
5660

5761
public void setGoal(String text, String user, Instant requested) {
58-
var newGoal = new Goal(text, user, requested);
62+
var newGoal = new Goal(text, truncateTooLongUser(user), requested);
5963
currentGoal = newGoal ;
6064
lastActivity = requested;
6165
goalRequestSink.tryEmitNext(newGoal);
6266
}
6367

6468
public void deleteGoal(String user, Instant requested) {
69+
user = truncateTooLongUser(user);
6570
lastActivity = requested;
6671
if(currentGoal.goal() != null){
6772
currentGoal = Goal.deleted(user, requested);
@@ -75,6 +80,13 @@ public void deleteGoal(String user, Instant requested) {
7580
}
7681
}
7782

83+
private static String truncateTooLongUser(String user) {
84+
if (user == null || user.length() <= MAX_USER_LENGTH) {
85+
return user;
86+
}
87+
return user.substring(0, MAX_USER_LENGTH);
88+
}
89+
7890
private String findNextUser(String user) {
7991
if (timerRequests.isEmpty()) {
8092
return null;
@@ -101,6 +113,7 @@ private String findNextUser(String user) {
101113
}
102114

103115
public void addBreaktimer(Long breaktimer, String user) {
116+
user = truncateTooLongUser(user);
104117
TimerRequest timerRequest =
105118
new TimerRequest(
106119
breaktimer,

0 commit comments

Comments
 (0)