Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jc/rename top mentions #25152

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 29 additions & 28 deletions article/app/controllers/LiveBlogController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class LiveBlogController(

def renderEmail(path: String): Action[AnyContent] = {
Action.async { implicit request =>
mapModel(path, ArticleBlocks, topMentionResult = None) {
mapModel(path, ArticleBlocks, topMentions = None) {
case (minute: MinutePage, _) =>
Future.successful(common.renderEmail(ArticleEmailHtmlPage.html(minute), minute))
case (blog: LiveBlogPage, _) => Future.successful(common.renderEmail(LiveBlogHtmlPage.html(blog), blog))
Expand Down Expand Up @@ -116,16 +116,16 @@ class LiveBlogController(
): Action[AnyContent] = {
Action.async { implicit request: Request[AnyContent] =>
val filter = shouldFilter(filterKeyEvents)
val topMentionResult = getTopMentions(path, topics)
val range = getRange(lastUpdate, page, topMentionResult)
val topMentions = getTopMentions(path, topics)
val range = getRange(lastUpdate, page, topMentions)
val availableTopics = topMentionsService.getTopics(path)

mapModel(path, range, filter, topMentionResult) {
mapModel(path, range, filter, topMentions) {
case (blog: LiveBlogPage, _) if rendered.contains(false) => getJsonForFronts(blog)
case (blog: LiveBlogPage, blocks) if request.forceDCR && lastUpdate.isEmpty =>
Future.successful(renderGuuiJson(blog, blocks, filter, availableTopics, selectedTopics = topics))
case (blog: LiveBlogPage, blocks) =>
getJson(blog, range, isLivePage, filter, blocks.requestedBodyBlocks.getOrElse(Map.empty), topMentionResult)
getJson(blog, range, isLivePage, filter, blocks.requestedBodyBlocks.getOrElse(Map.empty), topMentions)
case (minute: MinutePage, _) =>
Future.successful(common.renderJson(views.html.fragments.minuteBody(minute), minute))
case _ =>
Expand All @@ -140,13 +140,13 @@ class LiveBlogController(
path: String,
range: BlockRange,
filterKeyEvents: Boolean,
topMentionResult: Option[TopMentionsResult],
topMentions: Option[TopMentions],
availableTopics: Option[Seq[TopicWithCount]],
selectedTopics: Option[String],
)(implicit
request: RequestHeader,
): Future[Result] = {
mapModel(path, range, filterKeyEvents, topMentionResult) { (page, blocks) =>
mapModel(path, range, filterKeyEvents, topMentions) { (page, blocks) =>
{
val isAmpSupported = page.article.content.shouldAmplify
val pageType: PageType = PageType(page, request, context)
Expand Down Expand Up @@ -203,9 +203,9 @@ class LiveBlogController(
private[this] def getRange(
lastUpdate: Option[String],
page: Option[String],
topMentionResult: Option[TopMentionsResult],
topMentions: Option[TopMentions],
): BlockRange = {
(lastUpdate.map(ParseBlockId.fromBlockId), page.map(ParseBlockId.fromPageParam), topMentionResult) match {
(lastUpdate.map(ParseBlockId.fromBlockId), page.map(ParseBlockId.fromPageParam), topMentions) match {
case (Some(ParsedBlockId(id)), _, _) => SinceBlockId(id)
case (_, Some(ParsedBlockId(id)), _) => PageWithBlock(id)
case (_, _, Some(_)) => TopicsLiveBlog
Expand All @@ -225,7 +225,7 @@ class LiveBlogController(
isLivePage: Option[Boolean],
filterKeyEvents: Boolean,
requestedBodyBlocks: scala.collection.Map[String, Seq[Block]] = Map.empty,
topMentionResult: Option[TopMentionsResult],
topMentions: Option[TopMentions],
)(implicit request: RequestHeader): Future[Result] = {
val remoteRender = !request.forceDCROff

Expand All @@ -238,7 +238,7 @@ class LiveBlogController(
filterKeyEvents,
remoteRender,
requestedBodyBlocks,
topMentionResult,
topMentions,
)
case _ => Future.successful(common.renderJson(views.html.liveblog.liveBlogBody(liveblog), liveblog))
}
Expand All @@ -248,7 +248,7 @@ class LiveBlogController(
page: PageWithStoryPackage,
lastUpdateBlockId: SinceBlockId,
filterKeyEvents: Boolean,
topMentionResult: Option[TopMentionsResult],
topMentions: Option[TopMentions],
): Seq[BodyBlock] = {
val requestedBlocks = page.article.fields.blocks.toSeq.flatMap {
_.requestedBodyBlocks.getOrElse(lastUpdateBlockId.around, Seq())
Expand All @@ -260,8 +260,8 @@ class LiveBlogController(

if (filterKeyEvents) {
latestBlocks.filter(block => block.eventType == KeyEvent || block.eventType == SummaryEvent)
} else if (topMentionResult.isDefined) {
latestBlocks.filter(block => topMentionResult.get.blocks.contains(block.id))
} else if (topMentions.isDefined) {
latestBlocks.filter(block => topMentions.get.blocks.contains(block.id))
} else latestBlocks

}
Expand All @@ -270,7 +270,7 @@ class LiveBlogController(
requestedBodyBlocks: scala.collection.Map[String, Seq[Block]],
lastUpdateBlockId: SinceBlockId,
filterKeyEvents: Boolean,
topMentionResult: Option[TopMentionsResult],
topMentions: Option[TopMentions],
): Seq[Block] = {
val blocksAround = requestedBodyBlocks.getOrElse(lastUpdateBlockId.around, Seq.empty).takeWhile { block =>
block.id != lastUpdateBlockId.lastUpdate
Expand All @@ -280,8 +280,8 @@ class LiveBlogController(
blocksAround.filter(block =>
block.attributes.keyEvent.getOrElse(false) || block.attributes.summary.getOrElse(false),
)
} else if (topMentionResult.isDefined) {
blocksAround.filter(block => topMentionResult.get.blocks.contains(block.id))
} else if (topMentions.isDefined) {
blocksAround.filter(block => topMentions.get.blocks.contains(block.id))
} else blocksAround
}

Expand All @@ -300,10 +300,10 @@ class LiveBlogController(
filterKeyEvents: Boolean,
remoteRender: Boolean,
requestedBodyBlocks: scala.collection.Map[String, Seq[Block]],
topMentionResult: Option[TopMentionsResult],
topMentions: Option[TopMentions],
)(implicit request: RequestHeader): Future[Result] = {
val newBlocks = getNewBlocks(page, lastUpdateBlockId, filterKeyEvents, topMentionResult)
val newCapiBlocks = getNewBlocks(requestedBodyBlocks, lastUpdateBlockId, filterKeyEvents, topMentionResult)
val newBlocks = getNewBlocks(page, lastUpdateBlockId, filterKeyEvents, topMentions)
val newCapiBlocks = getNewBlocks(requestedBodyBlocks, lastUpdateBlockId, filterKeyEvents, topMentions)

val timelineHtml = views.html.liveblog.keyEvents(
"",
Expand Down Expand Up @@ -361,13 +361,13 @@ class LiveBlogController(
path: String,
range: BlockRange,
filterKeyEvents: Boolean = false,
topMentionResult: Option[TopMentionsResult],
topMentions: Option[TopMentions],
)(
render: (PageWithStoryPackage, Blocks) => Future[Result],
)(implicit request: RequestHeader): Future[Result] = {
capiLookup
.lookup(path, Some(range))
.map(responseToModelOrResult(range, filterKeyEvents, topMentionResult))
.map(responseToModelOrResult(range, filterKeyEvents, topMentions))
.recover(convertApiExceptions)
.flatMap {
case Left((model, blocks)) => render(model, blocks)
Expand All @@ -378,7 +378,7 @@ class LiveBlogController(
private[this] def responseToModelOrResult(
range: BlockRange,
filterKeyEvents: Boolean,
topMentionResult: Option[TopMentionsResult],
topMentions: Option[TopMentions],
)(response: ItemResponse)(implicit request: RequestHeader): Either[(PageWithStoryPackage, Blocks), Result] = {
val supportedContent: Option[ContentType] = response.content.filter(isSupported).map(Content(_))
val supportedContentResult: Either[ContentType, Result] = ModelOrResult(supportedContent, response)
Expand All @@ -395,7 +395,7 @@ class LiveBlogController(
response,
range,
filterKeyEvents,
topMentionResult,
topMentions,
).left
.map(_ -> blocks)
case unknown =>
Expand All @@ -416,11 +416,12 @@ class LiveBlogController(
topMentions <- topMentionsService.getSelectedTopic(blogId, topMentionTopic)
} yield topMentions

topMentionsResult match {
case Some(_) => log.info(s"top mention result was successfully retrieved for ${topics.get}")
case None => if (topics.isDefined) log.warn(s"top mention result couldn't be retrieved for ${topics.get}")
topMentions match {
case Some(_) => log.info(s"top mention result was successfully retrieved for ${selectedTopics.get}")
case None =>
if (selectedTopics.isDefined) log.warn(s"top mention result couldn't be retrieved for ${selectedTopics.get}")
}

topMentionsResult
topMentions
}
}
8 changes: 4 additions & 4 deletions article/app/model/LiveBlogHelpers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ object LiveBlogHelpers extends GuLogging {
liveBlog: Article,
range: BlockRange,
filterKeyEvents: Boolean,
topMentionResult: Option[TopMentionsResult],
topMentions: Option[TopMentions],
): Option[LiveBlogCurrentPage] = {

val pageSize = if (liveBlog.content.tags.tags.map(_.id).contains("sport/sport")) 30 else 10
Expand All @@ -51,7 +51,7 @@ object LiveBlogHelpers extends GuLogging {
_,
range,
filterKeyEvents,
topMentionResult,
topMentions,
),
)

Expand All @@ -64,7 +64,7 @@ object LiveBlogHelpers extends GuLogging {
response: ItemResponse,
range: BlockRange,
filterKeyEvents: Boolean,
topMentionResult: Option[TopMentionsResult],
topMentions: Option[TopMentions],
): Either[LiveBlogPage, Status] = {

val pageSize = if (liveBlog.content.tags.tags.map(_.id).contains("sport/sport")) 30 else 10
Expand All @@ -76,7 +76,7 @@ object LiveBlogHelpers extends GuLogging {
blocks,
range,
filterKeyEvents,
topMentionResult,
topMentions,
)
} getOrElse None

Expand Down
2 changes: 1 addition & 1 deletion article/app/topmentions/TopicService.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package topmentions

import common.{Box, GuLogging}
import model.{TopMentionsDetails, TopMentionsResult, TopMentionsTopic, TopicWithCount}
import model.{TopMentionsDetails, TopMentions, TopMentionsTopic, TopicWithCount}

import scala.concurrent.{ExecutionContext, Future}

Expand Down
10 changes: 5 additions & 5 deletions article/test/LiveBlogControllerTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import play.api.test._
import play.api.test.Helpers._
import org.scalatest.{BeforeAndAfterAll, DoNotDiscover}
import org.scalatestplus.mockito.MockitoSugar
import model.{LiveBlogPage, TopMentionsResult, TopMentionsTopic, TopMentionsTopicType, TopicWithCount, TopicsLiveBlog}
import model.{LiveBlogPage, TopMentions, TopMentionsTopic, TopMentionsTopicType, TopicWithCount, TopicsLiveBlog}
import topmentions.{TopicS3Client, TopicService}

import scala.concurrent.Future
Expand All @@ -31,7 +31,7 @@ import scala.concurrent.Future
trait Setup {
var fakeTopMentionsService = mock[TopicService]
var fakeDcr = new DCRFake()
val topMentionResult = TopMentionsResult(
val topMentions = TopMentions(
name = "Fifa",
`type` = TopMentionsTopicType.Org,
blocks = Seq("56d08042e4b0d38537b1f70b"),
Expand All @@ -55,7 +55,7 @@ import scala.concurrent.Future
when(
fakeTopMentionsService.getSelectedTopic(path, TopMentionsTopic(TopMentionsTopicType.Org, "Fifa")),
) thenReturn Some(
topMentionResult,
topMentions,
)

when(
Expand Down Expand Up @@ -293,8 +293,8 @@ import scala.concurrent.Future
liveBlogController.getTopMentions(path, Some("orgFifa")) should be(None)
}

"getTopMentionsForFilters" should "returns correct topMentionResult given a correct automatic filter query parameter" in new Setup {
liveBlogController.getTopMentions(path, Some("org:Fifa")) should be(Some(topMentionResult))
"getTopMentionsForFilters" should "returns correct topMentions given a correct automatic filter query parameter" in new Setup {
liveBlogController.getTopMentions(path, Some("org:Fifa")) should be(Some(topMentions))
}

"renderArticle" should "returns the first page of filtered blog by topics" in new Setup {
Expand Down
Loading