mirror of
https://github.com/Elysola/WebHooker.git
synced 2026-11-21 01:23:40 +00:00
fix(discord): reuse nonces after lost responses
A successful create may lose its response. Reuse a stable nonce with enforce_nonce so retries recover that message instead of posting duplicates or accepting an unreadable success.
This commit is contained in:
1 parent
966403bdc1
commit
7cf507f654
3 files changed
+28
-11
No files matched your search
@@ -12,7 +12,13 @@ export class DiscordDriver implements PlatformDriver {
|
|||||||
return { ok: false, error: "target.channelId is required", errorCode: "NO_TARGET" };
|
return { ok: false, error: "target.channelId is required", errorCode: "NO_TARGET" };
|
||||||
}
|
}
|
||||||
const token = env.DISCORD_TOKEN ?? "";
|
const token = env.DISCORD_TOKEN ?? "";
|
||||||
return sendMessage(token, channelId, renderNeutralMessage(message), target.threadId);
|
return sendMessage(
|
||||||
|
token,
|
||||||
|
channelId,
|
||||||
|
renderNeutralMessage(message),
|
||||||
|
target.threadId,
|
||||||
|
message.sendKey,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
async edit(
|
async edit(
|
||||||
|
|||||||
@@ -81,15 +81,11 @@ async function request(
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
let messageId: string | undefined;
|
// A success whose body cannot be read (cut off, timed out) is not known
|
||||||
try {
|
// to have happened: tried again — a create carries its nonce, so it is
|
||||||
const data = (await res.json()) as DiscordMessage;
|
// never doubled.
|
||||||
messageId = data.id;
|
const data = (await res.json()) as DiscordMessage | null;
|
||||||
} catch {
|
return { ok: true, status: res.status, messageId: data?.id, attempts: attempt + 1 };
|
||||||
// ignore malformed success body
|
|
||||||
}
|
|
||||||
|
|
||||||
return { ok: true, status: res.status, messageId, attempts: attempt + 1 };
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
log.error({ err, attempt, channelId }, "Failed to send message");
|
log.error({ err, attempt, channelId }, "Failed to send message");
|
||||||
if (attempt === 2)
|
if (attempt === 2)
|
||||||
@@ -106,11 +102,19 @@ export async function sendMessage(
|
|||||||
channelId: string,
|
channelId: string,
|
||||||
message: unknown,
|
message: unknown,
|
||||||
threadId?: string,
|
threadId?: string,
|
||||||
|
sendKey?: string,
|
||||||
): Promise<SendResult> {
|
): Promise<SendResult> {
|
||||||
// Ids are path segments: encoded, so no stored value can address another
|
// Ids are path segments: encoded, so no stored value can address another
|
||||||
// resource (`x/../123` is never the channel `123`).
|
// resource (`x/../123` is never the channel `123`).
|
||||||
const url = `${DISCORD_API}/channels/${encodeURIComponent(threadId || channelId)}/messages`;
|
const url = `${DISCORD_API}/channels/${encodeURIComponent(threadId || channelId)}/messages`;
|
||||||
return request(url, "POST", token, message, channelId);
|
// One nonce for every attempt of this send — and, given a `sendKey`, for
|
||||||
|
// every attempt of the delivery: when a create went through but its
|
||||||
|
// response was lost, Discord answers the retry with that same message
|
||||||
|
// instead of posting a second one (`enforce_nonce`, a few minutes' window).
|
||||||
|
const nonce = (sendKey ?? crypto.randomUUID().replace(/-/g, "")).slice(0, 25);
|
||||||
|
const body =
|
||||||
|
message && typeof message === "object" ? { ...message, nonce, enforce_nonce: true } : message;
|
||||||
|
return request(url, "POST", token, body, channelId);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function editMessage(
|
export async function editMessage(
|
||||||
|
|||||||
@@ -326,6 +326,13 @@ export interface NeutralMessage {
|
|||||||
* only to find a message tracked under the old key (then re-keyed).
|
* only to find a message tracked under the old key (then re-keyed).
|
||||||
*/
|
*/
|
||||||
legacyUpdateKey?: string;
|
legacyUpdateKey?: string;
|
||||||
|
/**
|
||||||
|
* Stable identity of this message for one target, the same in every
|
||||||
|
* attempt of a delivery (set by dispatch from delivery × route × target).
|
||||||
|
* Platforms with request idempotency (Discord `nonce`, Feishu `uuid`) use
|
||||||
|
* it, so a retry after a lost response never posts the message twice.
|
||||||
|
*/
|
||||||
|
sendKey?: string;
|
||||||
/**
|
/**
|
||||||
* Discord role ids to mention in the message content (set by dispatch from
|
* Discord role ids to mention in the message content (set by dispatch from
|
||||||
* the route's `discordRoleIds`). Only used by the Discord driver.
|
* the route's `discordRoleIds`). Only used by the Discord driver.
|
||||||
|
|||||||
Reference in new issue
Block a user