//Calendar implementation for the HTWK Leipzig timetable. Evaluation and display of the individual dates in iCal format. //Copyright (C) 2024 HTWKalender support@htwkalender.de //This program is free software: you can redistribute it and/or modify //it under the terms of the GNU Affero General Public License as published by //the Free Software Foundation, either version 3 of the License, or //(at your option) any later version. //This program is distributed in the hope that it will be useful, //but WITHOUT ANY WARRANTY; without even the implied warranty of //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //GNU Affero General Public License for more details. //You should have received a copy of the GNU Affero General Public License //along with this program. If not, see . package feed 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) { feeds, err := database.GetAllFeeds(db) if err != nil { slog.Error("CleanFeeds: failed to get all feeds", err) return } for _, feed := range feeds { // if retrieved time is older than a half year delete feed now := clock.Now() feedRetrievedTime := feed.Retrieved.Time() timeShift := now.AddDate(0, -months, 0) if feedRetrievedTime.Before(timeShift) { // delete feed var sqlResult sql.Result sqlResult, err = db.DB().Delete("feeds", dbx.NewExp("id = {:id}", dbx.Params{"id": feed.GetId()})).Execute() if err != nil { slog.Error("CleanFeeds: delete feed "+feed.GetId()+" failed", err) slog.Error("SQL Result: ", sqlResult) } else { slog.Info("CleanFeeds: delete feed " + feed.GetId() + " successful") } } } } 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 "," , 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 } } } }