scala code tweaks

pull/9566/head
Thibault Duplessis 2021-08-12 14:49:39 +02:00
parent 301cf7ef81
commit 3b07f60d85
4 changed files with 35 additions and 41 deletions

View File

@ -89,40 +89,33 @@ final class ClasApi(
}
def isTeacherOf(teacher: User.ID, student: User.ID): Fu[Boolean] =
fuccess(studentCache.isStudent(student)) >>&
colls.student
.aggregateExists(readPreference = ReadPreference.secondaryPreferred) { implicit framework =>
import framework._
Match($doc("userId" -> student)) -> List(
Project($doc("clasId" -> true)),
PipelineOperator(
$doc(
"$lookup" -> $doc(
"from" -> colls.clas.name,
"let" -> $doc("c" -> "$clasId"),
"pipeline" -> $arr(
$doc(
"$match" -> $doc(
"$expr" -> $doc(
"$and" -> $arr(
$doc("$eq" -> $arr("$_id", "$$c")),
$doc("$in" -> $arr(teacher, "$teachers"))
)
)
)
),
$doc("$limit" -> 1),
$doc("$project" -> $id(true))
),
"as" -> "clas"
)
studentCache.isStudent(student) ?? colls.student
.aggregateExists(readPreference = ReadPreference.secondaryPreferred) { implicit framework =>
import framework._
Match($doc("userId" -> student)) -> List(
Project($doc("clasId" -> true)),
PipelineOperator(
$lookup.pipeline(
from = colls.clas,
as = "clas",
local = "clasId",
foreign = "_id",
pipeline = List(
$doc(
"$match" -> $doc(
"$expr" -> $doc("$in" -> $arr(teacher, "$teachers"))
)
),
$doc("$limit" -> 1),
$doc("$project" -> $id(true))
)
),
Match("clas" $ne $arr()),
Limit(1),
Project($id(true))
)
}
)
),
Match("clas" $ne $arr()),
Limit(1),
Project($id(true))
)
}
def archive(c: Clas, t: User, v: Boolean): Funit =
coll.update

View File

@ -232,8 +232,9 @@ package fishnet {
package user {
case class Note(from: String, to: String, text: String, mod: Boolean)
case class KidId(id: String)
case class NonKidId(id: String)
sealed trait ClasId
case class KidId(id: String) extends ClasId
case class NonKidId(id: String) extends ClasId
}
package round {

View File

@ -157,7 +157,7 @@ final private class MsgSecurity(
private def kidCheck(contacts: User.Contacts, isNew: Boolean): Fu[Boolean] =
if (!isNew || !contacts.hasKid) fuTrue
else
(contacts.orig.kidId, contacts.dest.kidId) match {
(contacts.orig.clasId, contacts.dest.clasId) match {
case (a: KidId, b: KidId) => Bus.ask[Boolean]("clas") { AreKidsInSameClass(a, b, _) }
case (t: NonKidId, s: KidId) => isTeacherOf(t.id, s.id)
case (s: KidId, t: NonKidId) => isTeacherOf(t.id, s.id)
@ -165,10 +165,10 @@ final private class MsgSecurity(
}
}
private def isTeacherOf(contacts: User.Contacts) =
Bus.ask[Boolean]("clas") { IsTeacherOf(contacts.orig.id, contacts.dest.id, _) }
private def isTeacherOf(contacts: User.Contacts): Fu[Boolean] =
isTeacherOf(contacts.orig.id, contacts.dest.id)
private def isTeacherOf(teacher: User.ID, student: User.ID) =
private def isTeacherOf(teacher: User.ID, student: User.ID): Fu[Boolean] =
Bus.ask[Boolean]("clas") { IsTeacherOf(teacher, student, _) }
private def isLeaderOf(contacts: User.Contacts) =

View File

@ -6,7 +6,7 @@ import scala.concurrent.duration._
import lila.common.{ EmailAddress, LightUser, NormalizedEmailAddress }
import lila.rating.{ Perf, PerfType }
import lila.hub.actorApi.user.{ KidId, NonKidId }
import lila.hub.actorApi.user.{ ClasId, KidId, NonKidId }
case class User(
id: String,
@ -218,7 +218,7 @@ object User {
def isApiHog = roles.exists(_ contains "ROLE_API_HOG")
def isDaysOld(days: Int) = createdAt isBefore DateTime.now.minusDays(days)
def isHoursOld(hours: Int) = createdAt isBefore DateTime.now.minusHours(hours)
def kidId = if (isKid) KidId(id) else NonKidId(id)
def clasId: ClasId = if (isKid) KidId(id) else NonKidId(id)
}
case class Contacts(orig: Contact, dest: Contact) {
def hasKid = orig.isKid || dest.isKid