From f971a97378f50736e739889f067a1c6e40b7b4ff Mon Sep 17 00:00:00 2001 From: Elmar Kresse Date: Thu, 17 Oct 2024 10:39:17 +0200 Subject: [PATCH] feat:#49 added multiple room linkout and mapping for map --- .../ical/service/functions/roomMapping.go | 40 ++++++++++ .../service/functions/roomMapping_test.go | 73 +++++++++++++++++++ .../service/ical/icalDescriptionGenerator.go | 30 ++++---- 3 files changed, 128 insertions(+), 15 deletions(-) create mode 100644 services/ical/service/functions/roomMapping.go create mode 100644 services/ical/service/functions/roomMapping_test.go diff --git a/services/ical/service/functions/roomMapping.go b/services/ical/service/functions/roomMapping.go new file mode 100644 index 0000000..8163c9b --- /dev/null +++ b/services/ical/service/functions/roomMapping.go @@ -0,0 +1,40 @@ +package functions + +import ( + "regexp" + "strings" +) + +func MapRoom(room string, output bool) string { + // remove dots from room string + + if output { + //replace second point in TR_A1.23.1 -> TR_A1.23-1 with a minus + re := regexp.MustCompile(`\b[TR_]+[A-ZÄÖÜ]?[0-9]{1,4}[.][0-9]{1,4}[.]\b`) + room = re.ReplaceAllStringFunc(room, func(match string) string { + return match[:len(match)-1] + "-" + match[len(match)-1:] + }) + // If the output flag is set, remove all dots from the room string + room = strings.ReplaceAll(room, ".", "") + } else { + // If the output flag is false add a dot for all rooms with regexp TR_A123 -> TR_A1.23 + re := regexp.MustCompile(`\bTR_+[A-ZÄÖÜ]?[0-9]{1,4}[a-zäöü]?\b`) + room = re.ReplaceAllStringFunc(room, func(match string) string { + return match[:len(match)-2] + "." + match[len(match)-2:] + }) + room = strings.ReplaceAll(room, "-", ".") + } + + // Regular expression pattern to match room identifiers + // The pattern looks for strings that start with two uppercase letters, optionally followed by an underscore, + // followed by 1 to 3 digits, and ending with "-[A-Z]" + re := regexp.MustCompile(`\b[A-ZÄÖÜ]{2}([_]+[A-ZÄÖÜ])?[0-9]{1,4}[a-zäöü]?[-]?[0-9]?-[A-ZÄÖÜ]\b`) + + // Use the ReplaceAllStringFunc to process each match + room = re.ReplaceAllStringFunc(room, func(match string) string { + // Remove the last two characters (i.e., "-") + return match[:len(match)-2] + }) + + return room +} diff --git a/services/ical/service/functions/roomMapping_test.go b/services/ical/service/functions/roomMapping_test.go new file mode 100644 index 0000000..b4e36ea --- /dev/null +++ b/services/ical/service/functions/roomMapping_test.go @@ -0,0 +1,73 @@ +package functions + +import "testing" + +func TestMapRoom(t *testing.T) { + type args struct { + room string + output bool + } + tests := []struct { + name string + args args + want string + }{ + { + name: "Test 1 MapRoom", + args: args{ + room: "H.1.1", + output: true, + }, + want: "H11", + }, + { + name: "Test Treftsbau MapRoom", + args: args{ + room: "TR_L3.03-S,TR_L3.02-S,TR_L2.13-L,TR_L2.05-S,TR_L1.14-H,TR_L1.07-H,TR_L1.06-B,TR_L0.14-S,TR_Innenhof_FF,TR_C1.62-L,TR_B1.50-S,TR_B1.49-S,TR_B1.48-S,TR_B1.46-S,TR_B1.45-S,TR_B0.71-L,TR_B0.70-L,TR_B0.67-L,TR_A_Cafeteria,TR_A2.28-L,TR_A1.40-F,TR_A1.37-S,TR_A1.34-S,TR_A1.29-H,TR_A1.28-S,TR_A1.27-S,TR_A1.26-S,TR_A1.25-S,TR_A1.24-H,TR_A0.34-L,TR_A0.33-L,TR_A0.32.2-L,TR_A0.32.1-L,TR_A0.31.1-L,NI_FoyerK_F,NI_Foyer1_F,NI104-L,NI103-L,NI102-L,NI070-L,GU319-L,GU318-L,GU317b-L,GU317a-L,GU313-L,GU309-L,GU301-L,GU225-L,GU224-L,GU219-L,GU203-L,GU202-L,GU201-L,GU014-L,GU013-L,GU012-L,GU011-L,GU010-L,GU009-L,GU001-L,FÖ306-S,FÖ305-S,FÖ304-S", + output: true, + }, + want: "TR_L303,TR_L302,TR_L213,TR_L205,TR_L114,TR_L107,TR_L106,TR_L014,TR_Innenhof_FF,TR_C162,TR_B150,TR_B149,TR_B148,TR_B146,TR_B145,TR_B071,TR_B070,TR_B067,TR_A_Cafeteria,TR_A228,TR_A140,TR_A137,TR_A134,TR_A129,TR_A128,TR_A127,TR_A126,TR_A125,TR_A124,TR_A034,TR_A033,TR_A032-2,TR_A032-1,TR_A031-1,NI_FoyerK_F,NI_Foyer1_F,NI104,NI103,NI102,NI070,GU319,GU318,GU317b,GU317a,GU313,GU309,GU301,GU225,GU224,GU219,GU203,GU202,GU201,GU014,GU013,GU012,GU011,GU010,GU009,GU001,FÖ306,FÖ305,FÖ304", + }, + { + name: "Test Trefstbau MapRoom Input", + args: args{ + room: "TR_L321", + output: false, + }, + want: "TR_L3.21", + }, + { + name: "Test Trefstbau MapRoom Input", + args: args{ + room: "TR_A032-2", + output: false, + }, + want: "TR_A0.32.2", + }, + { + name: "Test Trefstbau MapRoom Input double point", + args: args{ + //TR_A1.23.1 -> TR_A1.23-1 + room: "TR_A1.23.1", + output: true, + }, + want: "TR_A123-1", + }, + { + name: "Test Trefstbau MapRoom Input double point with -S", + args: args{ + //TR_A1.23.1 -> TR_A1.23-1 + room: "TR_A1.23.1-S", + output: true, + }, + want: "TR_A123-1", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := MapRoom(tt.args.room, tt.args.output); got != tt.want { + t.Errorf("MapRoom() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/services/ical/service/ical/icalDescriptionGenerator.go b/services/ical/service/ical/icalDescriptionGenerator.go index 3717168..c967a19 100644 --- a/services/ical/service/ical/icalDescriptionGenerator.go +++ b/services/ical/service/ical/icalDescriptionGenerator.go @@ -20,6 +20,7 @@ import ( ics "github.com/arran4/golang-ical" "htwkalender/ical/model" "htwkalender/ical/service/functions" + "log/slog" "net/url" "strings" _ "time/tzdata" @@ -31,10 +32,6 @@ func generateUserAgentSpecificDescription(vEvent *ics.VEvent, event model.Event, description, altrep := generateDescription(event, userAgent) if isThunderbird(userAgent) && altrep != "" { - // Thunderbird-specific handling: Add DESCRIPTION with ALTREP attribute. - // create property altrep - //altrepParam := WithAltRep(altrep) - uri := &url.URL{ Scheme: "data", Opaque: altrep, @@ -103,20 +100,30 @@ func generateThunderbirdHTMLDescription(event model.Event) string { htmlDescription.WriteString("Typ: " + event.EventType + event.Compulsory + "
") } - // Add the HTML link to the room map. - htmlDescription.WriteString(`Link: HTWK-Karte`) + roomList := functions.SeperateRoomString(event.Rooms) + htmlDescription.WriteString("Orte:
") + for _, room := range roomList { + mapRoomName := functions.MapRoom(room, true) + _, bufferErr := htmlDescription.WriteString("" + room + "
") + + if bufferErr != nil { + slog.Error("Error while writing to buffer", "error", bufferErr) + return "" + } + } return htmlDescription.String() } // Generates a room description with links for Google Calendar. func generateGoogleCalendarDescription(rooms string) string { var description strings.Builder - roomList := strings.Split(rooms, " ") + roomList := functions.SeperateRoomString(rooms) description.WriteString("Orte: \n ") for _, room := range roomList { - description.WriteString("HTWK-Karte\n") + mapRoomName := functions.MapRoom(room, true) + description.WriteString(" " + mapRoomName + " \n") } return description.String() @@ -131,10 +138,3 @@ func isThunderbird(userAgent string) bool { func isGoogleCalendar(userAgent string) bool { return strings.Contains(userAgent, "Google-Calendar-Importer") } - -func WithAltRep(altRepUrl string) ics.PropertyParameter { - return &ics.KeyValues{ - Key: string(ics.ParameterAltrep), - Value: []string{altRepUrl}, - } -}