Skip to content

Commit 94df665

Browse files
committed
refactor: share Discord outbound payload options
1 parent 7c1484d commit 94df665

1 file changed

Lines changed: 68 additions & 76 deletions

File tree

extensions/discord/src/outbound-payload.ts

Lines changed: 68 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,61 @@ import { createDiscordPayloadSendContext } from "./outbound-send-context.js";
1717
import { createDiscordSendReceipt } from "./send.receipt.js";
1818
import type { DiscordSendComponents, DiscordSendEmbeds } from "./send.shared.js";
1919

20+
type DiscordOutboundPayloadContext = Parameters<
21+
NonNullable<ChannelOutboundAdapter["sendPayload"]>
22+
>[0];
23+
type DiscordPayloadSendContext = Awaited<ReturnType<typeof createDiscordPayloadSendContext>>;
24+
25+
function createDiscordUnknownPayloadResult(target: string) {
26+
return {
27+
messageId: "",
28+
channelId: target,
29+
receipt: createDiscordSendReceipt({
30+
platformMessageIds: [],
31+
channelId: target,
32+
kind: "unknown",
33+
}),
34+
};
35+
}
36+
37+
function resolveDiscordDeliveryOptions(
38+
ctx: DiscordOutboundPayloadContext,
39+
sendContext: DiscordPayloadSendContext,
40+
) {
41+
return {
42+
replyTo: sendContext.resolveReplyTo(),
43+
accountId: ctx.accountId ?? undefined,
44+
silent: ctx.silent ?? undefined,
45+
cfg: ctx.cfg,
46+
};
47+
}
48+
49+
function resolveDiscordFormattedDeliveryOptions(
50+
ctx: DiscordOutboundPayloadContext,
51+
sendContext: DiscordPayloadSendContext,
52+
) {
53+
return {
54+
...resolveDiscordDeliveryOptions(ctx, sendContext),
55+
...sendContext.formatting,
56+
};
57+
}
58+
59+
function resolveDiscordMediaDeliveryOptions(
60+
ctx: DiscordOutboundPayloadContext,
61+
sendContext: DiscordPayloadSendContext,
62+
mediaUrl: string,
63+
) {
64+
return {
65+
mediaUrl,
66+
mediaAccess: ctx.mediaAccess,
67+
mediaLocalRoots: ctx.mediaLocalRoots,
68+
mediaReadFile: ctx.mediaReadFile,
69+
...resolveDiscordFormattedDeliveryOptions(ctx, sendContext),
70+
};
71+
}
72+
2073
export async function sendDiscordOutboundPayload(params: {
21-
ctx: Parameters<NonNullable<ChannelOutboundAdapter["sendPayload"]>>[0];
74+
ctx: DiscordOutboundPayloadContext;
2275
fallbackAdapter: ChannelOutboundAdapter;
2376
}): Promise<Awaited<ReturnType<NonNullable<ChannelOutboundAdapter["sendPayload"]>>>> {
2477
const ctx = params.ctx;
@@ -32,23 +85,18 @@ export async function sendDiscordOutboundPayload(params: {
3285
if (payload.audioAsVoice && mediaUrls.length > 0) {
3386
let lastResult = await sendContext.withRetry(
3487
async () =>
35-
await sendContext.sendVoice(sendContext.target, mediaUrls[0], {
36-
cfg: ctx.cfg,
37-
replyTo: sendContext.resolveReplyTo(),
38-
accountId: ctx.accountId ?? undefined,
39-
silent: ctx.silent ?? undefined,
40-
}),
88+
await sendContext.sendVoice(
89+
sendContext.target,
90+
mediaUrls[0],
91+
resolveDiscordDeliveryOptions(ctx, sendContext),
92+
),
4193
);
4294
if (payload.text?.trim()) {
4395
lastResult = await sendContext.withRetry(
4496
async () =>
4597
await sendContext.send(sendContext.target, payload.text, {
4698
verbose: false,
47-
replyTo: sendContext.resolveReplyTo(),
48-
accountId: ctx.accountId ?? undefined,
49-
silent: ctx.silent ?? undefined,
50-
cfg: ctx.cfg,
51-
...sendContext.formatting,
99+
...resolveDiscordFormattedDeliveryOptions(ctx, sendContext),
52100
}),
53101
);
54102
}
@@ -57,15 +105,7 @@ export async function sendDiscordOutboundPayload(params: {
57105
async () =>
58106
await sendContext.send(sendContext.target, "", {
59107
verbose: false,
60-
mediaUrl,
61-
mediaAccess: ctx.mediaAccess,
62-
mediaLocalRoots: ctx.mediaLocalRoots,
63-
mediaReadFile: ctx.mediaReadFile,
64-
replyTo: sendContext.resolveReplyTo(),
65-
accountId: ctx.accountId ?? undefined,
66-
silent: ctx.silent ?? undefined,
67-
cfg: ctx.cfg,
68-
...sendContext.formatting,
108+
...resolveDiscordMediaDeliveryOptions(ctx, sendContext, mediaUrl),
69109
}),
70110
);
71111
}
@@ -91,15 +131,7 @@ export async function sendDiscordOutboundPayload(params: {
91131
const result = await sendPayloadMediaSequenceOrFallback({
92132
text: payload.text ?? "",
93133
mediaUrls,
94-
fallbackResult: {
95-
messageId: "",
96-
channelId: sendContext.target,
97-
receipt: createDiscordSendReceipt({
98-
platformMessageIds: [],
99-
channelId: sendContext.target,
100-
kind: "unknown",
101-
}),
102-
},
134+
fallbackResult: createDiscordUnknownPayloadResult(sendContext.target),
103135
sendNoMedia: async () =>
104136
await sendContext.withRetry(
105137
async () =>
@@ -108,30 +140,18 @@ export async function sendDiscordOutboundPayload(params: {
108140
components: nativeComponents,
109141
embeds,
110142
filename,
111-
replyTo: sendContext.resolveReplyTo(),
112-
accountId: ctx.accountId ?? undefined,
113-
silent: ctx.silent ?? undefined,
114-
cfg: ctx.cfg,
115-
...sendContext.formatting,
143+
...resolveDiscordFormattedDeliveryOptions(ctx, sendContext),
116144
}),
117145
),
118146
send: async ({ text, mediaUrl, isFirst }) =>
119147
await sendContext.withRetry(
120148
async () =>
121149
await sendContext.send(sendContext.target, text, {
122150
verbose: false,
123-
mediaUrl,
124-
mediaAccess: ctx.mediaAccess,
125-
mediaLocalRoots: ctx.mediaLocalRoots,
126-
mediaReadFile: ctx.mediaReadFile,
151+
...resolveDiscordMediaDeliveryOptions(ctx, sendContext, mediaUrl),
127152
components: isFirst ? nativeComponents : undefined,
128153
embeds: isFirst ? embeds : undefined,
129154
filename: isFirst ? filename : undefined,
130-
replyTo: sendContext.resolveReplyTo(),
131-
accountId: ctx.accountId ?? undefined,
132-
silent: ctx.silent ?? undefined,
133-
cfg: ctx.cfg,
134-
...sendContext.formatting,
135155
}),
136156
),
137157
});
@@ -150,56 +170,28 @@ export async function sendDiscordOutboundPayload(params: {
150170
const result = await sendPayloadMediaSequenceOrFallback({
151171
text: payload.text ?? "",
152172
mediaUrls,
153-
fallbackResult: {
154-
messageId: "",
155-
channelId: sendContext.target,
156-
receipt: createDiscordSendReceipt({
157-
platformMessageIds: [],
158-
channelId: sendContext.target,
159-
kind: "unknown",
160-
}),
161-
},
173+
fallbackResult: createDiscordUnknownPayloadResult(sendContext.target),
162174
sendNoMedia: async () =>
163175
await sendContext.withRetry(
164176
async () =>
165177
await sendDiscordComponentMessageLazy(sendContext.target, componentSpec, {
166-
replyTo: sendContext.resolveReplyTo(),
167-
accountId: ctx.accountId ?? undefined,
168-
silent: ctx.silent ?? undefined,
169-
cfg: ctx.cfg,
170-
...sendContext.formatting,
178+
...resolveDiscordFormattedDeliveryOptions(ctx, sendContext),
171179
}),
172180
),
173181
send: async ({ text, mediaUrl, isFirst }) => {
174182
if (isFirst) {
175183
return await sendContext.withRetry(
176184
async () =>
177185
await sendDiscordComponentMessageLazy(sendContext.target, componentSpec, {
178-
mediaUrl,
179-
mediaAccess: ctx.mediaAccess,
180-
mediaLocalRoots: ctx.mediaLocalRoots,
181-
mediaReadFile: ctx.mediaReadFile,
182-
replyTo: sendContext.resolveReplyTo(),
183-
accountId: ctx.accountId ?? undefined,
184-
silent: ctx.silent ?? undefined,
185-
cfg: ctx.cfg,
186-
...sendContext.formatting,
186+
...resolveDiscordMediaDeliveryOptions(ctx, sendContext, mediaUrl),
187187
}),
188188
);
189189
}
190190
return await sendContext.withRetry(
191191
async () =>
192192
await sendContext.send(sendContext.target, text, {
193193
verbose: false,
194-
mediaUrl,
195-
mediaAccess: ctx.mediaAccess,
196-
mediaLocalRoots: ctx.mediaLocalRoots,
197-
mediaReadFile: ctx.mediaReadFile,
198-
replyTo: sendContext.resolveReplyTo(),
199-
accountId: ctx.accountId ?? undefined,
200-
silent: ctx.silent ?? undefined,
201-
cfg: ctx.cfg,
202-
...sendContext.formatting,
194+
...resolveDiscordMediaDeliveryOptions(ctx, sendContext, mediaUrl),
203195
}),
204196
);
205197
},

0 commit comments

Comments
 (0)