feat:#53 added merge events for ical feed

This commit is contained in:
Elmar Kresse
2024-04-08 00:16:27 +02:00
parent c05dbc423c
commit e6f35845d2
4 changed files with 163 additions and 19 deletions

View File

@@ -20,9 +20,12 @@ import (
"database/sql"
"github.com/pocketbase/dbx"
"github.com/pocketbase/pocketbase/daos"
"htwkalender/model"
database "htwkalender/service/db"
"htwkalender/service/functions"
localTime "htwkalender/service/functions/time"
"log/slog"
"strings"
)
func ClearFeeds(db *daos.Dao, months int, clock localTime.Clock) {
@@ -50,3 +53,89 @@ func ClearFeeds(db *daos.Dao, months int, clock localTime.Clock) {
}
}
}
func CombineEventsInFeed(events model.Events) model.Events {
// Combine events with the same Prof, Name, Start and End time into one event
// if the note is empty then there is no room displayed in the new note
// room listing isn't displayed if there is only one event.
combinedEvents := make(model.Events, 0)
combinedEvents = append(combinedEvents, events[0])
for index1 := 1; index1 < len(events); index1++ {
for index2 := 0; index2 < len(combinedEvents); index2++ {
if events[index1].Name == combinedEvents[index2].Name &&
events[index1].Start == combinedEvents[index2].Start &&
events[index1].End == combinedEvents[index2].End &&
events[index1].Course == combinedEvents[index2].Course {
// if no notes are present skip
if !functions.OnlyWhitespace(events[index1].Notes) {
// if combinedEvents notes are empty, add the new notes
if functions.OnlyWhitespace(combinedEvents[index2].Notes) {
combinedEvents[index2].Notes = descriptionString(events[index1])
} else {
addNotesIfAlreadyRoomsAdded(events, combinedEvents, index2, index1)
}
}
combineRooms(events, index1, combinedEvents, index2)
combineProfs(events, index1, combinedEvents, index2)
// remove the event from the events list
events = append(events[:index1], events[index1+1:]...)
break
} else {
// if the event is not in the combinedEvents list, add it
if index2 == len(combinedEvents)-1 {
combinedEvents = append(combinedEvents, events[index1])
events = append(events[:index1], events[index1+1:]...)
break
}
}
}
}
return combinedEvents
}
func addNotesIfAlreadyRoomsAdded(events model.Events, combinedEvents model.Events, index2 int, index1 int) {
// check if combinedEvents[index2].Rooms string contains comma "," &#44
if !strings.Contains(combinedEvents[index2].Rooms, ",") {
combinedEvents[index2].Notes = descriptionString(combinedEvents[index2]) + "\n" + descriptionString(events[index1])
} else {
combinedEvents[index2].Notes += "\n" + descriptionString(events[index1])
}
}
func combineProfs(events model.Events, index1 int, combinedEvents model.Events, index2 int) {
// combine the profs
if events[index1].Prof != "" {
if combinedEvents[index2].Prof == "" {
combinedEvents[index2].Prof = events[index1].Prof
} else {
if !strings.Contains(combinedEvents[index2].Prof, events[index1].Prof) {
combinedEvents[index2].Prof += ", " + events[index1].Prof
}
}
}
}
func descriptionString(event model.Event) string {
return event.Rooms + " - " + event.Notes + " (" + event.Prof + ")"
}
func combineRooms(events model.Events, index1 int, combinedEvents model.Events, index2 int) {
// combine the rooms
if events[index1].Rooms != "" {
if combinedEvents[index2].Rooms == "" {
combinedEvents[index2].Rooms = events[index1].Rooms
} else {
if !strings.Contains(combinedEvents[index2].Rooms, events[index1].Rooms) {
combinedEvents[index2].Rooms += ", " + events[index1].Rooms
}
}
}
}