Skip to content

Commit

Permalink
Support user ban (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmvpm committed May 9, 2024
1 parent 480e421 commit 328a892
Showing 1 changed file with 32 additions and 13 deletions.
45 changes: 32 additions & 13 deletions bot/src/main/scala/com/github/mmvpm/bot/OfferServiceBot.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import com.github.mmvpm.bot.model.{ChatID, MessageID}
import com.github.mmvpm.bot.render.Renderer
import com.github.mmvpm.bot.state.State._
import com.github.mmvpm.bot.state.{State, StateManager, Storage}
import com.github.mmvpm.model.OfferStatus
import sttp.client3.SttpBackend

import scala.util.Try
Expand Down Expand Up @@ -80,18 +81,24 @@ class OfferServiceBot[F[_]: Concurrent](
}

private def replyResolved(tag: String)(implicit message: Message): F[Unit] =
for {
nextState <- stateManager.getNextState(tag, stateStorage.get)
_ = stateStorage.set(withoutError(nextState))
replies = renderer.render(nextState, lastMessageStorage.get, lastPhotosStorage.get)
_ <- requestLogged(replies)
} yield ()
Concurrent[F].ifM(isUserBanned)(
reply(
"""
|Вы были забанены за неоднократное нарушение правил сервиса
|
|Связаться с поддержкой: @mmvpm
|""".stripMargin
).void,
for {
nextState <- stateManager.getNextState(tag, stateStorage.get)
_ = stateStorage.set(withoutError(nextState))
replies = renderer.render(nextState, lastMessageStorage.get, lastPhotosStorage.get)
_ <- requestLogged(replies)
} yield ()
)

private def withoutError(state: State): State =
state match {
case Error(returnTo, _) => returnTo
case _ => state
}
private def fail(implicit message: Message): F[Unit] =
reply("Не понял вас :(").void

// internal

Expand Down Expand Up @@ -140,8 +147,11 @@ class OfferServiceBot[F[_]: Concurrent](
case ChatId.Channel(_) => sys.error("channels are not supported")
}

private def fail(implicit message: Message): F[Unit] =
reply("Не понял вас :(").void
private def withoutError(state: State): State =
state match {
case Error(returnTo, _) => returnTo
case _ => state
}

private def safe[A](block: => A, default: A): A =
Try {
Expand All @@ -150,4 +160,13 @@ class OfferServiceBot[F[_]: Concurrent](
logger.error("Bot failed", error)
default
}.get

private def isUserBanned(implicit message: Message): F[Boolean] =
ofsManager.getMyOffers.value.map {
case Left(error) =>
println(s"isUserBanned failed with $error")
false
case Right(offers) =>
offers.count(_.status == OfferStatus.Banned) >= 5
}
}

0 comments on commit 328a892

Please sign in to comment.