From b2405aaafe97c92a333b0fa091faec180c714a9f Mon Sep 17 00:00:00 2001 From: Thibault Duplessis Date: Mon, 23 Mar 2015 02:34:45 +0100 Subject: [PATCH] show video tag path --- app/controllers/Video.scala | 2 +- app/views/video/layout.scala.html | 6 ++- modules/video/src/main/VideoApi.scala | 47 ++++++++++++++----- modules/video/src/main/Youtube.scala | 2 +- .../src/main/{Filter.scala => control.scala} | 0 public/stylesheets/dark.css | 2 +- public/stylesheets/video.css | 8 +++- 7 files changed, 48 insertions(+), 19 deletions(-) rename modules/video/src/main/{Filter.scala => control.scala} (100%) diff --git a/app/controllers/Video.scala b/app/controllers/Video.scala index aca8bf7e16..2054d42919 100644 --- a/app/controllers/Video.scala +++ b/app/controllers/Video.scala @@ -16,7 +16,7 @@ object Video extends LilaController { private def WithUserControl[A](f: UserControl => Fu[A])(implicit ctx: Context): Fu[A] = { val reqTags = get("tags") ?? (_.split(',').toList.map(_.trim.toLowerCase)) - env.api.tag.popularAnd(25, reqTags) map { tags => + env.api.tag.pathsAnd(25, reqTags) map { tags => UserControl( filter = Filter(reqTags), tags = tags) diff --git a/app/views/video/layout.scala.html b/app/views/video/layout.scala.html index 20aa614544..93858e69ab 100644 --- a/app/views/video/layout.scala.html +++ b/app/views/video/layout.scala.html @@ -4,12 +4,14 @@
} diff --git a/modules/video/src/main/VideoApi.scala b/modules/video/src/main/VideoApi.scala index f65f2658db..f4a550a3a7 100644 --- a/modules/video/src/main/VideoApi.scala +++ b/modules/video/src/main/VideoApi.scala @@ -133,21 +133,27 @@ private[video] final class VideoApi( object tag { - private val cache: Cache[List[TagNb]] = LruCache(timeToLive = 1.day) + private val nbCache: Cache[List[TagNb]] = LruCache(timeToLive = 1.day) - def clearCache = fuccess(cache.clear) - - def popularAnd(max: Int, forced: List[Tag]): Fu[List[TagNb]] = popular map { all => - val tags = all take max - val missing = forced filterNot { t => - tags exists (_.tag == t) - } - tags.take(max - missing.size) ::: missing.flatMap { t => - all find (_.tag == t) - } + def clearCache = fuccess { + nbCache.clear } - def popular: Fu[List[TagNb]] = cache(true) { + def pathsAnd(max: Int, forced: List[Tag]): Fu[List[TagNb]] = + popular zip paths(forced) map { + case (all, paths) => + val tags = all take max map { t => + paths find (_._id == t._id) getOrElse TagNb(t._id, 0) + } + val missing = forced filterNot { t => + tags exists (_.tag == t) + } + tags.take(max - missing.size) ::: missing.flatMap { t => + all find (_.tag == t) + } + } + + def popular: Fu[List[TagNb]] = nbCache("") { import reactivemongo.core.commands._ val command = Aggregate(videoColl.name, Seq( Project("tags" -> BSONBoolean(true)), @@ -159,5 +165,22 @@ private[video] final class VideoApi( _.toList.flatMap(_.asOpt[TagNb]) } } + + def paths(tags: List[Tag]): Fu[List[TagNb]] = + if (tags.isEmpty) popular + else nbCache(tags.sorted.mkString(",")) { + import reactivemongo.core.commands._ + val command = Aggregate(videoColl.name, Seq( + Match(BSONDocument("tags" -> BSONDocument("$all" -> tags))), + Project("tags" -> BSONBoolean(true)), + Unwind("tags"), + // Match(BSONDocument("tags" -> BSONDocument("$nin" -> tags))), + GroupField("tags")("nb" -> SumValue(1)), + Sort(Seq(Descending("nb"))) + )) + videoColl.db.command(command) map { + _.toList.flatMap(_.asOpt[TagNb]) + } + } } } diff --git a/modules/video/src/main/Youtube.scala b/modules/video/src/main/Youtube.scala index 84306592f0..6d0f621259 100644 --- a/modules/video/src/main/Youtube.scala +++ b/modules/video/src/main/Youtube.scala @@ -47,7 +47,7 @@ private[video] final class Youtube( case JsSuccess(entries, _) => fuccess(entries.toList) } case res => - println(res.body) + println(s"key: $apiKey") fufail(s"[video youtube] fetch ${res.status}") } } diff --git a/modules/video/src/main/Filter.scala b/modules/video/src/main/control.scala similarity index 100% rename from modules/video/src/main/Filter.scala rename to modules/video/src/main/control.scala diff --git a/public/stylesheets/dark.css b/public/stylesheets/dark.css index 99f2bf3801..cd4c49d380 100644 --- a/public/stylesheets/dark.css +++ b/public/stylesheets/dark.css @@ -29,7 +29,7 @@ body.dark div.chat_menu > a.active { body.dark .crosstable td { border-color: #1a1a1a; } -body.dark #video_side .tag_list a:hover { +body.dark #video_side .tag_list a.full:hover { background: #222; border-color: #444; } diff --git a/public/stylesheets/video.css b/public/stylesheets/video.css index 6283e01ce0..6255d72c30 100644 --- a/public/stylesheets/video.css +++ b/public/stylesheets/video.css @@ -129,20 +129,24 @@ border-left: 2px solid transparent; transition: 0.13s; } -#video_side .tag_list a:hover { +#video_side .tag_list a.full:hover { background: #f8f8f8; border-color: #ccc; } #video_side .tag_list em { float: right; font-weight: bold; - opacity: 0.5; + opacity: 0.6; } #video_side .tag_list a.checked { font-weight: bold; background: #fff; border-color: #d85000!important; } +#video_side .tag_list a.empty { + opacity: 0.6; + cursor: default; +} #video .not_found { margin-top: 200px;