this repo has no description

feat: type cdn and extract to lib

dunkirk.sh 3ef74b0c 0a212597

verified
Changed files
+37 -26
src
+9 -26
src/index.ts
···
import { version } from "../package.json";
import { registerCommands } from "./commands";
import { channelMappings, userMappings } from "./db";
import { parseIRCFormatting, parseSlackMarkdown } from "./parser";
import type { CachetUser } from "./types";
···
unfurl_media: true,
});
}
-
console.log(`IRC → Slack: <${nick}> ${text}`);
} catch (error) {
console.error("Error posting to Slack:", error);
}
···
],
});
-
console.log(`IRC → Slack (action): ${actionText}`);
},
);
// Slack event handlers
-
slackApp.event("message", async ({ payload, context }) => {
// Ignore bot messages and threaded messages
if (payload.subtype && payload.subtype !== "file_share") return;
if (payload.bot_id) return;
···
// Handle file uploads
if (payload.files && payload.files.length > 0) {
try {
-
// Extract private file URLs
const fileUrls = payload.files.map((file) => file.url_private);
-
-
// Upload to Hack Club CDN
-
const response = await fetch("https://cdn.hackclub.com/api/v3/new", {
-
method: "POST",
-
headers: {
-
Authorization: `Bearer ${process.env.CDN_TOKEN}`,
-
"X-Download-Authorization": `Bearer ${process.env.SLACK_BOT_TOKEN}`,
-
"Content-Type": "application/json",
-
},
-
body: JSON.stringify(fileUrls),
-
});
-
if (response.ok) {
-
const data = await response.json();
-
-
// Send each uploaded file URL to IRC
-
for (const file of data.files) {
-
const fileMessage = `<${username}> ${file.deployedUrl}`;
-
ircClient.say(mapping.irc_channel, fileMessage);
-
console.log(`Slack → IRC (file): ${fileMessage}`);
-
}
-
} else {
-
console.error("Failed to upload files to CDN:", response.statusText);
}
} catch (error) {
console.error("Error uploading files to CDN:", error);
···
import { version } from "../package.json";
import { registerCommands } from "./commands";
import { channelMappings, userMappings } from "./db";
+
import { uploadToCDN } from "./lib/cdn";
import { parseIRCFormatting, parseSlackMarkdown } from "./parser";
import type { CachetUser } from "./types";
···
unfurl_media: true,
});
}
+
console.log(`IRC (${to}) → Slack: <${nick}> ${text}`);
} catch (error) {
console.error("Error posting to Slack:", error);
}
···
],
});
+
console.log(`IRC (${to}) → Slack (action): ${actionText}`);
},
);
// Slack event handlers
+
slackApp.event("message", async ({ payload }) => {
// Ignore bot messages and threaded messages
if (payload.subtype && payload.subtype !== "file_share") return;
if (payload.bot_id) return;
···
// Handle file uploads
if (payload.files && payload.files.length > 0) {
try {
const fileUrls = payload.files.map((file) => file.url_private);
+
const data = await uploadToCDN(fileUrls);
+
for (const file of data.files) {
+
const fileMessage = `<${username}> ${file.deployedUrl}`;
+
ircClient.say(mapping.irc_channel, fileMessage);
+
console.log(`Slack → IRC (file): ${fileMessage}`);
}
} catch (error) {
console.error("Error uploading files to CDN:", error);
+21
src/lib/cdn.ts
···
···
+
import type { CDNUploadResponse } from "../types";
+
+
export async function uploadToCDN(
+
fileUrls: string[],
+
): Promise<CDNUploadResponse> {
+
const response = await fetch("https://cdn.hackclub.com/api/v3/new", {
+
method: "POST",
+
headers: {
+
Authorization: `Bearer ${process.env.CDN_TOKEN}`,
+
"X-Download-Authorization": `Bearer ${process.env.SLACK_BOT_TOKEN}`,
+
"Content-Type": "application/json",
+
},
+
body: JSON.stringify(fileUrls),
+
});
+
+
if (!response.ok) {
+
throw new Error(`CDN upload failed: ${response.statusText}`);
+
}
+
+
return (await response.json()) as CDNUploadResponse;
+
}
+7
src/types.ts
···
imageUrl: string;
expiration: string;
}
···
imageUrl: string;
expiration: string;
}
+
+
export interface CDNUploadResponse {
+
files: Array<{
+
deployedUrl: string;
+
originalUrl: string;
+
}>;
+
}