this repo has no description

feat: type cdn and extract to lib

dunkirk.sh 3ef74b0c 0a212597

verified
+37 -26
+9 -26
src/index.ts
··· 3 3 import { version } from "../package.json"; 4 4 import { registerCommands } from "./commands"; 5 5 import { channelMappings, userMappings } from "./db"; 6 + import { uploadToCDN } from "./lib/cdn"; 6 7 import { parseIRCFormatting, parseSlackMarkdown } from "./parser"; 7 8 import type { CachetUser } from "./types"; 8 9 ··· 251 252 unfurl_media: true, 252 253 }); 253 254 } 254 - console.log(`IRC → Slack: <${nick}> ${text}`); 255 + console.log(`IRC (${to}) → Slack: <${nick}> ${text}`); 255 256 } catch (error) { 256 257 console.error("Error posting to Slack:", error); 257 258 } ··· 342 343 ], 343 344 }); 344 345 345 - console.log(`IRC → Slack (action): ${actionText}`); 346 + console.log(`IRC (${to}) → Slack (action): ${actionText}`); 346 347 }, 347 348 ); 348 349 349 350 // Slack event handlers 350 - slackApp.event("message", async ({ payload, context }) => { 351 + slackApp.event("message", async ({ payload }) => { 351 352 // Ignore bot messages and threaded messages 352 353 if (payload.subtype && payload.subtype !== "file_share") return; 353 354 if (payload.bot_id) return; ··· 431 432 // Handle file uploads 432 433 if (payload.files && payload.files.length > 0) { 433 434 try { 434 - // Extract private file URLs 435 435 const fileUrls = payload.files.map((file) => file.url_private); 436 - 437 - // Upload to Hack Club CDN 438 - const response = await fetch("https://cdn.hackclub.com/api/v3/new", { 439 - method: "POST", 440 - headers: { 441 - Authorization: `Bearer ${process.env.CDN_TOKEN}`, 442 - "X-Download-Authorization": `Bearer ${process.env.SLACK_BOT_TOKEN}`, 443 - "Content-Type": "application/json", 444 - }, 445 - body: JSON.stringify(fileUrls), 446 - }); 436 + const data = await uploadToCDN(fileUrls); 447 437 448 - if (response.ok) { 449 - const data = await response.json(); 450 - 451 - // Send each uploaded file URL to IRC 452 - for (const file of data.files) { 453 - const fileMessage = `<${username}> ${file.deployedUrl}`; 454 - ircClient.say(mapping.irc_channel, fileMessage); 455 - console.log(`Slack → IRC (file): ${fileMessage}`); 456 - } 457 - } else { 458 - console.error("Failed to upload files to CDN:", response.statusText); 438 + for (const file of data.files) { 439 + const fileMessage = `<${username}> ${file.deployedUrl}`; 440 + ircClient.say(mapping.irc_channel, fileMessage); 441 + console.log(`Slack → IRC (file): ${fileMessage}`); 459 442 } 460 443 } catch (error) { 461 444 console.error("Error uploading files to CDN:", error);
+21
src/lib/cdn.ts
··· 1 + import type { CDNUploadResponse } from "../types"; 2 + 3 + export async function uploadToCDN( 4 + fileUrls: string[], 5 + ): Promise<CDNUploadResponse> { 6 + const response = await fetch("https://cdn.hackclub.com/api/v3/new", { 7 + method: "POST", 8 + headers: { 9 + Authorization: `Bearer ${process.env.CDN_TOKEN}`, 10 + "X-Download-Authorization": `Bearer ${process.env.SLACK_BOT_TOKEN}`, 11 + "Content-Type": "application/json", 12 + }, 13 + body: JSON.stringify(fileUrls), 14 + }); 15 + 16 + if (!response.ok) { 17 + throw new Error(`CDN upload failed: ${response.statusText}`); 18 + } 19 + 20 + return (await response.json()) as CDNUploadResponse; 21 + }
+7
src/types.ts
··· 7 7 imageUrl: string; 8 8 expiration: string; 9 9 } 10 + 11 + export interface CDNUploadResponse { 12 + files: Array<{ 13 + deployedUrl: string; 14 + originalUrl: string; 15 + }>; 16 + }