@@ -147,67 +147,71 @@ package object spi {
147147 def sendWs (ws : Ws )(implicit system : ActorSystem [_]): NotificationAck = {
148148 implicit val ec : ExecutionContext = system.executionContext
149149 val classicSystem : ClassicSystem = system
150- val updatedWs =
151- ws.channel match {
152- case Some (channel) =>
153- val clients = WsChannels .lookupClients(channel) match {
154- case Some (clients) if clients.nonEmpty => clients // ++ ws.to
155- case _ => ws.to
156- }
157- ws.withTo(clients.toSeq)
158- case None => ws
159- }
160- val skipped = updatedWs.results
150+ val skipped = ws.results
161151 .filter(result => result.status.isSent || result.status.isDelivered)
162152 .map(_.recipient)
163- val clients = updatedWs.to .filter(clientId => ! skipped.contains(clientId ))
153+ val recipients = ws.recipients() .filter(recipient => ! skipped.contains(recipient ))
164154 val results : Seq [Future [NotificationStatusResult ]] = {
165- for (clientId <- clients) yield {
166- WsClients .lookup(clientId) match {
167- case Some (actorRef) =>
168- actorRef ! TextMessage .Strict (updatedWs.message)
169- Future .successful(
170- NotificationStatusResult .defaultInstance
171- .withUuid(updatedWs.uuid)
172- .withRecipient(clientId)
173- .withStatus(NotificationStatus .Sent )
174- )
175- case None =>
176- wsClientsDao.lookupKeyValue(clientId) flatMap {
177- case Some (ref) =>
178- classicSystem
179- .actorSelection(ref)
180- .resolveOne(FiniteDuration (5 , " seconds" ))
181- .map(actorRef => {
182- actorRef ! TextMessage .Strict (ws.message)
183- NotificationStatusResult .defaultInstance
184- .withUuid(ws.uuid)
185- .withRecipient(clientId)
186- .withStatus(NotificationStatus .Sent )
187- })
188- .recover { case e : Throwable =>
189- wsClientsDao.removeKeyValue(clientId)
190- Console .err.println(
191- s " Error while sending notification to client $clientId: ${e.getMessage}"
192- )
155+ if (recipients.nonEmpty) {
156+ for (recipient <- recipients) yield {
157+ WsClients .lookup(recipient) match {
158+ case Some (actorRef) =>
159+ actorRef ! TextMessage .Strict (ws.message)
160+ Future .successful(
161+ NotificationStatusResult .defaultInstance
162+ .withUuid(ws.uuid)
163+ .withRecipient(recipient)
164+ .withStatus(NotificationStatus .Sent )
165+ )
166+ case None =>
167+ wsClientsDao.lookupKeyValue(recipient) flatMap {
168+ case Some (ref) =>
169+ classicSystem
170+ .actorSelection(ref)
171+ .resolveOne(FiniteDuration (5 , " seconds" ))
172+ .map(actorRef => {
173+ actorRef ! TextMessage .Strict (ws.message)
174+ NotificationStatusResult .defaultInstance
175+ .withUuid(ws.uuid)
176+ .withRecipient(recipient)
177+ .withStatus(NotificationStatus .Sent )
178+ })
179+ .recover { case e : Throwable =>
180+ wsClientsDao.removeKeyValue(recipient)
181+ Console .err.println(
182+ s " Error while sending notification to client $recipient: ${e.getMessage}"
183+ )
184+ NotificationStatusResult .defaultInstance
185+ .withUuid(ws.uuid)
186+ .withRecipient(recipient)
187+ .withStatus(NotificationStatus .Rejected )
188+ .withError(e.getMessage)
189+ }
190+ case None =>
191+ val error = s " ActorRef for client $recipient not found "
192+ Console .err.println(error)
193+ Future .successful(
193194 NotificationStatusResult .defaultInstance
194195 .withUuid(ws.uuid)
195- .withRecipient(clientId )
196+ .withRecipient(recipient )
196197 .withStatus(NotificationStatus .Rejected )
197- .withError(e.getMessage)
198- }
199- case None =>
200- val error = s " ActorRef for client $clientId not found "
201- Console .err.println(error)
202- Future .successful(
203- NotificationStatusResult .defaultInstance
204- .withUuid(ws.uuid)
205- .withRecipient(clientId)
206- .withStatus(NotificationStatus .Rejected )
207- .withError(error)
208- )
209- }
198+ .withError(error)
199+ )
200+ }
201+ }
210202 }
203+ } else {
204+ val error = s " No recipient found for notification ${ws.uuid}"
205+ Console .err.println(error)
206+ Seq (
207+ Future .successful(
208+ NotificationStatusResult .defaultInstance
209+ .withUuid(ws.uuid)
210+ .withRecipient(" __unknown__" )
211+ .withStatus(NotificationStatus .Rejected )
212+ .withError(error)
213+ )
214+ )
211215 }
212216 }
213217 Future .sequence(results).flatMap(results => Future .successful(results)) complete () match {
@@ -234,10 +238,14 @@ package object spi {
234238
235239 object WsChannels {
236240 private [this ] var channels : Map [String , Set [String ]] = Map .empty
237- def addSession (channel : String , session : String ): Unit =
241+ def addSession (channel : String , session : String ): Unit = {
242+ Console .out.println(s " Adding session $session to channel $channel" )
238243 channels += channel -> channels.get(channel).map(_ + session).getOrElse(Set (session))
239- def removeSession (channel : String , session : String ): Unit =
244+ }
245+ def removeSession (channel : String , session : String ): Unit = {
246+ Console .out.println(s " Removing session $session from channel $channel" )
240247 channels += channel -> channels.get(channel).map(_ - session).getOrElse(Set .empty)
248+ }
241249 def lookupClients (channel : String ): Option [Set [String ]] = channels
242250 .get(channel)
243251 .map(sessions => sessions.flatMap(session => WsSessions .lookupClients(session)).flatten)
0 commit comments