1 /*
2  *  Make.org Core API
3  *  Copyright (C) 2020 Make.org
4  *
5  * This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU Affero General Public License as
7  *  published by the Free Software Foundation, either version 3 of the
8  *  License, or (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU Affero General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Affero General Public License
16  *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
17  *
18  */
19 
20 package org.make.api.widget
21 
22 import org.apache.commons.text.StringEscapeUtils
23 import org.make.api.ConfigComponent
24 import org.make.api.technical.IdGeneratorComponent
25 import org.make.api.technical.security.{SecurityConfigurationComponent, SecurityHelper}
26 import org.make.core.question.Question
27 import org.make.core.reference.{Country, Language}
28 import org.make.core.sequence.SequenceKind
29 import org.make.core.technical.Pagination
30 import org.make.core.user.UserId
31 import org.make.core.widget.{Source, SourceId, Widget, WidgetId}
32 import org.make.core.question.QuestionId
33 import org.make.core.{DateHelperComponent, Order}
34 
35 import scala.concurrent.Future
36 
37 trait DefaultWidgetServiceComponent extends WidgetServiceComponent {
38   self: ConfigComponent
39     with DateHelperComponent
40     with IdGeneratorComponent
41     with PersistentWidgetServiceComponent
42     with SecurityConfigurationComponent =>
43 
44   override val widgetService: WidgetService = new WidgetService {
45 
46     override def get(id: WidgetId): Future[Option[Widget]] = persistentWidgetService.get(id)
47 
48     override def count(sourceId: SourceId): Future[Int] = persistentWidgetService.count(sourceId)
49 
50     override def count(questionId: QuestionId): Future[Int] = persistentWidgetService.count(questionId)
51 
52     override def list(
53       sourceId: SourceId,
54       offset: Option[Pagination.Offset],
55       end: Option[Pagination.End],
56       sort: Option[String],
57       order: Option[Order]
58     ): Future[Seq[Widget]] = persistentWidgetService.list(sourceId, offset, end, sort, order)
59 
60     override def create(
61       source: Source,
62       question: Question,
63       country: Country,
64       language: Language,
65       sequenceKind: SequenceKind,
66       author: UserId
67     ): Future[Widget] = {
68       val id = idGenerator.nextWidgetId()
69       val sequenceKindUrl =
70         if (sequenceKind == SequenceKind.Standard) "" else s"&sequenceKind=${sequenceKind.entryName}"
71       val url =
72         s"?questionSlug=${question.slug}&source=${source.source}&country=${country.value}&language=${language.value}&widgetId=${id.value}$sequenceKindUrl"
73       val widgetHeight: Int = sequenceKind match {
74         case SequenceKind.Standard => 550
75         case _                     => 572
76       }
77       val hash = SecurityHelper.createSecureHash(url, securityConfiguration.secureHashSalt)
78       val style = s"display: block; max-width: 635px; margin: 0 auto; min-height: ${widgetHeight}px"
79       val script =
80         s"""<iframe frameborder="0" scrolling="no" referrerpolicy="no-referrer-when-downgrade" width="100%" height="$widgetHeight" style="$style" src="${config
81           .getString("make-api.urls.widget")}/${StringEscapeUtils.escapeHtml4(s"$url&hash=")}$hash"></iframe>"""
82       persistentWidgetService.persist(
83         Widget(
84           id = id,
85           sourceId = source.id,
86           questionId = question.questionId,
87           country = country,
88           language = language,
89           author = author,
90           version = Widget.Version.V6,
91           script = script,
92           createdAt = dateHelper.now(),
93           sequenceKind = sequenceKind
94         )
95       )
96     }
97 
98   }
99 }
Line Stmt Id Pos Tree Symbol Tests Code
44 27907 1679 - 1682 Apply org.make.api.widget.DefaultWidgetServiceComponent.$anon.<init> org.make.api.widget.widgetservicetest new $anon()
46 25491 1761 - 1792 Apply org.make.api.widget.PersistentWidgetService.get DefaultWidgetServiceComponent.this.persistentWidgetService.get(id)
48 24150 1852 - 1891 Apply org.make.api.widget.PersistentWidgetService.count DefaultWidgetServiceComponent.this.persistentWidgetService.count(sourceId)
50 28152 1955 - 1996 Apply org.make.api.widget.PersistentWidgetService.count DefaultWidgetServiceComponent.this.persistentWidgetService.count(questionId)
58 25763 2207 - 2271 Apply org.make.api.widget.PersistentWidgetService.list DefaultWidgetServiceComponent.this.persistentWidgetService.list(sourceId, offset, end, sort, order)
68 23545 2492 - 2518 Apply org.make.core.technical.IdGenerator.nextWidgetId org.make.api.widget.widgetservicetest DefaultWidgetServiceComponent.this.idGenerator.nextWidgetId()
70 24038 2598 - 2600 Literal <nosymbol> org.make.api.widget.widgetservicetest ""
70 26038 2559 - 2596 Apply java.lang.Object.== org.make.api.widget.widgetservicetest sequenceKind.==(org.make.core.sequence.SequenceKind.Standard)
70 22428 2575 - 2596 Select org.make.core.sequence.SequenceKind.Standard org.make.api.widget.widgetservicetest org.make.core.sequence.SequenceKind.Standard
70 25424 2606 - 2648 Typed <nosymbol> org.make.api.widget.widgetservicetest ("&sequenceKind=".+(sequenceKind.entryName): String)
70 27690 2598 - 2600 Block <nosymbol> org.make.api.widget.widgetservicetest ""
74 24163 2909 - 2912 Literal <nosymbol> org.make.api.widget.widgetservicetest 550
75 27895 2951 - 2954 Literal <nosymbol> org.make.api.widget.widgetservicetest 572
77 25772 3017 - 3053 Select org.make.api.technical.security.SecurityConfiguration.secureHashSalt org.make.api.widget.widgetservicetest DefaultWidgetServiceComponent.this.securityConfiguration.secureHashSalt
77 23552 2980 - 3054 Apply org.make.api.technical.security.SecurityHelper.createSecureHash org.make.api.widget.widgetservicetest org.make.api.technical.security.SecurityHelper.createSecureHash(url, DefaultWidgetServiceComponent.this.securityConfiguration.secureHashSalt)
82 24469 3454 - 3846 Apply org.make.api.widget.PersistentWidgetService.persist org.make.api.widget.widgetservicetest DefaultWidgetServiceComponent.this.persistentWidgetService.persist(org.make.core.widget.Widget.apply(id, source.id, question.questionId, country, language, author, org.make.core.widget.Widget.Version.V6, script, DefaultWidgetServiceComponent.this.dateHelper.now(), sequenceKind))
83 25429 3495 - 3838 Apply org.make.core.widget.Widget.apply org.make.api.widget.widgetservicetest org.make.core.widget.Widget.apply(id, source.id, question.questionId, country, language, author, org.make.core.widget.Widget.Version.V6, script, DefaultWidgetServiceComponent.this.dateHelper.now(), sequenceKind)
85 27154 3543 - 3552 Select org.make.core.widget.Source.id org.make.api.widget.widgetservicetest source.id
86 26048 3577 - 3596 Select org.make.core.question.Question.questionId org.make.api.widget.widgetservicetest question.questionId
90 24021 3705 - 3722 Select org.make.core.widget.Widget.Version.V6 org.make.api.widget.widgetservicetest org.make.core.widget.Widget.Version.V6
92 27626 3773 - 3789 Apply org.make.core.DateHelper.now org.make.api.widget.widgetservicetest DefaultWidgetServiceComponent.this.dateHelper.now()