Skip to content

Commit 3511fae

Browse files
Merge pull request #11 from kcoderhtml/Add-endpoint-for-user-details
Add endpoint for user details
2 parents a1549f2 + f958c11 commit 3511fae

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

index.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { getPostSummaries, getPostDetail } from "./utils/vrite.ts";
44
import { getMessage } from "./utils/files.ts";
55
import { streamData } from "./utils/streaming.ts";
66
import { getGist } from "./utils/apis.ts";
7-
import { get10DaysLeaderboard } from "./utils/slack.ts";
7+
import { get10DaysLeaderboard, get10daysDetailForUser } from "./utils/slack.ts";
88

99
const app = express();
1010
const port = 3000;
@@ -57,6 +57,14 @@ app.get("/s/10daysinpublic", async (req, res) => {
5757
streamData(req, res, leaderboard);
5858
});
5959

60+
app.get("/s/10daysinpublic/:user", async (req, res) => {
61+
const userDetail = await get10daysDetailForUser(
62+
decodeURIComponent(req.params.user),
63+
);
64+
65+
streamData(req, res, userDetail);
66+
});
67+
6068
// Create server
6169
const server = http.createServer(app);
6270

utils/slack.ts

+26-1
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,36 @@ export async function get10DaysLeaderboard(start: Date, end: Date) {
9898
});
9999

100100
// display the leaderboard in markdown format
101-
const leaderboardFormatted = `# 10 Days in Public Leaderboard from ${start.toISOString().split("T")[0]} to ${end.toISOString().split("T")[0]}\n\nGood Luck and have fun!\nTime next to the checkmarks is given in h:m:s local time for that user🚀\n\n${generateLeaderboardTable(users)}\n`;
101+
const leaderboardFormatted = `# 10 Days in Public Leaderboard from ${start.toISOString().split("T")[0]} to ${end.toISOString().split("T")[0]}\n\nGood Luck and have fun!\nTime next to the checkmarks is given in h:m:s local time for that user🚀\nYou can find out specific details per user by going to https://m.kieranklukas.com/s/10daysinpublic/userid\n\n${generateLeaderboardTable(users)}\n`;
102102

103103
return leaderboardFormatted;
104104
}
105105

106+
export async function get10daysDetailForUser(user: string) {
107+
const response = (
108+
await fetch("https://scrapbook.hackclub.com/api/r/10daysinpublic")
109+
).json();
110+
111+
const posts = (await response)
112+
.filter(
113+
(post: any) => post.user.username.toLowerCase() === user.toLowerCase(),
114+
)
115+
.sort((a: any, b: any) => a.timestamp - b.timestamp);
116+
117+
return posts.length > 0
118+
? `# ${user}\n` +
119+
posts.map((post: any) => {
120+
const timestampAdjusted = new Date();
121+
timestampAdjusted.setTime(
122+
(post.timestamp + post.user.timezoneOffset) * 1000,
123+
);
124+
125+
return `\n---\n${timestampAdjusted.toISOString().split("T")[0]} at ${timestampAdjusted.getUTCHours()}:${timestampAdjusted.getUTCMinutes()}:${timestampAdjusted.getUTCSeconds()} - ${post.text}`;
126+
}) +
127+
"\n---\n"
128+
: "No posts found for this user\n";
129+
}
130+
106131
export async function getSlackStatus() {
107132
// get slack status from the slack API
108133
const response = await fetch(

0 commit comments

Comments
 (0)