Skip to content

Commit

Permalink
Merge pull request #15687 from kraktus/safer_pub_wip
Browse files Browse the repository at this point in the history
Bus: use safer version for Class
  • Loading branch information
ornicar committed Jul 10, 2024
2 parents 4400c72 + fe55ced commit 662ecf3
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 19 deletions.
22 changes: 11 additions & 11 deletions modules/clas/src/main/Env.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package lila.clas
import com.softwaremill.macwire.*

import lila.core.config.*
import lila.core.misc.clas.ClasBus

@Module
final class Env(
Expand Down Expand Up @@ -40,17 +41,16 @@ final class Env(
def hasClas(using me: Me) =
lila.core.perm.Granter(_.Teacher) || studentCache.isStudent(me)

lila.common.Bus.subscribeFuns(
"finishGame" -> { case lila.core.game.FinishGame(game, _) => progressApi.onFinishGame(game) },
"clas" -> {
case lila.core.misc.clas.IsTeacherOf(teacher, student, promise) =>
promise.completeWith(api.clas.isTeacherOf(teacher, student))
case lila.core.misc.clas.AreKidsInSameClass(kid1, kid2, promise) =>
promise.completeWith(api.clas.areKidsInSameClass(kid1, kid2))
case lila.core.misc.clas.ClasMatesAndTeachers(kid, promise) =>
promise.completeWith(matesCache.get(kid.id))
}
)
lila.common.Bus.subscribeFun("finishGame"):
case lila.core.game.FinishGame(game, _) => progressApi.onFinishGame(game)

lila.common.Bus.sub[ClasBus]:
case ClasBus.IsTeacherOf(teacher, student, promise) =>
promise.completeWith(api.clas.isTeacherOf(teacher, student))
case ClasBus.AreKidsInSameClass(kid1, kid2, promise) =>
promise.completeWith(api.clas.areKidsInSameClass(kid1, kid2))
case ClasBus.ClasMatesAndTeachers(kid, promise) =>
promise.completeWith(matesCache.get(kid.id))

private class ClasColls(db: lila.db.Db):
val clas = db(CollName("clas_clas"))
Expand Down
13 changes: 13 additions & 0 deletions modules/common/src/main/Bus.scala
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,19 @@ object Bus:
.withTimeout(timeout, s"Bus.ask $channel $msg")
.monSuccess(_.bus.ask(s"${channel}_${msg.getClass}"))

def safeAsk[A, T <: Payload](makeMsg: Promise[A] => T, timeout: FiniteDuration = 2.second)(using
wc: WithChannel[T],
e: Executor,
s: Scheduler
): Fu[A] =
val promise = Promise[A]()
val channel = wc.channel
val msg = makeMsg(promise)
pub(msg)
promise.future
.withTimeout(timeout, s"Bus.safeAsk $channel $msg")
.monSuccess(_.bus.ask(s"${channel}_${msg.getClass}"))

private val bus = EventBus[Payload, Channel, Tellable](
initialCapacity = 4096,
publish = (tellable, event) => tellable ! event
Expand Down
9 changes: 6 additions & 3 deletions modules/core/src/main/misc.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ package map:
case class Exists(id: String, promise: Promise[Boolean])

package clas:
case class AreKidsInSameClass(kid1: UserId, kid2: UserId, promise: Promise[Boolean])
case class IsTeacherOf(teacher: UserId, student: UserId, promise: Promise[Boolean])
case class ClasMatesAndTeachers(kid: UserId, promise: Promise[Set[UserId]])
enum ClasBus:
case AreKidsInSameClass(kid1: UserId, kid2: UserId, promise: Promise[Boolean])
case IsTeacherOf(teacher: UserId, student: UserId, promise: Promise[Boolean])
case ClasMatesAndTeachers(kid: UserId, promise: Promise[Set[UserId]])
object ClasBus:
given bus.WithChannel[ClasBus] = bus.WithChannel[ClasBus]("clas")

package puzzle:
case class StormRun(userId: UserId, score: Int)
Expand Down
4 changes: 2 additions & 2 deletions modules/msg/src/main/MsgSearch.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import reactivemongo.api.bson.*
import lila.common.Bus
import lila.core.LightUser
import lila.db.dsl.{ *, given }
import lila.core.misc.clas.ClasMatesAndTeachers
import lila.core.misc.clas.ClasBus

import lila.core.user.KidMode
import lila.core.userId.UserSearch
Expand Down Expand Up @@ -35,7 +35,7 @@ final class MsgSearch(

private def forKid(q: String)(using me: Me): Fu[MsgSearch.Result] = for
threads <- searchThreads(q)
allMates <- Bus.ask[Set[UserId]]("clas") { ClasMatesAndTeachers(me, _) }
allMates <- Bus.safeAsk[Set[UserId], ClasBus] { ClasBus.ClasMatesAndTeachers(me, _) }
lower = q.toLowerCase
mateIds = allMates.view.filter(_.value.startsWith(lower)).toList.take(15)
mates <- lightUserApi.asyncMany(mateIds)
Expand Down
7 changes: 4 additions & 3 deletions modules/msg/src/main/MsgSecurity.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package lila.msg

import lila.common.Bus
import lila.db.dsl.{ *, given }
import lila.core.misc.clas.{ AreKidsInSameClass, IsTeacherOf }
import lila.core.misc.clas.ClasBus
import lila.core.team.IsLeaderOf
import lila.memo.RateLimit
import lila.core.perm.Granter
Expand Down Expand Up @@ -175,7 +175,8 @@ final private class MsgSecurity(
if !isNew || !hasKid then fuTrue
else
(orig.isKid, dest.isKid) match
case (true, true) => Bus.ask[Boolean]("clas") { AreKidsInSameClass(orig.id, dest.id, _) }
case (true, true) =>
Bus.safeAsk[Boolean, ClasBus] { ClasBus.AreKidsInSameClass(orig.id, dest.id, _) }
case (false, true) => isTeacherOf(orig.id, dest.id)
case (true, false) => isTeacherOf(dest.id, orig.id)
case _ => fuFalse
Expand All @@ -184,7 +185,7 @@ final private class MsgSecurity(
isTeacherOf(contacts.orig.id, contacts.dest.id)

private def isTeacherOf(teacher: UserId, student: UserId): Fu[Boolean] =
Bus.ask[Boolean]("clas") { IsTeacherOf(teacher, student, _) }
Bus.safeAsk[Boolean, ClasBus] { ClasBus.IsTeacherOf(teacher, student, _) }

private def isLeaderOf(contacts: Contacts) =
Bus.ask[Boolean]("teamIsLeaderOf") { IsLeaderOf(contacts.orig.id, contacts.dest.id, _) }
Expand Down

0 comments on commit 662ecf3

Please sign in to comment.