From 3eb43d4d19ca50bceecfe451fbe2aad0030219d2 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Mon, 28 Aug 2023 23:15:32 +0300 Subject: [PATCH] Add full chat extraction --- src/main/kotlin/extractMessages.kt | 4 +++- src/main/kotlin/main.kt | 22 +++++++++++++++++----- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/main/kotlin/extractMessages.kt b/src/main/kotlin/extractMessages.kt index 2c7977c..08bce32 100644 --- a/src/main/kotlin/extractMessages.kt +++ b/src/main/kotlin/extractMessages.kt @@ -47,7 +47,7 @@ private suspend fun SpaceClient.writeMessages( writer.appendLine( """ |* **(${message.created}) ${message.author.name}:** - |${message.text.trimIndent()} + |${message.text} """.replaceIndentByMargin(prefix) ) @@ -106,6 +106,8 @@ suspend fun SpaceClient.extractMessages( val file = parentDirectory.resolve("$name.md") + logger.info("Extracting messages from channel $chatId to $file") + file.parent.createDirectories() file.outputStream( diff --git a/src/main/kotlin/main.kt b/src/main/kotlin/main.kt index a281f96..22fd92a 100644 --- a/src/main/kotlin/main.kt +++ b/src/main/kotlin/main.kt @@ -10,6 +10,7 @@ import space.jetbrains.api.runtime.SpaceAppInstance import space.jetbrains.api.runtime.SpaceAuth import space.jetbrains.api.runtime.SpaceClient import space.jetbrains.api.runtime.ktorClientForSpace +import space.jetbrains.api.runtime.resources.chats import space.jetbrains.api.runtime.resources.projects import space.jetbrains.api.runtime.types.FolderIdentifier import space.jetbrains.api.runtime.types.ProjectIdentifier @@ -170,7 +171,7 @@ private class ExtractRepositoriesCommand : ExtractCommand("repos", "Extract repo } -private class ExtractChatsCommand: ExtractCommand("chat","Extract all messages from a chat"){ +private class ExtractMessagesCommand : ExtractCommand("messages", "Extract all messages from a chat") { val path: String by option( ArgType.String, @@ -182,7 +183,7 @@ private class ExtractChatsCommand: ExtractCommand("chat","Extract all messages f val spaceUrl = urlMatch.groups["spaceUrl"]?.value ?: error("Space Url token not recognized") - val chatId = urlMatch.groups["chatId"]?.value ?: error("Chat id token not recognized") + val chatId = urlMatch.groups["chatId"]?.value val appInstance = SpaceAppInstance( clientId ?: System.getProperty("space.clientId"), @@ -197,13 +198,19 @@ private class ExtractChatsCommand: ExtractCommand("chat","Extract all messages f ) runBlocking { - spaceClient.extractMessages(chatId, Path(path)) + if (chatId == null) { + spaceClient.chats.channels.listAllChannels(query = "").data.forEach { + spaceClient.extractMessages(it.channelId, Path(path)) + } + } else { + spaceClient.extractMessages(chatId, Path(path)) + } } } companion object { private val urlRegex = - """(?https?:\/\/[^\/]*)\/im\/group\/(?.*)""".toRegex() + """(?https?:\/\/[^\/]*)(\/im\/group\/(?.*))?""".toRegex() } } @@ -272,7 +279,12 @@ private class ExtractProjectCommand : ExtractCommand("project", "Extract all dat fun main(args: Array) { val parser = ArgParser("space-export") - parser.subcommands(ExtractDocumentsCommand(), ExtractRepositoriesCommand(), ExtractProjectCommand(), ExtractChatsCommand()) + parser.subcommands( + ExtractDocumentsCommand(), + ExtractRepositoriesCommand(), + ExtractProjectCommand(), + ExtractMessagesCommand() + ) parser.parse(args) } \ No newline at end of file