Repository navigation
Expand file tree
/
Copy pathbuttons.ts
More file actions
65 lines (57 loc) · 1.91 KB
/
Copy pathbuttons.ts
File metadata and controls
65 lines (57 loc) · 1.91 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
import type { Bot } from "gramio";
import { InlineKeyboard } from "gramio";
import type { Env } from "../env";
import * as d1 from "../storage/d1";
export async function addButtonToContent(
env: Env,
contentId: string,
text: string,
url: string,
) {
const existing = await d1.listButtons(env, contentId);
return d1.addButton(env, contentId, text, url, existing.length);
}
export function buildKeyboard(buttons: d1.ButtonRow[]): InlineKeyboard | undefined {
if (buttons.length === 0) return undefined;
let kb = new InlineKeyboard();
for (const b of buttons) {
kb = kb.url(b.text, b.url).row();
}
return kb;
}
// Changes a button's URL in D1, then edits every previously published
// message's reply markup so old posts point at the new URL too — no new
// post required (spec section 15).
export async function changeButtonUrl(
bot: Bot,
env: Env,
buttonId: string,
newUrl: string,
): Promise<{ updated: number; contentId: string } | null> {
const button = await d1.getButton(env, buttonId);
if (!button) return null;
await d1.updateButtonUrl(env, buttonId, newUrl);
const [buttons, publishedMessages] = await Promise.all([
d1.listButtons(env, button.contentId),
d1.listPublishedMessages(env, button.contentId),
]);
const keyboard = buildKeyboard(buttons);
let updated = 0;
for (const msg of publishedMessages) {
try {
await bot.api.editMessageReplyMarkup({
chat_id: msg.chatId,
message_id: msg.messageId,
reply_markup: keyboard,
});
updated++;
} catch (err) {
console.error(`Failed to update message ${msg.messageId} in ${msg.chatId}:`, err);
}
}
return { updated, contentId: button.contentId };
}
export function formatButtonList(buttons: d1.ButtonRow[]): string {
if (buttons.length === 0) return "No buttons on this content yet.";
return buttons.map((b) => `• ${b.id} — "${b.text}" → ${b.url}`).join("\n");
}