diff --git a/backend/service/functions/string.go b/backend/service/functions/string.go index f80b684..83b228b 100644 --- a/backend/service/functions/string.go +++ b/backend/service/functions/string.go @@ -1,6 +1,10 @@ package functions -import "strings" +import ( + "crypto/sha256" + "encoding/hex" + "strings" +) // check if string is empty or contains only whitespaces func OnlyWhitespace(word string) bool { @@ -25,3 +29,10 @@ func ReplaceEmptyString(word string, replacement string) string { } return word } + +func HashString(s string) string { + hash := sha256.New() + hash.Write([]byte(s)) + hashInBytes := hash.Sum(nil) + return hex.EncodeToString(hashInBytes) +} diff --git a/backend/service/functions/string_test.go b/backend/service/functions/string_test.go index 16cfc9d..e097015 100644 --- a/backend/service/functions/string_test.go +++ b/backend/service/functions/string_test.go @@ -31,3 +31,24 @@ func TestOnlyWhitespace(t *testing.T) { }) } } + +func TestHashString(t *testing.T) { + type args struct { + s string + } + tests := []struct { + name string + args args + want string + }{ + {"empty string", args{""}, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"}, + {"non-empty string", args{"abc"}, "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := HashString(tt.args.s); got != tt.want { + t.Errorf("HashString() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/backend/service/ical/icalFileGeneration.go b/backend/service/ical/icalFileGeneration.go index cee12d2..2b92f10 100644 --- a/backend/service/ical/icalFileGeneration.go +++ b/backend/service/ical/icalFileGeneration.go @@ -4,7 +4,6 @@ import ( "htwkalender/model" "htwkalender/service/functions" "htwkalender/service/names" - "strconv" "time" "github.com/jordic/goics" @@ -32,16 +31,18 @@ func (icalModel IcalModel) EmitICal() goics.Componenter { //add v time zone icalModel.vtimezone(c) - var timeStamp = time.Now().Local().In(europeTime).Format("20060102T150405") - - for i, event := range icalModel.Events { + for _, event := range icalModel.Events { mapEntry, mappingFound := icalModel.Mapping[event.UUID] s := goics.NewComponent() s.SetType("VEVENT") s.AddProperty(goics.FormatDateTime("DTSTAMP", time.Now().Local().In(europeTime))) - s.AddProperty("UID", strconv.FormatInt(int64(i), 16)+"-"+timeStamp+"@htwkalender.de") + + // create a unique id for the event by hashing the event start, end, course and name + var eventHash = functions.HashString(event.Start.String() + event.End.String() + event.Course + event.Name) + + s.AddProperty("UID", eventHash+"@htwkalender.de") s.AddProperty(goics.FormatDateTime("DTEND", event.End.Time().Local().In(europeTime))) s.AddProperty(goics.FormatDateTime("DTSTART", event.Start.Time().Local().In(europeTime)))