From 4eb4db55ef949bc8e0b3a1dc6c2969c449b5f1d4 Mon Sep 17 00:00:00 2001 From: ekardnam Date: Mon, 22 Jul 2019 09:07:39 +0200 Subject: [PATCH] Refactor --- .../congressodeiradicali/karlmarx/Bot.scala | 2 - .../karlmarx/BotUser.scala | 48 +++++++++---------- .../karlmarx/coreplugins/BanPlugin.scala | 6 +-- .../karlmarx/coreplugins/CorePlugin.scala | 4 ++ .../karlmarx/coreplugins/EchoPlugin.scala | 5 +- .../coreplugins/PluginManagerPlugin.scala | 4 +- .../karlmarx/coreplugins/SetPlugin.scala | 6 +-- 7 files changed, 36 insertions(+), 39 deletions(-) diff --git a/src/main/scala/org/congressodeiradicali/karlmarx/Bot.scala b/src/main/scala/org/congressodeiradicali/karlmarx/Bot.scala index be76827..454f9ae 100644 --- a/src/main/scala/org/congressodeiradicali/karlmarx/Bot.scala +++ b/src/main/scala/org/congressodeiradicali/karlmarx/Bot.scala @@ -42,10 +42,8 @@ class Bot(val token: String) extends TelegramBot val redis = new RedisAsyncClient(sys.env("REDIS_HOST"), 6379) - def getLogger() = logger private def plugin(plugin: Plugin) = plugin.identifier -> plugin - var plugins: Map[String, Plugin] = Map( plugin(new PluginManagerPlugin(this)), plugin(new BanPlugin(this)), diff --git a/src/main/scala/org/congressodeiradicali/karlmarx/BotUser.scala b/src/main/scala/org/congressodeiradicali/karlmarx/BotUser.scala index 3b961ab..e903694 100644 --- a/src/main/scala/org/congressodeiradicali/karlmarx/BotUser.scala +++ b/src/main/scala/org/congressodeiradicali/karlmarx/BotUser.scala @@ -24,7 +24,24 @@ import scala.concurrent.duration.Duration import scala.concurrent.{Await, ExecutionContext, Future, blocking} import scala.util.{Failure, Success, Try} -class BotUser(private val user: User, private val chat: Chat, private val bot: Bot) { +object BotUserRequest { + + def requestBotUser(user: User, chat: Chat, bot: Bot) = Future[Try[BotUser]] { + blocking { + Await.ready(bot.request(GetChatMember(ChatId.fromChat(chat.id), user.id)), Duration.Inf).value.get match { + case Success(chatMember) => Success(new BotUser(user, chat, bot, chatMember)) + case Failure(err) => Failure(err) + } + } + }(ExecutionContext.global) + +} + +class BotUser( + private val user: User, + private val chat: Chat, + private val bot: Bot, + private val chatMember: ChatMember) { def ban() : Unit = { // ban works by sending the kick user request @@ -37,32 +54,13 @@ class BotUser(private val user: User, private val chat: Chat, private val bot: B bot.request(UnbanChatMember(ChatId.fromChat(chat.id), user.id)) } - var chatMember: Option[ChatMember] = None - def init() : Future[Try[BotUser]] = Future { - blocking { - Await.ready(bot.request(GetChatMember(ChatId.fromChat(chat.id), user.id)), Duration.Inf).value.get match { - case Success(cm) => { - chatMember = Some(cm) - Success(this) - } - case Failure(err) => { - /* - bot.getLogger().error(s"Could not retrieve information about user ${user.id}") - bot.getLogger().error(err.getLocalizedMessage) - */ - Failure(err) - } - } - } - }(ExecutionContext.global) + def isCreator: Boolean = chatMember.status == MemberStatus.Creator + def isAdmin: Boolean = chatMember.status == MemberStatus.Administrator + def isMember: Boolean = chatMember.status == MemberStatus.Member + def isKicked: Boolean = chatMember.status == MemberStatus.Kicked - def isCreator: Boolean = chatMember.exists {_.status == MemberStatus.Creator} - def isAdmin: Boolean = chatMember.exists {_.status == MemberStatus.Administrator} - def isMember: Boolean = chatMember.exists {_.status == MemberStatus.Member} - def isKicked: Boolean = chatMember.exists {_.status == MemberStatus.Kicked} - - def canBanUsers : Boolean = chatMember.exists {_.canRestrictMembers.getOrElse(false)} + def canBanUsers : Boolean = chatMember.canRestrictMembers.getOrElse(false) } diff --git a/src/main/scala/org/congressodeiradicali/karlmarx/coreplugins/BanPlugin.scala b/src/main/scala/org/congressodeiradicali/karlmarx/coreplugins/BanPlugin.scala index 2cfa32a..51ee102 100644 --- a/src/main/scala/org/congressodeiradicali/karlmarx/coreplugins/BanPlugin.scala +++ b/src/main/scala/org/congressodeiradicali/karlmarx/coreplugins/BanPlugin.scala @@ -20,7 +20,7 @@ package org.congressodeiradicali.karlmarx.coreplugins import com.bot4s.telegram.api.declarative.{CommandFilterMagnet, CommandImplicits} import org.congressodeiradicali.karlmarx._ -import scala.concurrent.{ExecutionContext, Future} +import scala.concurrent.Future import scala.util.{Failure, Success} class BanPlugin(bot: Bot) extends CorePlugin with CommandImplicits { @@ -33,15 +33,13 @@ class BanPlugin(bot: Bot) extends CorePlugin with CommandImplicits { override var commandHandlers: Map[CommandFilterMagnet, CommandHandler] = Map { stringToCommandFilter("ban") -> { (msg, _) => - implicit val ec: ExecutionContext = ExecutionContext.global - val futureLocalizedString = msg.replyToMessage.fold { Future { LocalizableString.BAN_FAILED_REPLY } } { reply => reply.from.fold { Future { LocalizableString.BAN_FAILED_INVALID_USER } } { user => - new BotUser(user, msg.chat, bot).init().map { + BotUserRequest.requestBotUser(user, msg.chat, bot).map { case Success(target) if target.isAdmin || target.isCreator => LocalizableString.BAN_FAILED_BAN_ADMIN case Success(target) => target.ban() diff --git a/src/main/scala/org/congressodeiradicali/karlmarx/coreplugins/CorePlugin.scala b/src/main/scala/org/congressodeiradicali/karlmarx/coreplugins/CorePlugin.scala index ba459b1..7276468 100644 --- a/src/main/scala/org/congressodeiradicali/karlmarx/coreplugins/CorePlugin.scala +++ b/src/main/scala/org/congressodeiradicali/karlmarx/coreplugins/CorePlugin.scala @@ -19,8 +19,12 @@ package org.congressodeiradicali.karlmarx.coreplugins import org.congressodeiradicali.karlmarx.Plugin +import scala.concurrent.ExecutionContext + trait CorePlugin extends Plugin { + protected implicit val ec: ExecutionContext = ExecutionContext.global + override val license = "GNU AGPL" override val author = "Congresso dei Radicali" diff --git a/src/main/scala/org/congressodeiradicali/karlmarx/coreplugins/EchoPlugin.scala b/src/main/scala/org/congressodeiradicali/karlmarx/coreplugins/EchoPlugin.scala index a7dff55..b4f9ee2 100644 --- a/src/main/scala/org/congressodeiradicali/karlmarx/coreplugins/EchoPlugin.scala +++ b/src/main/scala/org/congressodeiradicali/karlmarx/coreplugins/EchoPlugin.scala @@ -20,7 +20,8 @@ package org.congressodeiradicali.karlmarx.coreplugins import com.bot4s.telegram.api.declarative.{CommandFilterMagnet, CommandImplicits} import org.congressodeiradicali.karlmarx.Bot -import scala.concurrent.{ExecutionContext, Future} +import scala.concurrent.Future + class EchoPlugin(bot: Bot) extends CorePlugin with CommandImplicits { @@ -30,7 +31,7 @@ class EchoPlugin(bot: Bot) extends CorePlugin with CommandImplicits { override val identifier: String = "echo" override var commandHandlers: Map[CommandFilterMagnet, CommandHandler] = Map( - stringToCommandFilter("echo") -> { (_, argv) => Future { Some(argv.drop(1).mkString(" ")) }(ExecutionContext.global) } + stringToCommandFilter("echo") -> { (_, argv) => Future { Some(argv.drop(1).mkString(" ")) } } ) override var messageHandlers: List[MessageHandler] = List() } diff --git a/src/main/scala/org/congressodeiradicali/karlmarx/coreplugins/PluginManagerPlugin.scala b/src/main/scala/org/congressodeiradicali/karlmarx/coreplugins/PluginManagerPlugin.scala index b884733..0370d40 100644 --- a/src/main/scala/org/congressodeiradicali/karlmarx/coreplugins/PluginManagerPlugin.scala +++ b/src/main/scala/org/congressodeiradicali/karlmarx/coreplugins/PluginManagerPlugin.scala @@ -21,15 +21,13 @@ import com.bot4s.telegram.api.declarative.{CommandFilterMagnet, CommandImplicits import com.bot4s.telegram.models.Message import org.congressodeiradicali.karlmarx.{Bot, LocalizableString} -import scala.concurrent.{ExecutionContext, Future} +import scala.concurrent.Future class PluginManagerPlugin(bot: Bot) extends CorePlugin with CommandImplicits { // This is enabled by default for obvious reasons - users need to be able to enable other plugins on the first start. // Todo: disable it when reasonable enabled = true - private implicit val ec: ExecutionContext = ExecutionContext.global - override val name = "Plugin Manager" override val description = "Manages other plugins" diff --git a/src/main/scala/org/congressodeiradicali/karlmarx/coreplugins/SetPlugin.scala b/src/main/scala/org/congressodeiradicali/karlmarx/coreplugins/SetPlugin.scala index d2bc755..e3d7117 100644 --- a/src/main/scala/org/congressodeiradicali/karlmarx/coreplugins/SetPlugin.scala +++ b/src/main/scala/org/congressodeiradicali/karlmarx/coreplugins/SetPlugin.scala @@ -21,7 +21,7 @@ import com.bot4s.telegram.api.declarative.{CommandFilterMagnet, CommandImplicits import com.bot4s.telegram.models.Message import org.congressodeiradicali.karlmarx.{Bot, LocalizableString} -import scala.concurrent.{ExecutionContext, Future} +import scala.concurrent.Future class SetPlugin(bot: Bot) extends CorePlugin with CommandImplicits { override val name: String = "Set Plugin" @@ -35,7 +35,7 @@ class SetPlugin(bot: Bot) extends CorePlugin with CommandImplicits { val response = argv.drop(2).mkString(" ") // Drop command + trigger bot.redis.set("PLUGIN_SET_" + trigger, response).map { _ => Some(bot.localize(LocalizableString.DONE)) - }(ExecutionContext.global) + } } ) override var messageHandlers: List[MessageHandler] = List( @@ -43,7 +43,7 @@ class SetPlugin(bot: Bot) extends CorePlugin with CommandImplicits { msg.text.map { text => val firstWord = text.takeWhile(_ != ' ') bot.redis.get("PLUGIN_SET_" + firstWord) - }.getOrElse(Future { None }(ExecutionContext.global)) + }.getOrElse(Future { None }) } ) }