make relays likeable

pull/3674/head
Thibault Duplessis 2017-10-06 19:56:06 -05:00
parent 867e6d999e
commit 8e20a6690f
13 changed files with 55 additions and 11 deletions

View File

@ -13,7 +13,7 @@ object Relay extends LilaController {
private val env = Env.relay
def index = Open { implicit ctx =>
env.api.all map { sel =>
env.api.all(ctx.me) map { sel =>
Ok(html.relay.index(sel))
}
}

View File

@ -1,6 +1,6 @@
@(sel: lila.relay.Relay.Selection)(implicit ctx: Context)
@sublist(name: String, relays: List[lila.relay.Relay.WithStudy]) = {
@sublist(name: String, relays: List[lila.relay.Relay.WithStudyAndLiked]) = {
@if(relays.nonEmpty) {
<section>
<h2>@name</h2>

View File

@ -1,8 +1,13 @@
@(r: lila.relay.Relay.WithStudy)(implicit ctx: Context)
@(r: lila.relay.Relay.WithStudyAndLiked)(implicit ctx: Context)
<a class="overlay" href="@routes.Relay.show(r.relay.slug, r.relay.id.value)"></a>
<h2>
<i class="icon" data-icon=""></i>
<strong>@r.relay.name</strong>
<span>
<i data-icon="@if(r.liked){}else{}"></i> @r.relay.likes.value •
@usernameOrId(r.relay.ownerId) •
@momentFromNow(r.relay.startsAt | r.relay.createdAt)
</span>
</h2>
<div class="body">
<p>@r.relay.description</p>

View File

@ -5,6 +5,8 @@ import reactivemongo.bson._
object BSONHandlers {
import lila.study.BSONHandlers.LikesBSONHandler
implicit val relayIdHandler = stringAnyValHandler[Relay.Id](_.value, Relay.Id.apply)
import Relay.Sync

View File

@ -35,6 +35,13 @@ final class Env(
sync = sync,
api = api
)))
system.lilaBus.subscribe(system.actorOf(Props(new Actor {
import lila.study.actorApi._
def receive = {
case lila.study.actorApi.StudyLikes(id, likes) => api.setLikes(Relay.Id(id.value), likes)
}
})), 'studyLikes)
}
object Env {

View File

@ -11,6 +11,7 @@ case class Relay(
description: String,
sync: Relay.Sync,
ownerId: User.ID,
likes: Study.Likes,
startsAt: Option[DateTime],
finishedAt: Option[DateTime],
createdAt: DateTime
@ -70,9 +71,11 @@ object Relay {
case class WithStudy(relay: Relay, study: Study)
case class WithStudyAndLiked(relay: Relay, study: Study, liked: Boolean)
case class Selection(
created: List[WithStudy],
started: List[WithStudy],
closed: List[WithStudy]
created: List[WithStudyAndLiked],
started: List[WithStudyAndLiked],
closed: List[WithStudyAndLiked]
)
}

View File

@ -17,6 +17,7 @@ final class RelayApi(
) {
import BSONHandlers._
import lila.study.BSONHandlers.LikesBSONHandler
def byId(id: Relay.Id) = coll.byId[Relay](id.value)
@ -33,10 +34,10 @@ final class RelayApi(
}
}
def all: Fu[Relay.Selection] =
created.flatMap(withStudy) zip
started.flatMap(withStudy) zip
closed.flatMap(withStudy) map {
def all(me: Option[User]): Fu[Relay.Selection] =
created.flatMap(withStudyAndLiked(me)) zip
started.flatMap(withStudyAndLiked(me)) zip
closed.flatMap(withStudyAndLiked(me)) map {
case c ~ s ~ t => Relay.Selection(c, s, t)
}
@ -55,6 +56,9 @@ final class RelayApi(
}
}
def setLikes(id: Relay.Id, likes: lila.study.Study.Likes): Funit =
coll.updateField($id(id), "likes", likes).void
def created = coll.find($doc(
"startsAt" $gt DateTime.now
)).sort($sort asc "startsAt").list[Relay]()
@ -134,4 +138,14 @@ final class RelayApi(
studies.find(_.id == relay.studyId) map { Relay.WithStudy(relay, _) }
}
}
private def withStudyAndLiked(me: Option[User])(relays: List[Relay]): Fu[List[Relay.WithStudyAndLiked]] =
studyApi byIds relays.map(_.studyId) flatMap studyApi.withLiked(me) map { s =>
relays.flatMap { relay =>
s.find(_.study.id == relay.studyId) map {
case Study.WithLiked(study, liked) => Relay.WithStudyAndLiked(relay, study, liked)
}
}
}
}

View File

@ -65,6 +65,7 @@ object RelayForm {
description = description,
ownerId = user.id,
sync = makeSync,
likes = lila.study.Study.Likes(1),
createdAt = DateTime.now,
finishedAt = none,
startsAt = startsAt

View File

@ -305,7 +305,7 @@ object BSONHandlers {
}
import Study.Likes
private[study] implicit val LikesBSONHandler = intAnyValHandler[Likes](_.value, Likes.apply)
implicit val LikesBSONHandler = intAnyValHandler[Likes](_.value, Likes.apply)
import Study.Rank
private[study] implicit val RankBSONHandler = dateIsoHandler[Rank](Iso[DateTime, Rank](Rank.apply, _.value))

View File

@ -147,6 +147,8 @@ object Study {
case class WithChaptersAndLiked(study: Study, chapters: Seq[Chapter.Name], liked: Boolean)
case class WithLiked(study: Study, liked: Boolean)
case class LightStudy(isPublic: Boolean, contributors: Set[User.ID])
val idSize = 8

View File

@ -600,6 +600,7 @@ final class StudyApi(
def like(studyId: Study.Id, userId: User.ID, v: Boolean, uid: Uid): Funit =
studyRepo.like(studyId, userId, v) map { likes =>
sendTo(studyId, Socket.SetLiking(Study.Liking(likes, v), uid))
bus.publish(actorApi.StudyLikes(studyId, likes), 'studyLikes)
if (v) studyRepo byId studyId foreach {
_ foreach { study =>
if (userId != study.ownerId && study.isPublic)
@ -615,6 +616,13 @@ final class StudyApi(
def chapterMetadatas = chapterRepo.orderedMetadataByStudy _
def withLiked(me: Option[User])(studies: Seq[Study]): Fu[Seq[Study.WithLiked]] =
me.?? { u => studyRepo.filterLiked(u, studies.map(_.id)) } map { liked =>
studies.map { study =>
Study.WithLiked(study, liked(study.id))
}
}
private def sendStudyEnters(study: Study, userId: User.ID) = bus.publish(
lila.hub.actorApi.study.StudyDoor(
userId = userId,

View File

@ -10,3 +10,4 @@ case class ExplorerGame(ch: Chapter.Id, path: String, gameId: String, insert: Bo
def chapterId = ch
val position = Position.Ref(chapterId, Path(path))
}
case class StudyLikes(studyId: Study.Id, likes: Study.Likes)

View File

@ -451,6 +451,7 @@ body.dark div.user_show div.user-infos.scroll-shadow-hard {
}
body.dark div.game_row:hover,
body.dark .studies .study:hover,
body.dark .relays .relay:hover,
body.dark .coaches .coach:hover {
background: rgba(27, 51, 68, 0.5)!important;
}