Skip to content

Commit

Permalink
allow up to 25 embedded PGNs in official lichess blog posts
Browse files Browse the repository at this point in the history
until we need 26
  • Loading branch information
ornicar committed Sep 24, 2024
1 parent 65eca16 commit e403d21
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion modules/api/src/main/Env.scala
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ final class Env(
promise.completeWith(chatFreshness.of(source))
},
"lpv" -> {
case AllPgnsFromText(text, p) => p.completeWith(textLpvExpand.allPgnsFromText(text))
case AllPgnsFromText(text, max, p) => p.completeWith(textLpvExpand.allPgnsFromText(text, max))
case LpvLinkRenderFromText(text, p) => p.completeWith(textLpvExpand.linkRenderFromText(text))
}
)
Expand Down
6 changes: 2 additions & 4 deletions modules/api/src/main/TextLpvExpand.scala
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,14 @@ final class TextLpvExpand(
(url.contains("/black")).option(attr("data-orientation") := "black")
)

private val maxPgnsFromBlog = 20

// used by blogs & ublogs to build game|chapter id -> pgn maps
// the substitution happens later in blog/BlogApi or common/MarkdownRender
def allPgnsFromText(text: String): Fu[Map[String, LpvEmbed]] =
def allPgnsFromText(text: String, max: Max): Fu[Map[String, LpvEmbed]] =
regex.blogPgnCandidatesRe
.findAllMatchIn(text)
.map(_.group(1))
.toList
.foldLeft(maxPgnsFromBlog -> List.empty[Fu[(String, Option[LpvEmbed])]]):
.foldLeft(max.value -> List.empty[Fu[(String, Option[LpvEmbed])]]):
case ((0, replacements), _) => 0 -> replacements
case ((counter, replacements), candidate) =>
val (cost, replacement) = candidate match
Expand Down
2 changes: 1 addition & 1 deletion modules/core/src/main/misc.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ package lpv:
case PublicPgn(pgn: PgnStr)
case PrivateStudy
type LinkRender = (String, String) => Option[scalatags.Text.Frag]
case class AllPgnsFromText(text: String, promise: Promise[Map[String, LpvEmbed]])
case class AllPgnsFromText(text: String, max: Max, promise: Promise[Map[String, LpvEmbed]])
case class LpvLinkRenderFromText(text: String, promise: Promise[LinkRender])

package mailer:
Expand Down
10 changes: 6 additions & 4 deletions modules/ublog/src/main/UblogMarkup.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,18 @@ final class UblogMarkup(
)

def apply(post: UblogPost) = cache
.get((post.id, post.markdown))
.get((post.id, post.markdown, maxPgn(post)))
.map: html =>
scalatags.Text.all.raw(html.value)

private val cache = cacheApi[(UblogPostId, Markdown), Html](1024, "ublog.markup"):
private def maxPgn(post: UblogPost) = Max(if post.isLichess then 25 else 20)

private val cache = cacheApi[(UblogPostId, Markdown, Max), Html](1024, "ublog.markup"):
_.maximumSize(2048)
.expireAfterWrite(if mode.isProd then 20 minutes else 1 second)
.buildAsyncFuture: (id, markdown) =>
.buildAsyncFuture: (id, markdown, max) =>
Bus
.ask("lpv")(AllPgnsFromText(markdown.value, _))
.ask("lpv")(AllPgnsFromText(markdown.value, max, _))
.andThen { case scala.util.Success(pgns) =>
pgnCache.putAll(pgns)
}
Expand Down

0 comments on commit e403d21

Please sign in to comment.