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

55 lines
1.7 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 { getConfig } from "../app/config.ts";
import { bot } from "../bot/mod.ts";
2023-10-08 21:23:54 +00:00
export const usersRoute = createPathFilter({
"{userId}": createMethodFilter({
GET: createEndpoint(
{ query: null, body: null },
async ({ params }) => {
const chat = await bot.api.getChat(params.userId);
if (chat.type !== "private") {
throw new Error("Chat is not private");
}
const config = await getConfig();
2023-10-10 16:21:25 +00:00
const isAdmin = chat.username && config?.adminUsernames?.includes(chat.username);
2023-10-08 21:23:54 +00:00
return {
status: 200,
body: {
type: "application/json",
2023-10-10 16:21:25 +00:00
data: { ...chat, isAdmin },
},
};
},
),
}),
"{userId}/photo": createMethodFilter({
GET: createEndpoint(
{ query: null, body: null },
async ({ params }) => {
const chat = await bot.api.getChat(params.userId);
if (chat.type !== "private") {
throw new Error("Chat is not private");
}
const photoData = chat.photo?.small_file_id
? await fetch(
`https://api.telegram.org/file/bot${bot.token}/${await bot.api.getFile(
chat.photo.small_file_id,
).then((file) => file.file_path)}`,
).then((resp) => resp.arrayBuffer())
: undefined;
if (!photoData) {
return { status: 404, body: { type: "text/plain", data: "User has no photo" } };
}
return {
status: 200,
body: {
type: "image/jpeg",
data: new Blob([photoData], { type: "image/jpeg" }),
2023-10-08 21:23:54 +00:00
},
};
},
),
}),
});