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,
user = ctx.me,
ip = ctx.ip,
userTv = get("userTv"),
apiVersion = lila.api.Mobile.Api.currentVersion // yeah it should be in the URL
userTv = get("userTv")
)
}
}
@ -37,7 +36,7 @@ object Round extends LilaController with TheftPrevention {
else getSocketUid("sri") match {
case Some(uid) =>
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))

View file

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

View file

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

View file

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

View file

@ -143,9 +143,9 @@ private[round] final class Socket(
)
} 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 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)
notifyCrowd
playerDo(color, _.ping)

View file

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

View file

@ -28,22 +28,11 @@ case class VersionedEvent(
else Json.obj(
"v" -> version,
"t" -> typ,
"d" -> dataForApiVersion(typ, decoded, m.apiVersion)
"d" -> decoded
)
}
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 =
if (watcher && m.owner) false
else if (owner && m.watcher) false

View file

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