Skip to content

Commit 745b9a9

Browse files
steipetePeter Steinberger
andauthored
fix(telegram): canonicalize topic mutation targets (#117001)
Co-authored-by: Peter Steinberger <[email protected]>
1 parent c572982 commit 745b9a9

2 files changed

Lines changed: 120 additions & 54 deletions

File tree

‎extensions/telegram/src/send-actions.ts‎

Lines changed: 30 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { isRecoverableTelegramNetworkError } from "./network-errors.js";
77
import {
88
createTelegramRequestWithDiag,
99
isTelegramMessageDeleteNoopError,
10-
normalizeMessageId,
1110
resolveAndPersistChatId,
1211
resolveTelegramApiContext,
1312
withTelegramApiContextLease,
@@ -102,23 +101,16 @@ async function reactMessageTelegramWithContext(
102101
opts: TelegramReactionOpts,
103102
context: TelegramApiContext,
104103
): Promise<{ ok: true } | { ok: false; warning: string }> {
105-
const { cfg, account, api } = context;
106-
const rawTarget = String(chatIdInput);
107-
const chatId = await resolveAndPersistChatId({
108-
cfg,
109-
api,
110-
lookupTarget: rawTarget,
111-
persistTarget: rawTarget,
112-
verbose: opts.verbose,
113-
gatewayClientScopes: opts.gatewayClientScopes,
114-
});
115-
const messageId = normalizeMessageId(messageIdInput);
116-
const requestWithDiag = createTelegramRequestWithDiag({
117-
cfg,
118-
account,
119-
retry: opts.retry,
120-
verbose: opts.verbose,
121-
shouldRetry: (err) => isRecoverableTelegramNetworkError(err, { context: "react" }),
104+
const { api } = context;
105+
const { chatId, messageId, request } = await prepareTelegramOutbound({
106+
to: chatIdInput,
107+
context,
108+
opts,
109+
messageIdInput,
110+
request: {
111+
kind: "standard",
112+
shouldRetry: (err) => isRecoverableTelegramNetworkError(err, { context: "react" }),
113+
},
122114
});
123115
const remove = opts.remove === true;
124116
const trimmedEmoji = emoji.trim();
@@ -132,7 +124,7 @@ async function reactMessageTelegramWithContext(
132124
throw new Error("Telegram reactions are unavailable in this bot API.");
133125
}
134126
try {
135-
await requestWithDiag(() => api.setMessageReaction(chatId, messageId, reactions), "reaction");
127+
await request(() => api.setMessageReaction(chatId, messageId, reactions), "reaction");
136128
} catch (err: unknown) {
137129
const msg = formatErrorMessage(err);
138130
if (/REACTION_INVALID/i.test(msg)) {
@@ -172,26 +164,19 @@ async function deleteMessageTelegramWithContext(
172164
opts: TelegramDeleteOpts,
173165
context: TelegramApiContext,
174166
): Promise<{ ok: true } | { ok: false; warning: string }> {
175-
const { cfg, account, api } = context;
176-
const rawTarget = String(chatIdInput);
177-
const chatId = await resolveAndPersistChatId({
178-
cfg,
179-
api,
180-
lookupTarget: rawTarget,
181-
persistTarget: rawTarget,
182-
verbose: opts.verbose,
183-
gatewayClientScopes: opts.gatewayClientScopes,
184-
});
185-
const messageId = normalizeMessageId(messageIdInput);
186-
const requestWithDiag = createTelegramRequestWithDiag({
187-
cfg,
188-
account,
189-
retry: opts.retry,
190-
verbose: opts.verbose,
191-
shouldRetry: (err) => isRecoverableTelegramNetworkError(err, { context: "delete" }),
167+
const { api } = context;
168+
const { chatId, messageId, request } = await prepareTelegramOutbound({
169+
to: chatIdInput,
170+
context,
171+
opts,
172+
messageIdInput,
173+
request: {
174+
kind: "standard",
175+
shouldRetry: (err) => isRecoverableTelegramNetworkError(err, { context: "delete" }),
176+
},
192177
});
193178
try {
194-
await requestWithDiag(() => api.deleteMessage(chatId, messageId), "deleteMessage", {
179+
await request(() => api.deleteMessage(chatId, messageId), "deleteMessage", {
195180
shouldLog: (err) => !isTelegramMessageDeleteNoopError(err),
196181
});
197182
} catch (err: unknown) {
@@ -264,24 +249,15 @@ async function unpinMessageTelegramWithContext(
264249
opts: TelegramDeleteOpts,
265250
context: TelegramApiContext,
266251
): Promise<{ ok: true; chatId: string; messageId?: string }> {
267-
const { cfg, account, api } = context;
268-
const rawTarget = String(chatIdInput);
269-
const chatId = await resolveAndPersistChatId({
270-
cfg,
271-
api,
272-
lookupTarget: rawTarget,
273-
persistTarget: rawTarget,
274-
verbose: opts.verbose,
275-
gatewayClientScopes: opts.gatewayClientScopes,
276-
});
277-
const messageId = messageIdInput === undefined ? undefined : normalizeMessageId(messageIdInput);
278-
const requestWithDiag = createTelegramRequestWithDiag({
279-
cfg,
280-
account,
281-
retry: opts.retry,
282-
verbose: opts.verbose,
252+
const { api } = context;
253+
const { chatId, messageId, request } = await prepareTelegramOutbound({
254+
to: chatIdInput,
255+
context,
256+
opts,
257+
...(messageIdInput !== undefined ? { messageIdInput } : {}),
258+
request: { kind: "standard" },
283259
});
284-
await requestWithDiag(() => api.unpinChatMessage(chatId, messageId), "unpinChatMessage");
260+
await request(() => api.unpinChatMessage(chatId, messageId), "unpinChatMessage");
285261
logVerbose(
286262
`[telegram] Unpinned ${messageId != null ? `message ${messageId}` : "active message"} in chat ${chatId}`,
287263
);
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { describe, expect, it, vi } from "vitest";
2+
import {
3+
getTelegramSendTestMocks,
4+
importTelegramSendModule,
5+
installTelegramSendTestHooks,
6+
} from "./send.test-harness.js";
7+
8+
installTelegramSendTestHooks();
9+
10+
const { botApi, maybePersistResolvedTelegramTarget } = getTelegramSendTestMocks();
11+
const { deleteMessageTelegram, pinMessageTelegram, reactMessageTelegram, unpinMessageTelegram } =
12+
await importTelegramSendModule();
13+
14+
const chatId = "-1001234567890";
15+
const messageId = 321;
16+
const getChat = vi.fn(async () => ({ id: Number(chatId) }));
17+
const opts = {
18+
cfg: {},
19+
token: "tok",
20+
accountId: "default",
21+
api: { ...botApi, getChat } as unknown as Parameters<typeof reactMessageTelegram>[3]["api"],
22+
gatewayClientScopes: ["operator.write"],
23+
};
24+
25+
const mutations = [
26+
{
27+
name: "reaction",
28+
run: (target: string) => reactMessageTelegram(target, messageId, "✅", opts),
29+
assertCall: () =>
30+
expect(botApi.setMessageReaction).toHaveBeenCalledWith(chatId, messageId, [
31+
{ type: "emoji", emoji: "✅" },
32+
]),
33+
},
34+
{
35+
name: "delete",
36+
run: (target: string) => deleteMessageTelegram(target, messageId, opts),
37+
assertCall: () => expect(botApi.deleteMessage).toHaveBeenCalledWith(chatId, messageId),
38+
},
39+
{
40+
name: "pin",
41+
run: (target: string) => pinMessageTelegram(target, messageId, opts),
42+
assertCall: () =>
43+
expect(botApi.pinChatMessage).toHaveBeenCalledWith(chatId, messageId, {
44+
disable_notification: true,
45+
}),
46+
},
47+
{
48+
name: "unpin",
49+
run: (target: string) => unpinMessageTelegram(target, messageId, opts),
50+
assertCall: () => expect(botApi.unpinChatMessage).toHaveBeenCalledWith(chatId, messageId),
51+
},
52+
{
53+
name: "unpin without an explicit message",
54+
run: (target: string) => unpinMessageTelegram(target, undefined, opts),
55+
assertCall: () => expect(botApi.unpinChatMessage).toHaveBeenCalledWith(chatId, undefined),
56+
},
57+
] as const;
58+
59+
describe("Telegram topic-qualified message mutations", () => {
60+
it.each(
61+
mutations.flatMap((mutation) => [
62+
{ ...mutation, target: `${chatId}:topic:77` },
63+
{ ...mutation, target: `telegram:group:${chatId}:topic:77` },
64+
{ ...mutation, target: "@mychannel:topic:77" },
65+
]),
66+
)(
67+
"routes $name mutations for $target to their base chat",
68+
async ({ run, assertCall, target }) => {
69+
botApi.setMessageReaction.mockResolvedValue(true);
70+
botApi.deleteMessage.mockResolvedValue(true);
71+
botApi.pinChatMessage.mockResolvedValue(true);
72+
botApi.unpinChatMessage.mockResolvedValue(true);
73+
getChat.mockClear();
74+
75+
await run(target);
76+
77+
assertCall();
78+
expect(maybePersistResolvedTelegramTarget).toHaveBeenCalledWith(
79+
expect.objectContaining({
80+
rawTarget: target,
81+
resolvedChatId: chatId,
82+
gatewayClientScopes: ["operator.write"],
83+
}),
84+
);
85+
if (target.startsWith("@")) {
86+
expect(getChat).toHaveBeenCalledWith("@mychannel");
87+
}
88+
},
89+
);
90+
});

0 commit comments

Comments
 (0)