2023-11-17 23:52:51 +00:00
|
|
|
import { Elysia, t } from "elysia";
|
|
|
|
import { adminStore } from "../app/adminStore.ts";
|
2023-10-05 09:00:51 +00:00
|
|
|
import { bot } from "../bot/mod.ts";
|
2023-10-23 00:39:01 +00:00
|
|
|
import { getUser } from "./withUser.ts";
|
2023-10-05 09:00:51 +00:00
|
|
|
|
2023-11-17 23:52:51 +00:00
|
|
|
export const usersRoute = new Elysia()
|
|
|
|
.get(
|
|
|
|
"/:userId/photo",
|
|
|
|
async ({ params }) => {
|
|
|
|
const user = await getUser(Number(params.userId));
|
|
|
|
if (!user.photo) {
|
|
|
|
throw new Error("User has no photo");
|
|
|
|
}
|
|
|
|
const photoFile = await bot.api.getFile(user.photo.small_file_id);
|
|
|
|
const photoData = await fetch(
|
|
|
|
`https://api.telegram.org/file/bot${bot.token}/${photoFile.file_path}`,
|
|
|
|
).then((resp) => {
|
|
|
|
if (!resp.ok) {
|
|
|
|
throw new Error("Failed to fetch photo");
|
2023-10-13 11:47:57 +00:00
|
|
|
}
|
2023-11-17 23:52:51 +00:00
|
|
|
return resp;
|
|
|
|
}).then((resp) => resp.arrayBuffer());
|
2023-10-13 11:47:57 +00:00
|
|
|
|
2023-11-17 23:52:51 +00:00
|
|
|
return new Response(new File([photoData], "avatar.jpg", { type: "image/jpeg" }));
|
|
|
|
},
|
|
|
|
{
|
|
|
|
params: t.Object({ userId: t.String() }),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.get(
|
|
|
|
"/:userId",
|
|
|
|
async ({ params }) => {
|
|
|
|
const user = await getUser(Number(params.userId));
|
|
|
|
const [adminEntry] = await adminStore.getBy("tgUserId", { value: user.id });
|
|
|
|
const admin = adminEntry?.value;
|
|
|
|
return {
|
|
|
|
id: user.id,
|
|
|
|
first_name: user.first_name,
|
|
|
|
last_name: user.last_name ?? null,
|
|
|
|
username: user.username ?? null,
|
|
|
|
bio: user.bio ?? null,
|
|
|
|
admin: admin ?? null,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
{
|
|
|
|
params: t.Object({ userId: t.String() }),
|
|
|
|
response: t.Object({
|
|
|
|
id: t.Number(),
|
|
|
|
first_name: t.String(),
|
|
|
|
last_name: t.Nullable(t.String()),
|
|
|
|
username: t.Nullable(t.String()),
|
|
|
|
bio: t.Nullable(t.String()),
|
|
|
|
admin: t.Nullable(t.Object({
|
|
|
|
tgUserId: t.Number(),
|
|
|
|
promotedBy: t.Nullable(t.String()),
|
|
|
|
})),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
);
|