<commit_before>package net.serverpeon.discord.internal.data

import net.serverpeon.discord.interaction.PermissionException
import net.serverpeon.discord.internal.rest.data.ChannelModel
import net.serverpeon.discord.internal.rest.data.MessageModel
import net.serverpeon.discord.internal.rest.data.WrappedId
import net.serverpeon.discord.internal.rest.retro.Channels.EditPermissionRequest
import net.serverpeon.discord.internal.rest.retro.Channels.SendMessageRequest
import net.serverpeon.discord.internal.rx
import net.serverpeon.discord.internal.rxObservable
import net.serverpeon.discord.internal.toFuture
import net.serverpeon.discord.internal.ws.data.inbound.Channels
import net.serverpeon.discord.internal.ws.data.inbound.Event
import net.serverpeon.discord.internal.ws.data.inbound.Misc
import net.serverpeon.discord.internal.ws.data.inbound.PrivateChannelModel
import net.serverpeon.discord.message.Message
import net.serverpeon.discord.model.*
import rx.Completable
import rx.Observable
import java.util.*
import java.util.concurrent.CompletableFuture
import java.util.regex.Pattern

abstract class ChannelNode private constructor(val root: DiscordNode,
                                               override val id: DiscordId<Channel>) : Channel, Channel.Text, Event.Visitor {
    override fun typingStart(e: Misc.TypingStart) {
        // Ignored, not something we need to represent
    }

    override fun toString(): String {
        return "Channel(id=$id)"
    }

    override fun delete(): CompletableFuture<Void> {
        return root.api.Channels.deleteChannel(WrappedId(id)).toFuture()
    }

    override fun messageHistory(limit: Int): Observable<PostedMessage> {
        checkPermission(PermissionSet.Permission.READ_MESSAGE_HISTORY)
        return Observable.concat<List<MessageModel>>(Observable.create { sub ->
            //TODO: some sort of rate limiting mechanism
            var currentLimit = limit
            var lastMessage: MessageModel? = null
            sub.setProducer {
                if (currentLimit > 0) {
                    //Produce the next call.
                    val requestLimit = Math.min(currentLimit, 100)
                    currentLimit -= requestLimit
                    val obs = root.api.Channels.getMessages(WrappedId(id),
                            limit = requestLimit,
                            before = lastMessage?.let {
                                WrappedId(it.id)
                            }
                    ).rxObservable().publish().apply {
                        // Set lastMessage to last message of list
                        sub.add(subscribe { lastMessage = it.last() })
                    }.refCount()

                    sub.onNext(obs)
                } else {
                    sub.onCompleted()
                }
            }
        }).flatMapIterable {
            it
        }.map { MessageNode.from(it, root) }
    }

    override fun sendMessage(message: Message, textToSpeech: Boolean?): CompletableFuture<PostedMessage> {
        checkPermission(PermissionSet.Permission.SEND_MESSAGES)
        if (textToSpeech == true) {
            checkPermission(PermissionSet.Permission.SEND_TTS_MESSAGES)
        }
        val mentions = message.mentions.map { it.id }.toList().toBlocking().first()
        return root.api.Channels.sendMessage(WrappedId(id), SendMessageRequest(
                message.encodedContent,
                mentions = if (mentions.isNotEmpty()) mentions else null,
                tts = textToSpeech
        )).toFuture().thenApply { error("Not Yet Implemented") }
    }
<commit_msg>Fix sendMessage<commit_after>package net.serverpeon.discord.internal.data

import net.serverpeon.discord.interaction.PermissionException
import net.serverpeon.discord.internal.rest.data.ChannelModel
import net.serverpeon.discord.internal.rest.data.MessageModel
import net.serverpeon.discord.internal.rest.data.WrappedId
import net.serverpeon.discord.internal.rest.retro.Channels.EditPermissionRequest
import net.serverpeon.discord.internal.rest.retro.Channels.SendMessageRequest
import net.serverpeon.discord.internal.rx
import net.serverpeon.discord.internal.rxObservable
import net.serverpeon.discord.internal.toFuture
import net.serverpeon.discord.internal.ws.data.inbound.Channels
import net.serverpeon.discord.internal.ws.data.inbound.Event
import net.serverpeon.discord.internal.ws.data.inbound.Misc
import net.serverpeon.discord.internal.ws.data.inbound.PrivateChannelModel
import net.serverpeon.discord.message.Message
import net.serverpeon.discord.model.*
import rx.Completable
import rx.Observable
import java.util.*
import java.util.concurrent.CompletableFuture
import java.util.regex.Pattern

abstract class ChannelNode private constructor(val root: DiscordNode,
                                               override val id: DiscordId<Channel>) : Channel, Channel.Text, Event.Visitor {
    override fun typingStart(e: Misc.TypingStart) {
        // Ignored, not something we need to represent
    }

    override fun toString(): String {
        return "Channel(id=$id)"
    }

    override fun delete(): CompletableFuture<Void> {
        return root.api.Channels.deleteChannel(WrappedId(id)).toFuture()
    }

    override fun messageHistory(limit: Int): Observable<PostedMessage> {
        checkPermission(PermissionSet.Permission.READ_MESSAGE_HISTORY)
        return Observable.concat<List<MessageModel>>(Observable.create { sub ->
            //TODO: some sort of rate limiting mechanism
            var currentLimit = limit
            var lastMessage: MessageModel? = null
            sub.setProducer {
                if (currentLimit > 0) {
                    //Produce the next call.
                    val requestLimit = Math.min(currentLimit, 100)
                    currentLimit -= requestLimit
                    val obs = root.api.Channels.getMessages(WrappedId(id),
                            limit = requestLimit,
                            before = lastMessage?.let {
                                WrappedId(it.id)
                            }
                    ).rxObservable().publish().apply {
                        // Set lastMessage to last message of list
                        sub.add(subscribe { lastMessage = it.last() })
                    }.refCount()

                    sub.onNext(obs)
                } else {
                    sub.onCompleted()
                }
            }
        }).flatMapIterable {
            it
        }.map { MessageNode.from(it, root) }
    }

    override fun sendMessage(message: Message, textToSpeech: Boolean?): CompletableFuture<PostedMessage> {
        checkPermission(PermissionSet.Permission.SEND_MESSAGES)
        if (textToSpeech == true) {
            checkPermission(PermissionSet.Permission.SEND_TTS_MESSAGES)
        }
        val mentions = message.mentions.map { it.id }.toList().toBlocking().first()
        return root.api.Channels.sendMessage(WrappedId(id), SendMessageRequest(
                message.encodedContent,
                mentions = if (mentions.isNotEmpty()) mentions else null,
                tts = textToSpeech
        )).toFuture().thenApply { MessageNode.from(it, root) }
    }

{'fraction_non_alphanumeric': 0.09199224161817678, 'fraction_numerical': 0.0011083402604599613, 'mean_word_length': 2.332871652816251, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2657331', 'n_tokens_mistral': 1931, 'n_tokens_neox': 1898, 'n_words': 354}