Skip to content

Commit

Permalink
Fix cycles deps
Browse files Browse the repository at this point in the history
  • Loading branch information
peterarsentev committed Jul 23, 2024
1 parent b026c69 commit f7510b1
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 37 deletions.
17 changes: 9 additions & 8 deletions src/main/kotlin/pro/dionea/service/actions/ActionConfig.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package pro.dionea.service.actions

import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.security.crypto.password.PasswordEncoder
Expand All @@ -13,19 +14,19 @@ class ActionConfig(
private val chatService: ChatService,
private val userService: UserService,
private val encoding: PasswordEncoder,
private val spamAnalysis: SpamAnalysis,
private val spamAnalysis: SpamAnalysis
) {

@Bean
fun actions(remoteChat: RemoteChat, botUsername: String): List<UpdateAction> {
fun actions(@Value("\${tg.name}") botUsername: String): List<UpdateAction> {
return listOf(
VoteCallBackAction(voteService, contactService, spamService, chatService, remoteChat),
VoteCallBackAction(voteService, contactService, spamService, chatService),
EmptyMessageAction(),
JoinAction(contactService, remoteChat),
ImageAttachedAction(contactService, remoteChat),
PrivateChatAction(userService, encoding, remoteChat),
ReplyAction(remoteChat, botUsername),
TextMessageAction(contactService, spamService, spamAnalysis, chatService, remoteChat)
JoinAction(contactService),
ImageAttachedAction(contactService),
PrivateChatAction(userService, encoding),
ReplyAction(botUsername),
TextMessageAction(contactService, spamService, spamAnalysis, chatService)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class EmptyMessageAction: UpdateAction {
override fun check(update: Update): Boolean
= !update.hasMessage()

override fun execute(update: Update) {
override fun execute(update: Update, remoteChat: RemoteChat) {
TODO("Not yet implemented")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,21 @@ package pro.dionea.service.actions
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.telegram.telegrambots.meta.api.methods.GetFile
import org.telegram.telegrambots.meta.api.methods.groupadministration.RestrictChatMember
import org.telegram.telegrambots.meta.api.methods.send.SendMessage
import org.telegram.telegrambots.meta.api.methods.updatingmessages.DeleteMessage
import org.telegram.telegrambots.meta.api.objects.ChatPermissions
import org.telegram.telegrambots.meta.api.objects.Update
import pro.dionea.service.ContactService
import java.awt.image.BufferedImage
import java.io.ByteArrayInputStream
import java.net.URL
import javax.imageio.ImageIO

class ImageAttachedAction(val contactService: ContactService,
val remoteChat: RemoteChat): UpdateAction {
class ImageAttachedAction(val contactService: ContactService): UpdateAction {

override fun check(update: Update): Boolean {
val photo = update.message.photo
return photo != null && photo.isNotEmpty()
}

override fun execute(update: Update) {
override fun execute(update: Update, remoteChat: RemoteChat) {
val message = update.message
val userContact = contactService.findIfNotCreate(message.from)
if (userContact.ham == 0) {
Expand Down
6 changes: 2 additions & 4 deletions src/main/kotlin/pro/dionea/service/actions/JoinAction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@ import org.telegram.telegrambots.meta.api.objects.Update
import pro.dionea.service.ContactService

class JoinAction(
val contactService: ContactService,
val remoteChat: RemoteChat
): UpdateAction {
val contactService: ContactService): UpdateAction {

override fun check(update: Update): Boolean
= update.message.newChatMembers.isNotEmpty()

override fun execute(update: Update) {
override fun execute(update: Update, remoteChat: RemoteChat) {
val message = update.message
val newMembers = message.newChatMembers
for (member in newMembers) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ import pro.dionea.service.KeyGen
import pro.dionea.service.UserService

class PrivateChatAction(val userService: UserService,
val encoding: PasswordEncoder,
val remoteChat: RemoteChat) : UpdateAction {
val encoding: PasswordEncoder) : UpdateAction {

override fun check(update: Update): Boolean
= update.message.chat.type == "private" && update.message.text.startsWith("/")

override fun execute(update: Update) {
override fun execute(update: Update, remoteChat: RemoteChat) {
val message = update.message
if (message.text.startsWith("/")) {
if ("/start" == message.text) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/pro/dionea/service/actions/Receiver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Receiver(
override fun onUpdateReceived(update: Update) {
for (action in actions) {
if (action.check(update)) {
action.execute(update)
action.execute(update, this)
return
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/pro/dionea/service/actions/ReplyAction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import org.telegram.telegrambots.meta.api.objects.Update
import org.telegram.telegrambots.meta.api.objects.replykeyboard.InlineKeyboardMarkup
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.InlineKeyboardButton

class ReplyAction(val remoteChat: RemoteChat, val botName: String): UpdateAction {
class ReplyAction(val botName: String): UpdateAction {

override fun check(update: Update): Boolean
= update.message.text.contains(botName) && update.message.isReply

override fun execute(update: Update) {
override fun execute(update: Update, remoteChat: RemoteChat) {
val message = update.message
val voteMessage = SendMessage().apply {
chatId = message.chatId.toString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ import java.sql.Timestamp
class TextMessageAction(val contactService: ContactService,
val spamService: SpamService,
val spamAnalysis: SpamAnalysis,
val chatService: ChatService,
val remoteChat: RemoteChat): UpdateAction {
val chatService: ChatService): UpdateAction {
override fun check(update: Update): Boolean
= update.message.text.isNullOrEmpty()

override fun execute(update: Update) {
override fun execute(update: Update, remoteChat: RemoteChat) {
val message = update.message
val chatStatistics = chatService.findOrCreate(message)
val userContact = contactService.findIfNotCreate(message.from)
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/pro/dionea/service/actions/UpdateAction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import org.telegram.telegrambots.meta.api.objects.Update

interface UpdateAction {
fun check(update: Update): Boolean
fun execute(update: Update)
fun execute(update: Update, remoteChat: RemoteChat)
}
11 changes: 5 additions & 6 deletions src/main/kotlin/pro/dionea/service/actions/VoteCallBackAction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@ class VoteCallBackAction(
val voteService: VoteService,
val contactService: ContactService,
val spamService: SpamService,
val chatService: ChatService,
val remoteChat: RemoteChat
) : UpdateAction {
val chatService: ChatService) : UpdateAction {

override fun check(update: Update): Boolean
= update.hasCallbackQuery()

override fun execute(update: Update) {
override fun execute(update: Update, remoteChat: RemoteChat) {
val spamMessage = update.callbackQuery.message.replyToMessage ?: return
val chtId = spamMessage.chat.id
val replyMessage = update.callbackQuery.message
Expand Down Expand Up @@ -64,15 +62,16 @@ class VoteCallBackAction(
updateVote.replyMarkup = markupInline
remoteChat.execute(updateVote)
if (votesYes >= Receiver.VOTE_SIZE_YES) {
deleteByVoteMessage(spamMessage, replyMessage, callBotMessageId)
deleteByVoteMessage(spamMessage, replyMessage, callBotMessageId, remoteChat)
}
if (votesNo >= Receiver.VOTE_SIZE_NO) {
remoteChat.execute(DeleteMessage(chtId.toString(), replyMessage.messageId))
remoteChat.execute(DeleteMessage(chtId.toString(), callBotMessageId))
}
}

private fun deleteByVoteMessage(spamMessage: Message, replyMessage: Message, callBotMessageId: Int) {
private fun deleteByVoteMessage(spamMessage: Message, replyMessage: Message,
callBotMessageId: Int, remoteChat: RemoteChat) {
val chtId = spamMessage.chatId.toString()
val spammer = contactService.findIfNotCreate(spamMessage.from)
contactService.increaseCountOfMessages(spammer, true)
Expand Down

0 comments on commit f7510b1

Please sign in to comment.