-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #710 from frog-pond/hawken/ical
- Loading branch information
Showing
14 changed files
with
133 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import {get} from '../ccc-lib/http.js' | ||
import moment from 'moment' | ||
import getUrls from 'get-urls' | ||
import {JSDOM} from 'jsdom' | ||
import InternetCalendar from 'ical.js' | ||
import {Event} from './types.js' | ||
import lodash from 'lodash' | ||
|
||
const {sortBy} = lodash | ||
|
||
/** | ||
* @param {InternetCalendar.Event[]} data | ||
* @param {typeof moment} now | ||
* @returns {Event[]} | ||
*/ | ||
function convertEvents(data, now = moment()) { | ||
return data.map((event) => { | ||
const startTime = moment(event.startDate.toString()) | ||
const endTime = moment(event.endDate.toString()) | ||
let description = JSDOM.fragment(event.description || '').textContent.trim() | ||
|
||
return Event.parse({ | ||
dataSource: 'ical', | ||
startTime: startTime.toISOString(), | ||
endTime: endTime.toISOString(), | ||
title: event.summary, | ||
description: description, | ||
location: event.location, | ||
isOngoing: startTime.isBefore(now, 'day'), | ||
links: [...getUrls(description)], | ||
metadata: { | ||
uid: event.uid, | ||
}, | ||
config: { | ||
startTime: true, | ||
endTime: true, | ||
subtitle: 'location', | ||
}, | ||
}) | ||
}) | ||
} | ||
|
||
export async function ical(url, {onlyFuture = true} = {}, now = moment()) { | ||
let body = await get(url).text() | ||
|
||
let comp = InternetCalendar.Component.fromString(body) | ||
let events = comp | ||
.getAllSubcomponents('vevent') | ||
.map((vevent) => new InternetCalendar.Event(vevent)) | ||
|
||
if (onlyFuture) { | ||
events = events.filter((event) => | ||
moment(event.endDate.toString()).isAfter(now, 'day'), | ||
) | ||
} | ||
|
||
return sortBy(convertEvents(events, now), (event) => event.startTime) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import {z} from 'zod' | ||
|
||
const EventConfig = z.object({ | ||
startTime: z.boolean(), | ||
endTime: z.boolean(), | ||
subtitle: z.union([z.literal('location')]), | ||
}) | ||
|
||
export const Event = z.object({ | ||
dataSource: z.string(), | ||
startTime: z.string().datetime(), | ||
endTime: z.string().datetime(), | ||
title: z.string(), | ||
description: z.string(), | ||
isOngoing: z.boolean(), | ||
links: z.array(z.unknown()), | ||
config: EventConfig, | ||
metadata: z.optional(z.unknown()), | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.