1 /*
2  *  Make.org Core API
3  *  Copyright (C) 2018 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.sequence
21 
22 import akka.util.Timeout
23 import org.make.api.proposal.{ProposalResponse, ProposalServiceComponent}
24 import org.make.api.sequence.SequenceBehaviour.ConsensusParam
25 import org.make.api.sequence.SequenceCacheManager.{GetConsensusProposal, GetControversyProposal, GetProposal}
26 import org.make.api.technical.{ActorSystemComponent, TimeSettings}
27 import org.make.api.technical.BetterLoggingActors.BetterLoggingTypedActorRef
28 import org.make.api.technical.security.SecurityConfigurationComponent
29 import org.make.core.RequestContext
30 import org.make.core.reference.Language
31 import org.make.core.proposal.indexed.IndexedProposal
32 import org.make.core.question.QuestionId
33 
34 import scala.concurrent.Future
35 import scala.concurrent.ExecutionContext.Implicits.global
36 
37 trait DefaultSequenceCacheManagerServiceComponent extends SequenceCacheManagerServiceComponent {
38   this: SequenceCacheManagerComponent
39     with ActorSystemComponent
40     with ProposalServiceComponent
41     with SecurityConfigurationComponent
42     with SequenceConfigurationComponent =>
43 
44   override lazy val sequenceCacheManagerService: SequenceCacheManagerService = new DefaultSequenceCacheManagerService
45 
46   class DefaultSequenceCacheManagerService extends SequenceCacheManagerService {
47     implicit val timeout: Timeout = TimeSettings.defaultTimeout
48 
49     // These methods are meant to be used by the widget.
50     // Widget does not support cookies thus users will not be authenticated.
51     // There's no need to populate `myProposal` and `voteAndQualifications`
52     override def getProposal(
53       id: QuestionId,
54       requestContext: RequestContext,
55       preferredLanguage: Option[Language],
56       questionDefaultLanguage: Language
57     ): Future[ProposalResponse] =
58       (sequenceCacheManager ?? (GetProposal(id, _)))
59         .flatMap(shapeResponse(_, requestContext, preferredLanguage, questionDefaultLanguage))
60 
61     override def getControversyProposal(
62       id: QuestionId,
63       requestContext: RequestContext,
64       preferredLanguage: Option[Language],
65       questionDefaultLanguage: Language
66     ): Future[ProposalResponse] =
67       (sequenceCacheManager ?? (GetControversyProposal(id, _)))
68         .flatMap(shapeResponse(_, requestContext, preferredLanguage, questionDefaultLanguage))
69 
70     override def getConsensusProposal(
71       id: QuestionId,
72       threshold: () => Future[ConsensusParam],
73       requestContext: RequestContext,
74       preferredLanguage: Option[Language],
75       questionDefaultLanguage: Language
76     ): Future[ProposalResponse] =
77       (sequenceCacheManager ?? (GetConsensusProposal(id, threshold, _)))
78         .flatMap(shapeResponse(_, requestContext, preferredLanguage, questionDefaultLanguage))
79 
80     def shapeResponse(
81       proposal: IndexedProposal,
82       requestContext: RequestContext,
83       preferredLanguage: Option[Language],
84       questionDefaultLanguage: Language
85     ): Future[ProposalResponse] = {
86       val proposalKey =
87         proposalService.generateProposalKeyHash(
88           proposal.id,
89           requestContext.sessionId,
90           requestContext.location,
91           securityConfiguration.secureVoteSalt
92         )
93 
94       val futureThreshold = proposal.question.map(_.questionId) match {
95         case None => Future.successful(0)
96         case Some(questionId) =>
97           sequenceConfigurationService
98             .getSequenceConfigurationByQuestionId(questionId)
99             .map(_.newProposalsVoteThreshold)
100       }
101 
102       futureThreshold.map(
103         newProposalsVoteThreshold =>
104           ProposalResponse(
105             indexedProposal = proposal,
106             myProposal = false,
107             voteAndQualifications = None,
108             proposalKey = proposalKey,
109             newProposalsVoteThreshold = newProposalsVoteThreshold,
110             preferredLanguage = preferredLanguage,
111             questionDefaultLanguage = Some(questionDefaultLanguage)
112           )
113       )
114     }
115   }
116 }
Line Stmt Id Pos Tree Symbol Tests Code
47 23751 2042 - 2069 Select org.make.api.technical.TimeSettings.defaultTimeout org.make.api.technical.TimeSettings.defaultTimeout
58 26541 2516 - 2516 Select org.make.api.sequence.DefaultSequenceCacheManagerServiceComponent.DefaultSequenceCacheManagerService.timeout DefaultSequenceCacheManagerService.this.timeout
58 23906 2516 - 2516 Select scala.concurrent.ExecutionContext.Implicits.global scala.concurrent.ExecutionContext.Implicits.global
58 26244 2520 - 2538 Apply org.make.api.sequence.SequenceCacheManager.GetProposal.apply org.make.api.sequence.SequenceCacheManager.GetProposal.apply(id, x$1)
58 22386 2495 - 2515 Select org.make.api.sequence.SequenceCacheManagerComponent.sequenceCacheManager DefaultSequenceCacheManagerServiceComponent.this.sequenceCacheManager
58 27634 2516 - 2516 Select org.make.api.technical.ActorSystemComponent.actorSystem DefaultSequenceCacheManagerServiceComponent.this.actorSystem
59 27941 2557 - 2557 Select scala.concurrent.ExecutionContext.Implicits.global scala.concurrent.ExecutionContext.Implicits.global
59 24354 2558 - 2634 Apply org.make.api.sequence.DefaultSequenceCacheManagerServiceComponent.DefaultSequenceCacheManagerService.shapeResponse DefaultSequenceCacheManagerService.this.shapeResponse(x$2, requestContext, preferredLanguage, questionDefaultLanguage)
59 25795 2495 - 2635 ApplyToImplicitArgs scala.concurrent.Future.flatMap org.make.api.technical.BetterLoggingActors.BetterLoggingTypedActorRef[org.make.api.sequence.SequenceCacheManager.Protocol](DefaultSequenceCacheManagerServiceComponent.this.sequenceCacheManager).??[org.make.core.proposal.indexed.IndexedProposal](((x$1: akka.actor.typed.ActorRef[org.make.core.proposal.indexed.IndexedProposal]) => org.make.api.sequence.SequenceCacheManager.GetProposal.apply(id, x$1)))(scala.concurrent.ExecutionContext.Implicits.global, DefaultSequenceCacheManagerServiceComponent.this.actorSystem, DefaultSequenceCacheManagerService.this.timeout).flatMap[org.make.api.proposal.ProposalResponse](((x$2: org.make.core.proposal.indexed.IndexedProposal) => DefaultSequenceCacheManagerService.this.shapeResponse(x$2, requestContext, preferredLanguage, questionDefaultLanguage)))(scala.concurrent.ExecutionContext.Implicits.global)
67 27644 2883 - 2883 Select org.make.api.sequence.DefaultSequenceCacheManagerServiceComponent.DefaultSequenceCacheManagerService.timeout DefaultSequenceCacheManagerService.this.timeout
67 22395 2887 - 2916 Apply org.make.api.sequence.SequenceCacheManager.GetControversyProposal.apply org.make.api.sequence.SequenceCacheManager.GetControversyProposal.apply(id, x$3)
67 23830 2883 - 2883 Select org.make.api.technical.ActorSystemComponent.actorSystem DefaultSequenceCacheManagerServiceComponent.this.actorSystem
67 23719 2862 - 2882 Select org.make.api.sequence.SequenceCacheManagerComponent.sequenceCacheManager DefaultSequenceCacheManagerServiceComponent.this.sequenceCacheManager
67 26087 2883 - 2883 Select scala.concurrent.ExecutionContext.Implicits.global scala.concurrent.ExecutionContext.Implicits.global
68 27949 2862 - 3013 ApplyToImplicitArgs scala.concurrent.Future.flatMap org.make.api.technical.BetterLoggingActors.BetterLoggingTypedActorRef[org.make.api.sequence.SequenceCacheManager.Protocol](DefaultSequenceCacheManagerServiceComponent.this.sequenceCacheManager).??[org.make.core.proposal.indexed.IndexedProposal](((x$3: akka.actor.typed.ActorRef[org.make.core.proposal.indexed.IndexedProposal]) => org.make.api.sequence.SequenceCacheManager.GetControversyProposal.apply(id, x$3)))(scala.concurrent.ExecutionContext.Implicits.global, DefaultSequenceCacheManagerServiceComponent.this.actorSystem, DefaultSequenceCacheManagerService.this.timeout).flatMap[org.make.api.proposal.ProposalResponse](((x$4: org.make.core.proposal.indexed.IndexedProposal) => DefaultSequenceCacheManagerService.this.shapeResponse(x$4, requestContext, preferredLanguage, questionDefaultLanguage)))(scala.concurrent.ExecutionContext.Implicits.global)
68 24280 2935 - 2935 Select scala.concurrent.ExecutionContext.Implicits.global scala.concurrent.ExecutionContext.Implicits.global
68 25318 2936 - 3012 Apply org.make.api.sequence.DefaultSequenceCacheManagerServiceComponent.DefaultSequenceCacheManagerService.shapeResponse DefaultSequenceCacheManagerService.this.shapeResponse(x$4, requestContext, preferredLanguage, questionDefaultLanguage)
77 22622 3306 - 3306 Select scala.concurrent.ExecutionContext.Implicits.global scala.concurrent.ExecutionContext.Implicits.global
77 23734 3310 - 3348 Apply org.make.api.sequence.SequenceCacheManager.GetConsensusProposal.apply org.make.api.sequence.SequenceCacheManager.GetConsensusProposal.apply(id, threshold, x$5)
77 23842 3306 - 3306 Select org.make.api.sequence.DefaultSequenceCacheManagerServiceComponent.DefaultSequenceCacheManagerService.timeout DefaultSequenceCacheManagerService.this.timeout
77 26398 3306 - 3306 Select org.make.api.technical.ActorSystemComponent.actorSystem DefaultSequenceCacheManagerServiceComponent.this.actorSystem
77 25719 3285 - 3305 Select org.make.api.sequence.SequenceCacheManagerComponent.sequenceCacheManager DefaultSequenceCacheManagerServiceComponent.this.sequenceCacheManager
78 24338 3285 - 3445 ApplyToImplicitArgs scala.concurrent.Future.flatMap org.make.api.technical.BetterLoggingActors.BetterLoggingTypedActorRef[org.make.api.sequence.SequenceCacheManager.Protocol](DefaultSequenceCacheManagerServiceComponent.this.sequenceCacheManager).??[org.make.core.proposal.indexed.IndexedProposal](((x$5: akka.actor.typed.ActorRef[org.make.core.proposal.indexed.IndexedProposal]) => org.make.api.sequence.SequenceCacheManager.GetConsensusProposal.apply(id, threshold, x$5)))(scala.concurrent.ExecutionContext.Implicits.global, DefaultSequenceCacheManagerServiceComponent.this.actorSystem, DefaultSequenceCacheManagerService.this.timeout).flatMap[org.make.api.proposal.ProposalResponse](((x$6: org.make.core.proposal.indexed.IndexedProposal) => DefaultSequenceCacheManagerService.this.shapeResponse(x$6, requestContext, preferredLanguage, questionDefaultLanguage)))(scala.concurrent.ExecutionContext.Implicits.global)
78 27786 3368 - 3444 Apply org.make.api.sequence.DefaultSequenceCacheManagerServiceComponent.DefaultSequenceCacheManagerService.shapeResponse DefaultSequenceCacheManagerService.this.shapeResponse(x$6, requestContext, preferredLanguage, questionDefaultLanguage)
78 25623 3367 - 3367 Select scala.concurrent.ExecutionContext.Implicits.global scala.concurrent.ExecutionContext.Implicits.global
87 26237 3692 - 3883 Apply org.make.api.proposal.ProposalService.generateProposalKeyHash DefaultSequenceCacheManagerServiceComponent.this.proposalService.generateProposalKeyHash(proposal.id, requestContext.sessionId, requestContext.location, DefaultSequenceCacheManagerServiceComponent.this.securityConfiguration.secureVoteSalt)
88 28257 3743 - 3754 Select org.make.core.proposal.indexed.IndexedProposal.id proposal.id
89 25729 3766 - 3790 Select org.make.core.RequestContext.sessionId requestContext.sessionId
90 23747 3802 - 3825 Select org.make.core.RequestContext.location requestContext.location
91 22544 3837 - 3873 Select org.make.api.technical.security.SecurityConfiguration.secureVoteSalt DefaultSequenceCacheManagerServiceComponent.this.securityConfiguration.secureVoteSalt
94 24139 3935 - 3947 Select org.make.core.proposal.indexed.IndexedProposalQuestion.questionId x$7.questionId
94 27795 3913 - 3948 Apply scala.Option.map proposal.question.map[org.make.core.question.QuestionId](((x$7: org.make.core.proposal.indexed.IndexedProposalQuestion) => x$7.questionId))
95 25636 3978 - 3998 Apply scala.concurrent.Future.successful scala.concurrent.Future.successful[Int](0)
99 28102 4149 - 4149 Select scala.concurrent.ExecutionContext.Implicits.global scala.concurrent.ExecutionContext.Implicits.global
99 24267 4150 - 4177 Select org.make.core.sequence.SequenceConfiguration.newProposalsVoteThreshold x$8.newProposalsVoteThreshold
99 25884 4042 - 4178 ApplyToImplicitArgs scala.concurrent.Future.map DefaultSequenceCacheManagerServiceComponent.this.sequenceConfigurationService.getSequenceConfigurationByQuestionId(questionId).map[Int](((x$8: org.make.core.sequence.SequenceConfiguration) => x$8.newProposalsVoteThreshold))(scala.concurrent.ExecutionContext.Implicits.global)
102 27803 4213 - 4213 Select scala.concurrent.ExecutionContext.Implicits.global scala.concurrent.ExecutionContext.Implicits.global
102 25555 4194 - 4638 ApplyToImplicitArgs scala.concurrent.Future.map futureThreshold.map[org.make.api.proposal.ProposalResponse](((newProposalsVoteThreshold: Int) => org.make.api.proposal.ProposalResponse.apply(proposal, false, scala.None, proposalKey, newProposalsVoteThreshold, preferredLanguage, scala.Some.apply[org.make.core.reference.Language](questionDefaultLanguage))))(scala.concurrent.ExecutionContext.Implicits.global)
104 23993 4262 - 4630 Apply org.make.api.proposal.ProposalResponse.apply org.make.api.proposal.ProposalResponse.apply(proposal, false, scala.None, proposalKey, newProposalsVoteThreshold, preferredLanguage, scala.Some.apply[org.make.core.reference.Language](questionDefaultLanguage))
106 23667 4345 - 4350 Literal <nosymbol> false
107 27337 4388 - 4392 Select scala.None scala.None
111 26161 4589 - 4618 Apply scala.Some.apply scala.Some.apply[org.make.core.reference.Language](questionDefaultLanguage)