WIP on firebase push

pull/5686/head
Vincent Velociter 2019-11-20 19:21:11 +01:00
parent 1a16732f4b
commit f807a4f58a
4 changed files with 71 additions and 1 deletions

View File

@ -303,7 +303,7 @@ lazy val playban = module("playban", Seq(common, db, game, message, chat)).setti
)
lazy val push = module("push", Seq(common, db, user, game, challenge, message)).settings(
libraryDependencies ++= provided(play.api, reactivemongo.driver)
libraryDependencies ++= Seq(googleApiClient) ++ provided(play.api, reactivemongo.driver)
)
lazy val slack = module("slack", Seq(common, hub, user)).settings(

View File

@ -289,6 +289,13 @@ push {
app_id = ""
key = ""
}
blocking-io-context {
executor = "thread-pool-executor"
throughput = 1
thread-pool-executor {
fixed-pool-size = 50
}
}
}
mod {
collection {

View File

@ -0,0 +1,62 @@
package lila.push
import java.io.FileInputStream
import scala.concurrent.ExecutionContext
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential
import collection.JavaConverters._
import play.api.libs.json._
import play.api.libs.ws.WS
import play.api.Play.current
private final class FirebasePush(
getDevices: String => Fu[List[Device]],
url: String,
appId: String,
key: String
) {
// private implicit val blockingIOContext: ExecutionContext =
// system.dispatchers.lookup("push.blocking-io-context")
def apply(userId: String)(data: => PushApi.Data): Funit =
getDevices(userId) flatMap {
case Nil => funit
case devices =>
WS.url(url)
.withHeaders(
"Authorization" -> s"key=$key",
"Accept" -> "application/json",
"Content-type" -> "application/json"
)
.post(Json.obj(
"app_id" -> appId,
"include_player_ids" -> devices.map(_.deviceId),
"headings" -> Map("en" -> data.title),
"contents" -> Map("en" -> data.body),
"data" -> data.payload,
"android_group" -> data.stacking.key,
"android_group_message" -> Map("en" -> data.stacking.message),
"collapse_id" -> data.stacking.key,
"ios_badgeType" -> "Increase",
"ios_badgeCount" -> 1
)).flatMap {
case res if res.status == 200 =>
(res.json \ "errors").asOpt[List[String]] match {
case Some(errors) =>
println(errors mkString ",")
fufail(s"[push] ${devices.map(_.deviceId)} $data ${res.status} ${res.body}")
case None => funit
}
case res => fufail(s"[push] ${devices.map(_.deviceId)} $data ${res.status} ${res.body}")
}
}
private def getAccessToken() {
val googleCredential = GoogleCredential
.fromStream(new FileInputStream("service-account.json"))
.createScoped(Set("https://www.googleapis.com/auth/firebase.messaging").asJava)
googleCredential.refreshToken()
googleCredential.getAccessToken()
}
}

View File

@ -39,6 +39,7 @@ object Dependencies {
val scaffeine = "com.github.blemale" %% "scaffeine" % "2.6.0" % "compile"
val netty = "io.netty" % "netty" % "3.10.6.Final"
val guava = "com.google.guava" % "guava" % "21.0"
val googleApiClient = "com.google.api-client" % "google-api-client" % "1.30.5"
val specs2 = "org.specs2" %% "specs2-core" % "4.0.2" % "test"
val specs2Scalaz = "org.specs2" %% "specs2-scalaz" % "4.0.2" % "test"
val scalaUri = "io.lemonlabs" %% "scala-uri" % "1.2.0"