Skip to content

Commit

Permalink
show latest update on homepage
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Dec 8, 2023
1 parent 313176f commit 27900f3
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 32 deletions.
6 changes: 3 additions & 3 deletions app/mashup/Preload.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ final class Preload(
lightUserApi: LightUserApi,
roundProxy: lila.round.GameProxyRepo,
simulIsFeaturable: SimulIsFeaturable,
lastPostCache: lila.blog.LastPostCache,
getLastUpdate: lila.blog.DailyFeed.GetLastUpdate,
lastPostsCache: AsyncLoadingCache[Unit, List[UblogPost.PreviewPost]],
msgApi: lila.msg.MsgApi,
relayApi: lila.relay.RelayApi,
Expand Down Expand Up @@ -105,7 +105,7 @@ final class Preload(
currentGame,
simulIsFeaturable,
blindGames,
lastPostCache.apply,
getLastUpdate(),
ublogPosts,
withPerfs,
hasUnreadLichessMessage = lichessMsg
Expand Down Expand Up @@ -148,7 +148,7 @@ object Preload:
currentGame: Option[Preload.CurrentGame],
isFeaturable: Simul => Boolean,
blindGames: List[Pov],
lastPost: Option[lila.blog.MiniPost],
lastUpdate: Option[lila.blog.DailyFeed.Update],
ublogPosts: List[UblogPost.PreviewPost],
me: Option[User.WithPerfs],
hasUnreadLichessMessage: Boolean
Expand Down
32 changes: 15 additions & 17 deletions app/views/lobby/bits.scala
Original file line number Diff line number Diff line change
Expand Up @@ -84,27 +84,25 @@ object bits:
)
)

def lastPosts(lichess: Option[lila.blog.MiniPost], uposts: List[lila.ublog.UblogPost.PreviewPost])(using
ctx: Context
def lastPosts(update: Option[lila.blog.DailyFeed.Update], uposts: List[lila.ublog.UblogPost.PreviewPost])(
using ctx: Context
): Frag =
div(cls := "lobby__blog ublog-post-cards")(
lichess
.filter(_.forKids || ctx.kid.no)
.map: post =>
val imgSize = UblogPost.thumbnail.Size.Small
a(cls := "ublog-post-card ublog-post-card--link", href := routes.Blog.show(post.id, post.slug))(
img(
src := post.image,
cls := "ublog-post-card__image",
widthA := imgSize.width,
heightA := imgSize.height
),
update
.map: up =>
div(
cls := List(
"ublog-post-card daily-feed__update" -> true,
"daily-feed__update--fresh" -> up.isFresh
)
)(
span(cls := "ublog-post-card__content")(
h2(cls := "ublog-post-card__title")(post.title),
semanticDate(post.date)(using ctx.lang)(cls := "ublog-post-card__over-image")
h2(cls := "daily-feed__update__day text", dataIcon := licon.Star)(
a(href := s"${routes.DailyFeed.index}#${up.dayString}")(semanticDate(up.day))
),
div(cls := "daily-feed__update__markup")(rawHtml(up.rendered))
)
)
,
),
ctx.kid.no option uposts.map:
views.html.ublog.post.card(_, showAuthor = views.html.ublog.post.ShowAt.bottom, showIntro = false)
)
Expand Down
2 changes: 1 addition & 1 deletion app/views/lobby/home.scala
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ object home:
,
puzzle.map: p =>
views.html.puzzle.embed.dailyLink(p)(cls := "lobby__puzzle"),
bits.lastPosts(lastPost, ublogPosts),
bits.lastPosts(lastUpdate, ublogPosts),
ctx.noBot option bits.underboards(tours, simuls, leaderboard, tournamentWinners),
div(cls := "lobby__support")(
a(href := routes.Plan.index)(
Expand Down
33 changes: 24 additions & 9 deletions modules/blog/src/main/DailyFeed.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ object DailyFeed:

lazy val dayString: String = day.toString

def title = "Daily update - " + dayString
lazy val title = "Daily update - " + dayString

lazy val isFresh = instant isAfter nowInstant.minusDays(1)

private val renderer = lila.common.MarkdownRender(
autoLink = false,
Expand All @@ -27,6 +29,8 @@ object DailyFeed:
header = true
)

type GetLastUpdate = () => Option[Update]

final class DailyFeed(coll: Coll, cacheApi: CacheApi)(using Executor):

import DailyFeed.Update
Expand All @@ -37,22 +41,33 @@ final class DailyFeed(coll: Coll, cacheApi: CacheApi)(using Executor):
)
private given BSONDocumentHandler[Update] = Macros.handler

private val cache = cacheApi[Max, List[Update]](512, "dailyFeed.updates"):
_.expireAfterWrite(3 seconds).buildAsyncFuture: nb =>
coll.find($empty).sort($sort.desc("_id")).cursor[Update]().list(nb.value)
private def clearCache() = cache.underlying.synchronous.invalidateAll()

export cache.{ get as recent }
private object cache:
private var mutableLastUpdate: Option[Update] = None
val store = cacheApi[Max, List[Update]](4, "dailyFeed.updates"):
_.expireAfterWrite(1 minute).buildAsyncFuture: nb =>
coll
.find($empty)
.sort($sort.desc("_id"))
.cursor[Update]()
.list(nb.value)
.addEffect: ups =>
mutableLastUpdate = ups.headOption
def clear() = store.underlying.synchronous.invalidateAll()
def lastUpdate: DailyFeed.GetLastUpdate = () => mutableLastUpdate
store.get(Max(1)) // populate lastUpdate

export cache.store.{ get as recent }
export cache.lastUpdate

def get(day: LocalDate): Fu[Option[Update]] = coll.one[Update]($id(day))

def set(update: Update, from: Option[Update]): Funit = for
_ <- from.filter(_.day != update.day).so(up => coll.delete.one($id(up.day)).void)
_ <- coll.update.one($id(update.day), update, upsert = true).void andDo clearCache()
_ <- coll.update.one($id(update.day), update, upsert = true).void andDo cache.clear()
yield ()

def delete(id: LocalDate): Funit =
coll.delete.one($id(id)).void andDo clearCache()
coll.delete.one($id(id)).void andDo cache.clear()

def form(from: Option[Update]) =
import play.api.data.*
Expand Down
6 changes: 4 additions & 2 deletions modules/blog/src/main/Env.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,7 @@ final class Env(

lazy val lastPostCache = wire[LastPostCache]

private lazy val feedColl = db(CollName("daily_feed"))
lazy val dailyFeed = wire[DailyFeed]
private val feedColl = db(CollName("daily_feed"))
val dailyFeed = wire[DailyFeed]

export dailyFeed.lastUpdate
18 changes: 18 additions & 0 deletions ui/lobby/css/_lobby.scss
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,24 @@ body {
.ublog-post-card:hover {
box-shadow: none;
}
.daily-feed {
&__update {
&--fresh {
outline: 1px solid $c-brag;
}
&__day {
color: $c-brag;
margin: 0.5em 0 0.6em 0;
}
a {
color: $c-brag;
}
time {
font-size: 1em;
opacity: 1;
}
}
}
}

.tour-team-icon {
Expand Down

0 comments on commit 27900f3

Please sign in to comment.