@@ -31,7 +31,7 @@ app.get('/', (req, res) => {
31
31
*/
32
32
app . post (
33
33
'/' ,
34
- verifyKeyMiddleware ( process . env . DISCORD_PUBLIC_KEY ) ,
34
+ verifyKeyMiddleware ( process . env . DISCORD_PUBLIC_KEY ! ) ,
35
35
async ( request , response ) => {
36
36
const message = request . body ;
37
37
if ( message . type === InteractionType . PING ) {
@@ -72,17 +72,17 @@ app.post(
72
72
if ( githubUserId ) {
73
73
cleanedUp = true ;
74
74
75
- // 4. Revoke Fitbit OAuth2 tokens
76
- await fitbit . revokeAccess ( fitbitUserId ) ;
77
- await storage . deleteLinkedFitbitUser ( userId ) ;
75
+ // 4. Revoke Github OAuth2 tokens
76
+ await github . revokeAccess ( githubUserId ) ;
77
+ await storage . deleteLinkedGithubUser ( userId ) ;
78
78
}
79
79
80
80
// 5. Let the user know the slash command worked
81
81
if ( cleanedUp ) {
82
82
response . send ( {
83
83
type : InteractionResponseType . CHANNEL_MESSAGE_WITH_SOURCE ,
84
84
data : {
85
- content : 'Fitbit account disconnected.' ,
85
+ content : 'Github account disconnected.' ,
86
86
} ,
87
87
} ) ;
88
88
} else {
@@ -93,19 +93,19 @@ app.post(
93
93
case GET_PROFILE . name . toLowerCase ( ) : {
94
94
/**
95
95
* GET PROFILE
96
- * If the user has a linked Fitbit account, fetch the profile data.
96
+ * If the user has a linked Github account, fetch the profile data.
97
97
*/
98
98
const userId = message . member . user . id ;
99
- const fitbitUserId = await storage . getLinkedFitbitUserId ( userId ) ;
100
- if ( ! fitbitUserId ) {
99
+ const githubUserId = await storage . getLinkedGithubUserId ( userId ) ;
100
+ if ( ! githubUserId ) {
101
101
return sendNoConnectionFound ( response ) ;
102
102
}
103
103
104
- const fitbitTokens = await storage . getFitbitTokens ( fitbitUserId ) ;
105
- if ( ! fitbitTokens ) {
104
+ const githubTokens = await storage . getGithubTokens ( githubUserId ) ;
105
+ if ( ! githubTokens ) {
106
106
return sendNoConnectionFound ( response ) ;
107
107
}
108
- const profile = await fitbit . getProfile ( fitbitUserId , fitbitTokens ) ;
108
+ const profile = await github . getProfile ( githubUserId , githubTokens ) ;
109
109
const metadata = {
110
110
averagedailysteps : profile . user . averageDailySteps ,
111
111
ambassador : profile . user . ambassador ,
@@ -134,7 +134,7 @@ app.post(
134
134
135
135
/**
136
136
* Route configured in the Discord developer console which facilitates the
137
- * connection between Discord and Fitbit . To start the flow, generate the OAuth2
137
+ * connection between Discord and Github . To start the flow, generate the OAuth2
138
138
* consent dialog url for Discord, and send the user there.
139
139
*/
140
140
app . get ( '/verified-role' , async ( req , res ) => {
@@ -155,8 +155,8 @@ app.get('/verified-role', async (req, res) => {
155
155
* completes a few steps:
156
156
* 1. Uses the code to acquire Discord OAuth2 tokens
157
157
* 2. Uses the Discord Access Token to fetch the user profile
158
- * 3. Stores the OAuth2 Discord Tokens in Redis / Firestore
159
- * 4. Generates an OAuth2 consent dialog url for Fitbit , and redirects the user.
158
+ * 3. Stores the OAuth2 Discord Tokens in Postgres
159
+ * 4. Generates an OAuth2 consent dialog url for Github , and redirects the user.
160
160
*/
161
161
app . get ( '/discord-oauth-callback' , async ( req , res ) => {
162
162
try {
@@ -182,16 +182,16 @@ app.get('/discord-oauth-callback', async (req, res) => {
182
182
expires_at : Date . now ( ) + tokens . expires_in * 1000 ,
183
183
} ) ;
184
184
185
- // start the fitbit OAuth2 flow by generating a new OAuth2 Url
186
- const { url, codeVerifier, state } = fitbit . getOAuthUrl ( ) ;
185
+ // start the Github OAuth2 flow by generating a new OAuth2 Url
186
+ const { url, codeVerifier, state } = github . getOAuthUrl ( ) ;
187
187
188
- // store the code verifier and state arguments required by the fitbit url
188
+ // store the code verifier and state arguments required by the Github url
189
189
await storage . storeStateData ( state , {
190
190
discordUserId : userId ,
191
191
codeVerifier,
192
192
} ) ;
193
193
194
- // send the user to the fitbit OAuth2 consent dialog screen
194
+ // send the user to the Github OAuth2 consent dialog screen
195
195
res . redirect ( url ) ;
196
196
} catch ( e ) {
197
197
console . error ( e ) ;
@@ -200,25 +200,25 @@ app.get('/discord-oauth-callback', async (req, res) => {
200
200
} ) ;
201
201
202
202
/**
203
- * Route configured in the Fitbit developer console, the redirect Url to which
204
- * the user is sent after approvingv the bot for their Fitbit account.
203
+ * Route configured in the Github developer console, the redirect Url to which
204
+ * the user is sent after approvingv the bot for their Github account.
205
205
* 1. Use the state in the querystring to fetch the code verifier and challenge
206
- * 2. Use the code in the querystring to acquire Fitbit OAuth2 tokens
207
- * 3. Store the Fitbit tokens in redis / Firestore
206
+ * 2. Use the code in the querystring to acquire Github OAuth2 tokens
207
+ * 3. Store the Github tokens in Postgres
208
208
* 4. Create a new subscription to ensure webhook events are sent for the current user
209
- * 5. Fetch Fitbit profile metadata, and push it to the Discord metadata service
209
+ * 5. Fetch Github profile metadata, and push it to the Discord metadata service
210
210
*/
211
- app . get ( '/fitbit -oauth-callback' , async ( req , res ) => {
211
+ app . get ( '/github -oauth-callback' , async ( req , res ) => {
212
212
try {
213
213
// 1. Use the state in the querystring to fetch the code verifier and challenge
214
214
const state = req . query [ 'state' ] as string ;
215
215
const { discordUserId, codeVerifier } = await storage . getStateData ( state ) ;
216
216
217
- // 2. Use the code in the querystring to acquire Fitbit OAuth2 tokens
217
+ // 2. Use the code in the querystring to acquire Github OAuth2 tokens
218
218
const code = req . query [ 'code' ] as string ;
219
- const tokens = await fitbit . getOAuthTokens ( code , codeVerifier ) ;
219
+ const tokens = await github . getOAuthTokens ( code , codeVerifier ) ;
220
220
221
- // 3. Store the Fitbit tokens in redis / Firestore
221
+ // 3. Store the Github tokens in Postgres
222
222
const userId = tokens . user_id ;
223
223
const data = {
224
224
discord_user_id : discordUserId ,
@@ -227,15 +227,15 @@ app.get('/fitbit-oauth-callback', async (req, res) => {
227
227
expires_at : Date . now ( ) + tokens . expires_in * 1000 ,
228
228
code_verifier : codeVerifier ,
229
229
} ;
230
- await storage . storeFitbitTokens ( userId , data ) ;
230
+ await storage . storeGithubTokens ( userId , data ) ;
231
231
232
232
// 4. Create a new subscription to ensure webhook events are sent for the current user
233
- await fitbit . createSubscription ( userId , data ) ;
233
+ await github . createSubscription ( userId , data ) ;
234
234
235
- // 5. Fetch Fitbit profile metadata, and push it to the Discord metadata service
235
+ // 5. Fetch Github profile metadata, and push it to the Discord metadata service
236
236
await updateMetadata ( userId ) ;
237
237
238
- await storage . setLinkedFitbitUserId ( discordUserId , userId ) ;
238
+ await storage . setLinkedGithubUserId ( discordUserId , userId ) ;
239
239
240
240
res . send ( 'You did it! Now go back to Discord.' ) ;
241
241
} catch ( e ) {
@@ -245,13 +245,13 @@ app.get('/fitbit-oauth-callback', async (req, res) => {
245
245
} ) ;
246
246
247
247
/**
248
- * Route configured in the Fitbit developer console, the route where user
249
- * events are sent. This is used once for url verification by the Fitbit API.
248
+ * Route configured in the Github developer console, the route where user
249
+ * events are sent. This is used once for url verification by the Github API.
250
250
* Note: this is a `GET`, and the actual webhook is a `POST`
251
251
* Verify subscriber as explained in:
252
252
* https://dev.fitbit.com/build/reference/web-api/developer-guide/using-subscriptions/#Verifying-a-Subscriber
253
253
*/
254
- app . get ( '/fitbit -webhook' , async ( req , res ) => {
254
+ app . get ( '/github -webhook' , async ( req , res ) => {
255
255
const verify = req . query [ 'verify' ] as string ;
256
256
console . log ( req . url ) ;
257
257
if ( verify === process . env . FITBIT_SUBSCRIBER_VERIFY ) {
@@ -263,20 +263,20 @@ app.get('/fitbit-webhook', async (req, res) => {
263
263
} ) ;
264
264
265
265
/**
266
- * Route configured in the Fitbit developer console, the route where user events are sent.
266
+ * Route configured in the Github developer console, the route where user events are sent.
267
267
* Takes a few steps:
268
- * 1. Fetch the Discord and Fitbit tokens from storage (redis / firestore )
269
- * 2. Fetch the user profile data from Fitbit and send it to Discord
268
+ * 1. Fetch the Discord and Github tokens from storage (Postgres )
269
+ * 2. Fetch the user profile data from Github and send it to Discord
270
270
*/
271
- app . post ( '/fitbit -webhook' , async ( req , res ) => {
271
+ app . post ( '/github -webhook' , async ( req , res ) => {
272
272
try {
273
- const body = req . body as fitbit . WebhookBody ;
273
+ const body = req . body as github . WebhookBody ;
274
274
275
- // 1. Fetch the Discord and Fitbit tokens from storage (redis / firestore )
275
+ // 1. Fetch the Discord and Github tokens from storage (Postgres )
276
276
const userId = body . ownerId ;
277
277
await updateMetadata ( userId ) ;
278
278
279
- // 2. Fetch the user profile data from Fitbit , and push it to Discord
279
+ // 2. Fetch the user profile data from Github , and push it to Discord
280
280
res . sendStatus ( 204 ) ;
281
281
} catch ( e ) {
282
282
res . sendStatus ( 500 ) ;
@@ -287,7 +287,7 @@ function sendNoConnectionFound(response) {
287
287
return response . send ( {
288
288
type : InteractionResponseType . CHANNEL_MESSAGE_WITH_SOURCE ,
289
289
data : {
290
- content : `🥴 no Fitbit connection info found. Visit ${ process . env . VERIFICATION_URL } to set it up.` ,
290
+ content : `🥴 no Github connection info found. Visit ${ process . env . VERIFICATION_URL } to set it up.` ,
291
291
} ,
292
292
} ) ;
293
293
}
0 commit comments