Skip to content

Commit 328a892

Browse files
committed
Support user ban (#46)
1 parent 480e421 commit 328a892

File tree

1 file changed

+32
-13
lines changed

1 file changed

+32
-13
lines changed

bot/src/main/scala/com/github/mmvpm/bot/OfferServiceBot.scala

+32-13
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import com.github.mmvpm.bot.model.{ChatID, MessageID}
1616
import com.github.mmvpm.bot.render.Renderer
1717
import com.github.mmvpm.bot.state.State._
1818
import com.github.mmvpm.bot.state.{State, StateManager, Storage}
19+
import com.github.mmvpm.model.OfferStatus
1920
import sttp.client3.SttpBackend
2021

2122
import scala.util.Try
@@ -80,18 +81,24 @@ class OfferServiceBot[F[_]: Concurrent](
8081
}
8182

8283
private def replyResolved(tag: String)(implicit message: Message): F[Unit] =
83-
for {
84-
nextState <- stateManager.getNextState(tag, stateStorage.get)
85-
_ = stateStorage.set(withoutError(nextState))
86-
replies = renderer.render(nextState, lastMessageStorage.get, lastPhotosStorage.get)
87-
_ <- requestLogged(replies)
88-
} yield ()
84+
Concurrent[F].ifM(isUserBanned)(
85+
reply(
86+
"""
87+
|Вы были забанены за неоднократное нарушение правил сервиса
88+
|
89+
|Связаться с поддержкой: @mmvpm
90+
|""".stripMargin
91+
).void,
92+
for {
93+
nextState <- stateManager.getNextState(tag, stateStorage.get)
94+
_ = stateStorage.set(withoutError(nextState))
95+
replies = renderer.render(nextState, lastMessageStorage.get, lastPhotosStorage.get)
96+
_ <- requestLogged(replies)
97+
} yield ()
98+
)
8999

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

96103
// internal
97104

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

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

146156
private def safe[A](block: => A, default: A): A =
147157
Try {
@@ -150,4 +160,13 @@ class OfferServiceBot[F[_]: Concurrent](
150160
logger.error("Bot failed", error)
151161
default
152162
}.get
163+
164+
private def isUserBanned(implicit message: Message): F[Boolean] =
165+
ofsManager.getMyOffers.value.map {
166+
case Left(error) =>
167+
println(s"isUserBanned failed with $error")
168+
false
169+
case Right(offers) =>
170+
offers.count(_.status == OfferStatus.Banned) >= 5
171+
}
153172
}

0 commit comments

Comments
 (0)