| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
10 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -19,13 +19,7 @@ class LatestIndexController( | |||
| 19 | 19 | def latest(path: String): Action[AnyContent] = Action.async { implicit request => | |
| 20 | 20 | loadLatest(path).map { _.map { index => | |
| 21 | 21 | index.page match { | |
| 22 | - case tag: Tag if tag.isSeries || tag.isBlog => index.trails.headOption.map(latest => { | ||
| 23 | - if (request.isEmail) { | ||
| 24 | - Redirect(latest.metadata.url + "/email", request.campaignCode.fold(Map[String, Seq[String]]())(c => Map("CMP" -> Seq(c)))) | ||
| 25 | - } | ||
| 26 | - else Found(latest.metadata.url) | ||
| 27 | - }).getOrElse(NotFound) | ||
| 28 | - | ||
| 22 | + case tag: Tag if tag.isSeries || tag.isBlog => handleSeriesBlogs(index) | ||
| 29 | 23 | case tag: Tag => MovedPermanently(s"${tag.metadata.url}/all") | |
| 30 | 24 | case section: Section => | |
| 31 | 25 | val url = if (section.isEditionalised) Paths.stripEditionIfPresent(section.metadata.url) else section.metadata.url | |
@@ -35,6 +29,20 @@ class LatestIndexController( | |||
| 35 | 29 | }.getOrElse(NotFound)}.map(r => Cached(300)(WithoutRevalidationResult(r))) | |
| 36 | 30 | } | |
| 37 | 31 | ||
| 32 | + private def handleSeriesBlogs(index: IndexPage)(implicit request: RequestHeader) = (index.trails.headOption, request.isEmail) match { | ||
| 33 | + case (Some(latest), true) => | ||
| 34 | + val queryString = request.campaignCode.fold(Map.empty[String, Seq[String]])(c => Map("CMP" -> Seq(c))) | ||
| 35 | + val emailJsonPrefix = if (request.isEmailJson) ".emailjson" else "" | ||
| 36 | + val url = s"${latest.metadata.url}/email$emailJsonPrefix" | ||
| 37 | + Redirect(url, queryString) | ||
| 38 | + | ||
| 39 | + case (Some(latest), false) => | ||
| 40 | + Found(latest.metadata.url) | ||
| 41 | + | ||
| 42 | + case (_, _) => | ||
| 43 | + NotFound | ||
| 44 | + } | ||
| 45 | + | ||
| 38 | 46 | // this is simply the latest by date. No lead content, editors picks, or anything else | |
| 39 | 47 | private def loadLatest(path: String)(implicit request: RequestHeader): Future[Option[IndexPage]] = { | |
| 40 | 48 | val result = contentApiClient.getResponse( | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -73,6 +73,7 @@ POST /atom/quiz/:id/*path | |||
| 73 | 73 | GET /$path<.+/\d\d\d\d/\w\w\w/\d\d> controllers.AllIndexController.on(path) | |
| 74 | 74 | GET /$path<.+>/latest controllers.LatestIndexController.latest(path) | |
| 75 | 75 | GET /$path<.+>/latest/email controllers.LatestIndexController.latest(path) | |
| 76 | + GET /$path<.+>/latest/email.emailjson controllers.LatestIndexController.latest(path) | ||
| 76 | 77 | GET /$path<.+>/$year<\d\d\d\d>/$month<\w\w\w>/$day<\d\d>/all controllers.AllIndexController.allOn(path, day, month, year) | |
| 77 | 78 | GET /$path<.+>/$year<\d\d\d\d>/$month<\w\w\w>/$day<\d\d>/altdate controllers.AllIndexController.altDate(path, day, month, year) | |
| 78 | 79 | GET /$path<.+>/all controllers.AllIndexController.all(path) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -15,6 +15,7 @@ import play.api.test.Helpers._ | |||
| 15 | 15 | ||
| 16 | 16 | private val MovedPermanently = 301 | |
| 17 | 17 | private val Found = 302 | |
| 18 | + private val SeeOther = 303 | ||
| 18 | 19 | lazy val latestIndexController = new LatestIndexController(testContentApiClient, play.api.test.Helpers.stubControllerComponents()) | |
| 19 | 20 | ||
| 20 | 21 | it should "redirect to latest for a series" in { | |
@@ -23,6 +24,20 @@ import play.api.test.Helpers._ | |||
| 23 | 24 | header("Location", result).head should include ("/football/20") | |
| 24 | 25 | } | |
| 25 | 26 | ||
| 27 | + it should "redirect to latest email for a blog" in { | ||
| 28 | + val result = latestIndexController.latest("fashion/fashion-blog")(TestRequest("/fashion/fashion-blog/email")) | ||
| 29 | + status(result) should be(SeeOther) | ||
| 30 | + header("Location", result).head should include ("/fashion-blog/") | ||
| 31 | + header("Location", result).head should endWith ("/email") | ||
| 32 | + } | ||
| 33 | + | ||
| 34 | + it should "redirect to latest emailjson for a blog" in { | ||
| 35 | + val result = latestIndexController.latest("fashion/fashion-blog")(TestRequest("/fashion/fashion-blog/email.emailjson")) | ||
| 36 | + status(result) should be(SeeOther) | ||
| 37 | + header("Location", result).head should include ("/fashion-blog/") | ||
| 38 | + header("Location", result).head should endWith ("/email.emailjson") | ||
| 39 | + } | ||
| 40 | + | ||
| 26 | 41 | it should "redirect to latest for a blog" in { | |
| 27 | 42 | val result = latestIndexController.latest("fashion/fashion-blog")(TestRequest()) | |
| 28 | 43 | status(result) should be(Found) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -24,10 +24,12 @@ GET /$publication<(theguardian|theobserver)>/$year<\d\d\d\d>/$month<\w\w\w>/ | |||
| 24 | 24 | ||
| 25 | 25 | GET /$path<[^/]+/([^/]+/)?live/.*>.json controllers.LiveBlogController.renderJson(path, lastUpdate: Option[String], rendered: Option[Boolean], isLivePage: Option[Boolean]) | |
| 26 | 26 | GET /$path<[^/]+/([^/]+/)?live/.*>/email controllers.LiveBlogController.renderEmail(path) | |
| 27 | + GET /$path<[^/]+/([^/]+/)?live/.*>/email.emailjson controllers.LiveBlogController.renderEmail(path) | ||
| 27 | 28 | GET /$path<[^/]+/([^/]+/)?live/.*> controllers.LiveBlogController.renderArticle(path, page: Option[String], format: Option[String]) | |
| 28 | 29 | ||
| 29 | 30 | # articles, finished liveblogs | |
| 30 | 31 | ||
| 31 | 32 | GET /*path.json controllers.ArticleController.renderJson(path) | |
| 32 | 33 | GET /*path/email controllers.ArticleController.renderEmail(path) | |
| 34 | + GET /*path/email.emailjson controllers.ArticleController.renderEmail(path) | ||
| 33 | 35 | GET /*path controllers.ArticleController.renderArticle(path) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -11,6 +11,7 @@ import model.Cached.RevalidatableResult | |||
| 11 | 11 | import model.{ApplicationContext, Cached, NoCache} | |
| 12 | 12 | import org.apache.commons.lang.exception.ExceptionUtils | |
| 13 | 13 | import play.api.Logger | |
| 14 | + import play.api.libs.json.{JsObject, JsString} | ||
| 14 | 15 | import play.api.mvc.{RequestHeader, Result} | |
| 15 | 16 | import play.twirl.api.Html | |
| 16 | 17 | ||
@@ -118,8 +119,14 @@ object `package` extends implicits.Strings with implicits.Requests with play.api | |||
| 118 | 119 | JsonComponent(page, json) | |
| 119 | 120 | } | |
| 120 | 121 | ||
| 121 | - def renderEmail(html: Html, page: model.Page)(implicit request: RequestHeader, context: ApplicationContext): Result = Cached(page){ | ||
| 122 | - RevalidatableResult.Ok(if (InlineEmailStyles.isSwitchedOn) InlineStyles(html) else html) | ||
| 122 | + def renderEmail(html: Html, page: model.Page)(implicit request: RequestHeader, context: ApplicationContext): Result = Cached(page) { | ||
| 123 | + val htmlWithInlineStyles = if (InlineEmailStyles.isSwitchedOn) InlineStyles(html) else html | ||
| 124 | + | ||
| 125 | + if (request.isEmailJson) { | ||
| 126 | + RevalidatableResult.Ok(JsObject(Map("body" -> JsString(htmlWithInlineStyles.toString)))) | ||
| 127 | + } else { | ||
| 128 | + RevalidatableResult.Ok(htmlWithInlineStyles) | ||
| 129 | + } | ||
| 123 | 130 | } | |
| 124 | 131 | ||
| 125 | 132 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,48 @@ | |||
| 1 | + package common | ||
| 2 | + | ||
| 3 | + import com.fasterxml.jackson.core.JsonParseException | ||
| 4 | + import com.gu.contentapi.client.model.v1._ | ||
| 5 | + import model.SimpleContentPage | ||
| 6 | + import org.scalatest.{FlatSpec, Matchers} | ||
| 7 | + import play.api.libs.json.JsValue | ||
| 8 | + import play.api.test.Helpers._ | ||
| 9 | + import play.twirl.api.Html | ||
| 10 | + import test.{TestRequest, WithTestApplicationContext} | ||
| 11 | + import scala.concurrent.Future | ||
| 12 | + | ||
| 13 | + class CommonPackageTest extends FlatSpec with Matchers with WithTestApplicationContext { | ||
| 14 | + | ||
| 15 | + trait PackageTestScope { | ||
| 16 | + val article = model.Content(Content( | ||
| 17 | + id = "/content", | ||
| 18 | + sectionId = None, | ||
| 19 | + sectionName = None, | ||
| 20 | + webPublicationDate = None, | ||
| 21 | + webTitle = "webTitle", | ||
| 22 | + webUrl = "webUrl", | ||
| 23 | + apiUrl = "apiUrl", | ||
| 24 | + tags = Nil, | ||
| 25 | + elements = None, | ||
| 26 | + fields = None | ||
| 27 | + )) | ||
| 28 | + val contentPage = SimpleContentPage(article) | ||
| 29 | + } | ||
| 30 | + | ||
| 31 | + "renderEmail" should "render an email result page" in new PackageTestScope { | ||
| 32 | + val html = Html("") | ||
| 33 | + val result = Future.successful(common.renderEmail(html, contentPage)(TestRequest(), testApplicationContext)) | ||
| 34 | + status(result) shouldBe 200 | ||
| 35 | + assertThrows[JsonParseException](contentAsJson(result)) | ||
| 36 | + contentAsString(result) should include ("<html") | ||
| 37 | + } | ||
| 38 | + | ||
| 39 | + "renderEmail" should "render an email json result page" in new PackageTestScope { | ||
| 40 | + val html = Html("") | ||
| 41 | + val result = Future.successful(common.renderEmail(html, contentPage)(TestRequest("/content/email.emailjson"), testApplicationContext)) | ||
| 42 | + | ||
| 43 | + val jsonResult: JsValue = contentAsJson(result) | ||
| 44 | + val (key, value) = jsonResult.as[Map[String,String]].head | ||
| 45 | + key shouldBe "body" | ||
| 46 | + value should include ("<html") | ||
| 47 | + } | ||
| 48 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -375,6 +375,7 @@ GET /$path<theguardian|theobserver>/$year<\d\d\d\d>/$month<\w\w\w>/$d | |||
| 375 | 375 | GET /$path<.+/\d\d\d\d/\w\w\w/\d\d> controllers.AllIndexController.on(path) | |
| 376 | 376 | GET /$path<.+>/latest controllers.LatestIndexController.latest(path) | |
| 377 | 377 | GET /$path<.+>/latest/email controllers.LatestIndexController.latest(path) | |
| 378 | + GET /$path<.+>/latest/email.emailjson controllers.LatestIndexController.latest(path) | ||
| 378 | 379 | GET /$path<.+>/$year<\d\d\d\d>/$month<\w\w\w>/$day<\d\d>/all controllers.AllIndexController.allOn(path, day, month, year) | |
| 379 | 380 | GET /$path<.+>/$year<\d\d\d\d>/$month<\w\w\w>/$day<\d\d>/altdate controllers.AllIndexController.altDate(path, day, month, year) | |
| 380 | 381 | GET /$path<.+>/all controllers.AllIndexController.all(path) | |
@@ -399,6 +400,7 @@ GET /rss | |||
| 399 | 400 | GET /$path<(uk|au|us|international)(/(culture|sport|commentisfree|business|money|travel|rss))?> controllers.FaciaController.renderFrontPress(path) | |
| 400 | 401 | GET /$path<email/.*> controllers.FaciaController.renderFrontPress(path) | |
| 401 | 402 | GET /*path/lite.json controllers.FaciaController.renderFrontJsonLite(path) | |
| 403 | + GET /*path.emailjson controllers.FaciaController.renderFrontJson(path) | ||
| 402 | 404 | GET /container/use-layout/*id.json controllers.FaciaController.renderContainerJsonWithFrontsLayout(id) | |
| 403 | 405 | GET /container/*id.json controllers.FaciaController.renderContainerJson(id) | |
| 404 | 406 | GET /most-relevant-container/*path.json controllers.FaciaController.renderMostRelevantContainerJson(path) | |
@@ -438,12 +440,14 @@ GET /$leftSide<[^+]+>+*rightSide | |||
| 438 | 440 | # Live Blogs | |
| 439 | 441 | GET /$path<[^/]+/([^/]+/)?live/.*>.json controllers.LiveBlogController.renderJson(path, lastUpdate: Option[String], rendered: Option[Boolean], isLivePage: Option[Boolean]) | |
| 440 | 442 | GET /$path<[^/]+/([^/]+/)?live/.*>/email controllers.LiveBlogController.renderEmail(path) | |
| 443 | + GET /$path<[^/]+/([^/]+/)?live/.*>/email.emailjson controllers.LiveBlogController.renderEmail(path) | ||
| 441 | 444 | GET /$path<[^/]+/([^/]+/)?live/.*> controllers.LiveBlogController.renderArticle(path, page: Option[String], format: Option[String]) | |
| 442 | 445 | ||
| 443 | 446 | # articles, finished liveblogs | |
| 444 | 447 | ||
| 445 | 448 | GET /*path.json controllers.ArticleController.renderJson(path) | |
| 446 | 449 | GET /*path/email controllers.ArticleController.renderEmail(path) | |
| 450 | + GET /*path/email.emailjson controllers.ArticleController.renderEmail(path) | ||
| 447 | 451 | GET /*path controllers.ArticleController.renderArticle(path) | |
| 448 | 452 | ||
| 449 | 453 | # Formstack form submission | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -148,7 +148,7 @@ trait FaciaController extends BaseController with Logging with ImplicitControlle | |||
| 148 | 148 | val htmResponseInlined = if (InlineEmailStyles.isSwitchedOn) InlineStyles(htmlResponse) else htmlResponse | |
| 149 | 149 | ||
| 150 | 150 | if (request.isEmailJson) { | |
| 151 | - val emailJson = JsObject(Map("html" -> JsString(htmResponseInlined.toString))) | ||
| 151 | + val emailJson = JsObject(Map("body" -> JsString(htmResponseInlined.toString))) | ||
| 152 | 152 | RevalidatableResult.Ok(emailJson) | |
| 153 | 153 | } else { | |
| 154 | 154 | RevalidatableResult.Ok(htmResponseInlined) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -194,7 +194,7 @@ import scala.concurrent.Await | |||
| 194 | 194 | status(emailJsonResponse) shouldBe 200 | |
| 195 | 195 | val jsonResponse = contentAsJson(emailJsonResponse) | |
| 196 | 196 | val (key, html) = jsonResponse.as[Map[String,String]].head | |
| 197 | - key shouldBe "html" | ||
| 197 | + key shouldBe "body" | ||
| 198 | 198 | html should include ("<!DOCTYPE html") | |
| 199 | 199 | } | |
| 200 | 200 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -152,6 +152,7 @@ POST /atom/quiz/:id/*path | |||
| 152 | 152 | GET /$path<.+/\d\d\d\d/\w\w\w/\d\d> controllers.AllIndexController.on(path) | |
| 153 | 153 | GET /$path<.+>/latest controllers.LatestIndexController.latest(path) | |
| 154 | 154 | GET /$path<.+>/latest/email controllers.LatestIndexController.latest(path) | |
| 155 | + GET /$path<.+>/latest/email.emailjson controllers.LatestIndexController.latest(path) | ||
| 155 | 156 | GET /$path<.+>/$year<\d\d\d\d>/$month<\w\w\w>/$day<\d\d>/all controllers.AllIndexController.allOn(path, day, month, year) | |
| 156 | 157 | GET /$path<.+>/$year<\d\d\d\d>/$month<\w\w\w>/$day<\d\d>/altdate controllers.AllIndexController.altDate(path, day, month, year) | |
| 157 | 158 | GET /$path<.+>/all controllers.AllIndexController.all(path) | |
@@ -196,9 +197,11 @@ GET /news-alert/alerts | |||
| 196 | 197 | # Articles | |
| 197 | 198 | GET /$path<[^/]+/([^/]+/)?live/.*>.json controllers.LiveBlogController.renderJson(path, lastUpdate: Option[String], rendered: Option[Boolean], isLivePage: Option[Boolean]) | |
| 198 | 199 | GET /$path<[^/]+/([^/]+/)?live/.*>/email controllers.LiveBlogController.renderEmail(path) | |
| 200 | + GET /$path<[^/]+/([^/]+/)?live/.*>/email.emailjson controllers.LiveBlogController.renderEmail(path) | ||
| 199 | 201 | GET /$path<[^/]+/([^/]+/)?live/.*> controllers.LiveBlogController.renderArticle(path, page: Option[String], format: Option[String]) | |
| 200 | 202 | GET /*path.json controllers.ArticleController.renderJson(path) | |
| 201 | 203 | GET /*path/email controllers.ArticleController.renderEmail(path) | |
| 204 | + GET /*path/email.emailjson controllers.ArticleController.renderEmail(path) | ||
| 202 | 205 | ||
| 203 | 206 | ||
| 204 | 207 | # Don't forward requests for favicon.ico to the Content API | |
| Back | FazBrowse Home | New Git URL |
0 commit comments