Skip to content

Commit

Permalink
Who uses this?
Browse files Browse the repository at this point in the history
  • Loading branch information
demidko committed Jan 3, 2025
1 parent fdf430a commit 2db24c1
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 46 deletions.
2 changes: 0 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ dependencies {
implementation("org.jetbrains.kotlin:kotlin-reflect:2.1.0-Beta1")
implementation("com.sksamuel.hoplite:hoplite-core:2.8.0")
implementation("com.squareup.retrofit2:retrofit:2.11.0")
implementation("org.slf4j:slf4j-api:2.0.16")
implementation("ch.qos.logback:logback-classic:1.5.15")
testImplementation("com.google.truth:truth:1.4.4")
testImplementation("io.mockk:mockk:1.13.12")
testImplementation("org.junit.jupiter:junit-jupiter-api:5.11.0")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.github.kotlintelegrambot.entities.ChatPermissions
import com.sksamuel.hoplite.ConfigLoaderBuilder
import com.sksamuel.hoplite.ExperimentalHoplite
import com.sksamuel.hoplite.addEnvironmentSource
import org.slf4j.LoggerFactory.getLogger
import java.time.Duration
import java.time.Duration.ofMinutes
import java.time.ZoneId
Expand All @@ -29,7 +28,7 @@ open class ApplicationFactory {

init {
val duration = config.restrictionsDuration.toKotlinDuration()
getLogger(javaClass).info("Using duration $duration")
println("Using duration $duration")
}

open val restrictions = ChatPermissions(
Expand Down
38 changes: 0 additions & 38 deletions src/main/kotlin/com/github/demidko/glock/ChatOps.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import com.github.kotlintelegrambot.entities.ChatPermissions
import com.github.kotlintelegrambot.entities.Message
import org.apache.commons.collections4.QueueUtils.synchronizedQueue
import org.apache.commons.collections4.queue.CircularFifoQueue
import org.slf4j.LoggerFactory
import java.time.Duration
import java.time.Duration.ofSeconds
import java.time.Instant.now
Expand All @@ -29,43 +28,6 @@ class ChatOps(
private val healingTimeZone: ZoneId
) {

init {
val log = LoggerFactory.getLogger(javaClass)
val chat = bot.getChat(chatId).getOrNull()
if (chat == null) {
log.warn("Chat $chatId cannot be displayed")
} else {
val info =
buildString {
with(chat) {
append("id").append(id).append(' ')
if (firstName != null) {
append(firstName).append(' ')
}
if (lastName != null) {
append(lastName).append(' ')
}
if (username != null) {
append('@').append(username).append(' ')
}
if (inviteLink != null) {
append(inviteLink).append(' ')
}
if (bio != null) {
appendLine(bio)
}
if (description != null) {
appendLine(description)
}
if (pinnedMessage != null) {
appendLine(pinnedMessage)
}
}
}
log.info(info)
}
}

private val messagesToLifetimes = ConcurrentHashMap<Long, Long>()
private val recentMessages = synchronizedQueue(CircularFifoQueue<Message>(12))
private val statuettes = ConcurrentLinkedQueue<Long>()
Expand Down
47 changes: 43 additions & 4 deletions src/main/kotlin/com/github/demidko/glock/GlockBot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.github.kotlintelegrambot.dispatcher.command
import com.github.kotlintelegrambot.dispatcher.handlers.HandleCommand
import com.github.kotlintelegrambot.dispatcher.handlers.HandleMessage
import com.github.kotlintelegrambot.dispatcher.message
import com.github.kotlintelegrambot.entities.Chat
import com.github.kotlintelegrambot.entities.ChatId.Companion.fromId
import com.github.kotlintelegrambot.entities.ChatPermissions
import com.github.kotlintelegrambot.entities.Message
Expand Down Expand Up @@ -55,8 +56,15 @@ class GlockBot(
bot.startPolling()
}

private fun getChatOps(chatId: Long): ChatOps {
return idToChatOps.computeIfAbsent(chatId, ::newChatOps)
private fun getChatOps(chat: Chat): ChatOps {
var chatOps = idToChatOps[chat.id]
if (chatOps == null) {
display(chat)
chatOps = newChatOps(chat.id)
idToChatOps[chat.id] = chatOps
}
return chatOps
//return idToChatOps.computeIfAbsent(chatId, ::newChatOps)
}

private fun newChatOps(chatId: Long): ChatOps {
Expand Down Expand Up @@ -95,7 +103,7 @@ class GlockBot(

private fun startVirtualThread(method: ChatOps.(Message) -> Unit, message: Message) {
startVirtualThread {
getChatOps(message.chat.id).method(message)
getChatOps(message.chat).method(message)
}
}

Expand All @@ -105,7 +113,38 @@ class GlockBot(
args: List<String>
) {
startVirtualThread {
getChatOps(message.chat.id).method(message, args)
getChatOps(message.chat).method(message, args)
}
}

private fun display(chat: Chat) {
val info =
buildString {
with(chat) {
append("id").append(id).append(' ')
if (firstName != null) {
append(firstName).append(' ')
}
if (lastName != null) {
append(lastName).append(' ')
}
if (username != null) {
append('@').append(username).append(' ')
}
if (inviteLink != null) {
append(inviteLink).append(' ')
}
if (bio != null) {
appendLine(bio).appendLine("---")
}
if (description != null) {
appendLine(description).appendLine("---")
}
if (pinnedMessage != null) {
appendLine(pinnedMessage).appendLine("---")
}
}
}
println(info)
}
}

0 comments on commit 2db24c1

Please sign in to comment.