drop old BC for mobile app API v1

This commit is contained in:
Thibault Duplessis 2017-05-27 17:41:14 +02:00
parent df2e43f861
commit 59340282b5
8 changed files with 27 additions and 56 deletions

View file

@ -24,8 +24,7 @@ object Round extends LilaController with TheftPrevention {
uid = uid, uid = uid,
user = ctx.me, user = ctx.me,
ip = ctx.ip, ip = ctx.ip,
userTv = get("userTv"), userTv = get("userTv")
apiVersion = lila.api.Mobile.Api.currentVersion // yeah it should be in the URL
) )
} }
} }
@ -37,7 +36,7 @@ object Round extends LilaController with TheftPrevention {
else getSocketUid("sri") match { else getSocketUid("sri") match {
case Some(uid) => case Some(uid) =>
requestAiMove(pov) >> requestAiMove(pov) >>
env.socketHandler.player(pov, uid, ctx.me, ctx.ip, ApiVersion(apiVersion)) map Right.apply env.socketHandler.player(pov, uid, ctx.me, ctx.ip) map Right.apply
case None => fuccess(Left(NotFound)) case None => fuccess(Left(NotFound))
} }
case None => fuccess(Left(NotFound)) case None => fuccess(Left(NotFound))

View file

@ -85,15 +85,14 @@ object Chat {
object BSONFields { object BSONFields {
val id = "_id" val id = "_id"
val lines = "l" val lines = "l"
val encoded = "e"
} }
import BSONFields._ import BSONFields._
import reactivemongo.bson.BSONDocument import reactivemongo.bson.BSONDocument
import Line.{ lineBSONHandler, userLineBSONHandler }
implicit val mixedChatBSONHandler = new BSON[MixedChat] { implicit val mixedChatBSONHandler = new BSON[MixedChat] {
def reads(r: BSON.Reader): MixedChat = { def reads(r: BSON.Reader): MixedChat = {
implicit val lineReader = Line.lineBSONHandler(r.boolO(encoded) | true)
MixedChat( MixedChat(
id = r str id, id = r str id,
lines = r.get[List[Line]](lines) lines = r.get[List[Line]](lines)
@ -101,14 +100,12 @@ object Chat {
} }
def writes(w: BSON.Writer, o: MixedChat) = BSONDocument( def writes(w: BSON.Writer, o: MixedChat) = BSONDocument(
id -> o.id, id -> o.id,
lines -> o.lines.map(Line.lineBSONHandler(false).write), lines -> o.lines
encoded -> false
) )
} }
implicit val userChatBSONHandler = new BSON[UserChat] { implicit val userChatBSONHandler = new BSON[UserChat] {
def reads(r: BSON.Reader): UserChat = { def reads(r: BSON.Reader): UserChat = {
implicit val lineReader = Line.userLineBSONHandler(r.boolO(encoded) | true)
UserChat( UserChat(
id = r str id, id = r str id,
lines = r.get[List[UserLine]](lines) lines = r.get[List[UserLine]](lines)
@ -116,8 +113,7 @@ object Chat {
} }
def writes(w: BSON.Writer, o: UserChat) = BSONDocument( def writes(w: BSON.Writer, o: UserChat) = BSONDocument(
id -> o.id, id -> o.id,
lines -> o.lines.map(Line.lineBSONHandler(false).write), lines -> o.lines
encoded -> false
) )
} }
} }

View file

@ -142,7 +142,7 @@ final class ChatApi(
$id(chatId), $id(chatId),
$doc("$push" -> $doc( $doc("$push" -> $doc(
Chat.BSONFields.lines -> $doc( Chat.BSONFields.lines -> $doc(
"$each" -> List(Line.lineBSONHandler(false).write(line)), "$each" -> List(line),
"$slice" -> -maxLinesPerChat "$slice" -> -maxLinesPerChat
) )
)), )),

View file

@ -43,17 +43,13 @@ object Line {
private val invalidLine = UserLine("", "[invalid character]", troll = false, deleted = true) private val invalidLine = UserLine("", "[invalid character]", troll = false, deleted = true)
def userLineBSONHandler(encoded: Boolean) = new BSONHandler[BSONString, UserLine] { private[chat] implicit val userLineBSONHandler = new BSONHandler[BSONString, UserLine] {
def read(bsonStr: BSONString) = strToUserLine { def read(bsonStr: BSONString) = strToUserLine(bsonStr.value) getOrElse invalidLine
if (encoded) unescapeHtml4(bsonStr.value) else bsonStr.value
} | invalidLine
def write(x: UserLine) = BSONString(userLineToStr(x)) def write(x: UserLine) = BSONString(userLineToStr(x))
} }
def lineBSONHandler(encoded: Boolean) = new BSONHandler[BSONString, Line] { private[chat] implicit val lineBSONHandler = new BSONHandler[BSONString, Line] {
def read(bsonStr: BSONString) = strToLine { def read(bsonStr: BSONString) = strToLine(bsonStr.value) getOrElse invalidLine
if (encoded) unescapeHtml4(bsonStr.value) else bsonStr.value
} | invalidLine
def write(x: Line) = BSONString(lineToStr(x)) def write(x: Line) = BSONString(lineToStr(x))
} }

View file

@ -143,9 +143,9 @@ private[round] final class Socket(
) )
} pipeTo sender } pipeTo sender
case Join(uid, user, color, playerId, ip, userTv, apiVersion) => case Join(uid, user, color, playerId, ip, userTv) =>
val (enumerator, channel) = Concurrent.broadcast[JsValue] val (enumerator, channel) = Concurrent.broadcast[JsValue]
val member = Member(channel, user, color, playerId, ip, userTv = userTv, apiVersion = apiVersion) val member = Member(channel, user, color, playerId, ip, userTv = userTv)
addMember(uid.value, member) addMember(uid.value, member)
notifyCrowd notifyCrowd
playerDo(color, _.ping) playerDo(color, _.ping)

View file

@ -11,7 +11,7 @@ import play.api.libs.json.{ JsObject, Json }
import actorApi._, round._ import actorApi._, round._
import lila.common.PimpedJson._ import lila.common.PimpedJson._
import lila.common.{ IpAddress, ApiVersion } import lila.common.IpAddress
import lila.game.{ Pov, PovRef, GameRepo } import lila.game.{ Pov, PovRef, GameRepo }
import lila.hub.actorApi.map._ import lila.hub.actorApi.map._
import lila.hub.actorApi.round.Berserk import lila.hub.actorApi.round.Berserk
@ -118,21 +118,19 @@ private[round] final class SocketHandler(
uid: Uid, uid: Uid,
user: Option[User], user: Option[User],
ip: IpAddress, ip: IpAddress,
userTv: Option[String], userTv: Option[String]
apiVersion: ApiVersion
): Fu[Option[JsSocketHandler]] = ): Fu[Option[JsSocketHandler]] =
GameRepo.pov(gameId, colorName) flatMap { GameRepo.pov(gameId, colorName) flatMap {
_ ?? { join(_, none, uid, user, ip, userTv = userTv, apiVersion) map some } _ ?? { join(_, none, uid, user, ip, userTv = userTv) map some }
} }
def player( def player(
pov: Pov, pov: Pov,
uid: Uid, uid: Uid,
user: Option[User], user: Option[User],
ip: IpAddress, ip: IpAddress
apiVersion: ApiVersion
): Fu[JsSocketHandler] = ): Fu[JsSocketHandler] =
join(pov, Some(pov.playerId), uid, user, ip, userTv = none, apiVersion) join(pov, Some(pov.playerId), uid, user, ip, userTv = none)
private def join( private def join(
pov: Pov, pov: Pov,
@ -140,8 +138,7 @@ private[round] final class SocketHandler(
uid: Uid, uid: Uid,
user: Option[User], user: Option[User],
ip: IpAddress, ip: IpAddress,
userTv: Option[String], userTv: Option[String]
apiVersion: ApiVersion
): Fu[JsSocketHandler] = { ): Fu[JsSocketHandler] = {
val join = Join( val join = Join(
uid = uid, uid = uid,
@ -149,8 +146,7 @@ private[round] final class SocketHandler(
color = pov.color, color = pov.color,
playerId = playerId, playerId = playerId,
ip = ip, ip = ip,
userTv = userTv, userTv = userTv
apiVersion = apiVersion
) )
socketHub ? Get(pov.gameId) mapTo manifest[ActorRef] flatMap { socket => socketHub ? Get(pov.gameId) mapTo manifest[ActorRef] flatMap { socket =>
Handler(hub, socket, uid, join) { Handler(hub, socket, uid, join) {

View file

@ -28,22 +28,11 @@ case class VersionedEvent(
else Json.obj( else Json.obj(
"v" -> version, "v" -> version,
"t" -> typ, "t" -> typ,
"d" -> dataForApiVersion(typ, decoded, m.apiVersion) "d" -> decoded
) )
} }
else Json.obj("v" -> version) else Json.obj("v" -> version)
private val mobileV1Escaper: Reads[JsObject] = (__ \ 't).json.update(
__.read[JsString].map { s => JsString(escapeHtml(s.value)) }
)
private def dataForApiVersion(typ: String, data: JsValue, apiVersion: ApiVersion): JsValue =
if (typ == "message" && apiVersion.v1) data match {
case o: JsObject => o transform mobileV1Escaper getOrElse o
case v => v
}
else data
private def visibleBy(m: Member): Boolean = private def visibleBy(m: Member): Boolean =
if (watcher && m.owner) false if (watcher && m.owner) false
else if (owner && m.watcher) false else if (owner && m.watcher) false

View file

@ -7,7 +7,7 @@ import scala.concurrent.Promise
import chess.{ Centis, Color } import chess.{ Centis, Color }
import chess.format.Uci import chess.format.Uci
import lila.common.{ IpAddress, ApiVersion } import lila.common.IpAddress
import lila.game.Event import lila.game.Event
import lila.socket.SocketMember import lila.socket.SocketMember
import lila.socket.Socket.Uid import lila.socket.Socket.Uid
@ -22,7 +22,6 @@ sealed trait Member extends SocketMember {
val troll: Boolean val troll: Boolean
val ip: IpAddress val ip: IpAddress
val userTv: Option[String] val userTv: Option[String]
val apiVersion: ApiVersion
def owner = playerIdOption.isDefined def owner = playerIdOption.isDefined
def watcher = !owner def watcher = !owner
@ -37,13 +36,12 @@ object Member {
color: Color, color: Color,
playerIdOption: Option[String], playerIdOption: Option[String],
ip: IpAddress, ip: IpAddress,
userTv: Option[String], userTv: Option[String]
apiVersion: ApiVersion
): Member = { ): Member = {
val userId = user map (_.id) val userId = user map (_.id)
val troll = user.??(_.troll) val troll = user.??(_.troll)
playerIdOption.fold[Member](Watcher(channel, userId, color, troll, ip, userTv, apiVersion)) { playerId => playerIdOption.fold[Member](Watcher(channel, userId, color, troll, ip, userTv)) { playerId =>
Owner(channel, userId, playerId, color, troll, ip, apiVersion) Owner(channel, userId, playerId, color, troll, ip)
} }
} }
} }
@ -54,8 +52,7 @@ case class Owner(
playerId: String, playerId: String,
color: Color, color: Color,
troll: Boolean, troll: Boolean,
ip: IpAddress, ip: IpAddress
apiVersion: ApiVersion
) extends Member { ) extends Member {
val playerIdOption = playerId.some val playerIdOption = playerId.some
@ -68,8 +65,7 @@ case class Watcher(
color: Color, color: Color,
troll: Boolean, troll: Boolean,
ip: IpAddress, ip: IpAddress,
userTv: Option[String], userTv: Option[String]
apiVersion: ApiVersion
) extends Member { ) extends Member {
val playerIdOption = none val playerIdOption = none
@ -81,8 +77,7 @@ case class Join(
color: Color, color: Color,
playerId: Option[String], playerId: Option[String],
ip: IpAddress, ip: IpAddress,
userTv: Option[String], userTv: Option[String]
apiVersion: ApiVersion
) )
case class Connected(enumerator: JsEnumerator, member: Member) case class Connected(enumerator: JsEnumerator, member: Member)
case class Bye(color: Color) case class Bye(color: Color)