-
Notifications
You must be signed in to change notification settings - Fork 570
Expand file tree
/
Copy pathLiveBlogController.scala
More file actions
479 lines (438 loc) · 17.3 KB
/
LiveBlogController.scala
File metadata and controls
479 lines (438 loc) · 17.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
package controllers
import com.gu.contentapi.client.model.v1.{Block, Blocks, ItemResponse, Content => ApiContent}
import common.`package`.{convertApiExceptions => _, renderFormat => _}
import common._
import contentapi.ContentApiClient
import implicits.{AmpFormat, AppsFormat, HtmlFormat}
import model.Cached.WithoutRevalidationResult
import model.LiveBlogHelpers._
import model.ParseBlockId.{InvalidFormat, ParsedBlockId}
import model.dotcomrendering.{DotcomRenderingDataModel, PageType}
import model.liveblog.BodyBlock
import model.liveblog.BodyBlock.{KeyEvent, SummaryEvent}
import model._
import pages.{ArticleEmailHtmlPage, LiveBlogHtmlPage, MinuteHtmlPage}
import play.api.libs.ws.WSClient
import play.api.mvc._
import play.twirl.api.Html
import renderers.DotcomRenderingService
import services.{CAPILookup, NewsletterService, MessageUsService}
import topics.TopicService
import utils.DotcomponentsLogger
import views.support.RenderOtherStatus
import scala.concurrent.Future
case class MinutePage(article: Article, related: RelatedContent) extends PageWithStoryPackage
class LiveBlogController(
contentApiClient: ContentApiClient,
val controllerComponents: ControllerComponents,
ws: WSClient,
remoteRenderer: renderers.DotcomRenderingService = DotcomRenderingService(),
newsletterService: NewsletterService,
topicService: TopicService,
messageUsService: MessageUsService,
)(implicit context: ApplicationContext)
extends BaseController
with GuLogging
with ImplicitControllerExecutionContext {
val capiLookup: CAPILookup = new CAPILookup(contentApiClient)
// we support liveblogs and also articles, so that minutes work
private def isSupported(c: ApiContent) = c.isLiveBlog || c.isArticle
// Main entry points
def renderEmail(path: String): Action[AnyContent] = {
Action.async { implicit request =>
mapModel(path, ArticleBlocks, topicResult = None) {
case (minute: MinutePage, _) =>
Future.successful(common.renderEmail(ArticleEmailHtmlPage.html(minute), minute))
case (blog: LiveBlogPage, _) => Future.successful(common.renderEmail(LiveBlogHtmlPage.html(blog), blog))
case _ => Future.successful(NotFound)
}
}
}
def renderArticle(
path: String,
page: Option[String] = None,
filterKeyEvents: Option[Boolean],
topics: Option[String],
): Action[AnyContent] = {
Action.async { implicit request =>
val filter = shouldFilter(filterKeyEvents)
val topicResult = if (filter) None else getTopicResult(path, topics)
val availableTopics = topicService.getAvailableTopics(path)
val messageUs = messageUsService.getBlogMessageUsData(path)
page.map(ParseBlockId.fromPageParam) match {
case Some(ParsedBlockId(id)) =>
renderWithRange(
path,
PageWithBlock(id),
filter,
topicResult,
availableTopics,
messageUs,
) // we know the id of a block
case Some(InvalidFormat) =>
Future.successful(
Cached(10)(WithoutRevalidationResult(NotFound)),
) // page param there but couldn't extract a block id
case None => {
topicResult match {
case Some(value) =>
renderWithRange(
path,
TopicsLiveBlog,
filter,
Some(value),
availableTopics,
messageUs,
) // no page param
case None =>
renderWithRange(
path,
CanonicalLiveBlog,
filter,
None,
availableTopics,
messageUs,
) // no page param
}
}
}
}
}
def renderJson(
path: String,
page: Option[String],
lastUpdate: Option[String],
rendered: Option[Boolean],
isLivePage: Option[Boolean],
filterKeyEvents: Option[Boolean],
topics: Option[String],
): Action[AnyContent] = {
Action.async { implicit request: Request[AnyContent] =>
val filter = shouldFilter(filterKeyEvents)
val topicResult = getTopicResult(path, topics)
val range = getRange(lastUpdate, page, topicResult)
val availableTopics = topicService.getAvailableTopics(path)
val messageUs = messageUsService.getBlogMessageUsData(path)
mapModel(path, range, filter, topicResult) {
case (blog: LiveBlogPage, _) if rendered.contains(false) => getJsonForFronts(blog)
/**
* When DCR requests new blocks from the client, it will add a `lastUpdate` parameter.
* If no such parameter is present, we should return a JSON representation of the whole
* payload that would be sent to DCR when initially server side rendering the LiveBlog page.
*/
case (blog: LiveBlogPage, blocks) if request.forceDCR && lastUpdate.isEmpty =>
Future.successful(renderDCRJson(blog, blocks, filter, availableTopics, topicResult, messageUs))
case (blog: LiveBlogPage, blocks) =>
getJson(
blog,
range,
isLivePage,
filter,
blocks.requestedBodyBlocks.getOrElse(Map.empty).map(entry => (entry._1, entry._2.toSeq)),
topicResult,
)
case (minute: MinutePage, _) =>
Future.successful(common.renderJson(views.html.fragments.minuteBody(minute), minute))
case _ =>
Future {
Cached(600)(WithoutRevalidationResult(NotFound))
}
}
}
}
private[this] def renderWithRange(
path: String,
range: BlockRange,
filterKeyEvents: Boolean,
topicResult: Option[TopicResult],
availableTopics: Option[Seq[Topic]],
messageUs: Option[MessageUsData],
)(implicit
request: RequestHeader,
): Future[Result] = {
mapModel(path, range, filterKeyEvents, topicResult) { (page, blocks) =>
{
val isAmpSupported = page.article.content.shouldAmplify
val pageType: PageType = PageType(page, request, context)
(page, request.getRequestFormat) match {
case (minute: MinutePage, HtmlFormat) =>
Future.successful(common.renderHtml(MinuteHtmlPage.html(minute), minute))
case (blog: LiveBlogPage, HtmlFormat) =>
val dcrCouldRender = true
val theme = blog.article.content.metadata.format.getOrElse(ContentFormat.defaultContentFormat).theme
val design = blog.article.content.metadata.format.getOrElse(ContentFormat.defaultContentFormat).design
val display = blog.article.content.metadata.format.getOrElse(ContentFormat.defaultContentFormat).display
val isDeadBlog = !blog.article.fields.isLive
val properties =
Map(
"participatingInTest" -> "false",
"dcrCouldRender" -> dcrCouldRender.toString,
"theme" -> theme.toString,
"design" -> design.toString,
"display" -> display.toString,
"isDead" -> isDeadBlog.toString,
"isLiveBlog" -> "true",
)
val remoteRendering = !request.forceDCROff
if (remoteRendering) {
DotcomponentsLogger.logger
.logRequest(s"liveblog executing in dotcomponents", properties, page.article)
val pageType: PageType = PageType(blog, request, context)
remoteRenderer.getArticle(
ws,
blog,
blocks,
pageType,
newsletter = None,
filterKeyEvents,
request.forceLive,
availableTopics,
topicResult,
messageUs,
)
} else {
DotcomponentsLogger.logger.logRequest(s"liveblog executing in web", properties, page.article)
Future.successful(common.renderHtml(LiveBlogHtmlPage.html(blog), blog))
}
case (blog: LiveBlogPage, AmpFormat) if isAmpSupported =>
remoteRenderer.getAMPArticle(ws, blog, blocks, pageType, newsletter = None, filterKeyEvents)
case (blog: LiveBlogPage, AmpFormat) =>
Future.successful(common.renderHtml(LiveBlogHtmlPage.html(blog), blog))
case (blog: LiveBlogPage, AppsFormat) =>
remoteRenderer.getAppsArticle(
ws,
blog,
blocks,
pageType,
newsletter = None,
filterKeyEvents,
request.forceLive,
availableTopics,
topicResult,
)
case _ => Future.successful(NotFound)
}
}
}
}
private[this] def getRange(
lastUpdate: Option[String],
page: Option[String],
topicResult: Option[TopicResult],
): BlockRange = {
(lastUpdate.map(ParseBlockId.fromBlockId), page.map(ParseBlockId.fromPageParam), topicResult) match {
case (Some(ParsedBlockId(id)), _, _) => SinceBlockId(id)
case (_, Some(ParsedBlockId(id)), _) => PageWithBlock(id)
case (_, _, Some(_)) => TopicsLiveBlog
case _ => CanonicalLiveBlog
}
}
private[this] def getJsonForFronts(liveblog: LiveBlogPage)(implicit request: RequestHeader): Future[Result] = {
Future {
Cached(liveblog)(JsonComponent("blocks" -> model.LiveBlogHelpers.blockTextJson(liveblog, 6)))
}
}
private[this] def getJson(
liveblog: LiveBlogPage,
range: BlockRange,
isLivePage: Option[Boolean],
filterKeyEvents: Boolean,
requestedBodyBlocks: scala.collection.Map[String, Seq[Block]] = Map.empty,
topicResult: Option[TopicResult],
)(implicit request: RequestHeader): Future[Result] = {
val remoteRender = !request.forceDCROff
range match {
case SinceBlockId(lastBlockId) =>
renderNewerUpdatesJson(
liveblog,
SinceBlockId(lastBlockId),
isLivePage,
filterKeyEvents,
remoteRender,
requestedBodyBlocks,
topicResult,
)
case _ => Future.successful(common.renderJson(views.html.liveblog.liveBlogBody(liveblog), liveblog))
}
}
private[this] def getNewBlocks(
page: PageWithStoryPackage,
lastUpdateBlockId: SinceBlockId,
filterKeyEvents: Boolean,
topicResult: Option[TopicResult],
): (Option[BodyBlock], Seq[BodyBlock]) = {
val requestedBlocks = page.article.fields.blocks.toSeq.flatMap {
_.requestedBodyBlocks.getOrElse(lastUpdateBlockId.around, Seq())
}
val latestBlocks = requestedBlocks.takeWhile { block =>
block.id != lastUpdateBlockId.lastUpdate
}
val filteredBlocks = if (filterKeyEvents) {
latestBlocks.filter(block => block.eventType == KeyEvent || block.eventType == SummaryEvent)
} else if (topicResult.isDefined) {
latestBlocks.filter(block => topicResult.get.blocks.contains(block.id))
} else latestBlocks
// the last block is picked from the unfiltered list
(latestBlocks.headOption, filteredBlocks)
}
private[this] def getNewBlocks(
requestedBodyBlocks: scala.collection.Map[String, Seq[Block]],
lastUpdateBlockId: SinceBlockId,
filterKeyEvents: Boolean,
topicResult: Option[TopicResult],
): Seq[Block] = {
val blocksAround = requestedBodyBlocks.getOrElse(lastUpdateBlockId.around, Seq.empty).takeWhile { block =>
block.id != lastUpdateBlockId.lastUpdate
}
if (filterKeyEvents) {
blocksAround.filter(block =>
block.attributes.keyEvent.getOrElse(false) || block.attributes.summary.getOrElse(false),
)
} else if (topicResult.isDefined) {
blocksAround.filter(block => topicResult.get.blocks.contains(block.id))
} else blocksAround
}
private def getDCRBlocksHTML(page: LiveBlogPage, blocks: Seq[Block])(implicit
request: RequestHeader,
): Future[Html] = {
remoteRenderer.getBlocks(ws, page, blocks) map { result =>
new Html(result)
}
}
private[this] def renderNewerUpdatesJson(
page: LiveBlogPage,
lastUpdateBlockId: SinceBlockId,
isLivePage: Option[Boolean],
filterKeyEvents: Boolean,
remoteRender: Boolean,
requestedBodyBlocks: scala.collection.Map[String, Seq[Block]],
topicResult: Option[TopicResult],
)(implicit request: RequestHeader): Future[Result] = {
val (newestBlock, newBlocks) = getNewBlocks(page, lastUpdateBlockId, filterKeyEvents, topicResult)
val newCapiBlocks = getNewBlocks(requestedBodyBlocks, lastUpdateBlockId, filterKeyEvents, topicResult)
val timelineHtml = views.html.liveblog.keyEvents(
"",
model.KeyEventData(newBlocks, Edition(request).timezone),
filterKeyEvents,
)
for {
blocksHtml <-
if (remoteRender) {
getDCRBlocksHTML(page, newCapiBlocks)
} else {
Future.successful(views.html.liveblog.liveBlogBlocks(newBlocks, page.article, Edition(request).timezone))
}
} yield {
val allPagesJson = Seq(
"timeline" -> timelineHtml,
"numNewBlocks" -> newBlocks.size,
)
val livePageJson = isLivePage.filter(_ == true).map { _ =>
"html" -> blocksHtml
}
val mostRecent = newestBlock.map { block =>
"mostRecentBlockId" -> s"block-${block.id}"
}
Cached(page)(JsonComponent(allPagesJson ++ livePageJson ++ mostRecent: _*))
}
}
/**
* Returns a JSON representation of the payload that's sent to DCR when rendering the whole LiveBlog page.
*/
private[this] def renderDCRJson(
blog: LiveBlogPage,
blocks: Blocks,
filterKeyEvents: Boolean,
availableTopics: Option[Seq[Topic]],
topicResult: Option[TopicResult],
messageUs: Option[MessageUsData],
)(implicit request: RequestHeader): Result = {
val pageType: PageType = PageType(blog, request, context)
val newsletter = newsletterService.getNewsletterForLiveBlog(blog)
val model =
DotcomRenderingDataModel.forLiveblog(
blog,
blocks,
request,
pageType,
filterKeyEvents,
request.forceLive,
availableTopics,
newsletter,
topicResult,
messageUs,
)
val json = DotcomRenderingDataModel.toJson(model)
common.renderJson(json, blog).as("application/json")
}
private[this] def mapModel(
path: String,
range: BlockRange,
filterKeyEvents: Boolean = false,
topicResult: Option[TopicResult],
)(
render: (PageWithStoryPackage, Blocks) => Future[Result],
)(implicit request: RequestHeader): Future[Result] = {
capiLookup
.lookup(path, Some(range))
.map(responseToModelOrResult(range, filterKeyEvents, topicResult))
.recover(convertApiExceptions)
.flatMap {
case Right((model, blocks)) => render(model, blocks)
case Left(other) => Future.successful(RenderOtherStatus(other))
}
}
private[this] def responseToModelOrResult(
range: BlockRange,
filterKeyEvents: Boolean,
topicResult: Option[TopicResult],
)(response: ItemResponse)(implicit request: RequestHeader): Either[Result, (PageWithStoryPackage, Blocks)] = {
val supportedContent: Option[ContentType] = response.content.filter(isSupported).map(Content(_))
val supportedContentResult: Either[Result, ContentType] = ModelOrResult(supportedContent, response)
val blocks = response.content.flatMap(_.blocks).getOrElse(Blocks())
val content = supportedContentResult.flatMap {
case minute: Article if minute.isTheMinute =>
Right(MinutePage(minute, StoryPackages(minute.metadata.id, response)), blocks)
case liveBlog: Article if liveBlog.isLiveBlog && request.isEmail =>
Right(MinutePage(liveBlog, StoryPackages(liveBlog.metadata.id, response)), blocks)
case liveBlog: Article if liveBlog.isLiveBlog =>
createLiveBlogModel(
liveBlog,
response,
range,
filterKeyEvents,
topicResult,
).map(_ -> blocks)
case nonLiveBlogArticle: Article =>
/**
* If `isLiveBlog` is false, it must be because the article has no blocks, or lacks
* the `tone/minutebyminute` tag, or both.
* Logging these values will help us to identify which is causing the issue.
*/
val hasBlocks = nonLiveBlogArticle.fields.blocks.nonEmpty;
val hasMinuteByMinuteTag = nonLiveBlogArticle.tags.isLiveBlog;
log.error(
s"Requested non-liveblog article as liveblog: ${nonLiveBlogArticle.metadata.id}: { hasBlocks: ${hasBlocks}, hasMinuteByMinuteTag: ${hasMinuteByMinuteTag} }",
)
Left(InternalServerError)
case unknown =>
log.error(s"Requested non-liveblog: ${unknown.metadata.id}")
Left(InternalServerError)
}
content
}
def shouldFilter(filterKeyEvents: Option[Boolean]): Boolean = {
filterKeyEvents.getOrElse(false)
}
def getTopicResult(blogId: String, topic: Option[String]) = {
val topicResult = for {
selectedTopic <- Topic.fromString(topic)
topicResult <- topicService.getSelectedTopic(blogId, selectedTopic)
} yield topicResult
topicResult match {
case Some(_) => log.info(s"topic was successfully retrieved for ${topic.get}")
case None => if (topic.isDefined) log.warn(s"topic result couldn't be retrieved for ${topic.get}")
}
topicResult
}
}