fix:#65 added new UID generation for ical File

This commit is contained in:
masterelmar
2023-11-29 11:23:43 +01:00
parent fe4ba9e529
commit f1d178049e
3 changed files with 39 additions and 6 deletions

View File

@ -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)
}

View File

@ -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)
}
})
}
}

View File

@ -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)))