2016-09-04 17:12:25 -06:00
|
|
|
package lila.mod
|
|
|
|
|
2017-08-17 14:25:50 -06:00
|
|
|
import lila.chat.{ Chat, UserChat }
|
2017-10-28 14:29:25 -06:00
|
|
|
import lila.report.Suspect
|
2021-09-23 09:19:53 -06:00
|
|
|
import lila.swiss.Swiss
|
2016-09-05 02:23:09 -06:00
|
|
|
import lila.tournament.Tournament
|
2021-03-31 12:27:34 -06:00
|
|
|
import lila.user.{ User, UserRepo }
|
2016-09-04 17:12:25 -06:00
|
|
|
|
2016-09-05 02:22:12 -06:00
|
|
|
final class PublicChat(
|
|
|
|
chatApi: lila.chat.ChatApi,
|
|
|
|
tournamentApi: lila.tournament.TournamentApi,
|
2021-09-23 09:19:53 -06:00
|
|
|
swissFeature: lila.swiss.SwissFeature,
|
2021-03-31 12:27:34 -06:00
|
|
|
userRepo: UserRepo
|
2019-12-13 18:17:43 -07:00
|
|
|
)(implicit ec: scala.concurrent.ExecutionContext) {
|
2016-09-04 17:12:25 -06:00
|
|
|
|
2021-09-23 09:19:53 -06:00
|
|
|
def all: Fu[(List[(Tournament, UserChat)], List[(Swiss, UserChat)])] =
|
|
|
|
tournamentChats zip swissChats
|
2017-10-28 14:29:25 -06:00
|
|
|
|
2021-03-31 12:27:34 -06:00
|
|
|
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
|
|
|
}
|
2017-10-28 14:29:25 -06:00
|
|
|
|
|
|
|
private def tournamentChats: Fu[List[(Tournament, UserChat)]] =
|
2019-12-13 07:30:20 -07:00
|
|
|
tournamentApi.fetchVisibleTournaments.flatMap { visibleTournaments =>
|
2021-09-23 09:19:53 -06:00
|
|
|
val ids = visibleTournaments.all.map(_.id) map Chat.Id
|
2019-12-13 07:30:20 -07:00
|
|
|
chatApi.userChat.findAll(ids).map { chats =>
|
2020-08-16 06:37:41 -06:00
|
|
|
chats.flatMap { chat =>
|
2021-09-23 09:19:53 -06:00
|
|
|
visibleTournaments.all.find(_.id == chat.id.value).map(_ -> 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
|
|
|
}
|
2016-09-04 17:12:25 -06:00
|
|
|
|
2021-09-23 09:19:53 -06:00
|
|
|
private def swissChats: Fu[List[(Swiss, UserChat)]] =
|
|
|
|
swissFeature.get(Nil).flatMap { swisses =>
|
|
|
|
val all = swisses.created ::: swisses.started
|
|
|
|
val ids = all.map(_.id.value) map Chat.Id
|
2019-12-13 07:30:20 -07:00
|
|
|
chatApi.userChat.findAll(ids).map { chats =>
|
2020-08-16 06:37:41 -06:00
|
|
|
chats.flatMap { chat =>
|
2021-09-23 09:19:53 -06:00
|
|
|
all.find(_.id.value == chat.id.value).map(_ -> 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-04 17:12:25 -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
|
|
|
*/
|
2016-09-06 02:39:04 -06:00
|
|
|
private def sortTournamentsByRelevance(tournaments: List[(Tournament, UserChat)]) =
|
2021-02-17 03:31:32 -07:00
|
|
|
tournaments.sortBy { t =>
|
|
|
|
(t._1.isFinished, -t._1.nbPlayers)
|
|
|
|
}
|
2016-09-04 17:12:25 -06:00
|
|
|
}
|