Skip to content

Commit f545e95

Browse files
committed
Don't hit slack on minutejob
1 parent d84553d commit f545e95

File tree

4 files changed

+22
-32
lines changed

4 files changed

+22
-32
lines changed

src/bot.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ app.action("action-waka-setup-unix", async ({ ack, body, client, logger }) => {
3434
track("action-waka-setup-unix", body.user.id);
3535

3636
const userInfo = await app.client.users.info({ user: body.user.id });
37-
const apiKeyResponse = await createWakaUser(userInfo).then((d) => d.json());
37+
const apiKeyResponse = await createWakaUser({ slackId: body.user.id }).then(
38+
(d) => d.json(),
39+
);
3840

3941
await ack();
4042

@@ -137,7 +139,9 @@ app.action(
137139
track("action-waka-setup-windows", body.user.id);
138140

139141
const userInfo = await app.client.users.info({ user: body.user.id });
140-
const apiKeyResponse = await createWakaUser(userInfo).then((d) => d.json());
142+
const apiKeyResponse = await createWakaUser({ slackId: body.user.id }).then(
143+
(d) => d.json(),
144+
);
141145

142146
await ack();
143147

src/jobs.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ export function registerJobs() {
5151
] of lastTrackedHbIds.entries()) {
5252
try {
5353
// The wakatime `time`s are in UTC. Convert them to the user's static time zone, then decide the day.
54-
const userInfo = await app.client.users.info({ user: slackId });
55-
if (!userInfo.user?.id)
56-
throw new Error(`No user found from provided Slack ID ${slackId}`);
54+
// const userInfo = await app.client.users.info({ user: slackId });
55+
// if (!userInfo.user?.id)
56+
// throw new Error(`No user found from provided Slack ID ${slackId}`);
5757

5858
const [user] =
5959
await sql`select * from users where slack_id = ${slackId}`;
@@ -87,7 +87,7 @@ export function registerJobs() {
8787
);
8888
const endOfDayUtc = new Date(endOfDayLocal.getTime() - userTzOffsetMs);
8989

90-
const { api_key } = await createWakaUser(userInfo).then((res) =>
90+
const { api_key } = await createWakaUser({ slackId }).then((res) =>
9191
res.json(),
9292
);
9393

@@ -103,18 +103,15 @@ export function registerJobs() {
103103
const summaryResJson = await summaryRes.json();
104104

105105
const secondsBefore =
106-
(await getSecondsCoded(userInfo.user.id, startOfDayUtc)) ?? 0;
106+
(await getSecondsCoded(slackId, startOfDayUtc)) ?? 0;
107107

108108
const date = startOfDayUtc.toISOString().split("T")[0];
109-
await sql`insert into user_hakatime_daily_summary (user_id, date, summary) values (${userInfo.user.id}, ${date}, ${summaryResJson}) on conflict (user_id, date) do update set summary = excluded.summary;`;
109+
await sql`insert into user_hakatime_daily_summary (user_id, date, summary) values (${slackId}, ${date}, ${summaryResJson}) on conflict (user_id, date) do update set summary = excluded.summary;`;
110110

111-
const secondsAfter = await getSecondsCoded(
112-
userInfo.user.id,
113-
startOfDayUtc,
114-
);
111+
const secondsAfter = await getSecondsCoded(slackId, startOfDayUtc);
115112

116113
app.logger.info(
117-
`Syncing ${userInfo.user.name} (${userInfo.user.id}) for ${date} before: ${secondsBefore / 60} mins, after: ${secondsAfter / 60} mins`,
114+
`Syncing(${slackId}) for ${date} before: ${secondsBefore / 60} mins, after: ${secondsAfter / 60} mins`,
118115
);
119116

120117
if (
@@ -125,7 +122,7 @@ export function registerJobs() {
125122
) {
126123
await app.client.chat.postMessage({
127124
channel: process.env.EVENT_CHANNEL!,
128-
text: `_Relieved sock noises_\n*Translation:* No lint on these toes - <@${userInfo.user.id}> just hit the 15 min mark for today!`,
125+
text: `_Relieved sock noises_\n*Translation:* No lint on these toes - <@${slackId}> just hit the 15 min mark for today!`,
129126
});
130127
}
131128
} catch (e) {

src/ui.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export async function buildSockView(
7676
const [extendedUserRow] =
7777
await sql`select users.*, clans.name as clan_name, clans.join_code from users left join clans on users.clan_id = clans.id where users.slack_id = ${slackId}`;
7878

79-
const wakaResponse = await createWakaUser(userInfo)
79+
const wakaResponse = await createWakaUser({ slackId })
8080
.then((d) => d.json())
8181
.catch((err) => app.logger.error(err));
8282

src/waka.ts

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,21 @@
1-
import type { UsersInfoResponse } from "@slack/web-api";
21
import { sql } from "bun";
32
import { hackSql } from "./db";
43

5-
export async function createWakaUser(userInfo: UsersInfoResponse) {
4+
export async function createWakaUser({ slackId }: { slackId: string }) {
65
if (!process.env.WAKA_API_KEY) {
76
console.error("Env var WAKA_API_KEY not set. Exiting.");
87
process.exit();
98
}
109

11-
if (
12-
!userInfo.ok ||
13-
!userInfo.user ||
14-
!userInfo.user.id ||
15-
!userInfo.user.name
16-
) {
17-
throw new Error("Invalid user info");
18-
}
19-
20-
const [userRow] =
21-
await sql`select hakatime_password from users where slack_id = ${userInfo.user.id};`;
10+
const [userRow] = await sql`select * from users where slack_id = ${slackId};`;
2211

2312
const payload = {
24-
location: userInfo.user.tz ?? "Factory",
25-
email: "[email protected]", //userInfo.user.profile?.email ?? "",
13+
location: userRow.tz ?? "Factory",
14+
email: userRow.email ?? "",
2615
password: userRow.hakatime_password,
2716
password_repeat: userRow.hakatime_password,
28-
name: userInfo.user.name,
29-
username: userInfo.user.id,
17+
name: userRow.real_name,
18+
username: userRow.slack_id,
3019
};
3120

3221
return await fetch("https://waka.hackclub.com/signup", {

0 commit comments

Comments
 (0)