Skip to content

Commit

Permalink
Merge pull request #3 from ScalaConsultants/direct_messages_refactor
Browse files Browse the repository at this point in the history
refactored DirectMessage API for more consistent sendig process.
  • Loading branch information
marioosh committed Jul 6, 2015
2 parents 34206d0 + b0122d7 commit bfeb9a7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
18 changes: 17 additions & 1 deletion src/main/scala/io/scalac/slack/bots/MessageListener.scala
Original file line number Diff line number Diff line change
@@ -1,19 +1,35 @@
package io.scalac.slack.bots

import akka.actor.{Actor, ActorLogging}
import akka.actor.{Actor, ActorLogging, ActorRef}
import akka.util.Timeout
import io.scalac.slack.MessageEventBus
import io.scalac.slack.common.{Incoming, MessageEvent, Outgoing, _}

import scala.concurrent.ExecutionContext
import scala.language.implicitConversions

trait MessagePublisher {

import akka.pattern._

def bus: MessageEventBus


implicit def publish(event: MessageEvent): Unit = {
bus.publish(event)
}

def publish(directMessage: DirectMessage)(implicit context: ExecutionContext, userStorage: ActorRef, timeout: Timeout): Unit = {
userStorage ? FindChannel(directMessage.key) onSuccess {
case Some(channel: String) =>
val eventToSend = directMessage.event match {
case message: OutboundMessage => message.copy(channel = channel)
case message: RichOutboundMessage => message.copy(channel = channel)
case other => other
}
publish(eventToSend)
}
}
}

abstract class MessageListener extends Actor with ActorLogging with MessagePublisher
Expand Down
24 changes: 6 additions & 18 deletions src/main/scala/io/scalac/slack/common/Messages.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
package io.scalac.slack.common

import akka.actor.ActorRef
import akka.pattern._
import akka.util.Timeout

import scala.annotation.tailrec
import scala.concurrent.ExecutionContext

/**
* Created on 08.02.15 22:04
Expand Down Expand Up @@ -123,7 +118,6 @@ object Attachment {

}


/**
* Classifier for message event
*/
Expand All @@ -141,18 +135,12 @@ object Outgoing extends MessageEventType
case class MessageType(messageType: String, subType: Option[String])

/**
* Direct message is message send by the private channel
* userStorage try to find channel ID and if will
* message is translated into basic message and send to this channel.
* DirectMessage is sent directly to choosen user
* @param key user's name, Id or channelId
* @param event message to send, channel inside event will be overwritten
*/
object DirectMessage extends {

def apply(key: String, message: String)(implicit context: ExecutionContext, userStorage: ActorRef, timeout: Timeout, forward: (OutboundMessage) => Unit): Unit = {
case class DirectMessage(key: String, event: MessageEvent)

userStorage ? FindChannel(key) onSuccess {
case Some(channel: String) =>
forward(OutboundMessage(channel, message))
}

}
object DirectMessage {
def apply(key: String, message: String): DirectMessage = DirectMessage(key, OutboundMessage("", message))
}

0 comments on commit bfeb9a7

Please sign in to comment.