From 41e83b5445e325c0ecf36b2ac125397b6f8e7af2 Mon Sep 17 00:00:00 2001 From: ekardnam Date: Fri, 12 Jul 2019 12:05:01 +0200 Subject: [PATCH] Redis async client --- .../congressodeiradicali/karlmarx/Bot.scala | 13 ++++--- .../karlmarx/Plugin.scala | 17 +++++++++ .../karlmarx/RedisAsyncClient.scala | 38 +++++++++++++++++++ .../karlmarx/coreplugins/BanPlugin.scala | 17 +++++++++ .../karlmarx/coreplugins/CorePlugin.scala | 17 +++++++++ .../karlmarx/coreplugins/EchoPlugin.scala | 17 +++++++++ .../coreplugins/PluginManagerPlugin.scala | 17 +++++++++ .../karlmarx/coreplugins/SetPlugin.scala | 30 ++++++++++++--- 8 files changed, 154 insertions(+), 12 deletions(-) create mode 100644 src/main/scala/org/congressodeiradicali/karlmarx/RedisAsyncClient.scala diff --git a/src/main/scala/org/congressodeiradicali/karlmarx/Bot.scala b/src/main/scala/org/congressodeiradicali/karlmarx/Bot.scala index 0bc53de..4fc0926 100644 --- a/src/main/scala/org/congressodeiradicali/karlmarx/Bot.scala +++ b/src/main/scala/org/congressodeiradicali/karlmarx/Bot.scala @@ -24,7 +24,6 @@ import com.bot4s.telegram.future.{Polling, TelegramBot} import com.bot4s.telegram.models.{Message, User} import org.congressodeiradicali.karlmarx.coreplugins.{BanPlugin, EchoPlugin, PluginManagerPlugin, SetPlugin} import slogging.{LogLevel, LoggerConfig, PrintLoggerFactory} -import com.redis._ import scala.concurrent.Future @@ -41,15 +40,17 @@ class Bot(val token: String) extends TelegramBot override val client: RequestHandler[Future] = new ScalajHttpClient(token) - val redis = new RedisClient(sys.env("REDIS_HOST"), 6379) + val redis = new RedisAsyncClient(sys.env("REDIS_HOST"), 6379) def getLogger() = logger + def plugin(plugin: Plugin) = plugin.identifier -> plugin + var plugins: Map[String, Plugin] = Map( - "pluginmanager" -> new PluginManagerPlugin(this), - "ban" -> new BanPlugin(this), - "echo" -> new EchoPlugin(this), - "set" -> new SetPlugin(this) + plugin(new PluginManagerPlugin(this)), + plugin(new BanPlugin(this)), + plugin(new EchoPlugin(this)), + plugin(new SetPlugin(this)) ) // Future[Option[T]] is just two wrappers; remove the inner, useless one diff --git a/src/main/scala/org/congressodeiradicali/karlmarx/Plugin.scala b/src/main/scala/org/congressodeiradicali/karlmarx/Plugin.scala index b264760..8618a2f 100644 --- a/src/main/scala/org/congressodeiradicali/karlmarx/Plugin.scala +++ b/src/main/scala/org/congressodeiradicali/karlmarx/Plugin.scala @@ -1,3 +1,20 @@ +/* + This file is part of karl-marx. + + karl-marx is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + karl-marx is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with karl-marx. If not, see . + */ + package org.congressodeiradicali.karlmarx import com.bot4s.telegram.api.declarative.CommandFilterMagnet diff --git a/src/main/scala/org/congressodeiradicali/karlmarx/RedisAsyncClient.scala b/src/main/scala/org/congressodeiradicali/karlmarx/RedisAsyncClient.scala new file mode 100644 index 0000000..d31bbcd --- /dev/null +++ b/src/main/scala/org/congressodeiradicali/karlmarx/RedisAsyncClient.scala @@ -0,0 +1,38 @@ +/* + This file is part of karl-marx. + + karl-marx is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + karl-marx is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with karl-marx. If not, see . + */ + +package org.congressodeiradicali.karlmarx + +import com.redis._ + +import scala.concurrent.{ExecutionContext, Future} + +class RedisAsyncClient(private val host: String, private val port: Int) { + + implicit val ec: ExecutionContext = ExecutionContext.global + + private val redisClient = new RedisClient(host, port) + + def set(key: Any, value: Any) = Future[Unit] { + redisClient.set(key, value) + } + + def get(key: Any) = Future[Option[String]] { + redisClient.get(key) + } + +} diff --git a/src/main/scala/org/congressodeiradicali/karlmarx/coreplugins/BanPlugin.scala b/src/main/scala/org/congressodeiradicali/karlmarx/coreplugins/BanPlugin.scala index 5e11a2e..2cfa32a 100644 --- a/src/main/scala/org/congressodeiradicali/karlmarx/coreplugins/BanPlugin.scala +++ b/src/main/scala/org/congressodeiradicali/karlmarx/coreplugins/BanPlugin.scala @@ -1,3 +1,20 @@ +/* + This file is part of karl-marx. + + karl-marx is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + karl-marx is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with karl-marx. If not, see . + */ + package org.congressodeiradicali.karlmarx.coreplugins import com.bot4s.telegram.api.declarative.{CommandFilterMagnet, CommandImplicits} diff --git a/src/main/scala/org/congressodeiradicali/karlmarx/coreplugins/CorePlugin.scala b/src/main/scala/org/congressodeiradicali/karlmarx/coreplugins/CorePlugin.scala index 4aa2ae1..ba459b1 100644 --- a/src/main/scala/org/congressodeiradicali/karlmarx/coreplugins/CorePlugin.scala +++ b/src/main/scala/org/congressodeiradicali/karlmarx/coreplugins/CorePlugin.scala @@ -1,3 +1,20 @@ +/* + This file is part of karl-marx. + + karl-marx is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + karl-marx is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with karl-marx. If not, see . + */ + package org.congressodeiradicali.karlmarx.coreplugins import org.congressodeiradicali.karlmarx.Plugin diff --git a/src/main/scala/org/congressodeiradicali/karlmarx/coreplugins/EchoPlugin.scala b/src/main/scala/org/congressodeiradicali/karlmarx/coreplugins/EchoPlugin.scala index 588aba6..f6ba683 100644 --- a/src/main/scala/org/congressodeiradicali/karlmarx/coreplugins/EchoPlugin.scala +++ b/src/main/scala/org/congressodeiradicali/karlmarx/coreplugins/EchoPlugin.scala @@ -1,3 +1,20 @@ +/* + This file is part of karl-marx. + + karl-marx is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + karl-marx is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with karl-marx. If not, see . + */ + package org.congressodeiradicali.karlmarx.coreplugins import com.bot4s.telegram.api.declarative.{CommandFilterMagnet, CommandImplicits} diff --git a/src/main/scala/org/congressodeiradicali/karlmarx/coreplugins/PluginManagerPlugin.scala b/src/main/scala/org/congressodeiradicali/karlmarx/coreplugins/PluginManagerPlugin.scala index 2606acc..8252794 100644 --- a/src/main/scala/org/congressodeiradicali/karlmarx/coreplugins/PluginManagerPlugin.scala +++ b/src/main/scala/org/congressodeiradicali/karlmarx/coreplugins/PluginManagerPlugin.scala @@ -1,3 +1,20 @@ +/* + This file is part of karl-marx. + + karl-marx is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + karl-marx is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with karl-marx. If not, see . + */ + package org.congressodeiradicali.karlmarx.coreplugins import com.bot4s.telegram.api.declarative.{CommandFilterMagnet, CommandImplicits} diff --git a/src/main/scala/org/congressodeiradicali/karlmarx/coreplugins/SetPlugin.scala b/src/main/scala/org/congressodeiradicali/karlmarx/coreplugins/SetPlugin.scala index c33e318..d2bc755 100644 --- a/src/main/scala/org/congressodeiradicali/karlmarx/coreplugins/SetPlugin.scala +++ b/src/main/scala/org/congressodeiradicali/karlmarx/coreplugins/SetPlugin.scala @@ -1,3 +1,20 @@ +/* + This file is part of karl-marx. + + karl-marx is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + karl-marx is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with karl-marx. If not, see . + */ + package org.congressodeiradicali.karlmarx.coreplugins import com.bot4s.telegram.api.declarative.{CommandFilterMagnet, CommandImplicits} @@ -16,16 +33,17 @@ class SetPlugin(bot: Bot) extends CorePlugin with CommandImplicits { stringToCommandFilter("set") -> { (_, argv) => val trigger = argv(1) val response = argv.drop(2).mkString(" ") // Drop command + trigger - bot.redis.set("PLUGIN_SET_" + trigger, response) - Future { Some(bot.localize(LocalizableString.DONE)) }(ExecutionContext.global) + bot.redis.set("PLUGIN_SET_" + trigger, response).map { _ => + Some(bot.localize(LocalizableString.DONE)) + }(ExecutionContext.global) } ) override var messageHandlers: List[MessageHandler] = List( - { msg: Message => Future { - msg.text.flatMap { text => + { msg: Message => + msg.text.map { text => val firstWord = text.takeWhile(_ != ' ') bot.redis.get("PLUGIN_SET_" + firstWord) - } - }(ExecutionContext.global) } + }.getOrElse(Future { None }(ExecutionContext.global)) + } ) }