lila/modules/mod/src/main/PublicChat.scala

66 lines
2.1 KiB
Scala
Raw Normal View History

package lila.mod
2017-08-17 14:25:50 -06:00
import lila.chat.{ Chat, UserChat }
import lila.report.Suspect
2016-09-05 02:23:09 -06:00
import lila.simul.Simul
import lila.tournament.Tournament
import lila.user.{ User, UserRepo }
final class PublicChat(
chatApi: lila.chat.ChatApi,
tournamentApi: lila.tournament.TournamentApi,
simulEnv: lila.simul.Env,
userRepo: UserRepo
)(implicit ec: scala.concurrent.ExecutionContext) {
def all: Fu[(List[(Tournament, UserChat)], List[(Simul, UserChat)])] =
tournamentChats zip simulChats
def deleteAll(userId: User.ID): Funit =
userRepo byId userId map2 Suspect flatMap { _ ?? deleteAll }
def deleteAll(suspect: Suspect): Funit =
2020-09-21 01:28:28 -06:00
all.flatMap { case (tours, simuls) =>
(tours.map(_._2) ::: simuls.map(_._2))
.filter(_ hasLinesOf suspect.user)
.map(chatApi.userChat.delete(_, suspect.user, _.Global))
.sequenceFu
.void
2020-05-05 22:11:15 -06:00
}
private def tournamentChats: Fu[List[(Tournament, UserChat)]] =
2019-12-13 07:30:20 -07:00
tournamentApi.fetchVisibleTournaments.flatMap { visibleTournaments =>
val ids = visibleTournaments.all.map(_.id) map Chat.Id.apply
chatApi.userChat.findAll(ids).map { chats =>
2020-08-16 06:37:41 -06:00
chats.flatMap { chat =>
2020-04-29 08:30:36 -06:00
visibleTournaments.all.find(_.id == chat.id.value).map(tour => (tour, chat))
2020-08-16 06:37:41 -06:00
}
2019-12-13 07:30:20 -07:00
} map sortTournamentsByRelevance
2016-09-05 02:19:48 -06:00
}
private def simulChats: Fu[List[(Simul, UserChat)]] =
2019-12-13 07:30:20 -07:00
fetchVisibleSimuls.flatMap { simuls =>
val ids = simuls.map(_.id) map Chat.Id.apply
chatApi.userChat.findAll(ids).map { chats =>
2020-08-16 06:37:41 -06:00
chats.flatMap { chat =>
2020-04-29 08:30:36 -06:00
simuls.find(_.id == chat.id.value).map(simul => (simul, chat))
2020-08-16 06:37:41 -06:00
}
2019-12-13 07:30:20 -07:00
}
2016-09-05 02:19:48 -06:00
}
2016-09-05 02:23:09 -06:00
private def fetchVisibleSimuls: Fu[List[Simul]] = {
2020-05-05 22:11:15 -06:00
simulEnv.allCreatedFeaturable.get {} zip
simulEnv.repo.allStarted zip
2020-09-21 01:28:28 -06:00
simulEnv.repo.allFinishedFeaturable(3) map { case ((created, started), finished) =>
2019-12-13 07:30:20 -07:00
created ::: started ::: finished
2020-09-21 01:28:28 -06:00
}
2016-09-05 02:19:48 -06:00
}
2020-10-10 03:08:23 -06:00
/** Sort the tournaments by the tournaments most likely to require moderation attention
2019-12-13 07:30:20 -07:00
*/
private def sortTournamentsByRelevance(tournaments: List[(Tournament, UserChat)]) =
tournaments.sortBy { t =>
(t._1.isFinished, -t._1.nbPlayers)
}
}