Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions Sources/Oatcake/Calendar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,8 @@ final class CalendarController {
}
}

/// The calendar event happening now (or starting within ten minutes), if any.
var currentEvent: CalendarEvent? {
let now = Date()
return events.first { !$0.allDay && $0.startDate.addingTimeInterval(-600) <= now && $0.endDate > now }
/// The calendar event happening now (or starting within ten minutes), if any, unless it's in `recorded`.
func currentEvent(unless recorded: Set<String> = []) -> CalendarEvent? {
eventHappening(events, at: Date(), unless: recorded)
}
}
2 changes: 1 addition & 1 deletion Sources/Oatcake/OatcakeApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCent
if action == Reminders.recordAction {
model.recording.recordCall()
} else if action == UNNotificationDefaultActionIdentifier {
model.newRecording(model.calendar.currentEvent)
model.newRecording(model.recording.noticedCallEvent)
}
return
}
Expand Down
15 changes: 13 additions & 2 deletions Sources/Oatcake/Recording.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ final class RecordingController {

/// Recorders running now, by recording ID.
private var active: [String: Recording] = [:]
/// Calendar events recorded since launch, kept after their recordings are saved and tidied away.
private var recordedEvents = Set<String>()
private var liveHelper: ChildProcess.Handle?
/// The recording whose live transcript helper is running or on its way up. A second helper for the
/// same recording would say every line again.
Expand Down Expand Up @@ -166,6 +168,7 @@ final class RecordingController {
guard active.isEmpty else { throw OatcakeError("Oatcake is already recording a call. Stop it first.") }
let rec = try Recordings.start(title: title, folder: folder, micUid: config.mic?.uid, options: options)
active[rec.id] = rec
if let eventId = options.eventId { recordedEvents.insert(eventId) }
// The prep is in the call's notes now.
if !options.prep.isEmpty { try? PrepNotes.save(options.eventId, []) }
goLive(rec)
Expand Down Expand Up @@ -500,7 +503,7 @@ final class RecordingController {
callOffered = true
} else if config.callNotice, !callOffered, now.timeIntervalSince(callSince!) >= 4 {
callOffered = true
Reminders.offerRecording(app: apps[0], event: calendar.currentEvent)
Reminders.offerRecording(app: apps[0], event: noticedCallEvent)
}
}

Expand Down Expand Up @@ -530,12 +533,20 @@ final class RecordingController {
}
}

/// The calendar event a call Oatcake noticed by itself belongs to: the one happening now, unless it
/// has been recorded already. Its call may have finished early and this be another one in its slot.
/// A recording started from the event itself (New recording, Upcoming) isn't asked this.
var noticedCallEvent: CalendarEvent? {
let recorded = recordedEvents.union(Recordings.list().compactMap(\.eventId))
return calendar.currentEvent(unless: recorded)
}

/// From the "On a call?" notification: record it, under the calendar event happening now if there is one.
func recordCall() {
showWindow()
guard active.isEmpty else { return selection = .live }
do {
if let event = calendar.currentEvent {
if let event = noticedCallEvent {
let folder = try folderForEvent(event.title, event.seriesId != nil)
try startRecording(title: event.title, folder: folder, options: startOptions(event))
} else {
Expand Down
10 changes: 10 additions & 0 deletions Sources/OatcakeCore/CalendarFeed.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ extension CalendarCache {
}
}

/**
* The event happening at `now` (or starting within ten minutes), if any. None if that event is in
* `recorded` (by ID): a call that finishes early and is followed by another while its slot is still on
* the clock (a quick debrief, say) would otherwise be named after the first call and get its invite.
*/
public func eventHappening(_ events: [CalendarEvent], at now: Date, unless recorded: Set<String> = []) -> CalendarEvent? {
let event = events.first { !$0.allDay && $0.startDate.addingTimeInterval(-600) <= now && $0.endDate > now }
return event.flatMap { recorded.contains($0.id) ? nil : $0 }
}

/**
* Whether an event is left out of Upcoming, the menu bar and reminders: its title starts with one of
* `ignored` (Settings › Calendar) as whole words, or has "Oatcake ignore" anywhere in it. Case and
Expand Down
21 changes: 21 additions & 0 deletions Tests/OatcakeCoreTests/CalendarTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,27 @@ import Testing
#expect(try JSONDecoder().decode(Config.self, from: Data(try encodeJSON(config).utf8)).ignoredEvents == [])
}

/// A call that finishes early and another in its slot: the second isn't named after the first.
@Test func callAfterAnEarlyFinish() {
func event(_ id: String, _ start: String, _ end: String, allDay: Bool = false) -> CalendarEvent {
CalendarEvent(id: id, seriesId: nil, title: id, start: start, end: end, allDay: allDay, attendees: [], meetUrl: nil)
}
let renewal = event("Renewal sync", "2026-11-18T09:00:00Z", "2026-11-18T10:00:00Z")
let next = event("Next call", "2026-11-18T10:00:00Z", "2026-11-18T11:00:00Z")
let offsite = event("Offsite", "2026-11-18T00:00:00Z", "2026-11-19T00:00:00Z", allDay: true)
func at(_ time: String) -> Date { parseISO("2026-11-18T\(time)Z")! }
// As before when nothing has been recorded.
#expect(eventHappening([offsite, renewal], at: at("09:01:00"))?.id == "Renewal sync")
#expect(eventHappening([renewal], at: at("08:51:00"))?.id == "Renewal sync")
#expect(eventHappening([renewal], at: at("08:49:00")) == nil)
#expect(eventHappening([renewal, next], at: at("09:51:00"))?.id == "Renewal sync")
#expect(eventHappening([renewal], at: at("10:00:00")) == nil)
// Recorded already: no event, rather than its name or the next call's.
#expect(eventHappening([renewal, next], at: at("09:47:00"), unless: ["Renewal sync"]) == nil)
#expect(eventHappening([renewal, next], at: at("09:51:00"), unless: ["Renewal sync"]) == nil)
#expect(eventHappening([renewal, next], at: at("09:51:00"), unless: ["Other call"])?.id == "Renewal sync")
}

@Test func callLinks() {
#expect(callApp("https://meet.google.com/abc") == "Google Meet")
#expect(callApp("https://acme.zoom.us/j/1") == "Zoom")
Expand Down
Loading