Skip to content
This repository has been archived by the owner on Feb 17, 2023. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
ex3ndr committed Jan 17, 2016
2 parents 9a3f83f + 3b59a26 commit 0f4d3e8
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import scala.concurrent.Future
trait ProductImplicits {

/**
* Implicit conversion for case classes and case objects.
* Allows to represent case class as ApiMap.
* Case classes with 0 fields and case objects are converted to empty ApiMap
* @param product case class or case object
*/
* Implicit conversion for case classes and case objects.
* Allows to represent case class as ApiMap.
* Case classes with 0 fields and case objects are converted to empty ApiMap
* @param product case class or case object
*/
implicit class Foo(product: Product) {
def asApiMap: ApiMapValue = {
//case object or empty case class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,17 @@ import akka.stream.actor.ActorPublisher
import akka.stream.scaladsl.Source
import akka.util.ByteString
import cats.data.Xor
import com.github.kxbmap.configs._
import com.typesafe.config.Config
import im.actor.server.db.DbExtension
import im.actor.server.persist.push.GooglePushCredentialsRepo
import io.circe.generic.auto._

import com.github.kxbmap.configs._
import com.typesafe.config.Config
import io.circe.jawn._
import io.circe.syntax._

import scala.annotation.tailrec
import scala.concurrent.Future
import scala.util.{ Failure, Success, Try }
import scala.util.{Failure, Success, Try}

case class GooglePushKey(projectId: Long, key: String)

Expand All @@ -45,6 +44,7 @@ object GooglePushManagerConfig {
}

final case class GooglePushMessage(
to: String,
collapseKey: Option[String],
data: Option[Map[String, String]]
)
Expand All @@ -69,25 +69,30 @@ final class GooglePushManager(config: GooglePushManagerConfig)(implicit system:
case Xor.Right(json)
json.asObject match {
case Some(obj)
obj("error") flatMap (_.asString) foreach {
case "InvalidRegistration"
obj("error") map (_.asString) foreach {
case Some("InvalidRegistration")
log.warning("Invalid registration, deleting")
remove(delivery.regId)
case "NotRegistered"
remove(delivery.m.to)
case Some("NotRegistered")
log.warning("Token is not registered, deleting")
remove(delivery.regId)
case other
remove(delivery.m.to)
case Some(other)
log.warning("Error in GCM response: {}", other)
case None
log.debug("Delivered successfully")
}
case None
log.error("Expected JSON Object but got: {}", json)
}
case Xor.Left(failure) log.error(failure.underlying, "Failed to parse response")
}
}
} else log.error("Status code was not OK: {}", resp.status)
} else log.error("Failed to deliver message, StatusCode was not OK: {}", resp.status)
case (Failure(e), delivery)
log.error(e, "Failed to deliver message: {}", delivery.m)
} onComplete {
case Failure(e) log.error(e, "Failure in stream")
case Success(_) log.debug("Stream completed")
}

private def remove(regId: String): Future[Int] = db.run(GooglePushCredentialsRepo.deleteByToken(regId))
Expand All @@ -97,10 +102,10 @@ final class GooglePushManager(config: GooglePushManagerConfig)(implicit system:
case GooglePushKey(projectId, key) projectId key
}).toMap

def send(projectId: Long, regId: String, message: GooglePushMessage): Unit =
def send(projectId: Long, message: GooglePushMessage): Unit =
keys get projectId match {
case Some(key)
deliveryPublisher ! GooglePushDelivery.Delivery(message, key, regId)
deliveryPublisher ! GooglePushDelivery.Delivery(message, key)
case None
log.warning("Key not found for projectId: {}", projectId)
}
Expand All @@ -110,7 +115,7 @@ private object GooglePushDelivery {

object Tick

final case class Delivery(m: GooglePushMessage, key: String, regId: String)
final case class Delivery(m: GooglePushMessage, key: String)

private val MaxQueue = 100000
private val MaxConnections = 4
Expand Down Expand Up @@ -157,11 +162,12 @@ private final class GooglePushDelivery extends ActorPublisher[(HttpRequest, Goog
}
}

private def mkJob(d: Delivery): (HttpRequest, Delivery) =
private def mkJob(d: Delivery): (HttpRequest, Delivery) = {
HttpRequest(
method = HttpMethods.POST,
uri = Uri("/gcm/send"),
headers = List(headers.Authorization(headers.GenericHttpCredentials(s"key=${d.regId}", Map.empty[String, String]))),
headers = List(headers.Authorization(headers.GenericHttpCredentials(s"key=${d.key}", Map.empty[String, String]))),
entity = HttpEntity(ContentTypes.`application/json`, d.m.asJson.toString())
) d
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import im.actor.server.model.push.GooglePushCredentials
private[sequence] final class GooglePushProvider(userId: Int, googlePushManager: GooglePushManager, system: ActorSystem) extends PushProvider {
def deliverInvisible(seq: Int, creds: GooglePushCredentials): Unit = {
val message = GooglePushMessage(
to = creds.regId,
collapseKey = Some(s"seq-invisible-${userId.toString}"),
data = Some(Map("seq" seq.toString))
)

googlePushManager.send(creds.projectId, creds.regId, message)
googlePushManager.send(creds.projectId, message)
}

def deliverVisible(
Expand All @@ -22,6 +23,7 @@ private[sequence] final class GooglePushProvider(userId: Int, googlePushManager:
isVibrationEnabled: Boolean
): Unit = {
val message = GooglePushMessage(
to = creds.regId,
collapseKey = Some(s"seq-visible-${userId.toString}"),
data = Some(Map("seq" seq.toString) ++ (
data.text match {
Expand All @@ -32,6 +34,6 @@ private[sequence] final class GooglePushProvider(userId: Int, googlePushManager:
))
)

googlePushManager.send(creds.projectId, creds.regId, message)
googlePushManager.send(creds.projectId, message)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE INDEX ON google_push_credentials(reg_id);

0 comments on commit 0f4d3e8

Please sign in to comment.