import { nextCursor, slackRequest } from './request.ts'
import { boundedLimit, inputRecord, optionalBoolean, optionalString, requiredString } from './validation.ts'
type HistoryResponse = {
ok: boolean
messages?: unknown[]
has_more?: boolean
response_metadata?: { next_cursor?: string }
[key: string]: unknown
}
/** Read message history from a Slack conversation available to the user. */
export default async function getHistory(params: Record<string, unknown>) {
const input = inputRecord(params)
const result = await slackRequest<HistoryResponse>('conversations.history', {
channel: requiredString(input, 'channel'),
limit: boundedLimit(input.limit),
cursor: optionalString(input, 'cursor'),
oldest: optionalString(input, 'oldest'),
latest: optionalString(input, 'latest'),
inclusive: optionalBoolean(input, 'inclusive'),
})
return {
messages: result.messages ?? [],
hasMore: result.has_more === true,
nextCursor: nextCursor(result),
}
}