Better code in BanPlugin

This commit is contained in:
ekardnam 2019-07-08 09:28:16 +02:00
parent 244d6ac03e
commit 59511fd829
3 changed files with 25 additions and 47 deletions

View file

@ -1,41 +1,38 @@
package org.congressodeiradicali.karlmarx
import com.bot4s.telegram.api.declarative.{CommandFilterMagnet, CommandImplicits}
import com.bot4s.telegram.models.Message
import scala.concurrent.{ExecutionContext, Future, blocking}
import scala.concurrent.{ExecutionContext, Future}
import scala.util.{Failure, Success}
class BanPlugin(localizer: Localizer, bot: Bot) extends Plugin with CommandImplicits {
def canBanUsers(msg: Message) : Boolean = msg.from.fold { false } {u =>
val user = new BotUser(u, msg.chat, bot)
// we should wait for the user to be ready
user.syncAfterInit{user => user.canBanUsers}{_ => false}
}
override var handlers: Map[CommandFilterMagnet, Handler] = Map {
stringToCommandFilter("ban") -> { msg =>
Future {
val text = msg.replyToMessage.fold {
LocalizableString.BAN_FAILED_REPLY
} { reply =>
reply.from.fold {
LocalizableString.BAN_FAILED_INVALID_USER
} { u =>
val user = new BotUser(u, msg.chat, bot)
blocking {
user.syncAfterInit { botuser =>
if (botuser.isAdmin || botuser.isCreator) {
LocalizableString.BAN_FAILED_BAN_ADMIN
} else {
user.ban()
LocalizableString.BAN_SUCCESSFUL
}
} { _ => LocalizableString.BAN_FAILED }
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 }
} { u =>
val user = new BotUser(u, msg.chat, bot)
user.init().map {
case Success(user) => if (user.isAdmin || user.isCreator) {
LocalizableString.BAN_FAILED_BAN_ADMIN
} else {
user.ban()
LocalizableString.BAN_SUCCESSFUL
}
case Failure(_) => LocalizableString.BAN_FAILED
}
}
}
Some(localizer.getString(text))
}(ExecutionContext.global)
futureLocalizedString.map { s =>
Some(localizer.getString(s))
}
}
}
}

View file

@ -40,17 +40,6 @@ class Bot(val token: String) extends TelegramBot
def getLogger() = logger
def fromAdmin(msg: Message) : Boolean = msg.from.fold {
// option is not present
false
} {u =>
val user = new BotUser(u, msg.chat, this)
// we should wait for the user to be ready
user.syncAfterInit { user =>
user.isAdmin || user.isCreator
} { _ => false }
}
var plugins: List[Plugin] = List[Plugin](new PluginManagerPlugin(localizer, this))
def runAgainst(handlerPair: Plugin#HandlerPair, botUser: Option[User], cmd: Command): Boolean = {

View file

@ -39,12 +39,12 @@ class BotUser(private val user: User, private val chat: Chat, private val bot: B
var chatMember: Option[ChatMember] = None
def init() : Future[Try[Unit]] = Future {
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()
Success(this)
}
case Failure(err) => {
/*
@ -57,14 +57,6 @@ class BotUser(private val user: User, private val chat: Chat, private val bot: B
}
}(ExecutionContext.global)
def syncAfterInit[T](callback: BotUser => T)(failed: Throwable => T) : T = {
Await.ready(init(), Duration.Inf).value.get match {
case Success(_) => callback(this)
case Failure(err) => failed(err)
}
}
def isCreator: Boolean = chatMember.exists {_.status == MemberStatus.Creator}
def isAdmin: Boolean = chatMember.exists {_.status == MemberStatus.Administrator}
def isMember: Boolean = chatMember.exists {_.status == MemberStatus.Member}