11package com .gu .facia .api .models
22
33import com .gu .contentapi .client .ContentApiClient
4- import com .gu .contentapi .client .model .v1 .{ Content , ItemResponse }
4+ import com .gu .contentapi .client .model .v1 .Content
55import com .gu .contentatom .thrift .{Atom , AtomData }
66import com .gu .contentatom .thrift .atom .media .MediaAtom
7- import com .gu .facia .api .contentapi .{ LatestSnapsRequest , LinkSnapsRequest }
7+ import com .gu .facia .api .contentapi .{LatestSnapsRequest , LinkSnapsRequest }
88import com .gu .facia .client .models .{CollectionJson , SupportingItem , TargetedTerritory , Trail }
99import org .joda .time .{DateTime , DateTimeZone }
1010import com .gu .facia .api .utils .BoostLevel
11- import com .gu .facia .api .{ CapiError , Response }
11+ import com .gu .facia .api .Response
1212import com .typesafe .scalalogging .StrictLogging
13+ import org .jsoup .Jsoup
1314
1415import scala .concurrent .{ExecutionContext , Future }
1516
16-
1717case class Collection (
1818 id : String ,
1919 displayName : String ,
@@ -131,7 +131,7 @@ object Collection extends StrictLogging {
131131 case faciaContent@ AtomId (atomId) if faciaContent.properties.videoReplace =>
132132 capiClient.getResponse(ContentApiClient .item(atomId)).map { response =>
133133 response.media.flatMap(atom =>
134- Option .when(isValidMediaAtom(atom, atomId ))(atom)
134+ Option .when(isValidMediaAtom(atom))(atom)
135135 )
136136 }.recover {
137137 case e =>
@@ -140,10 +140,12 @@ object Collection extends StrictLogging {
140140 }
141141
142142 case faciaContent : CuratedContent if faciaContent.properties.showMainVideo =>
143+ val mainField = faciaContent.content.fields.flatMap(_.main).get
143144 val mainAtom = for {
145+ atomId <- extractMainMediaAtomIdFromHtml(mainField)
144146 atoms <- faciaContent.content.atoms
145147 mediaAtoms <- atoms.media
146- validMediaAtom <- mediaAtoms.find(isValidMediaAtom(_, faciaContent.content.id ))
148+ validMediaAtom <- mediaAtoms.find(atom => atom.id == atomId && isValidMediaAtom(atom ))
147149 } yield validMediaAtom
148150 Future .successful(mainAtom)
149151 case _ => Future .successful(None )
@@ -152,17 +154,29 @@ object Collection extends StrictLogging {
152154 Response .Async .Right (futureMaybeAtomData)
153155 }
154156
155- def isValidMediaAtom (atom : Atom , id : String ): Boolean = {
157+ //
158+ // We need to make sure that we only select the main media atom rather than another embedded atom. This follows the same pattern implemented in Frontend.
159+ //
160+ def extractMainMediaAtomIdFromHtml (html : String ): Option [String ] = {
161+ for {
162+ document <- Some (Jsoup .parse(html))
163+ atomContainer <- Option (document.getElementsByClass(" element-atom" ).first())
164+ bodyElement <- Some (atomContainer.getElementsByTag(" gu-atom" ))
165+ atomId <- Some (bodyElement.attr(" data-atom-id" ))
166+ } yield atomId
167+ }
168+
169+ def isValidMediaAtom (atom : Atom ): Boolean = {
156170 atom.data match {
157171 case mediaData : AtomData .Media =>
158172 if (! isExpired(mediaData.media)) {
159173 true
160174 } else {
161- logger.warn(s " Media atom is expired in ${ id}" )
175+ logger.warn(s " Media atom ${atom. id} is expired " )
162176 false
163177 }
164178 case _ =>
165- logger.warn(s " No valid media atom found in ${ id}" )
179+ logger.warn(s " Media atom ${atom. id} is not valid " )
166180 false
167181 }
168182 }
0 commit comments