Skip to content

Commit

Permalink
Merge pull request #27510 from guardian/oa/audio-image-thumbnail
Browse files Browse the repository at this point in the history
Update audio image to include type and elementId
  • Loading branch information
oliverabrahams authored Oct 7, 2024
2 parents 84c9da7 + 64ea368 commit 621ad28
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ case class DotcomRenderingDataModel(
commercialProperties: Map[String, EditionCommercialProperties],
pageType: PageType,
starRating: Option[Int],
audioArticleImage: Option[ImageBlockElement],
audioArticleImage: Option[PageElement],
trailText: String,
nav: Nav,
showBottomSocialButtons: Boolean,
Expand Down
24 changes: 13 additions & 11 deletions common/app/model/dotcomrendering/ElementsEnhancer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,19 @@ import play.api.libs.json.{Json, _}
object ElementsEnhancer {

def enhanceElement(element: JsValue): JsValue = {
// Add an elementId to the element
val elementWithId = element.as[JsObject] ++ Json.obj("elementId" -> java.util.UUID.randomUUID.toString)
// Extract element type
val elementType = elementWithId.value("_type").as[String]
// Check if the element is a JsObject before proceeding
element match {
case obj: JsObject =>
val elementWithId = obj ++ Json.obj("elementId" -> java.util.UUID.randomUUID.toString)
val elementType = elementWithId.value("_type").as[String]

// If element has further nesting, continue to enhance. otherwise, return the enhanced element
elementType match {
case "model.dotcomrendering.pageElements.ListBlockElement" => enhanceListBlockElement(elementWithId)
case "model.dotcomrendering.pageElements.TimelineBlockElement" => enhanceTimelineBlockElement(elementWithId)
case _ => elementWithId;
elementType match {
case "model.dotcomrendering.pageElements.ListBlockElement" => enhanceListBlockElement(elementWithId)
case "model.dotcomrendering.pageElements.TimelineBlockElement" => enhanceTimelineBlockElement(elementWithId)
case _ => elementWithId;
}
case _ => element
}

}
def enhanceListBlockElement(elementWithId: JsObject): JsObject = {
val listItems = elementWithId.value("items").as[JsArray]
Expand Down Expand Up @@ -73,6 +74,7 @@ object ElementsEnhancer {
Json.obj("mainMediaElements" -> enhanceElements(obj.value("mainMediaElements"))) ++
Json.obj("keyEvents" -> enhanceObjectsWithElements(obj.value("keyEvents"))) ++
Json.obj("pinnedPost" -> enhanceObjectWithElements(obj.value("pinnedPost"))) ++
Json.obj("promotedNewsletter" -> obj.value("promotedNewsletter"))
Json.obj("promotedNewsletter" -> obj.value("promotedNewsletter")) ++
Json.obj("audioArticleImage" -> enhanceElement(obj.value("audioArticleImage")))
}
}
68 changes: 68 additions & 0 deletions common/test/model/dotcomrendering/ElementEnhancerTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package model.dotcomrendering

import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
import play.api.libs.json.{JsArray, JsNull, JsObject, JsValue, Json}
import model.dotcomrendering.ElementsEnhancer

class ElementEnhancerTest extends AnyFlatSpec with Matchers {

"enhanceElement" should "add elementId and enhance ListBlockElement" in {
val mockElement: JsObject = Json.obj(
"_type" -> "model.dotcomrendering.pageElements.ListBlockElement",
"items" -> Json.arr(
Json.obj("elements" -> Json.arr(Json.obj("_type" -> "model.dotcomrendering.pageElements.TextElement"))),
),
)

val result = ElementsEnhancer.enhanceElement(mockElement)

(result \ "elementId").asOpt[String] should not be empty
val items = (result \ "items").as[JsArray]
items.value.size shouldEqual 1
val elements = (items(0) \ "elements").as[JsArray]
elements.value.size shouldEqual 1
(elements(0) \ "elementId").as[String] should not be empty
}

"enhanceElement" should "add elementId and enhance TimelineBlockElement" in {
val mockElement: JsObject = Json.obj(
"_type" -> "model.dotcomrendering.pageElements.TimelineBlockElement",
"sections" -> Json.arr(
Json.obj(
"events" -> Json.arr(
Json.obj("body" -> Json.arr(Json.obj("_type" -> "model.dotcomrendering.pageElements.TextElement"))),
),
),
),
)

val result = ElementsEnhancer.enhanceElement(mockElement)

(result \ "elementId").asOpt[String] should not be empty

val sections = (result \ "sections").as[JsArray]
val events = (sections(0) \ "events").as[JsArray]
val body = (events(0) \ "body").as[JsArray]
val bodyElementIds = (body(0) \ "elementId").as[String]
bodyElementIds should not be empty
}

"enhanceElement" should "return the original element with elementId for unknown types" in {
val mockElement: JsObject = Json.obj(
"_type" -> "model.dotcomrendering.pageElements.TextElement",
"someField" -> "someValue",
)

val result = ElementsEnhancer.enhanceElement(mockElement)

(result \ "elementId").asOpt[String] should not be empty
(result \ "someField").asOpt[String] shouldEqual Some("someValue")
}

"enhanceElement" should "return JsNull if JsNull is passed" in {
val result = ElementsEnhancer.enhanceElement(JsNull)

result shouldEqual JsNull
}
}

0 comments on commit 621ad28

Please sign in to comment.