Skip to content

Commit 80caf13

Browse files
author
Simon Naumov
authored
🐛 wicked markdown (#140)
* 🐛 wicked markdown * ✅ fixing tests
1 parent d037f8e commit 80caf13

File tree

12 files changed

+57
-28
lines changed

12 files changed

+57
-28
lines changed

CHANGELOG.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Changelog
22

3+
## [1.2.4] - 19.04.2022
4+
5+
### Fixed
6+
7+
- Escaping markdown in messages
8+
- Chat logging
9+
310
## [1.2.3] - 19.04.2022
411

512
### Fixed
@@ -86,7 +93,7 @@
8693

8794
### Fixed
8895

89-
- Heroku Continious deployment build fixed
96+
- Heroku Continuous deployment build fixed
9097

9198
## [1.0.2] - 16.10.2020
9299

@@ -123,6 +130,8 @@
123130
- Implicit party pulling
124131
- Rude mode
125132

133+
[1.2.4]: https://github.com/pool-party/pull-party-bot/compare/v1.2.3...v1.2.4
134+
[1.2.3]: https://github.com/pool-party/pull-party-bot/compare/v1.2.2...v1.2.3
126135
[1.2.2]: https://github.com/pool-party/pull-party-bot/compare/v1.2.1...v1.2.2
127136
[1.2.1]: https://github.com/pool-party/pull-party-bot/compare/v1.2.0...v1.2.1
128137
[1.2.0]: https://github.com/pool-party/pull-party-bot/compare/v1.1.0.2...v1.2.0

Procfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
web: java -jar build/libs/pull-party-bot-1.2.2.jar
1+
web: java -jar build/libs/pull-party-bot-1.2.4.jar

build.gradle.kts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ plugins {
99
}
1010

1111
group = "org.pool-party"
12-
version = "1.2.2"
12+
version = "1.2.4"
1313

1414
repositories {
1515
maven("https://jitpack.io")
@@ -75,4 +75,6 @@ tasks.withType<KotlinCompile>().configureEach {
7575
kotlinOptions.jvmTarget = "1.8"
7676
kotlinOptions.freeCompilerArgs += "-opt-in=kotlinx.coroutines.DelicateCoroutinesApi"
7777
kotlinOptions.freeCompilerArgs += "-opt-in=kotlinx.serialization.ExperimentalSerializationApi"
78+
kotlinOptions.freeCompilerArgs += "-opt-in=kotlinx.serialization.ExperimentalSerializationApi"
79+
kotlinOptions.freeCompilerArgs += "-Xcontext-receivers"
7880
}

src/main/kotlin/com/github/pool_party/pull_party_bot/commands/Callback.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ interface Callback {
3030
class CallbackDispatcher(val callbacks: Map<CallbackAction, Callback>) : Interaction {
3131

3232
override fun onMessage(bot: Bot) = bot.onCallbackQuery {
33-
loggingError(bot) {
33+
bot.loggingError {
3434
logger.info { "callback ${it.from.username}@${it.message?.chat?.title}: ${it.data}" }
3535

3636
val callbackData = it.data?.let { Json.decodeFromString<CallbackData>(it) } ?: return@loggingError

src/main/kotlin/com/github/pool_party/pull_party_bot/commands/Command.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ abstract class AbstractCommand(
5555
"${LocalDateTime.now()} $command <- ${message.from?.username}@${message.chat.title}: \"${message.text}\""
5656
}
5757

58-
val nanoseconds = loggingError(bot) {
58+
val nanoseconds = bot.loggingError {
5959
measureNanoTime { bot.action(message, args) }
6060
}
6161

src/main/kotlin/com/github/pool_party/pull_party_bot/commands/EveryMessageInteraction.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ interface EveryMessageInteraction {
1111
class EveryMessageProcessor(private val interactions: List<EveryMessageInteraction>) : Interaction {
1212

1313
override fun onMessage(bot: Bot) = bot.onMessage { message ->
14-
loggingError(bot) {
14+
bot.loggingError {
1515
interactions.forEach { it.onMessage(bot, message) }
1616
}
1717
}

src/main/kotlin/com/github/pool_party/pull_party_bot/commands/Handlers.kt

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import com.github.pool_party.pull_party_bot.commands.handlers.callback.PingCallb
2323
import com.github.pool_party.pull_party_bot.commands.messages.onError
2424
import com.github.pool_party.pull_party_bot.database.dao.ChatDaoImpl
2525
import com.github.pool_party.pull_party_bot.database.dao.PartyDaoImpl
26+
import mu.KotlinLogging
2627

2728
fun Bot.initHandlers() {
2829

@@ -65,10 +66,21 @@ fun Bot.initHandlers() {
6566
setMyCommands(commands.map { it.toBotCommand() })
6667
}
6768

68-
suspend fun <T> loggingError(bot: Bot, action: suspend () -> T): T =
69+
private val logger = KotlinLogging.logger { }
70+
71+
suspend fun <T> Bot.loggingError(action: suspend () -> T): T =
6972
try {
7073
action()
71-
} catch (e: Throwable) {
72-
bot.sendMessageLogging(Configuration.DEVELOP_CHAT_ID, onError(e)).join()
73-
throw e
74+
} catch (throwable: Throwable) {
75+
processThrowable(throwable)
7476
}
77+
78+
private fun Bot.processThrowable(throwable: Throwable): Nothing {
79+
val (prefix, reason) =
80+
if (throwable is SendingMessageException) "${throwable.action}: " to throwable.reason else "" to throwable
81+
82+
logger.error { "$prefix${reason.message}:\n${reason.stackTraceToString()}" }
83+
sendMessage(Configuration.DEVELOP_CHAT_ID, onError(reason).escapeSpecial(), "MarkdownV2").join()
84+
85+
throw reason
86+
}

src/main/kotlin/com/github/pool_party/pull_party_bot/commands/MessageUtils.kt

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ import java.util.concurrent.CompletableFuture
88

99
private val logger = KotlinLogging.logger { }
1010

11-
fun <T> CompletableFuture<T>.logging(prefix: String = ""): CompletableFuture<T> = handleAsync { value, throwable ->
12-
if (throwable != null) {
13-
logger.error { "$prefix: ${throwable.message}:\n${throwable.stackTraceToString()}" }
14-
throw throwable
11+
class SendingMessageException(val action: String, val reason: Throwable) : RuntimeException()
12+
13+
context(Bot)
14+
fun <T> CompletableFuture<T>.logging(action: String = ""): T =
15+
try {
16+
join()
17+
} catch (throwable: Throwable) {
18+
throw SendingMessageException(action, throwable)
1519
}
16-
value
17-
}
1820

1921
private fun String.escape(symbols: String) = replace("[$symbols]".toRegex()) { "\\${it.groupValues[0]}" }
2022

@@ -27,18 +29,18 @@ fun Bot.sendMessageLogging(
2729
text: String,
2830
markup: ReplyKeyboard? = null,
2931
replyTo: Long? = null
30-
): CompletableFuture<out Message> {
32+
): Message {
3133
logger.debug { "Sending '$text'" }
3234
return sendMessage(chatId, text.escapeSpecial(), "MarkdownV2", replyTo = replyTo, markup = markup)
3335
.logging("Failed to send message \"$text\"")
3436
}
3537

36-
fun Bot.deleteMessageLogging(chatId: Long, messageId: Long): CompletableFuture<out Boolean> {
38+
fun Bot.deleteMessageLogging(chatId: Long, messageId: Long): Boolean {
3739
logger.debug { "Deleting message $chatId/$messageId" }
3840
return deleteMessage(chatId, messageId).logging("Failed to delete message $chatId/$messageId")
3941
}
4042

41-
fun Bot.answerCallbackQueryLogging(id: String, text: String? = null): CompletableFuture<out Boolean> {
43+
fun Bot.answerCallbackQueryLogging(id: String, text: String? = null): Boolean {
4244
logger.debug { "Answering callback query $id" }
4345
return answerCallbackQuery(id, text).logging("Answering callback query $id")
4446
}

src/main/kotlin/com/github/pool_party/pull_party_bot/commands/handlers/ListCommand.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,15 @@ class ListCommand(private val partyDao: PartyDao, chatDao: ChatDao) :
120120
if (currentString.length + line.length + 1 < Configuration.MESSAGE_LENGTH) {
121121
currentString.append("\n").append(line)
122122
} else {
123-
sendCaseMessage(chatId, currentString.toString()).join()
123+
sendCaseMessage(chatId, currentString.toString())
124124
currentString = StringBuilder(line)
125125
}
126126
}
127127

128128
if (emptySequence) {
129129
sendMessageLogging(chatId, onEmptyMessage)
130130
} else {
131-
sendCaseMessage(chatId, currentString.toString()).join()
131+
sendCaseMessage(chatId, currentString.toString())
132132
}
133133
}
134134

src/main/kotlin/com/github/pool_party/pull_party_bot/commands/handlers/PullPartyCommands.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import com.github.pool_party.pull_party_bot.commands.CallbackAction
1010
import com.github.pool_party.pull_party_bot.commands.CallbackData
1111
import com.github.pool_party.pull_party_bot.commands.EveryMessageInteraction
1212
import com.github.pool_party.pull_party_bot.commands.deleteMessageLogging
13+
import com.github.pool_party.pull_party_bot.commands.escapeMarkdown
1314
import com.github.pool_party.pull_party_bot.commands.messages.HELP_PARTY
1415
import com.github.pool_party.pull_party_bot.commands.messages.ON_ADMINS_PARTY_FAIL
1516
import com.github.pool_party.pull_party_bot.commands.messages.ON_PARTY_EMPTY
@@ -97,7 +98,7 @@ private suspend fun Bot.handleParty(
9798
.distinct()
9899
.joinToString(" ")
99100

100-
if (res.isNotBlank()) sendMessageLogging(chatId, res, replyTo = message.message_id)
101+
if (res.isNotBlank()) sendMessageLogging(chatId, res.escapeMarkdown(), replyTo = message.message_id)
101102

102103
if (failed.isEmpty()) return
103104

@@ -136,7 +137,7 @@ private suspend fun Bot.handleParty(
136137
}
137138
.toList()
138139
)
139-
).join()
140+
)
140141

141142
delay(Configuration.STALE_PING_SECONDS * 1000L)
142143

0 commit comments

Comments
 (0)