get-channel-history.usecase.tsโข1.75 kB
import { ISlackService } from "../../core/interfaces/slack.interface.js";
import { GetChannelHistoryRequest } from "../../core/types/slack.types.js";
export class GetChannelHistoryUseCase {
constructor(private readonly slackService: ISlackService) {}
async execute(request: GetChannelHistoryRequest) {
const result = await this.slackService.getChannelHistory(request);
if (!result.success) {
return {
content: [
{
type: "text" as const,
text: `โ Failed to get channel history: ${result.error}`,
},
],
};
}
const { messages } = result.data!;
if (messages.length === 0) {
return {
content: [
{
type: "text" as const,
text: `๐ญ No messages found in channel \`${request.channel_id}\`.`,
},
],
};
}
const messageList = messages
.map((message, index) => {
const timestamp = new Date(
parseFloat(message.ts) * 1000
).toLocaleString();
const threadInfo = message.thread_ts
? ` ๐งต (${message.reply_count || 0} replies)`
: "";
const reactions = message.reactions?.length
? ` ${message.reactions
.map((r) => `${r.name}(${r.count})`)
.join(" ")}`
: "";
return `**${index + 1}.** <@${
message.user
}> - ${timestamp}${threadInfo}\n ${message.text}${reactions}`;
})
.join("\n\n");
return {
content: [
{
type: "text" as const,
text: `๐ฌ **Channel History** (${messages.length} messages)\n**Channel**: <#${request.channel_id}>\n\n${messageList}`,
},
],
};
}
}