bump mobile version to 2, escape chat messages on version 1

This commit is contained in:
Thibault Duplessis 2016-07-12 12:51:06 +02:00
parent c2d1383769
commit 7942f546ef
4 changed files with 29 additions and 6 deletions

View file

@ -79,7 +79,7 @@ object Round extends LilaController with TheftPrevention {
else Env.api.roundApi.player(pov, apiVersion) zip
getPlayerChat(pov.game) map {
case (data, chat) => Ok(chat.fold(data) { c =>
data + ("chat" -> lila.chat.JsonView(c))
data + ("chat" -> lila.chat.JsonView(c, mobileEscape = apiVersion == 1))
})
}
}.mon(_.http.response.player.mobile)

View file

@ -15,7 +15,7 @@ object Mobile {
// date when the server stops accepting requests
unsupportedAt: DateTime)
def currentVersion = 1
def currentVersion = 2
def oldVersions: List[Old] = List(
// old version 0 is just an example, so the list is never empty :)
@ -23,14 +23,19 @@ object Mobile {
Old(
version = 0,
deprecatedAt = new DateTime("2014-08-01"),
unsupportedAt = new DateTime("2014-12-01"))
unsupportedAt = new DateTime("2014-12-01")),
Old( // chat messages are html escaped
version = 1,
deprecatedAt = new DateTime("2016-07-13"),
unsupportedAt = new DateTime("2016-10-13"))
)
private val PathPattern = """^.+/socket/v(\d+)$""".r
def requestVersion(req: RequestHeader): Option[Int] = {
val accepts = ~req.headers.get(HeaderNames.ACCEPT)
if (accepts contains "application/vnd.lichess.v1+json") some(1)
if (accepts contains "application/vnd.lichess.v2+json") Some(2)
else if (accepts contains "application/vnd.lichess.v1+json") Some(1)
else req.path match {
case PathPattern(version) => parseIntOption(version)
case _ => None

View file

@ -34,6 +34,8 @@ case class UserChat(
})
def add(line: UserLine) = copy(lines = lines :+ line)
def mapLines(f: UserLine => UserLine) = copy(lines = lines map f)
}
object UserChat {
@ -51,6 +53,8 @@ case class MixedChat(
case l: UserLine => !l.troll
case l: PlayerLine => true
}))
def mapLines(f: Line => Line) = copy(lines = lines map f)
}
object Chat {

View file

@ -1,16 +1,30 @@
package lila.chat
import lila.common.PimpedJson._
import lila.common.LightUser
import lila.common.PimpedJson._
import org.apache.commons.lang3.StringEscapeUtils.escapeHtml4
import play.api.libs.json._
object JsonView {
def apply(chat: AnyChat): JsValue = chat match {
def apply(chat: AnyChat, mobileEscape: Boolean = false): JsValue = {
if (mobileEscape) escapeHtml(chat)
else chat
} match {
case c: MixedChat => mixedChatWriter writes c
case c: UserChat => userChatWriter writes c
}
private def escapeHtml(chat: AnyChat) = chat match {
case c: MixedChat => c.mapLines {
case l: UserLine => l.copy(text = escapeHtml4(l.text))
case l: PlayerLine => l.copy(text = escapeHtml4(l.text))
}
case c: UserChat => c.mapLines { l =>
l.copy(text = escapeHtml4(l.text))
}
}
def apply(line: Line): JsValue = lineWriter writes line
def userModInfo(u: UserModInfo)(implicit lightUser: LightUser.Getter) =