Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds url to embeds when fetching by ids #530

Merged
merged 1 commit into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,8 @@ import scala.math.max
import scala.util.{Failure, Success, Try}

trait ReadService {
this: ArticleRepository
with FeideApiClient
with ConverterService
with ArticleSearchService
with SearchConverterService
with MemoizeHelpers
with Props
with ErrorHelpers
with FrontpageApiClient =>
this: ArticleRepository & FeideApiClient & ConverterService & ArticleSearchService & SearchConverterService &
MemoizeHelpers & Props & ErrorHelpers & FrontpageApiClient =>
val readService: ReadService

class ReadService extends StrictLogging {
Expand Down Expand Up @@ -153,9 +146,8 @@ trait ReadService {
private def addUrlOnEmbedTag(embedTag: Element): Unit = {
val typeAndPathOption = embedTag.attr(TagAttribute.DataResource.toString) match {
case resourceType
if resourceType == ResourceType.File.toString
|| resourceType == ResourceType.H5P.toString
&& embedTag.hasAttr(TagAttribute.DataPath.toString) =>
if resourceType == ResourceType.File.toString || resourceType == ResourceType.H5P.toString && embedTag
.hasAttr(TagAttribute.DataPath.toString) =>
val path = embedTag.attr(TagAttribute.DataPath.toString)
Some((resourceType, path))

Expand Down Expand Up @@ -278,9 +270,10 @@ trait ReadService {
): Try[Seq[api.ArticleV2]] = {
if (articleIds.isEmpty) Failure(ValidationException("ids", "Query parameter 'ids' is missing"))
else {
val offset = (page - 1) * pageSize
val domainArticles = articleRepository.withIds(articleIds, offset, pageSize).toArticles
val isFeideNeeded = domainArticles.exists(article => article.availability == Availability.teacher)
val offset = (page - 1) * pageSize
val domainArticles =
articleRepository.withIds(articleIds, offset, pageSize).toArticles.map(addUrlsOnEmbedResources)
val isFeideNeeded = domainArticles.exists(article => article.availability == Availability.teacher)
val filtered = if (isFeideNeeded) applyAvailabilityFilter(feideAccessToken, domainArticles) else domainArticles
filtered.traverse(article => converterService.toApiArticleV2(article, language, fallback))
}
Expand All @@ -307,7 +300,7 @@ trait ReadService {
articles.map(Cachable.merge)
}

def toArticleItem(article: api.ArticleV2): String = {
private def toArticleItem(article: api.ArticleV2): String = {
s"""<item>
| <title>${article.title.title}</title>
| <description>${article.metaDescription.metaDescription}</description>
Expand All @@ -322,7 +315,7 @@ trait ReadService {
.getOrElse(s"${props.ndlaFrontendUrl}/article/$id")

private val allBlankLinesRegex = """(?m)^\s*$[\r\n]*""".r
def toRSSXML(parentArticle: api.ArticleV2, articles: List[api.ArticleV2]): String = {
private def toRSSXML(parentArticle: api.ArticleV2, articles: List[api.ArticleV2]): String = {
val rss = s"""<?xml version="1.0" encoding="utf-8"?>
|<rss version="2.0">
| <channel>
Expand Down
31 changes: 12 additions & 19 deletions draft-api/src/main/scala/no/ndla/draftapi/service/ReadService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

package no.ndla.draftapi.service

import cats.implicits._
import cats.implicits.*
import io.lemonlabs.uri.{Path, Url}
import no.ndla.common.configuration.Constants.EmbedTagName
import no.ndla.common.errors.ValidationException
Expand All @@ -24,28 +24,20 @@ import no.ndla.draftapi.service.search.{
SearchConverterService,
TagSearchService
}
import no.ndla.validation._
import no.ndla.validation.*
import org.jsoup.nodes.Element
import scalikejdbc.ReadOnlyAutoSession

import scala.jdk.CollectionConverters._
import scala.jdk.CollectionConverters.*
import scala.math.max
import scala.util.{Failure, Success, Try}

trait ReadService {
this: DraftRepository
with ConverterService
with ArticleSearchService
with TagSearchService
with GrepCodesSearchService
with SearchConverterService
with UserDataRepository
with WriteService
with Props
with MemoizeHelpers =>
this: DraftRepository & ConverterService & ArticleSearchService & TagSearchService & GrepCodesSearchService &
SearchConverterService & UserDataRepository & WriteService & Props & MemoizeHelpers =>
val readService: ReadService

import props._
import props.*

class ReadService {

Expand All @@ -60,7 +52,7 @@ trait ReadService {
}

def getArticleBySlug(slug: String, language: String, fallback: Boolean = false): Try[api.Article] = {
draftRepository.withSlug(slug)(ReadOnlyAutoSession) match {
draftRepository.withSlug(slug)(ReadOnlyAutoSession).map(addUrlsOnEmbedResources) match {
case None => Failure(NotFoundException(s"The article with slug '$slug' was not found"))
case Some(article) => converterService.toApiArticle(article, language, fallback)
}
Expand Down Expand Up @@ -131,9 +123,8 @@ trait ReadService {
private def addUrlOnEmbedTag(embedTag: Element): Unit = {
val typeAndPathOption = embedTag.attr(TagAttribute.DataResource.toString) match {
case resourceType
if resourceType == ResourceType.File.toString
|| resourceType == ResourceType.H5P.toString
&& embedTag.hasAttr(TagAttribute.DataPath.toString) =>
if resourceType == ResourceType.File.toString || resourceType == ResourceType.H5P.toString && embedTag
.hasAttr(TagAttribute.DataPath.toString) =>
val path = embedTag.attr(TagAttribute.DataPath.toString)
Some((resourceType, path))

Expand Down Expand Up @@ -185,7 +176,9 @@ trait ReadService {
if (articleIds.isEmpty) Failure(ValidationException("ids", "Query parameter 'ids' is missing"))
else Success(articleIds)
domainArticles <- draftRepository.withIds(ids, offset, pageSize)
api <- domainArticles.traverse(article => converterService.toApiArticle(article, language, fallback))
api <- domainArticles.traverse(article =>
converterService.toApiArticle(addUrlsOnEmbedResources(article), language, fallback)
)
} yield api
}
}
Expand Down
Loading