Skip to content
Open
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: 6 additions & 1 deletion MeetingBar/Calendar/MBEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,12 @@ public struct MBEvent: Identifiable, Hashable, Sendable {
eventURL: url,
notes: notes,
calendarEmail: calendar.email,
currentUserEmail: currentUser?.email,
// For a calendar shared into another Google account, EventKit marks
// that other account as the current user; the owner must win.
currentUserEmail: MeetingLinkDetector.calendarOwnerEmail(
calendarID: calendar.id,
calendarTitle: calendar.title
) ?? currentUser?.email,
customRegexes: customRegexes
)
meetingLinkCandidate = meetingLinkCandidates.first
Expand Down
25 changes: 25 additions & 0 deletions MeetingBar/Meetings/MeetingLinkDetector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,31 @@ enum MeetingLinkDetector {
)
}

/// Email of the Google account that owns a calendar, when it can be told
/// from the calendar itself. A Google calendar ID is the owner's email for
/// primary calendars, and EventKit titles a subscribed primary calendar with
/// that email by default. Secondary, imported and holiday calendars use
/// `*.calendar.google.com` IDs, which are not sign-in accounts.
static func calendarOwnerEmail(calendarID: String, calendarTitle: String) -> String? {
// A non-account Google ID rules out the calendar, even if its title looks like an email.
guard !isGoogleNonAccountCalendarID(calendarID) else { return nil }
return [calendarID, calendarTitle].first(where: isAccountEmail)
}

private static func isGoogleNonAccountCalendarID(_ value: String) -> Bool {
value.lowercased().hasSuffix("calendar.google.com")
}

private static func isAccountEmail(_ value: String) -> Bool {
let parts = value.split(separator: "@", omittingEmptySubsequences: false)
let domainLabels = parts.count == 2 ? parts[1].split(separator: ".", omittingEmptySubsequences: false) : []
guard parts.count == 2, !parts[0].isEmpty, domainLabels.count >= 2,
domainLabels.allSatisfy({ !$0.isEmpty }),
!value.contains(where: \.isWhitespace)
else { return false }
return !isGoogleNonAccountCalendarID(value)
}

/// Personal Gmail plus-addresses are delivery aliases, not separate sign-in
/// accounts. Preserve other domains: an address alone cannot establish
/// whether a custom domain uses Google Workspace or supports these aliases.
Expand Down
36 changes: 36 additions & 0 deletions MeetingBarLogicTests/MeetingLinkDetectorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,42 @@ final class MeetingLinkDetectorTests: XCTestCase {
)
}

func testCalendarOwnerEmailComesFromCalendarIDOrTitle() {
XCTAssertEqual(
MeetingLinkDetector.calendarOwnerEmail(calendarID: "work@example.com", calendarTitle: "Work"),
"work@example.com"
)
XCTAssertEqual(
MeetingLinkDetector.calendarOwnerEmail(
calendarID: "6F9619FF-8B86-D011-B42D-00C04FC964FF",
calendarTitle: "work@example.com"
),
"work@example.com"
)
}

func testCalendarOwnerEmailIgnoresNonAccountCalendars() {
for (calendarID, calendarTitle) in [
("abc123@group.calendar.google.com", "Team"),
("en.usa#holiday@group.v.calendar.google.com", "Holidays"),
("abc123@import.calendar.google.com", "Imported"),
("6F9619FF-8B86-D011-B42D-00C04FC964FF", "Work"),
("calendar-id", "Lunch @ office"),
("calendar-id", "Team lunch@example.com"),
("calendar-id", "lunch@example .com"),
("calendar-id", "person@.com"),
("calendar-id", "person@example..com"),
("calendar-id", "person@example.com."),
("abc123@group.calendar.google.com", "team@example.com"),
("abc123@import.calendar.google.com", "team@example.com")
] {
XCTAssertNil(
MeetingLinkDetector.calendarOwnerEmail(calendarID: calendarID, calendarTitle: calendarTitle),
calendarID
)
}
}

func testGoogleMeetPrefersCurrentUserEmailOverCalendarEmail() {
// With multiple calendars connected to one account, the current-user
// attendee identity resolves the correct Google account better than
Expand Down
4 changes: 3 additions & 1 deletion MeetingBarTests/GoogleCalendarDataReaderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,11 @@ final class GoogleCalendarDataReaderTests: BaseTestCase {
XCTAssertEqual(events.first?.id, "standup")
XCTAssertEqual(events.first?.calendar.id, "work@example.com")
XCTAssertEqual(events.first?.meetingLink?.service, .meet)
// The calendar ID names the account that owns the shared calendar,
// which wins over the signed-in account it is listed under.
XCTAssertEqual(
events.first?.meetingLink?.url.absoluteString,
"https://meet.google.com/abc-defg-hij?authuser=owner@example.com"
"https://meet.google.com/abc-defg-hij?authuser=work@example.com"
)
XCTAssertEqual(observedRequests.count, 4)
let eventRequests = observedRequests.filter {
Expand Down
Loading