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

package wow.doge.chatto.service
import sttp.client.json4s._
import org.json4s._
import sttp.client._
import javax.inject.Inject
import wow.doge.chatto.types.AppTypes.HttpBackend
import com.typesafe.scalalogging.LazyLogging
import wow.doge.chatto.UserCredentials
import javax.inject._
import wow.doge.chatto.AppDataHandler
import org.json4s.ext.JavaTimeSerializers
import java.time.ZonedDateTime
import wow.doge.chatto.model.MessageCipher
import wow.doge.chatto.controller.EncryptedMessage
import cats.implicits._
class UserService @Inject() (
appDataHandler: AppDataHandler,
encryptionService: EncryptionService
)(
implicit backend: HttpBackend
) extends LazyLogging {
private implicit lazy val serialization = org.json4s.jackson.Serialization
private implicit lazy val formats =
DefaultFormats ++ JavaTimeSerializers.all + MessageCipher.rename
private val domain = "http://localhost:8080"
private lazy val baseUrl = uri"$domain/api/chat"
private lazy val tokenBasicRequest = (token: String) => {
basicRequest.header("X-AUTH-TOKEN", token)
}
private def endpoint(uri: String) = uri"$baseUrl/$uri"
def getUsers(credentials: UserCredentials) =
tokenBasicRequest(credentials.token)
.get(uri"http://localhost:8080/api/chat/get/users")
.response(asJson[List[String]])
.send()
def getEncryptedMessages(credentials: UserCredentials, user: String) =
Request
.messagesPaginated(credentials, user)
.send()
def getMessages(credentials: UserCredentials, user: String) =
Request
.messagesPaginated(credentials, user)
.mapResponseRight(
_.map(_.toMessage("password", encryptionService.decrypt)).sequence
)
.send()
def getActiveUsers(credentials: UserCredentials) =
tokenBasicRequest(credentials.token)
.get(uri"http://localhost:8080/api/chat/get/active-users")
.response(asJson[List[ActiveUser]])
.send()
object Request {
lazy val messagesPaginated = (credentials: UserCredentials, user: String) =>
tokenBasicRequest(credentials.token)
.get(
uri"http://localhost:8080/api/chat/get/messages/$user?page=0&size=9"
)
.response(asJson[List[EncryptedMessage]])
}
}
final case class HttpBinResponse(
url: String,
origin: String,
headers: Map[String, String]
)
final case class ActiveUser(
userName: String,
online: Boolean,
lastActive: Option[ZonedDateTime]
)
object ActiveUser {
def empty = ActiveUser("empty", false, None)
}