Versendung mit .ics Kalenderdatei
This commit is contained in:
1 parent
c08d43e7dd
commit
dc20d7ec5a
13 files changed
+650
-54
No files matched your search
@@ -0,0 +1,134 @@
|
||||
import Foundation
|
||||
|
||||
/// One appointment for the iCalendar export. The start is written as a
|
||||
/// floating local time so calendar applications show the parish's wall-clock
|
||||
/// time regardless of the device time zone.
|
||||
public struct CalendarEvent: Hashable, Sendable {
|
||||
public var uid: String
|
||||
public var start: Date
|
||||
public var durationMinutes: Int
|
||||
public var summary: String
|
||||
public var location: String
|
||||
public var description: String
|
||||
|
||||
public init(
|
||||
uid: String,
|
||||
start: Date,
|
||||
durationMinutes: Int = 60,
|
||||
summary: String,
|
||||
location: String = "",
|
||||
description: String = ""
|
||||
) {
|
||||
self.uid = uid
|
||||
self.start = start
|
||||
self.durationMinutes = durationMinutes
|
||||
self.summary = summary
|
||||
self.location = location
|
||||
self.description = description
|
||||
}
|
||||
}
|
||||
|
||||
/// Builds a published iCalendar document with one timed VEVENT per assignment.
|
||||
public enum CalendarExport {
|
||||
/// Returns a complete `.ics` document in RFC 5545 format.
|
||||
public static func ics(
|
||||
calendarName: String,
|
||||
events: [CalendarEvent],
|
||||
generatedAt: Date = Date()
|
||||
) -> String {
|
||||
let sorted = events.sorted { $0.start < $1.start }
|
||||
var output = ""
|
||||
appendLine("BEGIN:VCALENDAR", to: &output)
|
||||
appendLine("VERSION:2.0", to: &output)
|
||||
appendLine("PRODID:-//LektorenPlan//Gottesdienstplan//DE", to: &output)
|
||||
appendLine("CALSCALE:GREGORIAN", to: &output)
|
||||
appendLine("METHOD:PUBLISH", to: &output)
|
||||
appendLine("X-WR-CALNAME:\(escape(calendarName))", to: &output)
|
||||
|
||||
let stamp = utcFormatter.string(from: generatedAt)
|
||||
for event in sorted {
|
||||
let duration = event.durationMinutes > 0 ? event.durationMinutes : 60
|
||||
let end = event.start.addingTimeInterval(TimeInterval(duration * 60))
|
||||
appendLine("BEGIN:VEVENT", to: &output)
|
||||
appendLine("UID:\(escape(event.uid))@lektorenplan", to: &output)
|
||||
appendLine("DTSTAMP:\(stamp)", to: &output)
|
||||
appendLine("DTSTART:\(localFormatter.string(from: event.start))", to: &output)
|
||||
appendLine("DTEND:\(localFormatter.string(from: end))", to: &output)
|
||||
appendLine("SUMMARY:\(escape(event.summary))", to: &output)
|
||||
let location = event.location.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
if !location.isEmpty {
|
||||
appendLine("LOCATION:\(escape(location))", to: &output)
|
||||
}
|
||||
let description = event.description.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
if !description.isEmpty {
|
||||
appendLine("DESCRIPTION:\(escape(description))", to: &output)
|
||||
}
|
||||
appendLine("END:VEVENT", to: &output)
|
||||
}
|
||||
appendLine("END:VCALENDAR", to: &output)
|
||||
return output
|
||||
}
|
||||
|
||||
/// A filesystem-safe attachment name, for example
|
||||
/// `LektorenPlan-bad-godesberg-Kalender-2026-03-01.ics`.
|
||||
public static func filename(prefix: String, start: Date) -> String {
|
||||
var base = "LektorenPlan"
|
||||
let trimmed = prefix.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
if !trimmed.isEmpty {
|
||||
base += "-" + sanitizedComponent(trimmed)
|
||||
}
|
||||
return "\(base)-Kalender-\(DayMath.key(start)).ics"
|
||||
}
|
||||
|
||||
private static func sanitizedComponent(_ value: String) -> String {
|
||||
let allowed = CharacterSet.alphanumerics.union(CharacterSet(charactersIn: "-_"))
|
||||
let folded = value.folding(options: .diacriticInsensitive, locale: .current)
|
||||
return folded
|
||||
.replacingOccurrences(of: " ", with: "-")
|
||||
.components(separatedBy: allowed.inverted)
|
||||
.joined()
|
||||
}
|
||||
|
||||
private static let localFormatter: DateFormatter = {
|
||||
let formatter = DateFormatter()
|
||||
formatter.locale = Locale(identifier: "en_US_POSIX")
|
||||
formatter.timeZone = DayMath.calendar.timeZone
|
||||
formatter.dateFormat = "yyyyMMdd'T'HHmmss"
|
||||
return formatter
|
||||
}()
|
||||
|
||||
private static let utcFormatter: DateFormatter = {
|
||||
let formatter = DateFormatter()
|
||||
formatter.locale = Locale(identifier: "en_US_POSIX")
|
||||
formatter.timeZone = TimeZone(identifier: "UTC")
|
||||
formatter.dateFormat = "yyyyMMdd'T'HHmmss'Z'"
|
||||
return formatter
|
||||
}()
|
||||
|
||||
/// Appends one content line and folds it at 75 octets as required by RFC 5545.
|
||||
private static func appendLine(_ line: String, to output: inout String) {
|
||||
let limit = 75
|
||||
var used = 0
|
||||
for character in line {
|
||||
let width = String(character).utf8.count
|
||||
if used + width > limit {
|
||||
output += "\r\n "
|
||||
used = 1
|
||||
}
|
||||
output.append(character)
|
||||
used += width
|
||||
}
|
||||
output += "\r\n"
|
||||
}
|
||||
|
||||
/// Applies the TEXT escaping rules of RFC 5545.
|
||||
private static func escape(_ value: String) -> String {
|
||||
var escaped = value.replacingOccurrences(of: "\\", with: "\\\\")
|
||||
escaped = escaped.replacingOccurrences(of: ";", with: "\\;")
|
||||
escaped = escaped.replacingOccurrences(of: ",", with: "\\,")
|
||||
escaped = escaped.replacingOccurrences(of: "\r\n", with: "\\n")
|
||||
escaped = escaped.replacingOccurrences(of: "\n", with: "\\n")
|
||||
escaped = escaped.replacingOccurrences(of: "\r", with: "\\n")
|
||||
return escaped
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user