Skip to content

Commit

Permalink
add the document as a param to shouldAffiliateLinks and related funct…
Browse files Browse the repository at this point in the history
…ions
  • Loading branch information
dskamiotis committed May 13, 2024
1 parent 08f615e commit 3767127
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 3 deletions.
1 change: 1 addition & 0 deletions applications/app/views/fragments/galleryBody.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
firstPublishedDate = page.gallery.content.fields.firstPublicationDate,
pageUrl = request.uri,
contentType = "gallery",
// document = page.gallery.content
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import play.api.libs.json._
import play.api.mvc.RequestHeader
import views.support.CamelCase
import experiments.ActiveExperiments
import org.jsoup.Jsoup

// -----------------------------------------------------------------
// DCR Blocks DataModel
Expand Down Expand Up @@ -69,7 +70,9 @@ object DotcomBlocksRenderingDataModel {
bodyBlocks: Seq[APIBlock],
): DotcomBlocksRenderingDataModel = {
val content = page.item
val shouldAddAffiliateLinks = DotcomRenderingUtils.shouldAddAffiliateLinks(content)
val htmlBlocks = bodyBlocks.map { block => block.bodyHtml }
val document = Jsoup.parseBodyFragment(htmlBlocks.mkString(""))
val shouldAddAffiliateLinks = DotcomRenderingUtils.shouldAddAffiliateLinks(content, document)
val contentDateTimes = DotcomRenderingUtils.contentDateTimes(content)

val edition = Edition(request)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import common.Maps.RichMap
import common.commercial.EditionCommercialProperties
import common.{CanonicalLink, Chronos, Edition, Localisation, RichRequestHeader}
import conf.Configuration
import org.jsoup.Jsoup
import crosswords.CrosswordPageWithContent
import experiments.ActiveExperiments
import model.dotcomrendering.DotcomRenderingUtils._
Expand Down Expand Up @@ -487,7 +488,10 @@ object DotcomRenderingDataModel {
blocks.exists(block => DotcomRenderingUtils.stringContainsAffiliateableLinks(block.bodyHtml))
}

val shouldAddAffiliateLinks = DotcomRenderingUtils.shouldAddAffiliateLinks(content)
val htmlBlocks = bodyBlocks.map { block => block.bodyHtml }
val document = Jsoup.parseBodyFragment(htmlBlocks.mkString(""))

val shouldAddAffiliateLinks = DotcomRenderingUtils.shouldAddAffiliateLinks(content, document)
val shouldAddDisclaimer = hasAffiliateLinks(bodyBlocks)

val contentDateTimes: ArticleDateTimes = ArticleDateTimes(
Expand Down
4 changes: 3 additions & 1 deletion common/app/model/dotcomrendering/DotcomRenderingUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import model.{
Pillar,
}
import org.joda.time.format.DateTimeFormat
import org.jsoup.nodes.Document
import play.api.libs.json._
import play.api.mvc.RequestHeader
import views.support.AffiliateLinksCleaner
Expand Down Expand Up @@ -241,7 +242,7 @@ object DotcomRenderingUtils {
}
}

def shouldAddAffiliateLinks(content: ContentType): Boolean = {
def shouldAddAffiliateLinks(content: ContentType, document: Document): Boolean = {
AffiliateLinksCleaner.shouldAddAffiliateLinks(
switchedOn = Switches.AffiliateLinks.isSwitchedOn,
section = content.metadata.sectionId,
Expand All @@ -253,6 +254,7 @@ object DotcomRenderingUtils {
firstPublishedDate = content.content.fields.firstPublicationDate,
pageUrl = content.metadata.id,
contentType = "article",
document,
)
}

Expand Down
2 changes: 2 additions & 0 deletions common/app/views/support/HtmlCleaner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,7 @@ case class AffiliateLinksCleaner(
publishedDate,
pageUrl,
contentType,
document,
)
) {
AffiliateLinksCleaner.replaceLinksInHtml(document, pageUrl, skimlinksId)
Expand Down Expand Up @@ -946,6 +947,7 @@ object AffiliateLinksCleaner {
firstPublishedDate: Option[DateTime],
pageUrl: String,
contentType: String,
document: Document = null,
): Boolean = {
val publishedCutOffDate = new DateTime(2020, 8, 14, 0, 0)

Expand Down
11 changes: 11 additions & 0 deletions common/test/views/support/cleaner/AffiliateLinksCleanerTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class AffiliateLinksCleanerTest extends AnyFlatSpec with Matchers {
val oldPublishedDate = Some(new DateTime(2020, 8, 13, 0, 0))
val newPublishedDate = Some(new DateTime(2020, 8, 15, 0, 0))
val deniedPageUrl = "/fashion/2024/feb/16/sunscreen-in-winter-yep-spf-moisturiser-is-essential-all-year-round"
val validHtml = """<div class=\"content__article-body from-content-api js-article__body\" itemprop=\"articleBody\"\n data-test-id=\"article-review-body\" \n\n \n\n\n\n>\n <p><span class=\"drop-cap\"><span class=\"drop-cap__inner\">T</span></span>here are so many lipsticks to choose from now, which is fun but can be overwhelming. The longest-wearing formulation is liquid, which forms a sort of seal on the lips, but many people find them drying. The next best thing is a matt lipstick bullet, offering staying power without parched lips.</p>\n<p>Start with picking a colour you love. You told me your outfit and colourings: a cream dress suits bright red. If you’re new to red lipstick, I’d recommend a brown-ish red such as Charlotte Tilbury <a href=\"https://www.cultbeauty.co.uk/charlotte-tilbury-matte-revolution/13323147.html?affil=thggpsad&amp;amp;switchcurrency=GBP&amp;amp;shippingcountry=GB&amp;amp;variation=13323158&amp;amp;gad_source=1&amp;amp;gclid=Cj0KCQiAw6yuBhDrARIsACf94RW1go2lRZz-IU6LIlUC6NqWZEfPjdbXlxgM4WAvG_eu7iZ03L6iFAoaAgIaEALw_wcB&amp;amp;gclsrc=aw.ds\" data-link-name=\"in body link\" class=\"u-underline\">Matte Revolution in Walk of No Shame</a>. Or try something with a hint of orange, such as Chilli, a warm, brick red, or Lady Danger, a vivid coral, from Mac’s reformulated <a href=\"https://www.maccosmetics.co.uk/product/13854/123863/products/makeup/lips/lipstick/macximal-silky-matte-lipstick?gad_source=1&amp;amp;gclid=Cj0KCQiA84CvBhCaARIsAMkAvkLfrUsODrSO6I1MNBQUzYfb3N4s_EsUYYVo0SRsC46tdZTzROMwIZEaAtAxEALw_wcB&amp;amp;gclsrc=aw.ds\" data-link-name=\"in body link\" class=\"u-underline\">Macximal Silky Matte range</a>.</p>\n<p>A neutral shade similar to your own lip colour works for every outfit. <a href=\"https://www.sephora.co.uk/p/DIOR-Rouge-Dior-Lipstick-35g?curr=GBP&amp;amp;option=61504curr=GBP&amp;amp;gad_source=1&amp;amp;gclid=Cj0KCQiAw6yuBhDrARIsACf94RVewm4VSC6Clq1CctvYLroiCzOoLVwPtx1xqUZefCpdRK6DADDmQ9YaAhcvEALw_wcB&amp;amp;gclsrc=aw.ds\" data-link-name=\"in body link\" class=\"u-underline\">Dior Rouge Dior Couture Colour</a> range has incredible staying power, but for a creamier finish, its <a href=\"https://www.lookfantastic.com/makeup-revolution-irl-filter-finish-lip-creme-1.8ml-various-shades/13947237.html?affil=thggpsad&amp;amp;switchcurrency=GBP&amp;amp;shippingcountry=GB&amp;amp;variation=13947243&amp;amp;affil=thgppc&amp;amp;kwds=&amp;amp;thg_ppc_campaign=71700000117078542&amp;amp;adtype=pla&amp;amp;product_id=13947243&amp;amp;gclid=Cj0KCQiAw6yuBhDrARIsACf94RV9W_qH5nEKAQU_D-f_7hRIYbmmCYMRomyaDPpHdLc3_VQRqBl2-hEaApjJEALw_wcB&amp;amp;gclsrc=aw.ds\" data-link-name=\"in body link\" class=\"u-underline\">Make Up Revolution IRL Filter Finish Lip Crème </a>is in between a matt and a cream lipstick, and lasts well. Or try a pigmented balm that really lasts – <a href=\"https://www.cultbeauty.co.uk/kosas-wet-stick-moisturizing-shiny-sheer-lipstick-3.1g-various-shades/14877003.html?affil=thggpsad&amp;amp;switchcurrency=GBP&amp;amp;shippingcountry=GB&amp;amp;variation=14877015&amp;amp;gad_source=1&amp;amp;gclid=Cj0KCQiAw6yuBhDrARIsACf94RVK_Yrwf6PJA8iu8wOAHyk2IgcvHjNcv7tUWlnGE_LKVM5i9igvk4IaAqVEEALw_wcB&amp;amp;gclsrc=aw.ds\" data-link-name=\"in body link\" class=\"u-underline\">Kosas</a> and <a href=\"https://www.cultbeauty.co.uk/westman-atelier-squeaky-clean-liquid-lip-balm/13324065.html?affil=thggpsad&amp;amp;switchcurrency=GBP&amp;amp;shippingcountry=GB&amp;amp;variation=13324066&amp;amp;gad_source=1&amp;amp;gclid=Cj0KCQiAw6yuBhDrARIsACf94RXbp4_JlHUPYCRcRIbzMaIEe-P5OuDZmh07xuR3VTzsMAbr30ItNDAaAizLEALw_wcB&amp;amp;gclsrc=aw.ds\" data-link-name=\"in body link\" class=\"u-underline\">Westman Atelier</a> are my go-tos.</p>\n<p>Whatever colour or formulation you pick, always use a lipliner in a similar or slightly darker shade to line the lips and avoid any bleeding or feathering. But my biggest hack is to use the lipliner to also fill in your lips; this gives the lipstick something to “grip” on to and keeps it in place. Whatever you pick, if you like it, it’s perfect – whether it’s green, nude, red or sparkly purple.</p>\n<p><em>Got a beauty question for Anita? Email her at <a href=\"mailto:%[email protected]\" data-link-name=\"in body link\" class=\"u-underline\">[email protected]</a></em></p>\n </div>"""

shouldAddAffiliateLinks(
switchedOn = false,
Expand All @@ -32,6 +33,7 @@ class AffiliateLinksCleanerTest extends AnyFlatSpec with Matchers {
oldPublishedDate,
deniedPageUrl,
"article",
validHtml,
) should be(false)
shouldAddAffiliateLinks(
switchedOn = true,
Expand All @@ -44,6 +46,7 @@ class AffiliateLinksCleanerTest extends AnyFlatSpec with Matchers {
oldPublishedDate,
deniedPageUrl,
"article",
validHtml,
) should be(true)
shouldAddAffiliateLinks(
switchedOn = true,
Expand All @@ -56,6 +59,7 @@ class AffiliateLinksCleanerTest extends AnyFlatSpec with Matchers {
oldPublishedDate,
deniedPageUrl,
"article",
validHtml,
) should be(false)
shouldAddAffiliateLinks(
switchedOn = true,
Expand All @@ -68,6 +72,7 @@ class AffiliateLinksCleanerTest extends AnyFlatSpec with Matchers {
oldPublishedDate,
deniedPageUrl,
"article",
validHtml,
) should be(true)
shouldAddAffiliateLinks(
switchedOn = true,
Expand All @@ -80,6 +85,7 @@ class AffiliateLinksCleanerTest extends AnyFlatSpec with Matchers {
oldPublishedDate,
deniedPageUrl,
"article",
validHtml,
) should be(false)
shouldAddAffiliateLinks(
switchedOn = true,
Expand All @@ -92,6 +98,7 @@ class AffiliateLinksCleanerTest extends AnyFlatSpec with Matchers {
oldPublishedDate,
deniedPageUrl,
"article",
validHtml,
) should be(false)
shouldAddAffiliateLinks(
switchedOn = true,
Expand All @@ -104,6 +111,7 @@ class AffiliateLinksCleanerTest extends AnyFlatSpec with Matchers {
oldPublishedDate,
deniedPageUrl,
"article",
validHtml,
) should be(true)
shouldAddAffiliateLinks(
switchedOn = true,
Expand All @@ -116,6 +124,7 @@ class AffiliateLinksCleanerTest extends AnyFlatSpec with Matchers {
oldPublishedDate,
deniedPageUrl,
"article",
validHtml,
) should be(false)
shouldAddAffiliateLinks(
switchedOn = true,
Expand All @@ -128,6 +137,7 @@ class AffiliateLinksCleanerTest extends AnyFlatSpec with Matchers {
oldPublishedDate,
deniedPageUrl,
"article",
validHtml,
) should be(true)
shouldAddAffiliateLinks(
switchedOn = true,
Expand All @@ -140,6 +150,7 @@ class AffiliateLinksCleanerTest extends AnyFlatSpec with Matchers {
newPublishedDate,
deniedPageUrl,
"article",
validHtml,
) should be(false)
shouldAddAffiliateLinks(
switchedOn = true,
Expand Down

0 comments on commit 3767127

Please sign in to comment.