lila/modules/study/src/main/StudyApi.scala

221 lines
7.6 KiB
Scala
Raw Normal View History

2016-02-26 05:08:11 -07:00
package lila.study
import akka.actor.{ ActorRef, ActorSelection }
2016-02-26 05:08:11 -07:00
2016-04-16 07:26:01 -06:00
import chess.format.{ Forsyth, FEN }
2016-04-24 06:48:32 -06:00
import lila.chat.actorApi.SystemTalk
2016-02-26 05:08:11 -07:00
import lila.hub.actorApi.map.Tell
2016-02-27 04:30:38 -07:00
import lila.hub.Sequencer
2016-04-23 04:01:21 -06:00
import lila.socket.Socket.Uid
2016-04-20 22:42:49 -06:00
import lila.user.{ User, UserRepo }
2016-02-26 05:08:11 -07:00
final class StudyApi(
studyRepo: StudyRepo,
chapterRepo: ChapterRepo,
2016-02-27 04:30:38 -07:00
sequencers: ActorRef,
chat: ActorSelection,
2016-02-26 05:08:11 -07:00
socketHub: akka.actor.ActorRef) {
def byId = studyRepo byId _
2016-02-27 04:30:38 -07:00
def byIdWithChapter(id: Study.ID): Fu[Option[Study.WithChapter]] = byId(id) flatMap {
_ ?? { study =>
chapterRepo.byId(study.position.chapterId) map {
_ map { Study.WithChapter(study, _) }
}
}
}
def create(user: User): Fu[Study.WithChapter] = {
val preStudy = Study.make(user = user.light)
val chapter: Chapter = Chapter.make(
studyId = preStudy.id,
name = "Chapter 1",
2016-04-16 07:26:01 -06:00
setup = Chapter.Setup(
gameId = none,
variant = chess.variant.Standard,
orientation = chess.White),
root = Node.Root.default,
order = 1)
val study = preStudy withChapter chapter
studyRepo.insert(study) zip chapterRepo.insert(chapter) inject
Study.WithChapter(study, chapter)
2016-04-16 07:26:01 -06:00
}
private def pathExists(position: Position.Ref): Fu[Boolean] =
chapterRepo.byId(position.chapterId) map {
_ ?? { _.root pathExists position.path }
}
2016-04-23 04:01:21 -06:00
def setPath(userId: User.ID, studyId: Study.ID, position: Position.Ref, uid: Uid) = sequenceStudy(studyId) { study =>
Contribute(userId, study) {
pathExists(position) flatMap { exists =>
if (exists && study.position.chapterId == position.chapterId) {
(study.position.path != position.path) ?? {
studyRepo.setPosition(study.id, position) >>-
sendTo(study.id, Socket.SetPath(position, uid))
}
2016-04-22 23:52:09 -06:00
}
else funit >>- reloadUid(study, uid)
2016-04-22 23:52:09 -06:00
}
2016-04-20 22:42:49 -06:00
}
}
2016-02-27 04:30:38 -07:00
2016-04-23 04:01:21 -06:00
def addNode(studyId: Study.ID, position: Position.Ref, node: Node, uid: Uid) = sequenceStudyWithChapter(studyId) {
case Study.WithChapter(study, chapter) => Contribute(node.by, study) {
chapter.addNode(position.path, node) match {
2016-04-24 03:15:18 -06:00
case None => fufail(s"Invalid addNode $position $node") >>- reloadUid(study, uid)
case Some(newChapter) =>
chapterRepo.update(newChapter) >>
studyRepo.setPosition(study.id, position + node) >>-
2016-04-23 04:01:21 -06:00
sendTo(study.id, Socket.AddNode(position, node, uid))
}
2016-04-18 04:51:48 -06:00
}
2016-02-28 17:28:35 -07:00
}
2016-04-23 04:01:21 -06:00
def deleteNodeAt(userId: User.ID, studyId: Study.ID, position: Position.Ref, uid: Uid) = ???
// sequenceLocation(ref) { location =>
// (location.study canWrite userId) ?? {
// val newChapter = location.chapter.updateRoot { root =>
// root.withChildren(_.deleteNodeAt(path))
// }
// studyRepo.setChapter(location withChapter newChapter) >>-
2016-04-23 04:01:21 -06:00
// sendTo(ref.studyId, Socket.DelNode(Position.Ref(ref.chapterId, path), uid))
// }
// }
2016-04-23 04:01:21 -06:00
def promoteNodeAt(userId: User.ID, studyId: Study.ID, position: Position.Ref, uid: Uid) = ???
// sequenceLocation(ref) { location =>
// (location.study canWrite userId) ?? {
// val newChapter = location.chapter.updateRoot { root =>
// root.withChildren(_.promoteNodeAt(path))
// }
// studyRepo.setChapter(location withChapter newChapter)
// }
// }
def setRole(byUserId: User.ID, studyId: Study.ID, userId: User.ID, roleStr: String) = sequenceStudy(studyId) { study =>
(study isOwner byUserId) ?? {
val role = StudyMember.Role.byId.getOrElse(roleStr, StudyMember.Role.Read)
studyRepo.setRole(study, userId, role) >>- reloadMembers(study)
}
}
def invite(byUserId: User.ID, studyId: Study.ID, username: String) = sequenceStudy(studyId) { study =>
(study isOwner byUserId) ?? {
UserRepo.named(username).flatMap {
_.filterNot(study.members.contains) ?? { user =>
studyRepo.addMember(study, StudyMember.make(study, user))
}
} >>- reloadMembers(study)
}
}
def kick(byUserId: User.ID, studyId: Study.ID, userId: User.ID) = sequenceStudy(studyId) { study =>
2016-04-20 22:42:49 -06:00
study.members.contains(userId) ?? {
studyRepo.removeMember(study, userId)
2016-04-20 22:42:49 -06:00
} >>- reloadMembers(study)
}
2016-04-23 04:01:21 -06:00
def setShapes(userId: User.ID, studyId: Study.ID, shapes: List[Shape], uid: Uid) = sequenceStudy(studyId) { study =>
Contribute(userId, study) {
studyRepo.setShapes(study, shapes)
2016-04-23 04:01:21 -06:00
} >>- reloadShapes(study, uid)
2016-04-21 20:53:16 -06:00
}
2016-04-24 03:15:18 -06:00
def addChapter(byUserId: User.ID, studyId: Study.ID, name: String) = sequenceStudy(studyId) { study =>
(study isOwner byUserId) ?? {
chapterRepo.nextOrderByStudy(study.id) flatMap { order =>
val chapter = Chapter.make(
studyId = study.id,
name = name,
setup = Chapter.Setup(none, chess.variant.Standard, chess.White),
root = Node.Root.default,
order = order)
chapterRepo.insert(chapter) >>- reloadChapters(study)
}
}
}
def setChapter(byUserId: User.ID, studyId: Study.ID, chapterId: Chapter.ID, socket: ActorRef) = sequenceStudy(studyId) { study =>
(study.canContribute(byUserId) && study.position.chapterId != chapterId) ?? {
2016-04-24 04:45:34 -06:00
chapterRepo.byIdAndStudy(chapterId, studyId) flatMap {
_ ?? { chapter =>
studyRepo.update(study withChapter chapter) >>- {
reloadAll(study)
study.members.get(byUserId).foreach { member =>
2016-04-24 06:48:32 -06:00
import org.apache.commons.lang3.StringEscapeUtils.escapeHtml4
val message = s"${member.user.name} switched to ${escapeHtml4(chapter.name)}"
chat ! SystemTalk(study.id, message, socket)
}
}
}
}
}
}
2016-04-24 04:45:34 -06:00
def renameChapter(byUserId: User.ID, studyId: Study.ID, chapterId: Chapter.ID, name: String) = sequenceStudy(studyId) { study =>
chapterRepo.byIdAndStudy(chapterId, studyId) flatMap {
_ ?? { chapter =>
chapterRepo.update(chapter.copy(name = name)) >>- reloadChapters(study)
}
}
}
private def reloadUid(study: Study, uid: Uid) =
sendTo(study.id, Socket.ReloadUid(uid))
2016-04-20 22:42:49 -06:00
private def reloadMembers(study: Study) =
studyRepo.membersById(study.id).foreach {
2016-04-20 22:42:49 -06:00
_ foreach { members =>
sendTo(study.id, Socket.ReloadMembers(members))
}
}
2016-04-24 03:15:18 -06:00
private def reloadChapters(study: Study) =
chapterRepo.orderedMetadataByStudy(study.id).foreach { chapters =>
sendTo(study.id, Socket.ReloadChapters(chapters))
}
private def reloadAll(study: Study) =
chapterRepo.orderedMetadataByStudy(study.id).foreach { chapters =>
sendTo(study.id, Socket.ReloadAll(study, chapters))
}
2016-04-23 04:01:21 -06:00
private def reloadShapes(study: Study, uid: Uid) =
studyRepo.getShapes(study.id).foreach { shapes =>
2016-04-23 04:01:21 -06:00
sendTo(study.id, Socket.ReloadShapes(shapes, uid))
2016-04-18 04:51:48 -06:00
}
2016-04-20 04:19:34 -06:00
private def sequenceStudy(studyId: String)(f: Study => Funit): Funit =
byId(studyId) flatMap {
_ ?? { study =>
sequence(studyId)(f(study))
}
}
private def sequenceStudyWithChapter(studyId: String)(f: Study.WithChapter => Funit): Funit =
sequenceStudy(studyId) { study =>
chapterRepo.byId(study.position.chapterId) flatMap {
_ ?? { chapter =>
f(Study.WithChapter(study, chapter))
}
}
}
2016-02-27 04:30:38 -07:00
private def sequence(studyId: String)(f: => Funit): Funit = {
val promise = scala.concurrent.Promise[Unit]
val work = Sequencer.work(f, promise.some)
sequencers ! Tell(studyId, work)
promise.future
}
2016-04-20 01:04:38 -06:00
import ornicar.scalalib.Zero
private def Contribute[A](userId: User.ID, study: Study)(f: => A)(implicit default: Zero[A]): A =
if (study canContribute userId) f else default.zero
2016-04-20 01:04:38 -06:00
private def sendTo(studyId: String, msg: Any) {
socketHub ! Tell(studyId, msg)
}
2016-02-26 05:08:11 -07:00
}