Skip to content

Commit

Permalink
allow up to 128 user study topics
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Mar 28, 2022
1 parent 0afbb9c commit 508e434
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 32 deletions.
27 changes: 14 additions & 13 deletions app/ui/scalatags.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,20 @@ import lila.user.Title

// collection of lila attrs
trait ScalatagsAttrs {
val dataTag = attr("data-tag")
val dataIcon = attr("data-icon")
val dataHref = attr("data-href")
val dataCount = attr("data-count")
val dataColor = attr("data-color")
val dataFen = attr("data-fen")
val dataRel = attr("data-rel")
val novalidate = attr("novalidate").empty
val datetimeAttr = attr("datetime")
val dataBotAttr = attr("data-bot").empty
val deferAttr = attr("defer").empty
val downloadAttr = attr("download").empty
val viewBoxAttr = attr("viewBox")
val dataTag = attr("data-tag")
val dataIcon = attr("data-icon")
val dataHref = attr("data-href")
val dataCount = attr("data-count")
val dataColor = attr("data-color")
val dataFen = attr("data-fen")
val dataRel = attr("data-rel")
val novalidate = attr("novalidate").empty
val datetimeAttr = attr("datetime")
val dataBotAttr = attr("data-bot").empty
val deferAttr = attr("defer").empty
val downloadAttr = attr("download").empty
val viewBoxAttr = attr("viewBox")
def attrData(name: String) = attr(s"data-$name")

object frame {
val scrolling = attr("scrolling")
Expand Down
2 changes: 1 addition & 1 deletion app/views/study/topic.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ object topic {
frag(
h2(trans.study.myTopics()),
postForm(cls := "form3", action := routes.Study.topics)(
form3.textarea(form("topics"))(rows := 10),
form3.textarea(form("topics"))(rows := 10, attrData("max") := StudyTopics.userMax),
form3.submit(trans.save())
)
)
Expand Down
4 changes: 2 additions & 2 deletions modules/study/src/main/StudyApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,7 @@ final class StudyApi(
def setTopics(studyId: Study.Id, topicStrs: List[String])(who: Who) =
sequenceStudy(studyId) { study =>
Contribute(who.u, study) {
val topics = StudyTopics.fromStrs(topicStrs)
val topics = StudyTopics.fromStrs(topicStrs, StudyTopics.studyMax)
val newStudy = study.copy(topics = topics.some)
val newTopics = study.topics.fold(topics)(topics.diff)
(study != newStudy) ?? {
Expand All @@ -804,7 +804,7 @@ final class StudyApi(

def addTopics(studyId: Study.Id, topics: List[String]) =
sequenceStudy(studyId) { study =>
studyRepo.updateTopics(study addTopics StudyTopics.fromStrs(topics))
studyRepo.updateTopics(study addTopics StudyTopics.fromStrs(topics, StudyTopics.studyMax))
}

def editStudy(studyId: Study.Id, data: Study.Data)(who: Who) =
Expand Down
30 changes: 16 additions & 14 deletions modules/study/src/main/StudyTopic.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,16 @@ case class StudyTopics(value: List[StudyTopic]) extends AnyVal {

object StudyTopics {

val studyMax = 30
val userMax = 128

val empty = StudyTopics(Nil)

def fromStrs(strs: Seq[String]) =
def fromStrs(strs: Seq[String], max: Int) =
StudyTopics {
strs.view
.flatMap(StudyTopic.fromStr)
.take(64)
.take(max)
.toList
.distinct
}
Expand Down Expand Up @@ -89,8 +92,9 @@ final class StudyTopicApi(topicRepo: StudyTopicRepo, userTopicRepo: StudyUserTop
} dmap StudyTopics.apply

def userTopics(userId: User.ID): Fu[StudyTopics] =
userTopicRepo.coll(_.byId(userId)).dmap {
_.flatMap(_.getAsOpt[StudyTopics]("topics")) | StudyTopics.empty
userTopicRepo.coll {
_.primitiveOne[List[StudyTopic]]($id(userId), "topics")
.dmap(_.fold(StudyTopics.empty)(StudyTopics.apply))
}

private case class TagifyTopic(value: String)
Expand All @@ -101,7 +105,7 @@ final class StudyTopicApi(topicRepo: StudyTopicRepo, userTopicRepo: StudyUserTop
if (json.trim.isEmpty) StudyTopics.empty
else
Json.parse(json).validate[List[TagifyTopic]] match {
case JsSuccess(topics, _) => StudyTopics fromStrs topics.map(_.value)
case JsSuccess(topics, _) => StudyTopics.fromStrs(topics.map(_.value), StudyTopics.userMax)
case _ => StudyTopics.empty
}
userTopicRepo.coll {
Expand All @@ -114,15 +118,13 @@ final class StudyTopicApi(topicRepo: StudyTopicRepo, userTopicRepo: StudyUserTop
}

def userTopicsAdd(userId: User.ID, topics: StudyTopics): Funit =
topics.value.nonEmpty ??
userTopicRepo.coll {
_.update
.one(
$id(userId),
$addToSet("topics" -> $doc("$each" -> topics)),
upsert = true
)
}.void
topics.value.nonEmpty ?? userTopics(userId).flatMap { prev =>
val newTopics = prev ++ topics
(newTopics != prev) ??
userTopicRepo.coll {
_.update.one($id(userId), $set("topics" -> newTopics), upsert = true)
}.void
}

def popular(nb: Int): Fu[StudyTopics] =
topicRepo
Expand Down
6 changes: 4 additions & 2 deletions ui/analyse/src/plugins/studyTopicForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import * as xhr from 'common/xhr';
import Tagify from '@yaireo/tagify';

lichess.load.then(() => {
const tagify = new Tagify(document.getElementById('form3-topics') as HTMLInputElement, {
const input = document.getElementById('form3-topics') as HTMLInputElement;
console.log(input.dataset['max']);
const tagify = new Tagify(input, {
pattern: /.{2,}/,
maxTags: 30,
maxTags: parseInt(input.dataset['max']!) || 64,
});
const doFetch: (term: string) => Promise<string[]> = debounce(
(term: string) => xhr.json(xhr.url('/study/topic/autocomplete', { term })),
Expand Down

0 comments on commit 508e434

Please sign in to comment.