Skip to content

Commit

Permalink
qa wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Jul 3, 2014
1 parent 6443710 commit 1f41d1a
Show file tree
Hide file tree
Showing 14 changed files with 751 additions and 12 deletions.
1 change: 1 addition & 0 deletions app/Env.scala
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,5 @@ object Env {
def blog = lila.blog.Env.current
def pool = lila.pool.Env.current
def donation = lila.donation.Env.current
def qa = lila.qa.Env.current
}
2 changes: 1 addition & 1 deletion app/views/message/thread.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ <h1>@thread.nonEmptyName</h1>
</div>
}

@if(!Set("lichess", "lichess-blog").contains(thread.creatorId)) {
@if(!Set("lichess", "lichess-blog", "lichess-qa").contains(thread.creatorId)) {
<div class="answer" id="bottom">
@if(blocks) {
<p>This user blocks you. You cannot answer.</p>
Expand Down
8 changes: 8 additions & 0 deletions modules/common/src/main/paginator/Adapter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,12 @@ trait AdapterLike[A] {
results.map(f).sequenceFu
}
}

def mapFutureList[B](f: Seq[A] => Fu[Seq[B]]): AdapterLike[B] = new AdapterLike[B] {

def nbResults = AdapterLike.this.nbResults

def slice(offset: Int, length: Int) =
AdapterLike.this.slice(offset, length) flatMap f
}
}
24 changes: 22 additions & 2 deletions modules/db/src/main/PaginatorAdapter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ package paginator
import api._
import Implicits._
import play.api.libs.json._
import reactivemongo.api.collections.default.BSONCollection
import reactivemongo.api.SortOrder
import reactivemongo.api.{ QueryOpts, SortOrder }
import reactivemongo.bson._
import reactivemongo.core.commands.Count

import lila.common.paginator.AdapterLike

Expand All @@ -15,8 +19,8 @@ final class Adapter[A: TubeInColl](
def nbResults: Fu[Int] = $count(selector)

def slice(offset: Int, length: Int): Fu[Seq[A]] = $find(
pimpQB($query(selector)).sort(sort: _*) skip offset,
length
pimpQB($query(selector)).sort(sort: _*) skip offset,
length
)
}

Expand All @@ -27,3 +31,19 @@ final class CachedAdapter[A](
def slice(offset: Int, length: Int): Fu[Seq[A]] =
adapter.slice(offset, length)
}

final class BSONAdapter[A: BSONDocumentReader](
collection: BSONCollection,
selector: BSONDocument,
sort: BSONDocument) extends AdapterLike[A] {

def nbResults: Fu[Int] =
collection.db command Count(collection.name, Some(selector))

def slice(offset: Int, length: Int): Fu[Seq[A]] =
collection.find(selector)
.sort(sort)
.copy(options = QueryOpts(skipN = offset))
.cursor[A]
.collect[List](length)
}
15 changes: 15 additions & 0 deletions modules/db/src/main/Util.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package lila.db

import reactivemongo.bson._

import Types.Coll

object Util {

def findNextId(coll: Coll): Fu[Int] =
coll.find(BSONDocument(), BSONDocument("_id" -> true))
.sort(BSONDocument("_id" -> -1))
.one[BSONDocument] map {
_ flatMap { doc => doc.getAs[Int]("_id") map (1+) } getOrElse 1
}
}
9 changes: 1 addition & 8 deletions modules/puzzle/src/main/PuzzleApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private[puzzle] final class PuzzleApi(
case Failure(err) :: rest => insertPuzzles(rest) map { ps =>
(Failure(err): Try[PuzzleId]) :: ps
}
case Success(puzzle) :: rest => findNextId flatMap { id =>
case Success(puzzle) :: rest => lila.db.Util findNextId puzzleColl flatMap { id =>
val p = puzzle(id)
val fenStart = p.fen.split(' ').take(2).mkString(" ")
puzzleColl.db command Count(puzzleColl.name, BSONDocument(
Expand All @@ -59,13 +59,6 @@ private[puzzle] final class PuzzleApi(
}
}
}

private def findNextId: Fu[PuzzleId] =
puzzleColl.find(BSONDocument(), BSONDocument("_id" -> true))
.sort(BSONDocument("_id" -> -1))
.one[BSONDocument] map {
_ flatMap { doc => doc.getAs[Int]("_id") map (1+) } getOrElse 1
}
}

object attempt {
Expand Down
49 changes: 49 additions & 0 deletions modules/qa/src/main/DataForm.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package lila.qa

import play.api.data._
import play.api.data.Forms._

object Forms {

lazy val question = Form(
mapping(
"title" -> nonEmptyText(minLength = 10, maxLength = 150),
"body" -> nonEmptyText(minLength = 10, maxLength = 10000),
"hidden-tags" -> text
)(QuestionData.apply)(QuestionData.unapply)
)

def editQuestion(q: Question) = question fill QuestionData(
title = q.title,
body = q.body,
`hidden-tags` = q.tags mkString ",")

case class QuestionData(title: String, body: String, `hidden-tags`: String) {

def tags = `hidden-tags`.split(',').toList.map(_.trim).filter(_.nonEmpty)
}

lazy val answer = Form(
mapping(
"body" -> nonEmptyText(minLength = 30)
)(AnswerData.apply)(AnswerData.unapply)
)

case class AnswerData(body: String)

lazy val comment = Form(
mapping(
"body" -> nonEmptyText(minLength = 20)
)(CommentData.apply)(CommentData.unapply)
)

case class CommentData(body: String)

val vote = Form(single(
"vote" -> number
))

val favorite = Form(single(
"favorite" -> number
))
}
22 changes: 22 additions & 0 deletions modules/qa/src/main/Env.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package lila.qa

import com.typesafe.config.Config
import lila.common.PimpedConfig._

final class Env(
config: Config,
db: lila.db.Env) {

private val CollectionQuestion = config getString "collection.question"

// def forms = DataForm

// lazy val api = new qaApi(db(Collectionqa), MonthlyGoal)
}

object Env {

lazy val current = "[boot] donation" describes new Env(
config = lila.common.PlayApp loadConfig "donation",
db = lila.db.Env.current)
}
68 changes: 68 additions & 0 deletions modules/qa/src/main/Mailer.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package lila.qa

import lila.common.String._
import lila.user.User

private[qa] final class Mailer(sender: String) {

private[qa] def createAnswer(q: Question, a: Answer, u: User, favoriters: List[User]): Funit = ???
// send(
// to = (rudyEmail :: user.email :: favoriters.map(_.email)) filterNot (u.email.==),
// subject = s"""${u.displaynameOrFullname} answered your question""",
// content = s"""New answer on prismic.io Q&A: ${questionUrl(q)}#answer-${a.id}


// By ${u.displaynameOrFullname}
// On question <b>${q.title}</b>

// ${a.body}

// URL: ${questionUrl(q)}#answer-${a.id}""")

private[qa] def createQuestionComment(q: Question, c: Comment, u: User): Funit = ???
// send(
// to = List(rudyEmail, questionAuthor.email) filterNot (u.email.==),
// subject = s"""${u.displaynameOrFullname} commented your question""",
// content = s"""New comment on prismic.io Q&A: ${questionUrl(question)}#comment-${c.id}


// By ${u.displaynameOrFullname}
// On question <b>${question.title}</b>

// ${c.body}

// URL: ${questionUrl(question)}#comment-${c.id}""")
// case _ => Future successful ()

private[qa] def createAnswerComment(q: Question, a: Answer, c: Comment, u: User): Funit =
???
// QaApi.answer.withUser(a) flatMap {
// case Some(AnswerWithUser(answer, answerAuthor)) => send(
// to = List(rudyEmail, answerAuthor.email) filterNot (u.email.==),
// subject = s"""${u.displaynameOrFullname} commented your answer""",
// content = s"""New comment on prismic.io Q&A: ${questionUrl(q)}#comment-${c.id}


// By ${u.displaynameOrFullname}
// On question <b>${q.title}</b>

// ${c.body}

// URL: ${questionUrl(q)}#comment-${c.id}""")

private def questionUrl(q: Question) =
s"http:https://lichess.org/qa/${q.id}/${q.slug}"

private def send(to: List[String], subject: String, content: String) = {
to foreach { recipient =>
// common.utils.Mailer.send(
// to = recipient,
// from = Some(sender),
// fromname = Some(common.Wroom.domain),
// subject = s"[Q&A] $subject",
// content = Html(nl2br(content)))
}
fuccess(())
}
}

Loading

0 comments on commit 1f41d1a

Please sign in to comment.