-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #568 from NDLANO/set-hide-byline-for-images
Sett hidebyline på bilder i artikler som ikkje er copyrighta
- Loading branch information
Showing
8 changed files
with
204 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
...a/articleapi/db/migrationwithdependencies/V55__SetHideBylineForImagesNotCopyrighted.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package no.ndla.articleapi.db.migrationwithdependencies | ||
|
||
import no.ndla.articleapi.Props | ||
import no.ndla.articleapi.db.HtmlMigration | ||
import no.ndla.articleapi.integration.ImageApiClient | ||
import no.ndla.network.NdlaClient | ||
import org.jsoup.nodes.Element | ||
|
||
trait V55__SetHideBylineForImagesNotCopyrighted { | ||
this: ImageApiClient & NdlaClient & Props => | ||
|
||
class V55__SetHideBylineForImagesNotCopyrighted extends HtmlMigration { | ||
override val tableName: String = "contentdata" | ||
override val columnName: String = "document" | ||
|
||
/** Method to override that manipulates the content string */ | ||
override def convertHtml(doc: Element, language: String): Element = { | ||
val ids = List.newBuilder[String] | ||
doc | ||
.select("ndlaembed[data-resource='image']") | ||
.forEach(embed => { | ||
val noHideByline = !embed.hasAttr("data-hide-byline") | ||
if (noHideByline) { | ||
ids += embed.attr("data-resource_id") | ||
} | ||
}) | ||
if (ids.result().isEmpty) { | ||
return doc | ||
} | ||
val images = imageApiClient.getImagesWithIds(ids.result()).getOrElse(List.empty) | ||
doc | ||
.select("ndlaembed[data-resource='image']") | ||
.forEach(embed => { | ||
val imageId = embed.attr("data-resource_id") | ||
val image = images.find(i => i.id == imageId) | ||
embed | ||
.attr("data-hide-byline", s"${image.exists(i => !i.copyright.license.license.equals("COPYRIGHTED"))}"): Unit | ||
}) | ||
doc | ||
} | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
article-api/src/main/scala/no/ndla/articleapi/integration/ImageApiClient.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Part of NDLA article-api | ||
* Copyright (C) 2018 NDLA | ||
* | ||
* See LICENSE | ||
*/ | ||
|
||
package no.ndla.articleapi.integration | ||
|
||
import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder} | ||
import io.circe.{Decoder, Encoder} | ||
import no.ndla.articleapi.Props | ||
import no.ndla.articleapi.service.ConverterService | ||
import no.ndla.common.model.api.CopyrightDTO | ||
import no.ndla.network.NdlaClient | ||
import sttp.client3.quick.* | ||
|
||
import scala.util.Try | ||
|
||
trait ImageApiClient { | ||
this: NdlaClient & ConverterService & Props => | ||
val imageApiClient: ImageApiClient | ||
|
||
class ImageApiClient { | ||
private val Endpoint = s"http://${props.ImageApiHost}/image-api/v3/images" | ||
|
||
def getImagesWithIds(ids: Seq[String]): Try[Seq[ImageWithCopyright]] = { | ||
val idsParam = ids.mkString(",") | ||
get[Seq[ImageWithCopyright]](s"$Endpoint/ids", "ids" -> idsParam) | ||
} | ||
|
||
private def get[A: Decoder](endpointUrl: String, params: (String, String)*): Try[A] = { | ||
val request = quickRequest.get(uri"$endpointUrl".withParams(params*)) | ||
ndlaClient.fetch[A](request) | ||
} | ||
|
||
} | ||
} | ||
case class ImageWithCopyright(id: String, copyright: CopyrightDTO) | ||
|
||
object ImageWithCopyright { | ||
implicit val encoder: Encoder[ImageWithCopyright] = deriveEncoder | ||
implicit val decoder: Decoder[ImageWithCopyright] = deriveDecoder | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
...dla/draftapi/db/migrationwithdependencies/V66__SetHideBylineForImagesNotCopyrighted.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Part of NDLA draft-api | ||
* Copyright (C) 2024 NDLA | ||
* | ||
* See LICENSE | ||
*/ | ||
|
||
package no.ndla.draftapi.db.migrationwithdependencies | ||
|
||
import no.ndla.draftapi.Props | ||
import no.ndla.draftapi.db.HtmlMigration | ||
import no.ndla.draftapi.integration.ImageApiClient | ||
import no.ndla.network.NdlaClient | ||
import org.jsoup.nodes.Element | ||
import scalikejdbc.{SQLSyntax, scalikejdbcSQLInterpolationImplicitDef} | ||
|
||
trait V66__SetHideBylineForImagesNotCopyrighted { | ||
this: ImageApiClient & NdlaClient & Props => | ||
|
||
class V66__SetHideBylineForImagesNotCopyrighted extends HtmlMigration { | ||
override val tableName: String = "articledata a" | ||
override val columnName: String = "document" | ||
private lazy val columnNameSQL: SQLSyntax = SQLSyntax.createUnsafely(columnName) | ||
|
||
override lazy val whereClause: SQLSyntax = | ||
sqls"$columnNameSQL is not null and $columnNameSQL -> 'status' ->> 'current' != 'ARCHIVED' and $columnNameSQL -> 'status' ->> 'current' != 'UNPUBLISHED' and revision = (select max(revision) from articledata a2 where a2.article_id = a.article_id)" | ||
|
||
/** Method to override that manipulates the content string */ | ||
override def convertHtml(doc: Element, language: String): Element = { | ||
val ids = List.newBuilder[String] | ||
doc | ||
.select("ndlaembed[data-resource='image']") | ||
.forEach(embed => { | ||
val noHideByline = !embed.hasAttr("data-hide-byline") | ||
if (noHideByline) { | ||
ids += embed.attr("data-resource_id") | ||
} | ||
}) | ||
if (ids.result().isEmpty) { | ||
return doc | ||
} | ||
val images = imageApiClient.getImagesWithIds(ids.result()).getOrElse(List.empty) | ||
doc | ||
.select("ndlaembed[data-resource='image']") | ||
.forEach(embed => { | ||
val imageId = embed.attr("data-resource_id") | ||
val image = images.find(i => i.id == imageId) | ||
embed | ||
.attr("data-hide-byline", s"${image.exists(i => !i.copyright.license.license.equals("COPYRIGHTED"))}"): Unit | ||
}) | ||
doc | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
draft-api/src/main/scala/no/ndla/draftapi/integration/ImageApiClient.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Part of NDLA draft-api | ||
* Copyright (C) 2018 NDLA | ||
* | ||
* See LICENSE | ||
*/ | ||
|
||
package no.ndla.draftapi.integration | ||
|
||
import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder} | ||
import io.circe.{Decoder, Encoder} | ||
import no.ndla.common.model.api.CopyrightDTO | ||
import no.ndla.draftapi.Props | ||
import no.ndla.draftapi.service.ConverterService | ||
import no.ndla.network.NdlaClient | ||
import sttp.client3.quick.* | ||
|
||
import scala.util.Try | ||
|
||
trait ImageApiClient { | ||
this: NdlaClient & ConverterService & Props => | ||
val imageApiClient: ImageApiClient | ||
|
||
class ImageApiClient { | ||
private val Endpoint = s"http://${props.ImageApiHost}/image-api/v3/images" | ||
|
||
def getImagesWithIds(ids: Seq[String]): Try[Seq[ImageWithCopyright]] = { | ||
val idsParam = ids.mkString(",") | ||
get[Seq[ImageWithCopyright]](s"$Endpoint/ids", "ids" -> idsParam) | ||
} | ||
|
||
private def get[A: Decoder](endpointUrl: String, params: (String, String)*): Try[A] = { | ||
val request = quickRequest.get(uri"$endpointUrl".withParams(params*)) | ||
ndlaClient.fetch[A](request) | ||
} | ||
|
||
} | ||
} | ||
case class ImageWithCopyright(id: String, copyright: CopyrightDTO) | ||
|
||
object ImageWithCopyright { | ||
implicit val encoder: Encoder[ImageWithCopyright] = deriveEncoder | ||
implicit val decoder: Decoder[ImageWithCopyright] = deriveDecoder | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters