More async

This commit is contained in:
Thibault Duplessis 2012-12-10 16:18:58 +01:00
parent 84b22eee9a
commit bff1a2b618
10 changed files with 71 additions and 33 deletions

View file

@ -8,14 +8,22 @@ import play.api.libs.concurrent.Execution.Implicits._
trait AsyncResults { self: Controller
protected def AsyncOk[A](op: Fu[A])(implicit writer: Writeable[A], ctype: ContentTypeOf[A]) = Async {
op map { r Ok(r) }
protected def AsyncOk[A, B](fu: Fu[A])(op: A B)(implicit writer: Writeable[B], ctype: ContentTypeOf[B]) = Async {
fu map op map { x Ok(x) }
}
protected def AsyncUnit[A](fu: Fu[A]) = Async {
fu map { _ Ok("ok") }
}
protected def AsyncRedirect(op: Fu[Call]) = Async {
op map { Redirect(_) }
}
protected def AsyncRedirect(op: Funit)(call: Call) = Async {
op map { _ Redirect(call) }
}
protected def AsyncRedirectUrl(op: Fu[String]) = Async {
op map { Redirect(_) }
}

View file

@ -8,7 +8,6 @@ import http.Context
import play.api.mvc._
import play.api.mvc.Results._
import play.api.libs.concurrent.Execution.Implicits._
import scalaz.effects._
object Mod extends LilaController {
@ -16,32 +15,22 @@ object Mod extends LilaController {
private def modLogApi = env.mod.logApi
def engine(username: String) = Secure(Permission.MarkEngine) { _
me AsyncRedirect {
modApi.adjust(me, username) map { _ routes.User show username }
}
me AsyncRedirect(modApi.adjust(me, username))(routes.User show username)
}
def mute(username: String) = Secure(Permission.MutePlayer) { _
me AsyncRedirect {
modApi.mute(me, username) map { _ routes.User show username }
}
me AsyncRedirect(modApi.mute(me, username))(routes.User show username)
}
def ban(username: String) = Secure(Permission.IpBan) { implicit ctx
me AsyncRedirect {
modApi.ban(me, username) map { _ routes.User show username }
}
me AsyncRedirect(modApi.ban(me, username))(routes.User show username)
}
def ipban(ip: String) = Secure(Permission.IpBan) { implicit ctx
me AsyncOk(modApi.ipban(me, ip))
me AsyncUnit(modApi.ipban(me, ip))
}
val log = Auth { implicit ctx
me Async {
modLogApi.recent map { docs
Ok(html.mod.log(docs))
}
}
me AsyncOk(modLogApi.recent) { html.mod.log(_) }
}
}

View file

@ -7,7 +7,6 @@ import play.api.mvc.Codec
trait ResponseWriter {
// I like Unit requests.
implicit def wUnit(implicit codec: Codec): Writeable[Unit] =
Writeable[Unit]((_: Unit) codec encode "ok")
implicit def ctoUnit: ContentTypeOf[Unit] =

View file

@ -32,7 +32,7 @@ final class Api(
} yield threadOption
def makeThread(data: DataForm.ThreadData, me: User) = {
val thread = Thread(
val thread = Thread.make(
name = data.subject,
text = data.text,
creator = me,
@ -44,7 +44,7 @@ final class Api(
}
def makePost(thread: Thread, text: String, me: User) = {
val post = Post(
val post = Post.make(
text = text,
isByCreator = thread isCreator me)
val newThread = thread + post

View file

@ -20,7 +20,7 @@ object Post {
val idSize = 8
def apply(
def make(
text: String,
isByCreator: Boolean): Post = Post(
id = Random nextString idSize,
@ -28,4 +28,17 @@ object Post {
isByCreator = isByCreator,
isRead = false,
createdAt = DateTime.now)
import play.api.libs.json._
import play.api.libs.functional.syntax._
val json = mongodb.JsonTube((
(__ \ 'id).read[String] and
(__ \ 'text).read[String] and
(__ \ 'isByCreator).read[Boolean] and
(__ \ 'isRead).read[Boolean] and
(__ \ 'createdAt).read[DateTime]
)(Post.apply _),
Json.writes[Post]
)
}

View file

@ -50,7 +50,7 @@ object Thread {
val idSize = 8
def apply(
def make(
name: String,
text: String,
creator: User,
@ -59,11 +59,28 @@ object Thread {
name = name,
createdAt = DateTime.now,
updatedAt = DateTime.now,
posts = List(Post(
posts = List(Post.make(
text = text,
isByCreator = true
)),
creatorId = creator.id,
invitedId = invited.id,
visibleByUserIds = List(creator.id, invited.id))
import play.api.libs.json._
import play.api.libs.functional.syntax._
import Post.json.implicits._
val json = mongodb.JsonTube((
(__ \ 'id).read[String] and
(__ \ 'name).read[String] and
(__ \ 'createdAt).read[DateTime] and
(__ \ 'updatedAt).read[DateTime] and
(__ \ 'posts).read[List[Post]] and
(__ \ 'creatorId).read[String] and
(__ \ 'invitedId).read[String] and
(__ \ 'visibleByUserIds).read[List[String]]
)(Thread.apply _),
Json.writes[Thread]
)
}

View file

@ -36,7 +36,7 @@ object Modlog {
import play.api.libs.json._
import play.api.libs.functional.syntax._
val json = JsonTube((
val json = mongodb.JsonTube((
(__ \ 'mod).read[String] and
(__ \ 'user).read[Option[String]] and
(__ \ 'action).read[String] and

View file

@ -2,5 +2,7 @@ package lila
package mod
final class ModlogRepo(db: LilaDB, name: String)
extends Coll[Modlog](db, name, Modlog.json) {
extends mongodb.Coll[Modlog](db, name, Modlog.json) {
def recent(nb: Int): Fu[List[Modlog]] = find(query.sort(sortNaturalDesc), nb)
}

View file

@ -1,5 +1,5 @@
package lila
package mod
package mongodb
import reactivemongo.api._
import reactivemongo.bson._
@ -35,13 +35,18 @@ abstract class Coll[Doc](db: LilaDB, name: String, json: JsonTube[Doc]) {
def count(query: QueryBuilder): Fu[Int] = db command Count(name, query.makeQueryDocument.some)
def count: Fu[Int] = db command Count(name, none)
def findOne(query: QueryBuilder): Fu[Option[Doc]] = coll.find[Doc](query).headOption
def findOne(query: QueryBuilder): Fu[Option[Doc]] = cursor(query).headOption
def find(query: QueryBuilder): Fu[List[Doc]] = coll.find[Doc](query).toList
def find(query: QueryBuilder, nb: Int): Fu[List[Doc]] = coll.find[Doc](query) toList nb
def find(query: QueryBuilder): Fu[List[Doc]] = cursor(query).toList
def find(query: QueryBuilder, nb: Int): Fu[List[Doc]] = cursor(query) toList nb
def recent(nb: Int): Fu[List[Doc]] =
find(QueryBuilder().sort("$natural" -> SortOrder.Descending), nb)
def cursor(query: QueryBuilder): FlattenedCursor[Doc] = coll.find[Doc](query)
def query = QueryBuilder()
///////////// sorting /////////////
def sortNaturalDesc = BSONDocument("$natural" -> BSONInteger(-1))
private val coll = db(name)
}

View file

@ -1,5 +1,5 @@
package lila
package mod
package mongodb
import play.api.libs.json._
@ -7,6 +7,11 @@ case class JsonTube[Doc](
reads: Reads[Doc],
writes: Writes[Doc]) {
lazy val implicits = new {
implicit val iReads = reads
implicit val iWrites = writes
}
def read(js: JsObject): JsResult[Doc] = reads reads js
def unsafeRead(js: JsObject): Doc = read(js) recover { err