-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- moved UsersStorage - moved DirectMessageModel - moved Test
- Loading branch information
Showing
3 changed files
with
192 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package io.scalac.slack.common | ||
|
||
import akka.actor.{Actor, ActorLogging} | ||
import io.scalac.slack.api.Ok | ||
import io.scalac.slack.models.{DirectChannel, Presence, SlackUser} | ||
|
||
import scala.language.{implicitConversions, postfixOps} | ||
|
||
/** | ||
* Maintainer: @marioosh | ||
*/ | ||
class UsersStorage extends Actor with ActorLogging { | ||
|
||
var userCatalog = List.empty[UserInfo] | ||
var channelCatalog = List.empty[DirectChannel] | ||
|
||
implicit def convertUsers(su: SlackUser): UserInfo = UserInfo(su.id.trim, su.name.trim, su.presence) | ||
|
||
override def receive: Receive = { | ||
case RegisterUsers(users@_*) => | ||
users.filterNot(u => u.deleted).foreach(addUser(_)) | ||
sender ! Ok | ||
|
||
case FindUser(key) => sender ! userCatalog.find { user => | ||
val matcher = key.trim.toLowerCase | ||
matcher == user.id || matcher == user.name | ||
} | ||
|
||
case RegisterDirectChannels(channels@_*) => | ||
channels foreach addDirectChannel | ||
sender ! Ok | ||
|
||
case FindChannel(key) => | ||
|
||
val id = userCatalog.find(u => u.name == key.trim.toLowerCase) match { | ||
case Some(user) => user.id | ||
case None => key | ||
} | ||
sender ! channelCatalog.find(c => c.id == id || c.userId == id).map(_.id) | ||
|
||
} | ||
|
||
def addDirectChannel(channel: DirectChannel): Unit = { | ||
channelCatalog = channel :: channelCatalog.filterNot(_.userId == channel.userId) | ||
} | ||
|
||
private def addUser(user: UserInfo): Unit = { | ||
userCatalog = user :: userCatalog.filterNot(_.id == user.id) | ||
} | ||
|
||
} | ||
|
||
case class UserInfo(id: String, name: String, presence: Presence) { | ||
def userLink() = s"""<@$id|name>""" | ||
} | ||
|
||
case class RegisterUsers(slackUsers: SlackUser*) | ||
|
||
case class RegisterDirectChannels(ims: DirectChannel*) | ||
|
||
case class FindUser(key: String) | ||
|
||
case class FindChannel(key: String) |
103 changes: 103 additions & 0 deletions
103
src/test/scala/io/scalac/slack/common/UsersStorageTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package io.scalac.slack.common | ||
|
||
import akka.actor.{ActorSystem, Props} | ||
import akka.testkit.{DefaultTimeout, ImplicitSender, TestKit} | ||
import io.scalac.slack.api.Ok | ||
import io.scalac.slack.models.{Active, DirectChannel, SlackUser} | ||
import org.scalatest.{BeforeAndAfterAll, Matchers, WordSpecLike} | ||
|
||
import scala.concurrent.duration._ | ||
import scala.language.postfixOps | ||
|
||
/** | ||
* Maintainer: @marioosh | ||
*/ | ||
class UsersStorageTest(_system: ActorSystem) extends TestKit(_system) with DefaultTimeout with ImplicitSender with Matchers with WordSpecLike with BeforeAndAfterAll { | ||
|
||
def this() = this(ActorSystem("UsersStorageTestActorSystem")) | ||
|
||
override protected def afterAll(): Unit = TestKit.shutdownActorSystem(system) | ||
|
||
"UserStorage" must { | ||
|
||
"save incoming users" in { | ||
val mario = SlackUser("1234", "mario", deleted = false, Some(false), None, None, None, None, None, None, Active) | ||
val stefek = SlackUser("12345", "stefek", deleted = false, Some(false), None, None, None, None, None, None, Active) | ||
val us = system.actorOf(Props[UsersStorage]) | ||
|
||
within(2 seconds) { | ||
us ! RegisterUsers(mario) | ||
expectMsg(Ok) | ||
} | ||
|
||
} | ||
|
||
"find user from storage" in { | ||
val mario = SlackUser("1234", "mario", deleted = false, Some(false), None, None, None, None, None, None, Active) | ||
val stefek = SlackUser("12345", "stefek", deleted = false, Some(false), None, None, None, None, None, None, Active) | ||
val us = system.actorOf(Props[UsersStorage]) | ||
|
||
within(1 second) { | ||
us ! RegisterUsers(mario, stefek) | ||
expectMsg(Ok) | ||
us ! FindUser("mario") | ||
expectMsg(Some(UserInfo("1234", "mario", Active))) | ||
us ! FindUser("12345") | ||
expectMsg(Some(UserInfo("12345", "stefek", Active))) | ||
us ! FindUser("gitara") | ||
expectMsg(None) | ||
|
||
} | ||
} | ||
} | ||
|
||
|
||
"Channel Catalog" must { | ||
"be able to add new channel and find it" in { | ||
val im1 = DirectChannel("D123", "U123") | ||
val im2 = DirectChannel("D124", "U124") | ||
val im3 = DirectChannel("D125", "U124") | ||
|
||
val us = system.actorOf(Props[UsersStorage]) | ||
|
||
within(1 second) { | ||
us ! RegisterDirectChannels(im1, im2) | ||
expectMsg(Ok) | ||
us ! RegisterDirectChannels(im3) | ||
expectMsg(Ok) | ||
us ! FindChannel("D12234") | ||
expectMsg(None) | ||
us ! FindChannel("D123") | ||
expectMsg(Some("D123")) | ||
us ! FindChannel("D125") | ||
expectMsg(Some("D125")) | ||
us ! FindChannel("U124") | ||
expectMsg(Some("D125")) | ||
|
||
} | ||
} | ||
"be able to find channel by username" in { | ||
val im1 = DirectChannel("D123", "U123") | ||
val mario = SlackUser("U123", "mario", deleted = false, Some(false), None, None, None, None, None, None, Active) | ||
|
||
val us = system.actorOf(Props[UsersStorage]) | ||
|
||
within(1 second) { | ||
us ! RegisterUsers(mario) | ||
expectMsg(Ok) | ||
us ! RegisterDirectChannels(im1) | ||
expectMsg(Ok) | ||
//find | ||
us ! FindChannel("D123") | ||
expectMsg(Some("D123")) | ||
us ! FindChannel("U123") | ||
expectMsg(Some("D123")) | ||
us ! FindChannel("mario") | ||
expectMsg(Some("D123")) | ||
|
||
} | ||
|
||
} | ||
} | ||
|
||
} |