in src/main/kotlin/chats/ChatsCommand.kt [97:168]
suspend fun fetchMessages(channel: ExportedChannel): List<ExportedMessage> {
var nextStartFromDate: Instant? = kotlinx.datetime.Clock.System.now()
val messages = mutableListOf<ExportedMessage>()
while (nextStartFromDate != null) {
val response = client.spaceClient.chats.messages.getChannelMessages(
ChannelIdentifier.Id(channel.id),
MessagesSorting.FromNewestToOldest,
startFromDate = nextStartFromDate,
batchSize = 50
) {
nextStartFromDate()
orgLimitReached()
messages {
id()
author()
time()
text()
attachments()
reactions {
emojiReactions()
}
thread()
}
}
if (nextStartFromDate == response.nextStartFromDate) {
nextStartFromDate = null
} else {
nextStartFromDate = response.nextStartFromDate
}
messages.addAll(response.messages.map { message ->
ExportedMessage(
message.id,
message.author.name,
message.time,
message.text,
unfurls = message.attachments?.mapNotNull {
(it.details as? UnfurlAttachment)?.unfurl?.let {
ExportedUnfurl(
it.text,
it.link,
it.image
)
}
} ?: emptyList(),
attachments = message.attachments?.mapNotNull { info ->
when (val details = info.details) {
is ImageAttachment -> ExportedAttachment(attachmentUrl(channel, message, details.id), details.name ?: details.id)
is VideoAttachment -> ExportedAttachment(attachmentUrl(channel, message, details.id), details.name ?: details.id)
is FileAttachment -> ExportedAttachment(attachmentUrl(channel, message, details.id), details.filename)
else -> null
}
} ?: emptyList(),
reactions = (message.reactions?.emojiReactions?.map { ExportedReaction(it.emoji, it.count) })
?: emptyList(),
thread = message.thread?.let {
fetchMessages(
ExportedChannel(
it.id,
"thread",
ExportedChannel.Type.Thread
)
)
} ?: emptyList()
)
})
if (response.orgLimitReached) {
error("Org Limit reached")
}
}
return messages
}