| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,9 +1,45 @@ | |||
| 1 | 1 | package common.facia | |
| 2 | 2 | ||
| 3 | + import model.facia.PressedCollection | ||
| 4 | + import model.{FrontProperties, PressedPage, SeoData} | ||
| 3 | 5 | import model.pressed._ | |
| 4 | 6 | ||
| 5 | 7 | object FixtureBuilder { | |
| 6 | 8 | ||
| 9 | + def mkContent(id: Int): PressedContent = FixtureBuilder.mkPressedContent(id) | ||
| 10 | + | ||
| 11 | + def mkPressedCollection(id: String, curated: Seq[PressedContent] = IndexedSeq.empty, backfill: Seq[PressedContent] = IndexedSeq.empty, maxItemsToDisplay: Option[Int] = None) = { | ||
| 12 | + PressedCollection( | ||
| 13 | + id = "test-collection", | ||
| 14 | + displayName = s"Test Collection $id", | ||
| 15 | + curated = curated.toList, | ||
| 16 | + backfill = backfill.toList, | ||
| 17 | + treats = List.empty, | ||
| 18 | + lastUpdated = None, | ||
| 19 | + href = None, | ||
| 20 | + description = None, | ||
| 21 | + collectionType = "unknown", | ||
| 22 | + groups = None, | ||
| 23 | + uneditable = false, | ||
| 24 | + showTags = false, | ||
| 25 | + showSections = false, | ||
| 26 | + hideKickers = false, | ||
| 27 | + showDateHeader = false, | ||
| 28 | + showLatestUpdate = false, | ||
| 29 | + config = CollectionConfig.empty.copy(displayHints = maxItemsToDisplay.map(m => DisplayHints(Some(m)))), | ||
| 30 | + hasMore = false | ||
| 31 | + ) | ||
| 32 | + } | ||
| 33 | + | ||
| 34 | + def mkPressedPage(collections: List[PressedCollection]) = { | ||
| 35 | + PressedPage( | ||
| 36 | + id = "test-pressed-page", | ||
| 37 | + seoData = SeoData.empty, | ||
| 38 | + frontProperties= FrontProperties.empty, | ||
| 39 | + collections = collections | ||
| 40 | + ) | ||
| 41 | + } | ||
| 42 | + | ||
| 7 | 43 | def mkPressedContent(id: Int, kicker: Option[ItemKicker] = None): PressedContent = { | |
| 8 | 44 | ||
| 9 | 45 | def mkProperties(): PressedProperties = PressedProperties( | |
@@ -18,7 +54,7 @@ object FixtureBuilder { | |||
| 18 | 54 | isCrossword = false, | |
| 19 | 55 | byline = None, | |
| 20 | 56 | image = None, | |
| 21 | - webTitle = "", | ||
| 57 | + webTitle = s"webTitle $id", | ||
| 22 | 58 | linkText = None, | |
| 23 | 59 | embedType = None, | |
| 24 | 60 | embedCss = None, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,19 +1,16 @@ | |||
| 1 | 1 | package layout | |
| 2 | 2 | ||
| 3 | 3 | import common.facia.FixtureBuilder | |
| 4 | - import model.facia.PressedCollection | ||
| 5 | - import model.pressed.{CollectionConfig, DisplayHints, PressedContent} | ||
| 6 | - import model.{FrontProperties, PressedPage, SeoData} | ||
| 7 | 4 | import org.scalatest.{FlatSpec, Matchers, OptionValues} | |
| 8 | 5 | ||
| 9 | 6 | class CollectionEmailTest extends FlatSpec with Matchers with OptionValues { | |
| 10 | 7 | ||
| 11 | 8 | it should "respect the maxItemsToDisplay property if set" in { | |
| 12 | - val pressedPage = mkPressedPage( | ||
| 13 | - List(mkPressedCollection( | ||
| 9 | + val pressedPage = FixtureBuilder.mkPressedPage( | ||
| 10 | + List(FixtureBuilder.mkPressedCollection( | ||
| 14 | 11 | id = "1", | |
| 15 | - curated = (1 to 4).map(mkContent), | ||
| 16 | - backfill = (5 to 8).map(mkContent), | ||
| 12 | + curated = (1 to 4).map(FixtureBuilder.mkContent), | ||
| 13 | + backfill = (5 to 8).map(FixtureBuilder.mkContent), | ||
| 17 | 14 | maxItemsToDisplay = Some(8)) | |
| 18 | 15 | ) | |
| 19 | 16 | ) | |
@@ -23,11 +20,11 @@ class CollectionEmailTest extends FlatSpec with Matchers with OptionValues { | |||
| 23 | 20 | } | |
| 24 | 21 | ||
| 25 | 22 | it should "exclude empty containers" in { | |
| 26 | - val pressedPage = mkPressedPage( | ||
| 23 | + val pressedPage = FixtureBuilder.mkPressedPage( | ||
| 27 | 24 | List( | |
| 28 | - mkPressedCollection(id = "1", curated = (10 to 12).map(mkContent), backfill = (13 to 15).map(mkContent)), | ||
| 29 | - mkPressedCollection(id = "2", curated = Nil, backfill = Nil), | ||
| 30 | - mkPressedCollection(id = "3", curated = (30 to 32).map(mkContent), backfill = (33 to 35).map(mkContent)) | ||
| 25 | + FixtureBuilder.mkPressedCollection(id = "1", curated = (10 to 12).map(FixtureBuilder.mkContent), backfill = (13 to 15).map(FixtureBuilder.mkContent)), | ||
| 26 | + FixtureBuilder.mkPressedCollection(id = "2", curated = Nil, backfill = Nil), | ||
| 27 | + FixtureBuilder.mkPressedCollection(id = "3", curated = (30 to 32).map(FixtureBuilder.mkContent), backfill = (33 to 35).map(FixtureBuilder.mkContent)) | ||
| 31 | 28 | ) | |
| 32 | 29 | ) | |
| 33 | 30 | ||
@@ -36,37 +33,4 @@ class CollectionEmailTest extends FlatSpec with Matchers with OptionValues { | |||
| 36 | 33 | result.contentCollections.map(_.displayName) shouldEqual List("Test Collection 1", "Test Collection 3") | |
| 37 | 34 | } | |
| 38 | 35 | ||
| 39 | - private def mkContent(id: Int): PressedContent = FixtureBuilder.mkPressedContent(id) | ||
| 40 | - | ||
| 41 | - private def mkPressedCollection(id: String, curated: Seq[PressedContent] = IndexedSeq.empty, backfill: Seq[PressedContent] = IndexedSeq.empty, maxItemsToDisplay: Option[Int] = None) = { | ||
| 42 | - PressedCollection( | ||
| 43 | - id = "test-colleciton", | ||
| 44 | - displayName = s"Test Collection $id", | ||
| 45 | - curated = curated.toList, | ||
| 46 | - backfill = backfill.toList, | ||
| 47 | - treats = List.empty, | ||
| 48 | - lastUpdated = None, | ||
| 49 | - href = None, | ||
| 50 | - description = None, | ||
| 51 | - collectionType = "unknown", | ||
| 52 | - groups = None, | ||
| 53 | - uneditable = false, | ||
| 54 | - showTags = false, | ||
| 55 | - showSections = false, | ||
| 56 | - hideKickers = false, | ||
| 57 | - showDateHeader = false, | ||
| 58 | - showLatestUpdate = false, | ||
| 59 | - config = CollectionConfig.empty.copy(displayHints = maxItemsToDisplay.map(m => DisplayHints(Some(m)))), | ||
| 60 | - hasMore = false | ||
| 61 | - ) | ||
| 62 | - } | ||
| 63 | - | ||
| 64 | - private def mkPressedPage(collections: List[PressedCollection]) = { | ||
| 65 | - PressedPage( | ||
| 66 | - id = "test-pressed-page", | ||
| 67 | - seoData = SeoData.empty, | ||
| 68 | - frontProperties= FrontProperties.empty, | ||
| 69 | - collections = collections | ||
| 70 | - ) | ||
| 71 | - } | ||
| 72 | 36 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,7 +3,7 @@ package controllers | |||
| 3 | 3 | import common._ | |
| 4 | 4 | import controllers.front._ | |
| 5 | 5 | import layout.{CollectionEssentials, ContentCard, FaciaCard, FaciaCardAndIndex, FaciaContainer, Front} | |
| 6 | - import model.Cached.{RevalidatableResult, WithoutRevalidationResult} | ||
| 6 | + import model.Cached.{CacheableResult, RevalidatableResult, WithoutRevalidationResult} | ||
| 7 | 7 | import model._ | |
| 8 | 8 | import model.facia.PressedCollection | |
| 9 | 9 | import model.pressed.{CollectionConfig, PressedContent} | |
@@ -83,6 +83,17 @@ trait FaciaController extends BaseController with Logging with ImplicitControlle | |||
| 83 | 83 | ||
| 84 | 84 | def rootEditionRedirect(): Action[AnyContent] = renderFront(path = "") | |
| 85 | 85 | ||
| 86 | + def renderFrontHeadline(path: String): Action[AnyContent] = Action.async { implicit request => | ||
| 87 | + def notFound() = { | ||
| 88 | + log.warn(s"headline not found for $path") | ||
| 89 | + FrontHeadline.headlineNotFound | ||
| 90 | + } | ||
| 91 | + | ||
| 92 | + frontJsonFapi.get(path, liteRequestType) | ||
| 93 | + .map(_.fold[CacheableResult](notFound())(FrontHeadline.renderEmailHeadline)) | ||
| 94 | + .map(Cached(CacheTime.Facia)) | ||
| 95 | + } | ||
| 96 | + | ||
| 86 | 97 | def renderFront(path: String): Action[AnyContent] = Action.async { implicit request => | |
| 87 | 98 | log.info(s"Serving Path: $path") | |
| 88 | 99 | if (shouldEditionRedirect(path)) | |
@@ -134,7 +145,7 @@ trait FaciaController extends BaseController with Logging with ImplicitControlle | |||
| 134 | 145 | ||
| 135 | 146 | private def renderEmail(faciaPage: PressedPage)(implicit request: RequestHeader) = { | |
| 136 | 147 | if (request.isEmailHeadlineText) { | |
| 137 | - renderEmailHeadline(faciaPage) | ||
| 148 | + FrontHeadline.renderEmailHeadline(faciaPage) | ||
| 138 | 149 | } else { | |
| 139 | 150 | renderEmailFront(faciaPage) | |
| 140 | 151 | } | |
@@ -152,15 +163,6 @@ trait FaciaController extends BaseController with Logging with ImplicitControlle | |||
| 152 | 163 | } | |
| 153 | 164 | } | |
| 154 | 165 | ||
| 155 | - private def renderEmailHeadline(faciaPage: PressedPage) = { | ||
| 156 | - val webTitle = for { | ||
| 157 | - topCollection <- faciaPage.collections.headOption | ||
| 158 | - topCurated <- topCollection.curatedPlusBackfillDeduplicated.headOption | ||
| 159 | - } yield RevalidatableResult.Ok(topCurated.properties.webTitle) | ||
| 160 | - | ||
| 161 | - webTitle.getOrElse(WithoutRevalidationResult(NotFound("Could not extract headline from front"))) | ||
| 162 | - } | ||
| 163 | - | ||
| 164 | 166 | def renderFrontPress(path: String): Action[AnyContent] = Action.async { implicit request => renderFrontPressResult(path) } | |
| 165 | 167 | ||
| 166 | 168 | def renderContainer(id: String, preserveLayout: Boolean = false): Action[AnyContent] = Action.async { implicit request => | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,24 @@ | |||
| 1 | + package controllers.front | ||
| 2 | + | ||
| 3 | + import common.Logging | ||
| 4 | + import model.Cached.{RevalidatableResult, WithoutRevalidationResult} | ||
| 5 | + import model.{Cached, PressedPage} | ||
| 6 | + import play.api.mvc.Results | ||
| 7 | + | ||
| 8 | + object FrontHeadline extends Results with Logging { | ||
| 9 | + | ||
| 10 | + val headlineNotFound: Cached.CacheableResult = WithoutRevalidationResult(NotFound("Could not extract headline from front")) | ||
| 11 | + | ||
| 12 | + def renderEmailHeadline(faciaPage: PressedPage): Cached.CacheableResult = { | ||
| 13 | + val webTitle = for { | ||
| 14 | + topCollection <- faciaPage.collections.headOption | ||
| 15 | + topCurated <- topCollection.curatedPlusBackfillDeduplicated.headOption | ||
| 16 | + } yield RevalidatableResult.Ok(topCurated.properties.webTitle) | ||
| 17 | + | ||
| 18 | + webTitle.getOrElse { | ||
| 19 | + log.warn(s"headline not found for ${faciaPage.id}") | ||
| 20 | + headlineNotFound | ||
| 21 | + } | ||
| 22 | + } | ||
| 23 | + | ||
| 24 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -32,5 +32,6 @@ GET /*path/rss | |||
| 32 | 32 | GET /*path/lite.json controllers.FaciaController.renderFrontJsonLite(path) | |
| 33 | 33 | GET /*path.emailjson controllers.FaciaController.renderFrontJson(path) | |
| 34 | 34 | GET /*path.json controllers.FaciaController.renderFrontJson(path) | |
| 35 | + GET /*path/headline.txt controllers.FaciaController.renderFrontHeadline(path) | ||
| 35 | 36 | GET / controllers.FaciaController.rootEditionRedirect() | |
| 36 | 37 | GET /*path controllers.FaciaController.renderFront(path) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,33 @@ | |||
| 1 | + package controllers.front | ||
| 2 | + | ||
| 3 | + import akka.util.Timeout | ||
| 4 | + import common.facia.FixtureBuilder | ||
| 5 | + import model.Cached.RevalidatableResult | ||
| 6 | + import org.scalatest.{FunSuite, Matchers} | ||
| 7 | + import play.api.test.Helpers | ||
| 8 | + import scala.concurrent.duration._ | ||
| 9 | + import scala.concurrent.Future | ||
| 10 | + import scala.language.postfixOps | ||
| 11 | + | ||
| 12 | + class FrontHeadlineTest extends FunSuite with Matchers { | ||
| 13 | + implicit val timeout: Timeout = Timeout(5 seconds) | ||
| 14 | + | ||
| 15 | + test("renderEmailHeadline extracts headline from pressed page") { | ||
| 16 | + val pressedPage = FixtureBuilder.mkPressedPage( | ||
| 17 | + List(FixtureBuilder.mkPressedCollection( | ||
| 18 | + id = "1", | ||
| 19 | + curated = (1 to 4).map(FixtureBuilder.mkContent), | ||
| 20 | + backfill = (5 to 8).map(FixtureBuilder.mkContent), | ||
| 21 | + maxItemsToDisplay = Some(8)) | ||
| 22 | + ) | ||
| 23 | + ) | ||
| 24 | + | ||
| 25 | + val RevalidatableResult(result, _) = FrontHeadline.renderEmailHeadline(pressedPage) | ||
| 26 | + val resultFuture = Future.successful(result) | ||
| 27 | + val headline = Helpers.contentAsString(resultFuture) | ||
| 28 | + val status = Helpers.status(resultFuture) | ||
| 29 | + | ||
| 30 | + headline shouldBe "webTitle 1" | ||
| 31 | + status shouldBe 200 | ||
| 32 | + } | ||
| 33 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments