From 496e11e56ec8b5edb9fb0b96e54fabed868fb901 Mon Sep 17 00:00:00 2001 From: Thibault Duplessis Date: Sat, 18 Jan 2020 14:10:02 -0600 Subject: [PATCH] preload student light users --- app/controllers/Clas.scala | 9 +++++++-- modules/user/src/main/LightUserApi.scala | 7 ++++--- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/app/controllers/Clas.scala b/app/controllers/Clas.scala index ca68a3db2a..46978cb263 100644 --- a/app/controllers/Clas.scala +++ b/app/controllers/Clas.scala @@ -45,11 +45,15 @@ final class Clas( } } + private def preloadStudentUsers(students: List[lila.clas.Student.WithUser]): Unit = + env.user.lightUserApi.preloadUsers(students.map(_.user)) + def show(id: String) = Auth { implicit ctx => me => isGranted(_.Teacher).??(env.clas.api.clas.isTeacherOf(me, lila.clas.Clas.Id(id))) flatMap { case true => WithClass(me, id) { _ => clas => env.clas.api.student.allOfWithUsers(clas) map { students => + preloadStudentUsers(students) views.html.clas.clas.showToTeacher(clas, students) } } @@ -57,9 +61,10 @@ final class Clas( env.clas.api.clas.byId(lila.clas.Clas.Id(id)) flatMap { _ ?? { clas => env.clas.api.student.activeOfWithUsers(clas) flatMap { students => - if (students.exists(_.student is me)) + if (students.exists(_.student is me)) { + preloadStudentUsers(students) Ok(views.html.clas.clas.showToStudent(clas, students)).fuccess - else notFound + } else notFound } } } diff --git a/modules/user/src/main/LightUserApi.scala b/modules/user/src/main/LightUserApi.scala index b581cd6fa7..b481120ae9 100644 --- a/modules/user/src/main/LightUserApi.scala +++ b/modules/user/src/main/LightUserApi.scala @@ -23,9 +23,10 @@ final class LightUserApi( def invalidate = cache invalidate _ - def preloadOne = cache preloadOne _ - def preloadMany = cache preloadMany _ - def preloadUser(user: User) = cache.set(user.id, user.light.some) + def preloadOne = cache preloadOne _ + def preloadMany = cache preloadMany _ + def preloadUser(user: User) = cache.set(user.id, user.light.some) + def preloadUsers(users: Seq[User]) = users.foreach(preloadUser) private val cache = cacheApi.sync[User.ID, Option[LightUser]]( name = "user.light",