FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Merge pull request #20152 from guardian/email-json-enpdoints · devhttps/frontend@dfc08b7 · GitHub

Commit dfc08b7

Browse files
authored
Merge pull request guardian#20152 from guardian/email-json-enpdoints
add emailjson redirect endpoint for applications
2 parents 4c7f872 + 932f3bf commit dfc08b7

10 files changed

Lines changed: 99 additions & 11 deletions

File tree

‎applications/app/controllers/LatestIndexController.scala‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,7 @@ class LatestIndexController(
1919
def latest(path: String): Action[AnyContent] = Action.async { implicit request =>
2020
loadLatest(path).map { _.map { index =>
2121
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)
2923
case tag: Tag => MovedPermanently(s"${tag.metadata.url}/all")
3024
case section: Section =>
3125
val url = if (section.isEditionalised) Paths.stripEditionIfPresent(section.metadata.url) else section.metadata.url
@@ -35,6 +29,20 @@ class LatestIndexController(
3529
}.getOrElse(NotFound)}.map(r => Cached(300)(WithoutRevalidationResult(r)))
3630
}
3731

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+
3846
// this is simply the latest by date. No lead content, editors picks, or anything else
3947
private def loadLatest(path: String)(implicit request: RequestHeader): Future[Option[IndexPage]] = {
4048
val result = contentApiClient.getResponse(

‎applications/conf/routes‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ POST /atom/quiz/:id/*path
7373
GET /$path<.+/\d\d\d\d/\w\w\w/\d\d> controllers.AllIndexController.on(path)
7474
GET /$path<.+>/latest controllers.LatestIndexController.latest(path)
7575
GET /$path<.+>/latest/email controllers.LatestIndexController.latest(path)
76+
GET /$path<.+>/latest/email.emailjson controllers.LatestIndexController.latest(path)
7677
GET /$path<.+>/$year<\d\d\d\d>/$month<\w\w\w>/$day<\d\d>/all controllers.AllIndexController.allOn(path, day, month, year)
7778
GET /$path<.+>/$year<\d\d\d\d>/$month<\w\w\w>/$day<\d\d>/altdate controllers.AllIndexController.altDate(path, day, month, year)
7879
GET /$path<.+>/all controllers.AllIndexController.all(path)

‎applications/test/LatestIndexControllerTest.scala‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import play.api.test.Helpers._
1515

1616
private val MovedPermanently = 301
1717
private val Found = 302
18+
private val SeeOther = 303
1819
lazy val latestIndexController = new LatestIndexController(testContentApiClient, play.api.test.Helpers.stubControllerComponents())
1920

2021
it should "redirect to latest for a series" in {
@@ -23,6 +24,20 @@ import play.api.test.Helpers._
2324
header("Location", result).head should include ("/football/20")
2425
}
2526

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+
2641
it should "redirect to latest for a blog" in {
2742
val result = latestIndexController.latest("fashion/fashion-blog")(TestRequest())
2843
status(result) should be(Found)

‎article/conf/routes‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@ GET /$publication<(theguardian|theobserver)>/$year<\d\d\d\d>/$month<\w\w\w>/
2424

2525
GET /$path<[^/]+/([^/]+/)?live/.*>.json controllers.LiveBlogController.renderJson(path, lastUpdate: Option[String], rendered: Option[Boolean], isLivePage: Option[Boolean])
2626
GET /$path<[^/]+/([^/]+/)?live/.*>/email controllers.LiveBlogController.renderEmail(path)
27+
GET /$path<[^/]+/([^/]+/)?live/.*>/email.emailjson controllers.LiveBlogController.renderEmail(path)
2728
GET /$path<[^/]+/([^/]+/)?live/.*> controllers.LiveBlogController.renderArticle(path, page: Option[String], format: Option[String])
2829

2930
# articles, finished liveblogs
3031

3132
GET /*path.json controllers.ArticleController.renderJson(path)
3233
GET /*path/email controllers.ArticleController.renderEmail(path)
34+
GET /*path/email.emailjson controllers.ArticleController.renderEmail(path)
3335
GET /*path controllers.ArticleController.renderArticle(path)

‎common/app/common/package.scala‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import model.Cached.RevalidatableResult
1111
import model.{ApplicationContext, Cached, NoCache}
1212
import org.apache.commons.lang.exception.ExceptionUtils
1313
import play.api.Logger
14+
import play.api.libs.json.{JsObject, JsString}
1415
import play.api.mvc.{RequestHeader, Result}
1516
import play.twirl.api.Html
1617

@@ -118,8 +119,14 @@ object `package` extends implicits.Strings with implicits.Requests with play.api
118119
JsonComponent(page, json)
119120
}
120121

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+
}
123130
}
124131

125132
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
}

‎dev-build/conf/routes‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,7 @@ GET /$path<theguardian|theobserver>/$year<\d\d\d\d>/$month<\w\w\w>/$d
375375
GET /$path<.+/\d\d\d\d/\w\w\w/\d\d> controllers.AllIndexController.on(path)
376376
GET /$path<.+>/latest controllers.LatestIndexController.latest(path)
377377
GET /$path<.+>/latest/email controllers.LatestIndexController.latest(path)
378+
GET /$path<.+>/latest/email.emailjson controllers.LatestIndexController.latest(path)
378379
GET /$path<.+>/$year<\d\d\d\d>/$month<\w\w\w>/$day<\d\d>/all controllers.AllIndexController.allOn(path, day, month, year)
379380
GET /$path<.+>/$year<\d\d\d\d>/$month<\w\w\w>/$day<\d\d>/altdate controllers.AllIndexController.altDate(path, day, month, year)
380381
GET /$path<.+>/all controllers.AllIndexController.all(path)
@@ -399,6 +400,7 @@ GET /rss
399400
GET /$path<(uk|au|us|international)(/(culture|sport|commentisfree|business|money|travel|rss))?> controllers.FaciaController.renderFrontPress(path)
400401
GET /$path<email/.*> controllers.FaciaController.renderFrontPress(path)
401402
GET /*path/lite.json controllers.FaciaController.renderFrontJsonLite(path)
403+
GET /*path.emailjson controllers.FaciaController.renderFrontJson(path)
402404
GET /container/use-layout/*id.json controllers.FaciaController.renderContainerJsonWithFrontsLayout(id)
403405
GET /container/*id.json controllers.FaciaController.renderContainerJson(id)
404406
GET /most-relevant-container/*path.json controllers.FaciaController.renderMostRelevantContainerJson(path)
@@ -438,12 +440,14 @@ GET /$leftSide<[^+]+>+*rightSide
438440
# Live Blogs
439441
GET /$path<[^/]+/([^/]+/)?live/.*>.json controllers.LiveBlogController.renderJson(path, lastUpdate: Option[String], rendered: Option[Boolean], isLivePage: Option[Boolean])
440442
GET /$path<[^/]+/([^/]+/)?live/.*>/email controllers.LiveBlogController.renderEmail(path)
443+
GET /$path<[^/]+/([^/]+/)?live/.*>/email.emailjson controllers.LiveBlogController.renderEmail(path)
441444
GET /$path<[^/]+/([^/]+/)?live/.*> controllers.LiveBlogController.renderArticle(path, page: Option[String], format: Option[String])
442445

443446
# articles, finished liveblogs
444447

445448
GET /*path.json controllers.ArticleController.renderJson(path)
446449
GET /*path/email controllers.ArticleController.renderEmail(path)
450+
GET /*path/email.emailjson controllers.ArticleController.renderEmail(path)
447451
GET /*path controllers.ArticleController.renderArticle(path)
448452

449453
# Formstack form submission

‎facia/app/controllers/FaciaController.scala‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ trait FaciaController extends BaseController with Logging with ImplicitControlle
148148
val htmResponseInlined = if (InlineEmailStyles.isSwitchedOn) InlineStyles(htmlResponse) else htmlResponse
149149

150150
if (request.isEmailJson) {
151-
val emailJson = JsObject(Map("html" -> JsString(htmResponseInlined.toString)))
151+
val emailJson = JsObject(Map("body" -> JsString(htmResponseInlined.toString)))
152152
RevalidatableResult.Ok(emailJson)
153153
} else {
154154
RevalidatableResult.Ok(htmResponseInlined)

‎facia/test/FaciaControllerTest.scala‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ import scala.concurrent.Await
194194
status(emailJsonResponse) shouldBe 200
195195
val jsonResponse = contentAsJson(emailJsonResponse)
196196
val (key, html) = jsonResponse.as[Map[String,String]].head
197-
key shouldBe "html"
197+
key shouldBe "body"
198198
html should include ("<!DOCTYPE html")
199199
}
200200

‎preview/conf/routes‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ POST /atom/quiz/:id/*path
152152
GET /$path<.+/\d\d\d\d/\w\w\w/\d\d> controllers.AllIndexController.on(path)
153153
GET /$path<.+>/latest controllers.LatestIndexController.latest(path)
154154
GET /$path<.+>/latest/email controllers.LatestIndexController.latest(path)
155+
GET /$path<.+>/latest/email.emailjson controllers.LatestIndexController.latest(path)
155156
GET /$path<.+>/$year<\d\d\d\d>/$month<\w\w\w>/$day<\d\d>/all controllers.AllIndexController.allOn(path, day, month, year)
156157
GET /$path<.+>/$year<\d\d\d\d>/$month<\w\w\w>/$day<\d\d>/altdate controllers.AllIndexController.altDate(path, day, month, year)
157158
GET /$path<.+>/all controllers.AllIndexController.all(path)
@@ -196,9 +197,11 @@ GET /news-alert/alerts
196197
# Articles
197198
GET /$path<[^/]+/([^/]+/)?live/.*>.json controllers.LiveBlogController.renderJson(path, lastUpdate: Option[String], rendered: Option[Boolean], isLivePage: Option[Boolean])
198199
GET /$path<[^/]+/([^/]+/)?live/.*>/email controllers.LiveBlogController.renderEmail(path)
200+
GET /$path<[^/]+/([^/]+/)?live/.*>/email.emailjson controllers.LiveBlogController.renderEmail(path)
199201
GET /$path<[^/]+/([^/]+/)?live/.*> controllers.LiveBlogController.renderArticle(path, page: Option[String], format: Option[String])
200202
GET /*path.json controllers.ArticleController.renderJson(path)
201203
GET /*path/email controllers.ArticleController.renderEmail(path)
204+
GET /*path/email.emailjson controllers.ArticleController.renderEmail(path)
202205

203206

204207
# Don't forward requests for favicon.ico to the Content API

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL