Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

複数のカレンダーの取得に対応 #85

Merged
merged 1 commit into from
Jan 23, 2025
Merged
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
64 changes: 32 additions & 32 deletions src/lib/googleCalendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,47 +17,47 @@ export class GoogleCalendar implements Calendar {

async getEvents(): Promise<CalEvent[]> {
const { data: calendars } = await this.client.calendarList.list()
const primaryCalendarId = calendars.items?.find(calendar => calendar.primary)?.id
if (!primaryCalendarId) {
throw new Error("Primary calendar not found")
}

const { data: events } = await this.client.events.list({
calendarId: primaryCalendarId!,
timeMin: new Date().toISOString(),
timeMax: new Date(Date.now() + FETCH_EVENTS_DURATION).toISOString(),
singleEvents: true,
orderBy: "startTime",
})
const calendarIds = calendars.items?.map(calendar => calendar.id).filter(it => it != null) ?? []

const events = await Promise.all(
calendarIds.map(async calendarId => {
const { data: events } = await this.client.events.list({
calendarId,
timeMin: new Date().toISOString(),
timeMax: new Date(Date.now() + FETCH_EVENTS_DURATION).toISOString(),
singleEvents: true,
orderBy: "startTime",
})
return events
}),
)

return (
events.items?.map(event => ({
id: event.id ?? null,
summary: event.summary ?? DEFAULT_EVENT.summary,
description: event.description ?? null,
location: event.location ?? null,
start: event.start ? new Date(event.start.dateTime!) : DEFAULT_EVENT.start,
end: event.end ? new Date(event.end.dateTime!) : DEFAULT_EVENT.end,
status: event.status
? this.validateEventStatus(event.status)
? event.status
: null
: null,
attendees: event.attendees?.map(it => ({ email: it.email ?? null })) ?? [],
})) ?? []
return events.flatMap(
event =>
event.items?.map(event => ({
id: event.id ?? null,
summary: event.summary ?? DEFAULT_EVENT.summary,
description: event.description ?? null,
location: event.location ?? null,
start: event.start ? new Date(event.start.dateTime!) : DEFAULT_EVENT.start,
end: event.end ? new Date(event.end.dateTime!) : DEFAULT_EVENT.end,
status: event.status
? this.validateEventStatus(event.status)
? event.status
: null
: null,
attendees: event.attendees?.map(it => ({ email: it.email ?? null })) ?? [],
})) ?? [],
)
}

async getBusyPeriods(): Promise<SlimedCalEvent[]> {
const { data: calendars } = await this.client.calendarList.list()
const primaryCalendarId = calendars.items?.find(calendar => calendar.primary)?.id
if (!primaryCalendarId) {
throw new Error("Primary calendar not found")
}
const calendarIds = calendars.items?.map(calendar => calendar.id) ?? []

const freebusyResult = await this.client.freebusy.query({
requestBody: {
items: [{ id: primaryCalendarId }],
items: calendarIds.map(id => ({ id })),
groupExpansionMax: 100,
calendarExpansionMax: 50,
timeMin: new Date().toISOString(),
Expand Down