Skip to content

Commit

Permalink
fit: switch to drip system
Browse files Browse the repository at this point in the history
  • Loading branch information
hellos3b committed Jan 9, 2024
1 parent f1746f2 commit 29b5415
Show file tree
Hide file tree
Showing 17 changed files with 9,878 additions and 948 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ report*json
.merlin
bot/lib
.bsb.lock
*.bs.js
*.bs.js
packages/*/lib
3 changes: 0 additions & 3 deletions .pnpm-workspace.yaml

This file was deleted.

2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit"
},
"editor.formatOnSave": false,
"editor.formatOnSave": true,
"typescript.implementationsCodeLens.enabled": true,
"typescript.referencesCodeLens.enabled": true
}
3 changes: 2 additions & 1 deletion bot/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"brace-style": ["warn", "stroustrup", { "allowSingleLine": true }],
"no-unused-vars": "off",
"@typescript-eslint/no-namespace": 0,
"@typescript-eslint/no-unused-vars": ["warn", { "argsIgnorePattern": "^_", "varsIgnorePattern": "^_" }]
"@typescript-eslint/no-unused-vars": ["warn", { "argsIgnorePattern": "^_", "varsIgnorePattern": "^_" }],
"@typescript-eslint/ban-ts-comment": "off"
}
}
1 change: 1 addition & 0 deletions bot/error.log
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Unknown
3 changes: 2 additions & 1 deletion bot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"@bored-bot/fitness": "workspace:^",
"@discordjs/builders": "^0.15.0",
"@discordjs/rest": "^0.5.0",
"@hapi/boom": "^9.1.2",
Expand All @@ -25,7 +26,7 @@
"chalk": "4",
"date-fns": "^2.28.0",
"discord-api-types": "^0.36.2",
"discord.js": "^14.8.0",
"discord.js": "^14.10.2",
"dotenv": "^10.0.0",
"fromnow": "^3.0.1",
"logfmt": "^1.3.2",
Expand Down
112 changes: 56 additions & 56 deletions bot/src/commands/fit/ActivityWebhook.ts
Original file line number Diff line number Diff line change
@@ -1,69 +1,69 @@
import Hapi from "@hapi/hapi";
import * as Discord from "discord.js";
import { env } from "../../environment";
import { logger } from "../../logger";
// import Hapi from "@hapi/hapi";
// import * as Discord from "discord.js";
// import { env } from "../../environment";
// import { logger } from "../../logger";

import * as WorkoutEmbed from "./WorkoutEmbed";
// import * as WorkoutEmbed from "./WorkoutEmbed";

// Whenever an activity is created/updated/deleted,
// we get notified via a webhook with this event data
export type event = {
aspect_type: "create" | "update" | "delete";
object_type: "activity" | "athlete";
// The ID of what was updated
object_id: number;
// Athlete ID
owner_id: number;
}
// // Whenever an activity is created/updated/deleted,
// // we get notified via a webhook with this event data
// export type event = {
// aspect_type: "create" | "update" | "delete";
// object_type: "activity" | "athlete";
// // The ID of what was updated
// object_id: number;
// // Athlete ID
// owner_id: number;
// }

const log = logger ("fit:activity-webhook");
// const log = logger ("fit:activity-webhook");

/** Simple util that just resolves a promise after a certain amount of milliseconds */
const wait = (ms: number) : Promise<void> => new Promise (resolve => setTimeout (resolve, ms));
// /** Simple util that just resolves a promise after a certain amount of milliseconds */
// const wait = (ms: number) : Promise<void> => new Promise (resolve => setTimeout (resolve, ms));

// When an activity is first posted as 'created',
// We'll give the user some (n) amount of time to edit their activity
// This set just keeps track of which activities are waiting to be posted
const pending = new Set<number> ();
// // When an activity is first posted as 'created',
// // We'll give the user some (n) amount of time to edit their activity
// // This set just keeps track of which activities are waiting to be posted
// const pending = new Set<number> ();

// Adds a small buffer to the first post
const post = async (client: Discord.Client, athleteId: number, activityId: number, delay = 0) => {
// Skip if we're already awaiting on this activity
if (pending.has (activityId))
return;
// // Adds a small buffer to the first post
// const post = async (client: Discord.Client, athleteId: number, activityId: number, delay = 0) => {
// // Skip if we're already awaiting on this activity
// if (pending.has (activityId))
// return;

pending.add (activityId);
// pending.add (activityId);

try {
delay && await wait (delay);
await WorkoutEmbed.post (client, athleteId, activityId);
}
catch (error) {
log.error ("Workout failed to post", error);
}
finally {
pending.delete (activityId);
}
};
// try {
// delay && await wait (delay);
// await WorkoutEmbed.post (client, athleteId, activityId);
// }
// catch (error) {
// log.error ("Workout failed to post", error);
// }
// finally {
// pending.delete (activityId);
// }
// };

export const handleEvent = (client: Discord.Client) => async (req: Hapi.Request) : Promise<string> => {
const params = req.payload as event;
log.info ("Strava Webhook Request", params);
// export const handleEvent = (client: Discord.Client) => async (req: Hapi.Request) : Promise<string> => {
// const params = req.payload as event;
// log.info ("Strava Webhook Request", params);

if (params.object_type === "athlete") {
log.debug ("Ignoring athlete update");
return "";
}
// if (params.object_type === "athlete") {
// log.debug ("Ignoring athlete update");
// return "";
// }

// Do not have a current feature to delete
// a posted workout, but it's on the TODO list
if (params.aspect_type === "delete") {
log.debug ("No support for DELETE");
return "";
}
// // Do not have a current feature to delete
// // a posted workout, but it's on the TODO list
// if (params.aspect_type === "delete") {
// log.debug ("No support for DELETE");
// return "";
// }

const delay = (env.NODE_ENV === "production" && params.aspect_type === "create") ? 60 * 1000 : 0;
post (client, params.owner_id, params.object_id, delay);
// const delay = (env.NODE_ENV === "production" && params.aspect_type === "create") ? 60 * 1000 : 0;
// post (client, params.owner_id, params.object_id, delay);

return "Done!";
};
// return "Done!";
// };
11 changes: 8 additions & 3 deletions bot/src/commands/fit/Fit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@ import * as Settings from "./Settings";
import * as Admin from "./Admin";
import * as UserAuth from "./UserAuth";
import * as UserProfile from "./UserProfile";
import * as ActivityWebhook from "./ActivityWebhook";
// import * as ActivityWebhook from "./ActivityWebhook";
import * as Promotions from "./Promotions";
import * as Leaders from "./Leaders";
import * as LastActive from "./LastActive";

// @ts-ignore
import * as Fit2 from "@bored-bot/fitness";

import { env } from "../../environment";
import { getInstance } from "../../deprecating/legacy_instance";

const { Filter } = Command;

Expand Down Expand Up @@ -46,7 +51,7 @@ const fit = Command.filtered ({
.with ("help", () => message.channel.send (help))
.with ("settings", () => message.reply ("Settings menu is available only in DMs"))
.with (__.nullish, () => message.channel.send (help))
.run ()
.otherwise (() => message.reply ("Unrecognized command"))
});

const settings = Command.filtered ({
Expand Down Expand Up @@ -107,7 +112,7 @@ export const routes = (client: Discord.Client) => [
{
method: "POST",
path: "/fit/api/webhook",
handler: ActivityWebhook.handleEvent (client)
handler: req => Fit2.stravaWebhookHandler (client, getInstance ().mongodb) (req)
}
];

Expand Down
Loading

0 comments on commit 29b5415

Please sign in to comment.