-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcallbacks.js
More file actions
80 lines (70 loc) · 2.9 KB
/
Copy pathcallbacks.js
File metadata and controls
80 lines (70 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { telegram } from "../telegram.js";
import { Content } from "../modules/content.js";
import { Community } from "../modules/community.js";
import { Support } from "../modules/support.js";
import { isAdminOrOwner } from "../kv/config.js";
import { validateCallbackIds } from "../utils/validate.js";
// Callback prefixes that require Owner/Admin before executing.
const PRIVILEGED_PREFIXES = ["menu:content", "menu:community", "join:", "support:resolve", "content:"];
export async function handleCallback(update, env) {
const cq = update.callback_query;
if (!cq) return;
const data = cq.data || "";
const chatId = cq.message?.chat?.id;
const userId = cq.from?.id;
const requiresAuth = PRIVILEGED_PREFIXES.some((prefix) => data.startsWith(prefix));
if (requiresAuth && !(await isAdminOrOwner(userId, env))) {
await telegram.answerCallbackQuery(cq.id, env, { text: "Not authorized.", show_alert: true });
return;
}
try {
if (data === "menu:content") {
await Content.showMenu(chatId, env);
} else if (data === "menu:community") {
await telegram.sendMessage(chatId, "Community join requests are routed to admins automatically.", env);
} else if (data === "menu:support") {
await Support.start(chatId, env);
} else if (data === "menu:close") {
if (cq.message?.message_id) {
await telegram.editMessageText(chatId, cq.message.message_id, "Closed.", env);
}
} else if (data.startsWith("join:approve:") || data.startsWith("join:reject:")) {
await handleJoinDecision(data, chatId, cq, env);
} else if (data.startsWith("support:resolve:")) {
await handleTicketResolve(data, chatId, userId, cq, env);
} else if (data.startsWith("content:")) {
await telegram.sendMessage(chatId, "This part of Content management is coming soon.", env);
}
await telegram.answerCallbackQuery(cq.id, env);
} catch (err) {
await telegram.answerCallbackQuery(cq.id, env, { text: "Something went wrong.", show_alert: true });
throw err;
}
}
async function handleJoinDecision(data, chatId, cq, env) {
const [, action, rawChatId, rawUserId] = data.split(":");
const { joinChatId, joinUserId } = validateCallbackIds({
joinChatId: rawChatId,
joinUserId: rawUserId,
});
if (action === "approve") {
await Community.approve(joinChatId, joinUserId, env);
} else {
await Community.reject(joinChatId, joinUserId, env);
}
if (cq.message?.message_id) {
await telegram.editMessageText(
chatId,
cq.message.message_id,
`Join request ${action === "approve" ? "approved" : "rejected"}.`,
env
);
}
}
async function handleTicketResolve(data, chatId, userId, cq, env) {
const [, , ticketId] = data.split(":");
await Support.resolve(ticketId, "", userId, env);
if (cq.message?.message_id) {
await telegram.editMessageText(chatId, cq.message.message_id, `Ticket #${ticketId} marked resolved.`, env);
}
}