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.sessionhistory
21
22 import akka.actor.typed.ActorRef
23 import org.make.api.sessionhistory.SessionHistoryResponse.Error.{Expired, LockError}
24 import org.make.api.sessionhistory.SessionHistoryResponse.{Envelope, LockResponse}
25 import org.make.api.technical.{ActorCommand, ActorProtocol}
26 import org.make.core.RequestContext
27 import org.make.core.technical.Pagination
28 import org.make.core.history.HistoryActions.VoteAndQualifications
29 import org.make.core.proposal.{ProposalId, QualificationKey}
30 import org.make.core.session.SessionId
31 import org.make.core.user.UserId
32
33 // Protocol
34
35 sealed trait SessionHistoryActorProtocol extends ActorProtocol
36
37 // Commands
38
39 sealed trait SessionHistoryCommand extends ActorCommand[SessionId] with SessionHistoryActorProtocol {
40 def id: SessionId = sessionId
41 def sessionId: SessionId
42 }
43
44 sealed trait SessionExpiredCommand[E >: Expired, T] extends SessionHistoryCommand {
45 def replyTo: ActorRef[SessionHistoryResponse[E, T]]
46 }
47
48 final case class EventEnvelope[T <: LoggableHistoryEvent[_]](
49 sessionId: SessionId,
50 event: T,
51 replyTo: ActorRef[SessionHistoryResponse[Expired, LogResult]]
52 ) extends SessionExpiredCommand[Expired, LogResult]
53
54 final case class GetCurrentSession(
55 sessionId: SessionId,
56 newSessionId: SessionId,
57 replyTo: ActorRef[Envelope[SessionId]]
58 ) extends SessionHistoryCommand
59
60 final case class SessionVoteValuesCommand(
61 sessionId: SessionId,
62 proposalIds: Seq[ProposalId],
63 replyTo: ActorRef[SessionHistoryResponse[Expired, Map[ProposalId, VoteAndQualifications]]]
64 ) extends SessionExpiredCommand[Expired, Map[ProposalId, VoteAndQualifications]]
65
66 final case class SessionVotedProposalsPaginateCommand(
67 sessionId: SessionId,
68 proposalsIds: Option[Seq[ProposalId]] = None,
69 limit: Pagination.Limit,
70 offset: Pagination.Offset,
71 replyTo: ActorRef[SessionHistoryResponse[Expired, Seq[ProposalId]]]
72 ) extends SessionExpiredCommand[Expired, Seq[ProposalId]]
73
74 final case class UserConnected(
75 sessionId: SessionId,
76 userId: UserId,
77 requestContext: RequestContext,
78 replyTo: ActorRef[SessionHistoryResponse[Expired, LogResult]]
79 ) extends SessionExpiredCommand[Expired, LogResult]
80
81 final case class LockProposalForVote(
82 sessionId: SessionId,
83 proposalId: ProposalId,
84 replyTo: ActorRef[SessionHistoryResponse[LockError, LockResponse]]
85 ) extends SessionExpiredCommand[LockError, LockResponse]
86
87 final case class LockProposalForQualification(
88 sessionId: SessionId,
89 proposalId: ProposalId,
90 key: QualificationKey,
91 replyTo: ActorRef[SessionHistoryResponse[LockError, LockResponse]]
92 ) extends SessionExpiredCommand[LockError, LockResponse]
93
94 final case class ReleaseProposalForVote(
95 sessionId: SessionId,
96 proposalId: ProposalId,
97 replyTo: ActorRef[SessionHistoryResponse[Expired, LockResponse]]
98 ) extends SessionExpiredCommand[Expired, LockResponse]
99
100 final case class ReleaseProposalForQualification(
101 sessionId: SessionId,
102 proposalId: ProposalId,
103 key: QualificationKey,
104 replyTo: ActorRef[SessionHistoryResponse[Expired, LockResponse]]
105 ) extends SessionExpiredCommand[Expired, LockResponse]
106
107 // Internal commands
108
109 final case class UserHistorySuccess[T](sessionId: SessionId, response: T, replyTo: ActorRef[Envelope[T]])
110 extends SessionHistoryCommand
111
112 final case class UserHistoryFailure[T](
113 sessionId: SessionId,
114 response: T,
115 exception: Throwable,
116 replyTo: ActorRef[Envelope[T]]
117 ) extends SessionHistoryCommand
118
119 final case class SessionClosed(
120 sessionId: SessionId,
121 userId: UserId,
122 replyTo: ActorRef[SessionHistoryResponse[Expired, LogResult]]
123 ) extends SessionHistoryCommand
124
125 // Test only
126
127 final case class GetState(sessionId: SessionId, replyTo: ActorRef[SessionHistoryResponse[Expired, State]])
128 extends SessionHistoryCommand
129
130 final case class StopSessionActor(sessionId: SessionId, replyTo: ActorRef[Envelope[LogResult]])
131 extends SessionHistoryCommand
132
133 // Responses
134
135 sealed trait SessionHistoryResponse[+E, +T] extends SessionHistoryActorProtocol
136
137 object SessionHistoryResponse {
138
139 final case class Envelope[T](value: T) extends SessionHistoryResponse[Nothing, T]
140
141 sealed trait LockResponse extends SessionHistoryResponse[Nothing, LockResponse]
142 case object LockAcquired extends LockResponse
143 case object LockReleased extends LockResponse
144
145 sealed trait Error[E <: Error[E]] extends SessionHistoryResponse[E, Nothing]
146
147 object Error {
148
149 sealed trait LockError
150
151 sealed trait Expired extends Error[Expired] with LockError
152 final case class ExpiredSession(newSessionId: SessionId) extends Expired
153
154 final case class LockAlreadyAcquired(message: String) extends Error[LockAlreadyAcquired] with LockError
155 }
156 }
157
158 // Common to SessionHistory and UserHistory
159 sealed trait LogResult
160
161 case object Ack extends LogResult
162 case object Failed extends LogResult