@@ -2,7 +2,15 @@ package data
22
33import com .gu .atom .data .PreviewDynamoDataStore
44import com .gu .media .CapiAccess
5- import com .gu .media .model .{ContentChangeDetails , Image , MediaAtom }
5+ import com .gu .media .model .Platform .{Url , Youtube }
6+ import com .gu .media .model .VideoPlayerFormat .Loop
7+ import com .gu .media .model .{
8+ ContentChangeDetails ,
9+ Image ,
10+ MediaAtom ,
11+ Platform ,
12+ VideoPlayerFormat
13+ }
614import com .gu .media .util .TestFilters
715import model .commands .CommandExceptions .AtomDataStoreError
816import model .{MediaAtomList , MediaAtomSummary }
@@ -14,7 +22,7 @@ trait AtomListStore {
1422 search : Option [String ],
1523 limit : Option [Int ],
1624 shouldUseCreatedDateForSort : Boolean ,
17- mediaPlatform : Option [String ],
25+ platformFilter : Option [String ],
1826 orderByOldest : Boolean
1927 ): MediaAtomList
2028}
@@ -30,7 +38,7 @@ class CapiBackedAtomListStore(capi: CapiAccess)
3038 search : Option [String ],
3139 limit : Option [Int ],
3240 shouldUseCreatedDateForSort : Boolean ,
33- mediaPlatform : Option [String ],
41+ platformFilter : Option [String ],
3442 orderByOldest : Boolean
3543 ): MediaAtomList = {
3644 val pagination = Pagination .option(CapiMaxPageSize , limit)
@@ -40,7 +48,7 @@ class CapiBackedAtomListStore(capi: CapiAccess)
4048 case false => Map .empty
4149 }
4250
43- val mediaPlatformFilter = mediaPlatform match {
51+ val mediaPlatformFilter = platformFilter match {
4452 case Some (mPlatform) => Map (" media-platform" -> mPlatform)
4553 case _ => Map .empty
4654 }
@@ -142,38 +150,42 @@ class CapiBackedAtomListStore(capi: CapiAccess)
142150 (asset \ " version" ).as[Long ]
143151 }
144152
145- val mediaPlatforms = (atom \ " assets" )
146- .as[JsArray ]
147- .value
148- .flatMap { asset =>
149- (asset \ " platform" ).asOpt[String ].map(_.toLowerCase)
150- }
151- .toList
152- .distinct
153+ val atomPlatform =
154+ (atom \ " platform" ).asOpt[Platform ]
153155
154- val currentAsset = (atom \ " assets" ).as[JsArray ].value.find { asset =>
156+ val activeAsset = (atom \ " assets" ).as[JsArray ].value.find { asset =>
155157 val assetVersion = (asset \ " version" ).as[Long ]
156158 activeVersion.contains(assetVersion)
157159 }
158160
159- val currentMediaPlatform = currentAsset.flatMap { asset =>
160- (asset \ " platform" ).asOpt[ String ].map(_.toLowerCase)
161+ val activeAssetPlatform = activeAsset.map { asset =>
162+ (asset \ " platform" ).as[ Platform ]
161163 }
162164
163- // sort media platforms so the current one is first
164- val sortedMediaPlatforms = currentMediaPlatform match {
165- case Some (current) => current :: mediaPlatforms.filter(_ != current)
166- case None => mediaPlatforms
167- }
165+ val firstAssetPlatform =
166+ (atom \ " assets" ).as[JsArray ].value.headOption.map { asset =>
167+ (asset \ " platform" ).as[Platform ]
168+ }
169+
170+ val platform = Platform .getPlatform(
171+ atomPlatform,
172+ activeAssetPlatform,
173+ firstAssetPlatform
174+ )
175+
176+ val videoPlayerFormat =
177+ (atom \ " metadata" \ " selfHost" \ " videoPlayerFormat" )
178+ .asOpt[VideoPlayerFormat ]
179+ .orElse(if (platform == Url ) Some (Loop ) else None )
168180
169181 Some (
170182 MediaAtomSummary (
171183 id,
172184 title,
173185 posterImage,
174186 contentChangeDetails,
175- sortedMediaPlatforms ,
176- currentMediaPlatform
187+ platform ,
188+ videoPlayerFormat
177189 )
178190 )
179191 }
@@ -242,27 +254,13 @@ class DynamoBackedAtomListStore(store: PreviewDynamoDataStore)
242254 }
243255
244256 private def fromAtom (atom : MediaAtom ): MediaAtomSummary = {
245- val versions = atom.assets.map(_.version).toSet
246- val currentAsset = atom.assets.find(asset =>
247- asset.version == atom.activeVersion.getOrElse(versions.max)
248- )
249- val mediaPlatforms = atom.assets.map(_.platform.name.toLowerCase).distinct
250- val currentMediaPlatform =
251- currentAsset.map(_.platform.name).map(_.toLowerCase)
252-
253- // sort media platforms so the current one is first
254- val sortedMediaPlatforms = currentMediaPlatform match {
255- case Some (current) => current :: mediaPlatforms.filter(_ != current)
256- case None => mediaPlatforms
257- }
258-
259257 MediaAtomSummary (
260258 atom.id,
261259 atom.title,
262260 atom.posterImage,
263261 atom.contentChangeDetails,
264- sortedMediaPlatforms ,
265- currentMediaPlatform
262+ atom.platform.getOrElse( Youtube ) ,
263+ atom.videoPlayerFormat
266264 )
267265 }
268266}
0 commit comments