progress on watcher chat

This commit is contained in:
Thibault Duplessis 2012-06-09 12:05:57 +02:00
parent b1ec3ca07e
commit 081a12e409
9 changed files with 105 additions and 18 deletions

View file

@ -57,13 +57,13 @@ object Round extends LilaController {
}
}
private def watch(pov: Pov)(implicit ctx: Context): IO[Result] =
pov.game.hasBookmarks.fold(
private def watch(pov: Pov)(implicit ctx: Context): IO[Result] = for {
bookmarkers pov.game.hasBookmarks.fold(
bookmarkApi usersByGame pov.game,
io(Nil)
) map { bookmarkers
Ok(html.round.watcher(pov, version(pov.gameId), bookmarkers))
}
)
roomHtml messenger renderWatcher pov.game
} yield Ok(html.round.watcher(pov, version(pov.gameId), Html(roomHtml), bookmarkers))
private def join(pov: Pov)(implicit ctx: Context): IO[Result] =
joiner(pov.game, ctx.me).fold(

View file

@ -64,6 +64,7 @@ final class Settings(config: Config) {
val MongoCollectionMessage = getString("mongo.collection.message")
val MongoCollectionHistory = getString("mongo.collection.history")
val MongoCollectionRoom = getString("mongo.collection.room")
val MongoCollectionWatcherRoom = getString("mongo.collection.watcher_room")
val MongoCollectionConfig = getString("mongo.collection.config")
val MongoCollectionCache = getString("mongo.collection.cache")
val MongoCollectionSecurity = getString("mongo.collection.security")

View file

@ -11,6 +11,7 @@ import scalaz.effects._
final class Messenger(
roomRepo: RoomRepo,
watcherRoomRepo: WatcherRoomRepo,
i18nKeys: I18nKeys) {
def init(game: DbGame): IO[List[Event]] =
@ -29,12 +30,24 @@ final class Messenger(
def playerMessage(
ref: PovRef,
message: String): IO[List[Event]] =
if (message.size <= 140 && message.nonEmpty)
roomRepo.addMessage(ref.gameId, ref.color.name, message.replace(""""""", "'")) map { _
List(Message(ref.color.name, message))
}
else io(Nil)
text: String): IO[List[Event]] =
cleanupText(text).fold(
t roomRepo.addMessage(ref.gameId, ref.color.name, t) map { _
List(Message(ref.color.name, t))
},
io(Nil)
)
def watcherMessage(
gameId: String,
author: String,
text: String): IO[List[Event]] =
cleanupText(text).fold(
t watcherRoomRepo.addMessage(gameId, author, t) map { _
List(Message(author, t))
},
io(Nil)
)
def systemMessages(game: DbGame, messages: List[SelectI18nKey]): IO[List[Event]] =
game.invited.isHuman.fold(
@ -61,6 +74,14 @@ final class Messenger(
def render(roomId: String): IO[String] =
roomRepo room roomId map (_.render)
def renderWatcher(game: DbGame): IO[String] =
watcherRoomRepo room game.id map (_.render)
private def cleanupText(text: String) = {
val cleanedUp = text.trim.replace(""""""", "'")
(cleanedUp.size <= 140 && cleanedUp.nonEmpty) option cleanedUp
}
private def messageToEn(message: SelectI18nKey): String =
message(i18nKeys).en()
}

View file

@ -70,7 +70,12 @@ final class RoundEnv(
lazy val messenger = new Messenger(
roomRepo = roomRepo,
watcherRoomRepo = watcherRoomRepo,
i18nKeys = i18nKeys)
lazy val roomRepo = new RoomRepo(mongodb(MongoCollectionRoom))
lazy val roomRepo = new RoomRepo(
mongodb(MongoCollectionRoom))
lazy val watcherRoomRepo = new WatcherRoomRepo(
mongodb(MongoCollectionWatcherRoom))
}

View file

@ -0,0 +1,35 @@
package lila
package round
import com.novus.salat.annotations.Key
import org.apache.commons.lang3.StringEscapeUtils.escapeXml
import scala.collection.JavaConversions._
case class WatcherRoom(
@Key("_id") id: String,
messages: List[String]) {
def render: String =
messages map ((WatcherRoom.render _) compose WatcherRoom.decode) mkString ""
}
object WatcherRoom {
case class Message(author: String, text: String)
def encode(author: String, text: String): String =
author + "|" + text
def decode(encoded: String): (String, String) =
encoded.split("|").toList match {
case author :: rest (author, rest mkString "|")
case Nil ("", "")
}
def render(msg: (String, String)): String = msg match {
case (author, text) """<li>%s%s</li>""".format(
author,
escapeXml(text)
)
}
}

View file

@ -0,0 +1,25 @@
package lila
package round
import com.novus.salat._
import com.novus.salat.dao._
import com.mongodb.casbah.MongoCollection
import com.mongodb.casbah.Imports._
import scalaz.effects._
class WatcherRoomRepo(collection: MongoCollection)
extends SalatDAO[WatcherRoom, String](collection) {
def room(id: String): IO[WatcherRoom] = io {
findOneByID(id) | WatcherRoom(id, Nil)
}
def addMessage(id: String, author: String, text: String): IO[Unit] = io {
collection.update(
DBObject("_id" -> id),
$push("messages" -> WatcherRoom.encode(author, text)),
upsert = true,
multi = false
)
}
}

View file

@ -1,4 +1,4 @@
@(pov: Pov, version: Int, bookmarkers: List[User])(implicit ctx: Context)
@(pov: Pov, version: Int, roomHtml: Html, bookmarkers: List[User])(implicit ctx: Context)
@import pov._
@ -53,7 +53,10 @@
</div>
}
@round.layout(title = title, goodies = goodies) {
@round.layout(
title = title,
goodies = goodies,
chat = round.room(roomHtml).some) {
<div class="lichess_game clearfix lichess_player_@color"
data-socket-url="@routes.Round.websocketWatcher(gameId, color.name)"
data-table-url="@routes.Round.tableWatcher(gameId, color.name)"

View file

@ -10,6 +10,7 @@ mongo {
message = lobby_room
history = user_history
room = room
watcher_room = watcher_room
config = config
cache = cache
security = security

4
todo
View file

@ -24,12 +24,8 @@ star people and games (and forum threads?)
autoclose top menus
tournaments http://www.chess.com/tournaments/help.html
long name display issue http://en.lichess.org/forum/lichess-feedback/long-names-alter-layout
remove bookmark when removing game
fix game list translations
next deploy:
db.star.renameCollection("bookmark")
new translations:
-rematchOfferCanceled=Rematch offer canceled
-rematchOfferDeclined=Rematch offer declined