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.proposal
21 
22 import akka.actor.typed.{ActorSystem, Behavior}
23 import akka.util.Timeout
24 import grizzled.slf4j.Logging
25 import org.make.api.proposal.PublishedProposalEvent._
26 import org.make.api.technical.KafkaConsumerBehavior.Protocol
27 import org.make.api.technical.{KafkaConsumerBehavior, TimeSettings}
28 import org.make.api.userhistory._
29 
30 import scala.concurrent.Future
31 
32 class ProposalUserHistoryConsumerBehavior(userHistoryCoordinatorService: UserHistoryCoordinatorService)
33     extends KafkaConsumerBehavior[ProposalEventWrapper]
34     with Logging {
35 
36   override protected val topicKey: String = ProposalKafkaProducerBehavior.topicKey
37   override val groupId = "proposal-user-history"
38 
39   implicit val timeout: Timeout = TimeSettings.defaultTimeout
40 
41   override def handleMessage(message: ProposalEventWrapper)(system: ActorSystem[_]): Future[_] = {
42     message.event match {
43       case event: ProposalProposed             => handleProposalProposed(event)
44       case event: ProposalAccepted             => handleProposalAccepted(event)
45       case event: ProposalRefused              => handleProposalRefused(event)
46       case event: ProposalReportedNotice       => doNothing(event)
47       case event: ProposalPostponed            => handleProposalPostponed(event)
48       case event: ProposalLocked               => handleProposalLocked(event)
49       case event: ProposalViewed               => doNothing(event)
50       case event: ProposalUpdated              => doNothing(event)
51       case event: ProposalVotesVerifiedUpdated => doNothing(event)
52       case event: ProposalVotesUpdated         => doNothing(event)
53       case event: ReindexProposal              => doNothing(event)
54       case event: ProposalVoted                => doNothing(event)
55       case event: ProposalUnvoted              => doNothing(event)
56       case event: ProposalQualified            => doNothing(event)
57       case event: ProposalUnqualified          => doNothing(event)
58       case event: SimilarProposalsAdded        => doNothing(event)
59       case event: ProposalPatched              => doNothing(event)
60       case event: ProposalAddedToOperation     => doNothing(event)
61       case event: ProposalRemovedFromOperation => doNothing(event)
62       case event: ProposalAnonymized           => doNothing(event)
63       case event: ProposalKeywordsSet          => doNothing(event)
64     }
65 
66   }
67 
68   def handleProposalProposed(event: ProposalProposed): Future[Unit] = {
69     userHistoryCoordinatorService.logHistory(
70       LogUserProposalEvent(
71         userId = event.userId,
72         requestContext = event.requestContext,
73         action = UserAction(
74           date = event.eventDate,
75           actionType = LogUserProposalEvent.action,
76           arguments = UserProposal(content = event.content)
77         )
78       )
79     )
80     Future.unit
81   }
82 
83   def handleProposalLocked(event: ProposalLocked): Future[Unit] = {
84     userHistoryCoordinatorService.logHistory(
85       LogLockProposalEvent(
86         userId = event.moderatorId,
87         moderatorName = event.moderatorName,
88         requestContext = event.requestContext,
89         action = UserAction(date = event.eventDate, actionType = ProposalLocked.actionType, arguments = event)
90       )
91     )
92     Future.unit
93   }
94 
95   def handleProposalAccepted(event: ProposalAccepted): Future[Unit] = {
96     userHistoryCoordinatorService.logHistory(
97       LogAcceptProposalEvent(
98         userId = event.moderator,
99         requestContext = event.requestContext,
100         action = UserAction(date = event.eventDate, actionType = ProposalAccepted.actionType, arguments = event)
101       )
102     )
103     Future.unit
104   }
105 
106   def handleProposalRefused(event: ProposalRefused): Future[Unit] = {
107     userHistoryCoordinatorService.logHistory(
108       LogRefuseProposalEvent(
109         userId = event.moderator,
110         requestContext = event.requestContext,
111         action = UserAction(date = event.eventDate, actionType = ProposalRefused.actionType, arguments = event)
112       )
113     )
114     Future.unit
115   }
116 
117   def handleProposalPostponed(event: ProposalPostponed): Future[Unit] = {
118     userHistoryCoordinatorService.logHistory(
119       LogPostponeProposalEvent(
120         userId = event.moderator,
121         requestContext = event.requestContext,
122         action = UserAction(date = event.eventDate, actionType = ProposalPostponed.actionType, arguments = event)
123       )
124     )
125     Future.unit
126   }
127 }
128 
129 object ProposalUserHistoryConsumerBehavior {
130   val name: String = "proposal-user-history-consumer"
131 
132   def apply(userHistoryCoordinatorService: UserHistoryCoordinatorService): Behavior[Protocol] =
133     new ProposalUserHistoryConsumerBehavior(userHistoryCoordinatorService).createBehavior(name)
134 }
Line Stmt Id Pos Tree Symbol Tests Code
36 6535 1353 - 1391 Select org.make.api.proposal.ProposalKafkaProducerBehavior.topicKey ProposalKafkaProducerBehavior.topicKey
37 5658 1417 - 1440 Literal <nosymbol> "proposal-user-history"
39 7112 1476 - 1503 Select org.make.api.technical.TimeSettings.defaultTimeout org.make.api.technical.TimeSettings.defaultTimeout
42 6326 1608 - 1621 Select org.make.api.proposal.ProposalEventWrapper.event message.event
43 7646 1680 - 1709 Apply org.make.api.proposal.ProposalUserHistoryConsumerBehavior.handleProposalProposed ProposalUserHistoryConsumerBehavior.this.handleProposalProposed(event)
44 7195 1760 - 1789 Apply org.make.api.proposal.ProposalUserHistoryConsumerBehavior.handleProposalAccepted ProposalUserHistoryConsumerBehavior.this.handleProposalAccepted(event)
45 6361 1840 - 1868 Apply org.make.api.proposal.ProposalUserHistoryConsumerBehavior.handleProposalRefused ProposalUserHistoryConsumerBehavior.this.handleProposalRefused(event)
46 7854 1919 - 1935 Apply org.make.api.technical.KafkaConsumerBehavior.doNothing ProposalUserHistoryConsumerBehavior.this.doNothing(event)
47 6926 1986 - 2016 Apply org.make.api.proposal.ProposalUserHistoryConsumerBehavior.handleProposalPostponed ProposalUserHistoryConsumerBehavior.this.handleProposalPostponed(event)
48 6125 2067 - 2094 Apply org.make.api.proposal.ProposalUserHistoryConsumerBehavior.handleProposalLocked ProposalUserHistoryConsumerBehavior.this.handleProposalLocked(event)
49 5660 2145 - 2161 Apply org.make.api.technical.KafkaConsumerBehavior.doNothing ProposalUserHistoryConsumerBehavior.this.doNothing(event)
50 7069 2212 - 2228 Apply org.make.api.technical.KafkaConsumerBehavior.doNothing ProposalUserHistoryConsumerBehavior.this.doNothing(event)
51 6333 2279 - 2295 Apply org.make.api.technical.KafkaConsumerBehavior.doNothing ProposalUserHistoryConsumerBehavior.this.doNothing(event)
52 7609 2346 - 2362 Apply org.make.api.technical.KafkaConsumerBehavior.doNothing ProposalUserHistoryConsumerBehavior.this.doNothing(event)
53 7320 2413 - 2429 Apply org.make.api.technical.KafkaConsumerBehavior.doNothing ProposalUserHistoryConsumerBehavior.this.doNothing(event)
54 6364 2480 - 2496 Apply org.make.api.technical.KafkaConsumerBehavior.doNothing ProposalUserHistoryConsumerBehavior.this.doNothing(event)
55 7859 2547 - 2563 Apply org.make.api.technical.KafkaConsumerBehavior.doNothing ProposalUserHistoryConsumerBehavior.this.doNothing(event)
56 7030 2614 - 2630 Apply org.make.api.technical.KafkaConsumerBehavior.doNothing ProposalUserHistoryConsumerBehavior.this.doNothing(event)
57 6160 2681 - 2697 Apply org.make.api.technical.KafkaConsumerBehavior.doNothing ProposalUserHistoryConsumerBehavior.this.doNothing(event)
58 5788 2748 - 2764 Apply org.make.api.technical.KafkaConsumerBehavior.doNothing ProposalUserHistoryConsumerBehavior.this.doNothing(event)
59 7169 2815 - 2831 Apply org.make.api.technical.KafkaConsumerBehavior.doNothing ProposalUserHistoryConsumerBehavior.this.doNothing(event)
60 6337 2882 - 2898 Apply org.make.api.technical.KafkaConsumerBehavior.doNothing ProposalUserHistoryConsumerBehavior.this.doNothing(event)
61 7723 2949 - 2965 Apply org.make.api.technical.KafkaConsumerBehavior.doNothing ProposalUserHistoryConsumerBehavior.this.doNothing(event)
62 6872 3016 - 3032 Apply org.make.api.technical.KafkaConsumerBehavior.doNothing ProposalUserHistoryConsumerBehavior.this.doNothing(event)
63 6484 3083 - 3099 Apply org.make.api.technical.KafkaConsumerBehavior.doNothing ProposalUserHistoryConsumerBehavior.this.doNothing(event)
69 6358 3188 - 3534 Apply org.make.api.userhistory.UserHistoryCoordinatorService.logHistory ProposalUserHistoryConsumerBehavior.this.userHistoryCoordinatorService.logHistory(org.make.api.userhistory.LogUserProposalEvent.apply(event.userId, event.requestContext, org.make.api.userhistory.UserAction.apply[org.make.api.userhistory.UserProposal](event.eventDate, org.make.api.userhistory.LogUserProposalEvent.action, org.make.api.userhistory.UserProposal.apply(event.content))))
70 6829 3236 - 3528 Apply org.make.api.userhistory.LogUserProposalEvent.apply org.make.api.userhistory.LogUserProposalEvent.apply(event.userId, event.requestContext, org.make.api.userhistory.UserAction.apply[org.make.api.userhistory.UserProposal](event.eventDate, org.make.api.userhistory.LogUserProposalEvent.action, org.make.api.userhistory.UserProposal.apply(event.content)))
71 7842 3275 - 3287 Select org.make.api.proposal.PublishedProposalEvent.ProposalProposed.userId event.userId
72 7034 3314 - 3334 Select org.make.api.proposal.PublishedProposalEvent.ProposalProposed.requestContext event.requestContext
73 7645 3353 - 3520 Apply org.make.api.userhistory.UserAction.apply org.make.api.userhistory.UserAction.apply[org.make.api.userhistory.UserProposal](event.eventDate, org.make.api.userhistory.LogUserProposalEvent.action, org.make.api.userhistory.UserProposal.apply(event.content))
74 6116 3382 - 3397 Select org.make.api.proposal.PublishedProposalEvent.ProposalProposed.eventDate event.eventDate
75 5726 3422 - 3449 Select org.make.api.userhistory.LogUserProposalEvent.action org.make.api.userhistory.LogUserProposalEvent.action
76 7170 3496 - 3509 Select org.make.api.proposal.PublishedProposalEvent.ProposalProposed.content event.content
76 6307 3473 - 3510 Apply org.make.api.userhistory.UserProposal.apply org.make.api.userhistory.UserProposal.apply(event.content)
80 7843 3539 - 3550 Select scala.concurrent.Future.unit scala.concurrent.Future.unit
84 6362 3628 - 3950 Apply org.make.api.userhistory.UserHistoryCoordinatorService.logHistory ProposalUserHistoryConsumerBehavior.this.userHistoryCoordinatorService.logHistory(org.make.api.userhistory.LogLockProposalEvent.apply(event.moderatorId, event.moderatorName, event.requestContext, org.make.api.userhistory.UserAction.apply[org.make.api.proposal.PublishedProposalEvent.ProposalLocked](event.eventDate, org.make.api.proposal.PublishedProposalEvent.ProposalLocked.actionType, event)))
85 6838 3676 - 3944 Apply org.make.api.userhistory.LogLockProposalEvent.apply org.make.api.userhistory.LogLockProposalEvent.apply(event.moderatorId, event.moderatorName, event.requestContext, org.make.api.userhistory.UserAction.apply[org.make.api.proposal.PublishedProposalEvent.ProposalLocked](event.eventDate, org.make.api.proposal.PublishedProposalEvent.ProposalLocked.actionType, event))
86 6998 3715 - 3732 Select org.make.api.proposal.PublishedProposalEvent.ProposalLocked.moderatorId event.moderatorId
87 6124 3758 - 3777 Select org.make.api.proposal.PublishedProposalEvent.ProposalLocked.moderatorName event.moderatorName
88 5686 3804 - 3824 Select org.make.api.proposal.PublishedProposalEvent.ProposalLocked.requestContext event.requestContext
89 7066 3861 - 3876 Select org.make.api.proposal.PublishedProposalEvent.ProposalLocked.eventDate event.eventDate
89 7622 3843 - 3936 Apply org.make.api.userhistory.UserAction.apply org.make.api.userhistory.UserAction.apply[org.make.api.proposal.PublishedProposalEvent.ProposalLocked](event.eventDate, org.make.api.proposal.PublishedProposalEvent.ProposalLocked.actionType, event)
89 6268 3891 - 3916 Select org.make.api.proposal.PublishedProposalEvent.ProposalLocked.actionType org.make.api.proposal.PublishedProposalEvent.ProposalLocked.actionType
92 7762 3955 - 3966 Select scala.concurrent.Future.unit scala.concurrent.Future.unit
96 6844 4048 - 4327 Apply org.make.api.userhistory.UserHistoryCoordinatorService.logHistory ProposalUserHistoryConsumerBehavior.this.userHistoryCoordinatorService.logHistory(org.make.api.userhistory.LogAcceptProposalEvent.apply(event.moderator, event.requestContext, org.make.api.userhistory.UserAction.apply[org.make.api.proposal.PublishedProposalEvent.ProposalAccepted](event.eventDate, org.make.api.proposal.PublishedProposalEvent.ProposalAccepted.actionType, event)))
97 7721 4096 - 4321 Apply org.make.api.userhistory.LogAcceptProposalEvent.apply org.make.api.userhistory.LogAcceptProposalEvent.apply(event.moderator, event.requestContext, org.make.api.userhistory.UserAction.apply[org.make.api.proposal.PublishedProposalEvent.ProposalAccepted](event.eventDate, org.make.api.proposal.PublishedProposalEvent.ProposalAccepted.actionType, event))
98 7027 4137 - 4152 Select org.make.api.proposal.PublishedProposalEvent.ProposalAccepted.moderator event.moderator
99 6089 4179 - 4199 Select org.make.api.proposal.PublishedProposalEvent.ProposalAccepted.requestContext event.requestContext
100 7072 4266 - 4293 Select org.make.api.proposal.PublishedProposalEvent.ProposalAccepted.actionType org.make.api.proposal.PublishedProposalEvent.ProposalAccepted.actionType
100 7472 4236 - 4251 Select org.make.api.proposal.PublishedProposalEvent.ProposalAccepted.eventDate event.eventDate
100 6239 4218 - 4313 Apply org.make.api.userhistory.UserAction.apply org.make.api.userhistory.UserAction.apply[org.make.api.proposal.PublishedProposalEvent.ProposalAccepted](event.eventDate, org.make.api.proposal.PublishedProposalEvent.ProposalAccepted.actionType, event)
103 6483 4332 - 4343 Select scala.concurrent.Future.unit scala.concurrent.Future.unit
107 7685 4423 - 4701 Apply org.make.api.userhistory.UserHistoryCoordinatorService.logHistory ProposalUserHistoryConsumerBehavior.this.userHistoryCoordinatorService.logHistory(org.make.api.userhistory.LogRefuseProposalEvent.apply(event.moderator, event.requestContext, org.make.api.userhistory.UserAction.apply[org.make.api.proposal.PublishedProposalEvent.ProposalRefused](event.eventDate, org.make.api.proposal.PublishedProposalEvent.ProposalRefused.actionType, event)))
108 6306 4471 - 4695 Apply org.make.api.userhistory.LogRefuseProposalEvent.apply org.make.api.userhistory.LogRefuseProposalEvent.apply(event.moderator, event.requestContext, org.make.api.userhistory.UserAction.apply[org.make.api.proposal.PublishedProposalEvent.ProposalRefused](event.eventDate, org.make.api.proposal.PublishedProposalEvent.ProposalRefused.actionType, event))
109 7765 4512 - 4527 Select org.make.api.proposal.PublishedProposalEvent.ProposalRefused.moderator event.moderator
110 6991 4554 - 4574 Select org.make.api.proposal.PublishedProposalEvent.ProposalRefused.requestContext event.requestContext
111 7182 4593 - 4687 Apply org.make.api.userhistory.UserAction.apply org.make.api.userhistory.UserAction.apply[org.make.api.proposal.PublishedProposalEvent.ProposalRefused](event.eventDate, org.make.api.proposal.PublishedProposalEvent.ProposalRefused.actionType, event)
111 7477 4641 - 4667 Select org.make.api.proposal.PublishedProposalEvent.ProposalRefused.actionType org.make.api.proposal.PublishedProposalEvent.ProposalRefused.actionType
111 6197 4611 - 4626 Select org.make.api.proposal.PublishedProposalEvent.ProposalRefused.eventDate event.eventDate
114 6824 4706 - 4717 Select scala.concurrent.Future.unit scala.concurrent.Future.unit
118 6291 4801 - 5083 Apply org.make.api.userhistory.UserHistoryCoordinatorService.logHistory ProposalUserHistoryConsumerBehavior.this.userHistoryCoordinatorService.logHistory(org.make.api.userhistory.LogPostponeProposalEvent.apply(event.moderator, event.requestContext, org.make.api.userhistory.UserAction.apply[org.make.api.proposal.PublishedProposalEvent.ProposalPostponed](event.eventDate, org.make.api.proposal.PublishedProposalEvent.ProposalPostponed.actionType, event)))
119 7044 4849 - 5077 Apply org.make.api.userhistory.LogPostponeProposalEvent.apply org.make.api.userhistory.LogPostponeProposalEvent.apply(event.moderator, event.requestContext, org.make.api.userhistory.UserAction.apply[org.make.api.proposal.PublishedProposalEvent.ProposalPostponed](event.eventDate, org.make.api.proposal.PublishedProposalEvent.ProposalPostponed.actionType, event))
120 6345 4892 - 4907 Select org.make.api.proposal.PublishedProposalEvent.ProposalPostponed.moderator event.moderator
121 7867 4934 - 4954 Select org.make.api.proposal.PublishedProposalEvent.ProposalPostponed.requestContext event.requestContext
122 6994 4991 - 5006 Select org.make.api.proposal.PublishedProposalEvent.ProposalPostponed.eventDate event.eventDate
122 7542 4973 - 5069 Apply org.make.api.userhistory.UserAction.apply org.make.api.userhistory.UserAction.apply[org.make.api.proposal.PublishedProposalEvent.ProposalPostponed](event.eventDate, org.make.api.proposal.PublishedProposalEvent.ProposalPostponed.actionType, event)
122 6199 5021 - 5049 Select org.make.api.proposal.PublishedProposalEvent.ProposalPostponed.actionType org.make.api.proposal.PublishedProposalEvent.ProposalPostponed.actionType
125 7691 5088 - 5099 Select scala.concurrent.Future.unit scala.concurrent.Future.unit
130 6835 5173 - 5205 Literal <nosymbol> "proposal-user-history-consumer"
133 7833 5307 - 5398 Apply org.make.api.technical.KafkaConsumerBehavior.createBehavior new ProposalUserHistoryConsumerBehavior(userHistoryCoordinatorService).createBehavior(ProposalUserHistoryConsumerBehavior.this.name)
133 6021 5393 - 5397 Select org.make.api.proposal.ProposalUserHistoryConsumerBehavior.name ProposalUserHistoryConsumerBehavior.this.name