Skip to content

Commit 44b1ac2

Browse files
Handle multiple rooms in calendar event notes (#211)
- Updated `reRoom` regex to be non-greedy to correctly identify the first room in a list for the Location field. - Updated `cleanEvent` to find all NavigaTUM room IDs in the location string and generate navigation links for each of them in the event description. - Added `TestMultipleRooms` to verify the fix. Fixes #158 Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent 08aa6ba commit 44b1ac2

File tree

2 files changed

+60
-4
lines changed

2 files changed

+60
-4
lines changed

internal/app.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ var unneeded = []string{
337337
"(Online)",
338338
}
339339

340-
var reRoom = regexp.MustCompile("^(.*?),.*(\\d{4})\\.(?:\\d\\d|EG|UG|DG|Z\\d|U\\d)\\.\\d+")
340+
var reRoom = regexp.MustCompile("^(.*?),.*?(\\d{4})\\.(?:\\d\\d|EG|UG|DG|Z\\d|U\\d)\\.\\d+")
341341

342342
// matches strings like: (5612.03.017), (5612.EG.017), (5612.EG.010B)
343343
var reNavigaTUM = regexp.MustCompile("\\(\\d{4}\\.[a-zA-Z0-9]{2}\\.\\d{3}[A-Z]?\\)")
@@ -381,9 +381,11 @@ func (a *App) cleanEvent(event *ics.VEvent) {
381381
description = location + "\n" + description
382382
event.SetLocation(building)
383383
}
384-
if roomID := reNavigaTUM.FindString(location); roomID != "" {
385-
roomID = strings.Trim(roomID, "()")
386-
description = fmt.Sprintf("https://nav.tum.de/room/%s\n%s", roomID, description)
384+
if roomIDs := reNavigaTUM.FindAllString(location, -1); len(roomIDs) > 0 {
385+
for _, roomID := range roomIDs {
386+
roomID = strings.Trim(roomID, "()")
387+
description = fmt.Sprintf("https://nav.tum.de/room/%s\n%s", roomID, description)
388+
}
387389
}
388390
}
389391
event.SetDescription(description)

internal/app_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,60 @@ func TestDeduplication(t *testing.T) {
7171
}
7272
}
7373

74+
func TestMultipleRooms(t *testing.T) {
75+
// Setup app with building replacements
76+
app, err := newApp()
77+
if err != nil {
78+
t.Fatal(err)
79+
}
80+
81+
// Create a dummy event with multiple rooms
82+
event := ics.NewEvent("test-uid")
83+
84+
// Using 5508 and 5612 which are present in buildings.json
85+
// 5508 -> Boltzmannstr. 15, 85748 Garching b. München
86+
// 5612 -> Boltzmannstr. 3, 85748 Garching b. München
87+
88+
// Construct a location string with multiple rooms.
89+
// RoomA, DescA (5508.01.001), RoomB, DescB (5612.01.001)
90+
91+
location := "RoomA, DescA (5508.01.001), RoomB, DescB (5612.01.001)"
92+
event.SetProperty(ics.ComponentPropertyLocation, location)
93+
event.SetProperty(ics.ComponentPropertySummary, "Test Event")
94+
event.SetProperty(ics.ComponentPropertyDescription, "Original Description")
95+
event.SetProperty(ics.ComponentPropertyStatus, "CONFIRMED")
96+
97+
app.cleanEvent(event)
98+
99+
desc := event.GetProperty(ics.ComponentPropertyDescription).Value
100+
loc := event.GetProperty(ics.ComponentPropertyLocation).Value
101+
102+
// Check if both rooms are present in description or nav links
103+
if !strings.Contains(desc, "5508.01.001") {
104+
t.Errorf("Description should contain first room ID")
105+
}
106+
if !strings.Contains(desc, "5612.01.001") {
107+
t.Errorf("Description should contain second room ID")
108+
}
109+
110+
// Check if nav links are generated for both
111+
// 5508.01.001 -> https://nav.tum.de/room/5508.01.001
112+
// 5612.01.001 -> https://nav.tum.de/room/5612.01.001
113+
114+
if !strings.Contains(desc, "https://nav.tum.de/room/5508.01.001") {
115+
t.Error("Missing nav link for first room")
116+
}
117+
if !strings.Contains(desc, "https://nav.tum.de/room/5612.01.001") {
118+
t.Error("Missing nav link for second room")
119+
}
120+
121+
// With non-greedy regex, the location should be the first building (5508)
122+
expectedLoc := "Boltzmannstr. 15, 85748 Garching b. München"
123+
if loc != expectedLoc {
124+
t.Errorf("Location should be %s but is %s", expectedLoc, loc)
125+
}
126+
}
127+
74128
func TestNameShortening(t *testing.T) {
75129
testData, app := getTestData(t, "nameshortening.ics")
76130
calendar, err := app.getCleanedCalendar([]byte(testData), map[string]bool{})

0 commit comments

Comments
 (0)