WIP desktop client for Chatto reimplemented in ScalaFX and Sapphire Framework
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
2.5 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. package wow.doge.chatto.service
  2. import sttp.client.json4s._
  3. import org.json4s._
  4. import sttp.client._
  5. import javax.inject.Inject
  6. import wow.doge.chatto.types.AppTypes.HttpBackend
  7. import com.typesafe.scalalogging.LazyLogging
  8. import wow.doge.chatto.UserCredentials
  9. import javax.inject._
  10. import wow.doge.chatto.AppDataHandler
  11. import org.json4s.ext.JavaTimeSerializers
  12. import java.time.ZonedDateTime
  13. import wow.doge.chatto.model.MessageCipher
  14. import wow.doge.chatto.controller.EncryptedMessage
  15. import cats.implicits._
  16. class UserService @Inject() (
  17. appDataHandler: AppDataHandler,
  18. encryptionService: EncryptionService
  19. )(
  20. implicit backend: HttpBackend
  21. ) extends LazyLogging {
  22. private implicit lazy val serialization = org.json4s.jackson.Serialization
  23. private implicit lazy val formats =
  24. DefaultFormats ++ JavaTimeSerializers.all + MessageCipher.rename
  25. private val domain = "http://localhost:8080"
  26. private lazy val baseUrl = uri"$domain/api/chat"
  27. private lazy val tokenBasicRequest = (token: String) => {
  28. basicRequest.header("X-AUTH-TOKEN", token)
  29. }
  30. private def endpoint(uri: String) = uri"$baseUrl/$uri"
  31. def getUsers(credentials: UserCredentials) =
  32. tokenBasicRequest(credentials.token)
  33. .get(uri"http://localhost:8080/api/chat/get/users")
  34. .response(asJson[List[String]])
  35. .send()
  36. def getEncryptedMessages(credentials: UserCredentials, user: String) =
  37. Request
  38. .messagesPaginated(credentials, user)
  39. .send()
  40. def getMessages(credentials: UserCredentials, user: String) =
  41. Request
  42. .messagesPaginated(credentials, user)
  43. .mapResponseRight(
  44. _.map(_.toMessage("password", encryptionService.decrypt)).sequence
  45. )
  46. .send()
  47. def getActiveUsers(credentials: UserCredentials) =
  48. tokenBasicRequest(credentials.token)
  49. .get(uri"http://localhost:8080/api/chat/get/active-users")
  50. .response(asJson[List[ActiveUser]])
  51. .send()
  52. object Request {
  53. lazy val messagesPaginated = (credentials: UserCredentials, user: String) =>
  54. tokenBasicRequest(credentials.token)
  55. .get(
  56. uri"http://localhost:8080/api/chat/get/messages/$user?page=0&size=9"
  57. )
  58. .response(asJson[List[EncryptedMessage]])
  59. }
  60. }
  61. final case class HttpBinResponse(
  62. url: String,
  63. origin: String,
  64. headers: Map[String, String]
  65. )
  66. final case class ActiveUser(
  67. userName: String,
  68. online: Boolean,
  69. lastActive: Option[ZonedDateTime]
  70. )
  71. object ActiveUser {
  72. def empty = ActiveUser("empty", false, None)
  73. }