Skip to content

Commit

Permalink
force broadcast studies visibility based on broadcast tier
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Nov 11, 2024
1 parent 7173a73 commit 2ed018e
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 6 deletions.
4 changes: 2 additions & 2 deletions modules/relay/src/main/Env.scala
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ final class Env(

private lazy val notifier = wire[RelayNotifier]

private lazy val studyPropagation = wire[RelayStudyPropagation]

lazy val jsonView = wire[JsonView]

lazy val listing: RelayListing = wire[RelayListing]
Expand Down Expand Up @@ -124,8 +126,6 @@ final class Env(

private val relayFidePlayerApi = wire[RelayFidePlayerApi]

private val studyPropagation = wire[RelayStudyPropagation]

import lila.common.config.given
private val syncOnlyIds = config.getOptional[List[String]]("relay.syncOnlyIds").map(RelayTourId.from)

Expand Down
5 changes: 4 additions & 1 deletion modules/relay/src/main/RelayApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ final class RelayApi(
formatApi: RelayFormatApi,
cacheApi: CacheApi,
players: RelayPlayerApi,
studyPropagation: RelayStudyPropagation,
picfitApi: PicfitApi
)(using Executor, akka.stream.Materializer, play.api.Mode):

Expand Down Expand Up @@ -231,6 +232,7 @@ final class RelayApi(
)
_ <- data.grouping.so(updateGrouping(tour, _))
_ <- playerEnrich.onPlayerTextareaUpdate(tour, prev)
_ <- (tour.tier != prev.tier).so(studyPropagation.onTierChange(tour))
yield
players.invalidate(tour.id)
(tour.id :: data.grouping.so(_.tourIds)).foreach(withTours.invalidate)
Expand Down Expand Up @@ -265,6 +267,7 @@ final class RelayApi(
me,
withRatings = true,
_.copy(
visibility = tour.studyVisibility,
members = lastStudy.fold(StudyMembers.empty)(_.members) + StudyMember(me, StudyMember.Role.Write)
)
)
Expand Down Expand Up @@ -415,7 +418,7 @@ final class RelayApi(
s,
_.copy(
id = round.studyId,
visibility = lila.core.study.Visibility.public
visibility = to.studyVisibility
)
) >>
studyApi.addTopics(round.studyId, List(StudyTopic.broadcast.value))
Expand Down
14 changes: 11 additions & 3 deletions modules/relay/src/main/RelayStudyPropagation.scala
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
package lila.relay

import lila.study.{ StudyRepo, Study }
import lila.study.{ StudyRepo, Study, StudyApi }

/* Propagates study changes across all studies of a broadcast,
* or even all studies of all broadcasts of the group */
private final class RelayStudyPropagation(
roundRepo: RelayRoundRepo,
groupRepo: RelayGroupRepo,
playerEnrich: RelayPlayerEnrich,
studyRepo: StudyRepo
studyRepo: StudyRepo,
studyApi: StudyApi
)(using Executor):

// force studies visibility based on broadcast tier
def onTierChange(tour: RelayTour) =
roundRepo
.studyIdsOf(tour.id)
.flatMap:
_.sequentiallyVoid: id =>
studyApi.setVisibility(id, tour.studyVisibility)

// if the study is a round, propagate members to all round studies of the tournament group
def onStudyMembersChange(study: Study) =
study.isRelay.so:
Expand Down
6 changes: 6 additions & 0 deletions modules/relay/src/main/RelayTour.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import lila.core.i18n.Language
import lila.core.id.ImageId
import lila.core.misc.PicfitUrl
import lila.core.fide.FideTC
import lila.core.study.Visibility

case class RelayTour(
@Key("_id") id: RelayTourId,
Expand Down Expand Up @@ -47,6 +48,11 @@ case class RelayTour(
def tierIs(selector: RelayTour.Tier.Selector) =
tier.fold(false)(_ == selector(RelayTour.Tier))

def studyVisibility: Visibility =
if tier.has(RelayTour.Tier.PRIVATE)
then Visibility.`private`
else Visibility.public

object RelayTour:

val maxRelays = 64
Expand Down
6 changes: 6 additions & 0 deletions modules/study/src/main/StudyApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,12 @@ final class StudyApi(
sendTo(study.id)(_.setTopics(topics, who))
topicApi.recompute()

def setVisibility(studyId: StudyId, visibility: hub.Visibility): Funit =
sequenceStudy(studyId): study =>
(study.visibility != visibility).so:
for _ <- studyRepo.updateSomeFields(study.copy(visibility = visibility))
yield sendTo(study.id)(_.reloadAll)

def addTopics(studyId: StudyId, topics: List[String]) =
sequenceStudy(studyId): study =>
studyRepo.updateTopics(study.addTopics(StudyTopics.fromStrs(topics, StudyTopics.studyMax)))
Expand Down

0 comments on commit 2ed018e

Please sign in to comment.