suspend fun buildMessageTextForSlack()

in space-slack-sync/src/main/kotlin/org/jetbrains/spaceSlackSync/space/SpaceMessageParsing.kt [10:38]


suspend fun buildMessageTextForSlack(
    spaceClient: SpaceClient,
    slackClient: SlackClient,
    spaceMessageText: String
): String {
    return buildString {
        val spaceRichText = spaceClient.richText.parseMarkdown(spaceMessageText)
        val spaceUserIds = gatherSpaceUserIds(spaceRichText)
        // TODO: add API method to avoid N+1
        val spaceProfiles = spaceUserIds.map { spaceUserId ->
            spaceClient.teamDirectory.profiles.getProfile(ProfileIdentifier.Id(spaceUserId)) {
                id()
                emails {
                    email()
                    blocked()
                }
            }
        }

        val slackUserBySpaceUserId =
            spaceProfiles.mapNotNull { profile ->
                matchSlackUserByEmails(slackClient, profile)?.let { slackUser ->
                    profile.id to slackUser
                }
            }.toMap()

        appendDocument(spaceRichText, slackUserBySpaceUserId)
    }
}