forked from lichess-org/lila
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVideo.scala
78 lines (70 loc) · 2.17 KB
/
Video.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package controllers
import lila.api.Context
import lila.app._
import lila.common.HTTPRequest
import lila.video.{ Filter, UserControl, View }
import views._
final class Video(env: Env) extends LilaController(env) {
private def api = env.video.api
private def WithUserControl[A](f: UserControl => Fu[A])(implicit ctx: Context): Fu[A] = {
val reqTags = get("tags") ?? (_.split('/').toList.map(_.trim.toLowerCase))
api.tag.paths(reqTags) map { tags =>
UserControl(
filter = Filter(reqTags),
tags = tags,
query = get("q"),
bot = HTTPRequest isCrawler ctx.req
)
} flatMap f
}
def index =
Open { implicit ctx =>
pageHit
WithUserControl { control =>
control.query match {
case Some(query) =>
api.video.search(ctx.me, query, getInt("page") | 1) map { videos =>
Ok(html.video.search(videos, control))
}
case None =>
api.video.byTags(ctx.me, control.filter.tags, getInt("page") | 1) zip
api.video.count.apply map {
case (videos, count) =>
Ok(html.video.index(videos, count, control))
}
}
}
}
def show(id: String) =
Open { implicit ctx =>
WithUserControl { control =>
api.video.find(id) flatMap {
case None => fuccess(NotFound(html.video.bits.notFound(control)))
case Some(video) =>
api.video.similar(ctx.me, video, 9) zip
ctx.userId.?? { userId =>
api.view.add(View.make(videoId = video.id, userId = userId))
} map {
case (similar, _) =>
Ok(html.video.show(video, similar, control))
}
}
}
}
def author(author: String) =
Open { implicit ctx =>
WithUserControl { control =>
api.video.byAuthor(ctx.me, author, getInt("page") | 1) map { videos =>
Ok(html.video.bits.author(author, videos, control))
}
}
}
def tags =
Open { implicit ctx =>
WithUserControl { control =>
api.tag.allPopular map { tags =>
Ok(html.video.bits.tags(tags, control))
}
}
}
}