forked from pinks/eris
1
0
Fork 0
eris/api/usersRoute.ts

51 lines
1.6 KiB
TypeScript
Raw Normal View History

2023-10-08 21:23:54 +00:00
import { createEndpoint, createMethodFilter, createPathFilter } from "t_rest/server";
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";
import { adminStore } from "../app/adminStore.ts";
2023-10-05 09:00:51 +00:00
2023-10-08 21:23:54 +00:00
export const usersRoute = createPathFilter({
2023-10-13 11:47:57 +00:00
"{userId}/photo": createMethodFilter({
2023-10-08 21:23:54 +00:00
GET: createEndpoint(
{ query: null, body: null },
async ({ params }) => {
2023-10-23 00:39:01 +00:00
const user = await getUser(Number(params.userId!));
const photoData = user.photo?.small_file_id
2023-10-13 11:47:57 +00:00
? await fetch(
`https://api.telegram.org/file/bot${bot.token}/${await bot.api.getFile(
2023-10-23 00:39:01 +00:00
user.photo.small_file_id,
2023-10-13 11:47:57 +00:00
).then((file) => file.file_path)}`,
).then((resp) => resp.arrayBuffer())
: undefined;
if (!photoData) {
return { status: 404, body: { type: "text/plain", data: "User has no photo" } };
}
2023-10-08 21:23:54 +00:00
return {
status: 200,
body: {
2023-10-13 11:47:57 +00:00
type: "image/jpeg",
data: new Blob([photoData], { type: "image/jpeg" }),
2023-10-10 16:21:25 +00:00
},
};
},
),
}),
2023-10-13 11:47:57 +00:00
"{userId}": createMethodFilter({
2023-10-10 16:21:25 +00:00
GET: createEndpoint(
{ query: null, body: null },
async ({ params }) => {
2023-10-23 00:39:01 +00:00
const user = await getUser(Number(params.userId!));
const [adminEntry] = await adminStore.getBy("tgUserId", { value: user.id });
const admin = adminEntry?.value;
2023-10-10 16:21:25 +00:00
return {
status: 200,
body: {
2023-10-13 11:47:57 +00:00
type: "application/json",
2023-10-23 00:39:01 +00:00
data: { ...user, admin },
2023-10-08 21:23:54 +00:00
},
};
},
),
}),
});