Skip to content
This repository was archived by the owner on Jul 10, 2023. It is now read-only.

Commit 9c560d9

Browse files
committed
idk what im doing :3
1 parent 62b86c6 commit 9c560d9

File tree

3 files changed

+50
-168
lines changed

3 files changed

+50
-168
lines changed

src/github.ts

Lines changed: 0 additions & 109 deletions
This file was deleted.

src/server.ts

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ app.get('/', (req, res) => {
3131
*/
3232
app.post(
3333
'/',
34-
verifyKeyMiddleware(process.env.DISCORD_PUBLIC_KEY),
34+
verifyKeyMiddleware(process.env.DISCORD_PUBLIC_KEY!),
3535
async (request, response) => {
3636
const message = request.body;
3737
if (message.type === InteractionType.PING) {
@@ -72,17 +72,17 @@ app.post(
7272
if (githubUserId) {
7373
cleanedUp = true;
7474

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);
7878
}
7979

8080
// 5. Let the user know the slash command worked
8181
if (cleanedUp) {
8282
response.send({
8383
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
8484
data: {
85-
content: 'Fitbit account disconnected.',
85+
content: 'Github account disconnected.',
8686
},
8787
});
8888
} else {
@@ -93,19 +93,19 @@ app.post(
9393
case GET_PROFILE.name.toLowerCase(): {
9494
/**
9595
* 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.
9797
*/
9898
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) {
101101
return sendNoConnectionFound(response);
102102
}
103103

104-
const fitbitTokens = await storage.getFitbitTokens(fitbitUserId);
105-
if (!fitbitTokens) {
104+
const githubTokens = await storage.getGithubTokens(githubUserId);
105+
if (!githubTokens) {
106106
return sendNoConnectionFound(response);
107107
}
108-
const profile = await fitbit.getProfile(fitbitUserId, fitbitTokens);
108+
const profile = await github.getProfile(githubUserId, githubTokens);
109109
const metadata = {
110110
averagedailysteps: profile.user.averageDailySteps,
111111
ambassador: profile.user.ambassador,
@@ -134,7 +134,7 @@ app.post(
134134

135135
/**
136136
* 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
138138
* consent dialog url for Discord, and send the user there.
139139
*/
140140
app.get('/verified-role', async (req, res) => {
@@ -155,8 +155,8 @@ app.get('/verified-role', async (req, res) => {
155155
* completes a few steps:
156156
* 1. Uses the code to acquire Discord OAuth2 tokens
157157
* 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.
160160
*/
161161
app.get('/discord-oauth-callback', async (req, res) => {
162162
try {
@@ -182,16 +182,16 @@ app.get('/discord-oauth-callback', async (req, res) => {
182182
expires_at: Date.now() + tokens.expires_in * 1000,
183183
});
184184

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();
187187

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
189189
await storage.storeStateData(state, {
190190
discordUserId: userId,
191191
codeVerifier,
192192
});
193193

194-
// send the user to the fitbit OAuth2 consent dialog screen
194+
// send the user to the Github OAuth2 consent dialog screen
195195
res.redirect(url);
196196
} catch (e) {
197197
console.error(e);
@@ -200,25 +200,25 @@ app.get('/discord-oauth-callback', async (req, res) => {
200200
});
201201

202202
/**
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.
205205
* 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
208208
* 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
210210
*/
211-
app.get('/fitbit-oauth-callback', async (req, res) => {
211+
app.get('/github-oauth-callback', async (req, res) => {
212212
try {
213213
// 1. Use the state in the querystring to fetch the code verifier and challenge
214214
const state = req.query['state'] as string;
215215
const { discordUserId, codeVerifier } = await storage.getStateData(state);
216216

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
218218
const code = req.query['code'] as string;
219-
const tokens = await fitbit.getOAuthTokens(code, codeVerifier);
219+
const tokens = await github.getOAuthTokens(code, codeVerifier);
220220

221-
// 3. Store the Fitbit tokens in redis / Firestore
221+
// 3. Store the Github tokens in Postgres
222222
const userId = tokens.user_id;
223223
const data = {
224224
discord_user_id: discordUserId,
@@ -227,15 +227,15 @@ app.get('/fitbit-oauth-callback', async (req, res) => {
227227
expires_at: Date.now() + tokens.expires_in * 1000,
228228
code_verifier: codeVerifier,
229229
};
230-
await storage.storeFitbitTokens(userId, data);
230+
await storage.storeGithubTokens(userId, data);
231231

232232
// 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);
234234

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
236236
await updateMetadata(userId);
237237

238-
await storage.setLinkedFitbitUserId(discordUserId, userId);
238+
await storage.setLinkedGithubUserId(discordUserId, userId);
239239

240240
res.send('You did it! Now go back to Discord.');
241241
} catch (e) {
@@ -245,13 +245,13 @@ app.get('/fitbit-oauth-callback', async (req, res) => {
245245
});
246246

247247
/**
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.
250250
* Note: this is a `GET`, and the actual webhook is a `POST`
251251
* Verify subscriber as explained in:
252252
* https://dev.fitbit.com/build/reference/web-api/developer-guide/using-subscriptions/#Verifying-a-Subscriber
253253
*/
254-
app.get('/fitbit-webhook', async (req, res) => {
254+
app.get('/github-webhook', async (req, res) => {
255255
const verify = req.query['verify'] as string;
256256
console.log(req.url);
257257
if (verify === process.env.FITBIT_SUBSCRIBER_VERIFY) {
@@ -263,20 +263,20 @@ app.get('/fitbit-webhook', async (req, res) => {
263263
});
264264

265265
/**
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.
267267
* 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
270270
*/
271-
app.post('/fitbit-webhook', async (req, res) => {
271+
app.post('/github-webhook', async (req, res) => {
272272
try {
273-
const body = req.body as fitbit.WebhookBody;
273+
const body = req.body as github.WebhookBody;
274274

275-
// 1. Fetch the Discord and Fitbit tokens from storage (redis / firestore)
275+
// 1. Fetch the Discord and Github tokens from storage (Postgres)
276276
const userId = body.ownerId;
277277
await updateMetadata(userId);
278278

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
280280
res.sendStatus(204);
281281
} catch (e) {
282282
res.sendStatus(500);
@@ -287,7 +287,7 @@ function sendNoConnectionFound(response) {
287287
return response.send({
288288
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
289289
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.`,
291291
},
292292
});
293293
}

src/storage.ts

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { createClient, RedisClientType } from 'redis';
2-
import { Firestore, Timestamp } from '@google-cloud/firestore';
1+
import { Pool } from 'pg';
32

43
/**
54
* OAuth2 inherently requires storing access tokens, refresh tokens, and
@@ -30,7 +29,6 @@ export interface GithubData {
3029
interface StateData {
3130
codeVerifier: string;
3231
discordUserId: string;
33-
ttl?: Timestamp;
3432
}
3533

3634
export async function storeDiscordTokens(userId: string, data: DiscordData) {
@@ -88,21 +86,14 @@ export async function deleteLinkedGithubUser(discordUserId: string) {
8886
* Postgres storage provider.
8987
*/
9088
export class PostgresClient implements StorageProvider {
91-
private _client: PostgresClientType;
92-
9389
async getClient() {
94-
if (!this._client) {
95-
this._client = createClient();
96-
this._client.on('error', (err: string) => {
97-
console.log('Postgres Client Error', err);
98-
});
99-
await this._client.connect();
100-
return this._client;
101-
}
102-
if (!this._client.isOpen) {
103-
await this._client.connect();
104-
}
105-
return this._client;
90+
const pool = new Pool({
91+
user: 'me',
92+
host: 'localhost',
93+
database: 'api',
94+
password: 'password',
95+
port: 5432,
96+
});
10697
}
10798

10899
async setData(key: string, data: unknown, ttlSeconds?: number) {

0 commit comments

Comments
 (0)