@@ -20,10 +20,10 @@ import rateLimiter from "api/plugins/rateLimiter.js";
20
20
import { getCacheCounter } from "api/functions/cache.js" ;
21
21
22
22
const repeatingIcalMap : Record < EventRepeatOptions , ICalEventJSONRepeatingData > =
23
- {
24
- weekly : { freq : ICalEventRepeatingFreq . WEEKLY } ,
25
- biweekly : { freq : ICalEventRepeatingFreq . WEEKLY , interval : 2 } ,
26
- } ;
23
+ {
24
+ weekly : { freq : ICalEventRepeatingFreq . WEEKLY } ,
25
+ biweekly : { freq : ICalEventRepeatingFreq . WEEKLY , interval : 2 } ,
26
+ } ;
27
27
28
28
function generateHostName ( host : string ) {
29
29
if ( host == "ACM" || ! host ) {
@@ -66,97 +66,98 @@ const icalPlugin: FastifyPluginAsync = async (fastify, _options) => {
66
66
}
67
67
68
68
reply . header ( "etag" , etag ) ;
69
- if ( host ) {
70
- if ( ! OrganizationList . includes ( host ) ) {
71
- throw new ValidationError ( {
72
- message : `Invalid host parameter "${ host } " in path.` ,
73
- } ) ;
74
- }
75
- queryParams = {
76
- ...queryParams ,
77
- } ;
78
- response = await fastify . dynamoClient . send (
79
- new QueryCommand ( {
80
- ...queryParams ,
81
- ExpressionAttributeValues : {
82
- ":host" : {
83
- S : host ,
84
- } ,
85
- } ,
86
- KeyConditionExpression : "host = :host" ,
87
- IndexName : "HostIndex" ,
88
- } ) ,
89
- ) ;
90
- } else {
91
- response = await fastify . dynamoClient . send ( new ScanCommand ( queryParams ) ) ;
92
- }
93
- const dynamoItems = response . Items
94
- ? response . Items . map ( ( x ) => unmarshall ( x ) )
95
- : null ;
96
- if ( ! dynamoItems ) {
97
- throw new NotFoundError ( {
98
- endpointName : host ? `/api/v1/ical/${ host } ` : "/api/v1/ical" ,
69
+ }
70
+ if ( host ) {
71
+ if ( ! OrganizationList . includes ( host ) ) {
72
+ throw new ValidationError ( {
73
+ message : `Invalid host parameter "${ host } " in path.` ,
99
74
} ) ;
100
75
}
101
- // generate friendly calendar name
102
- let calendarName =
103
- host && host . includes ( "ACM" )
104
- ? `${ host } Events`
105
- : `ACM@UIUC - ${ host } Events` ;
106
- if ( host == "ACM" ) {
107
- calendarName = "ACM@UIUC - Major Events" ;
108
- }
109
- if ( ! host ) {
110
- calendarName = "ACM@UIUC - All Events" ;
111
- }
112
- const calendar = ical ( { name : calendarName } ) ;
113
- calendar . timezone ( {
114
- name : "America/Chicago" ,
115
- generator : getVtimezoneComponent ,
76
+ queryParams = {
77
+ ...queryParams ,
78
+ } ;
79
+ response = await fastify . dynamoClient . send (
80
+ new QueryCommand ( {
81
+ ...queryParams ,
82
+ ExpressionAttributeValues : {
83
+ ":host" : {
84
+ S : host ,
85
+ } ,
86
+ } ,
87
+ KeyConditionExpression : "host = :host" ,
88
+ IndexName : "HostIndex" ,
89
+ } ) ,
90
+ ) ;
91
+ } else {
92
+ response = await fastify . dynamoClient . send ( new ScanCommand ( queryParams ) ) ;
93
+ }
94
+ const dynamoItems = response . Items
95
+ ? response . Items . map ( ( x ) => unmarshall ( x ) )
96
+ : null ;
97
+ if ( ! dynamoItems ) {
98
+ throw new NotFoundError ( {
99
+ endpointName : host ? `/api/v1/ical/${ host } ` : "/api/v1/ical" ,
116
100
} ) ;
117
- calendar . method ( ICalCalendarMethod . PUBLISH ) ;
118
- for ( const rawEvent of dynamoItems ) {
119
- let event = calendar . createEvent ( {
120
- start : moment . tz ( rawEvent . start , "America/Chicago" ) ,
121
- end : rawEvent . end
122
- ? moment . tz ( rawEvent . end , "America/Chicago" )
123
- : moment . tz ( rawEvent . start , "America/Chicago" ) ,
124
- summary : rawEvent . title ,
125
- description : rawEvent . locationLink
126
- ? `Host: ${ rawEvent . host } \nGoogle Maps Link: ${ rawEvent . locationLink } \n\n` +
101
+ }
102
+ // generate friendly calendar name
103
+ let calendarName =
104
+ host && host . includes ( "ACM" )
105
+ ? `${ host } Events`
106
+ : `ACM@UIUC - ${ host } Events` ;
107
+ if ( host == "ACM" ) {
108
+ calendarName = "ACM@UIUC - Major Events" ;
109
+ }
110
+ if ( ! host ) {
111
+ calendarName = "ACM@UIUC - All Events" ;
112
+ }
113
+ const calendar = ical ( { name : calendarName } ) ;
114
+ calendar . timezone ( {
115
+ name : "America/Chicago" ,
116
+ generator : getVtimezoneComponent ,
117
+ } ) ;
118
+ calendar . method ( ICalCalendarMethod . PUBLISH ) ;
119
+ for ( const rawEvent of dynamoItems ) {
120
+ let event = calendar . createEvent ( {
121
+ start : moment . tz ( rawEvent . start , "America/Chicago" ) ,
122
+ end : rawEvent . end
123
+ ? moment . tz ( rawEvent . end , "America/Chicago" )
124
+ : moment . tz ( rawEvent . start , "America/Chicago" ) ,
125
+ summary : rawEvent . title ,
126
+ description : rawEvent . locationLink
127
+ ? `Host: ${ rawEvent . host } \nGoogle Maps Link: ${ rawEvent . locationLink } \n\n` +
127
128
rawEvent . description
128
- : `Host: ${ rawEvent . host } \n\n` + rawEvent . description ,
129
- timezone : "America/Chicago" ,
130
- organizer : generateHostName ( host ) ,
131
- id : rawEvent . id ,
132
- } ) ;
129
+ : `Host: ${ rawEvent . host } \n\n` + rawEvent . description ,
130
+ timezone : "America/Chicago" ,
131
+ organizer : generateHostName ( host ) ,
132
+ id : rawEvent . id ,
133
+ } ) ;
133
134
134
- if ( rawEvent . repeats ) {
135
- if ( rawEvent . repeatEnds ) {
136
- event = event . repeating ( {
137
- ...repeatingIcalMap [ rawEvent . repeats as EventRepeatOptions ] ,
138
- until : moment . tz ( rawEvent . repeatEnds , "America/Chicago" ) ,
139
- } ) ;
140
- } else {
141
- event . repeating (
142
- repeatingIcalMap [ rawEvent . repeats as EventRepeatOptions ] ,
143
- ) ;
144
- }
145
- }
146
- if ( rawEvent . location ) {
147
- event = event . location ( {
148
- title : rawEvent . location ,
135
+ if ( rawEvent . repeats ) {
136
+ if ( rawEvent . repeatEnds ) {
137
+ event = event . repeating ( {
138
+ ...repeatingIcalMap [ rawEvent . repeats as EventRepeatOptions ] ,
139
+ until : moment . tz ( rawEvent . repeatEnds , "America/Chicago" ) ,
149
140
} ) ;
141
+ } else {
142
+ event . repeating (
143
+ repeatingIcalMap [ rawEvent . repeats as EventRepeatOptions ] ,
144
+ ) ;
150
145
}
151
146
}
147
+ if ( rawEvent . location ) {
148
+ event = event . location ( {
149
+ title : rawEvent . location ,
150
+ } ) ;
151
+ }
152
+ }
152
153
153
- reply
154
- . headers ( {
155
- "Content-Type" : "text/calendar; charset=utf-8" ,
156
- "Content-Disposition" : 'attachment; filename="calendar.ics"' ,
157
- } )
158
- . send ( calendar . toString ( ) ) ;
159
- } ) ;
154
+ reply
155
+ . headers ( {
156
+ "Content-Type" : "text/calendar; charset=utf-8" ,
157
+ "Content-Disposition" : 'attachment; filename="calendar.ics"' ,
158
+ } )
159
+ . send ( calendar . toString ( ) ) ;
160
+ } ) ;
160
161
} ;
161
162
162
163
export default icalPlugin ;
0 commit comments