Skip to content

Commit

Permalink
Report.Id WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Dec 21, 2022
1 parent a0790fa commit d48c204
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 46 deletions.
10 changes: 3 additions & 7 deletions app/controllers/Appeal.scala
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,9 @@ final class Appeal(env: Env, reportC: => report.Report, prismicC: => Prismic, us
private def asMod(
username: UserStr
)(f: (lila.appeal.Appeal, Suspect) => Fu[Result])(implicit ctx: Context): Fu[Result] =
env.user.repo byId username flatMap {
_ ?? { user =>
env.appeal.api get user flatMap {
_ ?? { appeal =>
f(appeal, Suspect(user)) dmap some
}
}
env.user.repo byId username ifThen { user =>
env.appeal.api get user ifThen { appeal =>
f(appeal, Suspect(user)) dmap some
}
} flatMap {
_.fold(notFound)(fuccess)
Expand Down
2 changes: 1 addition & 1 deletion app/views/base/layout.scala
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ object layout:
ctx.blind option h2("Navigation"),
!ctx.isAppealUser option frag(
topnav(),
ctx.me.exists(!_.isPatron) && !zenable option a(cls := "site-title-nav__donate")(
ctx.noKid && ctx.me.exists(!_.isPatron) && !zenable option a(cls := "site-title-nav__donate")(
href := routes.Plan.index
)(trans.patron.donate())
)
Expand Down
5 changes: 2 additions & 3 deletions app/views/base/topnav.scala
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,8 @@ object topnav:
a(href := routes.Team.home())(trans.team.teams()),
ctx.noKid option a(href := routes.ForumCateg.index)(trans.forum()),
ctx.noKid option a(href := langHref(routes.Ublog.communityAll()))(trans.blog()),
ctx.me.exists(!_.kid) option a(cls := "community-patron", href := routes.Plan.index)(
trans.patron.donate()
)
ctx.noKid && ctx.me.exists(_.isPatron) option
a(cls := "community-patron", href := routes.Plan.index)(trans.patron.donate())
)
),
st.section(
Expand Down
6 changes: 3 additions & 3 deletions modules/appeal/src/main/Appeal.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package lila.appeal

import org.joda.time.DateTime
import reactivemongo.api.bson.Macros.Annotations.Key

import lila.user.User

case class Appeal(
_id: UserId,
@Key("_id") id: UserId,
msgs: Vector[AppealMsg],
status: Appeal.Status, // from the moderators POV
createdAt: DateTime,
Expand All @@ -14,12 +15,11 @@ case class Appeal(
// https://github.com/lichess-org/lila/issues/7564
firstUnrepliedAt: DateTime
):
def id = _id
def isRead = status == Appeal.Status.Read
def isMuted = status == Appeal.Status.Muted
def isUnread = status == Appeal.Status.Unread

def isAbout(userId: UserId) = _id == userId
def isAbout(userId: UserId) = id == userId

def post(text: String, by: User) =
val msg = AppealMsg(
Expand Down
2 changes: 2 additions & 0 deletions modules/common/src/main/base/LilaLibraryExtensions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -353,3 +353,5 @@ trait LilaLibraryExtensions extends LilaTypes:
fua.value match
case Some(scala.util.Success(v)) => v
case _ => None

def ifThen[B: Zero](fub: A => Fu[B])(using EC): Fu[B] = fua.flatMap { _ ?? fub }
46 changes: 23 additions & 23 deletions modules/report/src/main/Report.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package lila.report
import org.joda.time.DateTime
import cats.data.NonEmptyList
import ornicar.scalalib.ThreadLocalRandom
import reactivemongo.api.bson.Macros.Annotations.Key

import lila.user.User
import lila.common.Iso

case class Report(
_id: Report.ID, // also the url slug
user: UserId, // the reportee
@Key("_id") id: Report.Id, // also the url slug
user: UserId, // the reportee
reason: Reason,
room: Room,
atoms: NonEmptyList[Report.Atom], // most recent first
Expand All @@ -21,10 +22,9 @@ case class Report(

import Report.{ Atom, Score }

private given Ordering[Double] = scala.math.Ordering.Double.TotalOrdering
// private given Ordering[Double] = scala.math.Ordering.Double.TotalOrdering

def id = _id
def slug = _id
inline def slug = id

def closed = !open
def suspect = SuspectId(user)
Expand Down Expand Up @@ -86,7 +86,8 @@ case class Report(

object Report:

type ID = String
opaque type Id = String
object Id extends OpaqueString[Id]

opaque type Score = Double
object Score extends OpaqueDouble[Score]:
Expand Down Expand Up @@ -155,20 +156,19 @@ object Report:
private[report] val appealText = "Appeal"

def make(c: Candidate.Scored, existing: Option[Report]) =
c match
case c @ Candidate.Scored(candidate, score) =>
existing.fold(
Report(
_id = ThreadLocalRandom nextString 8,
user = candidate.suspect.user.id,
reason = candidate.reason,
room = Room(candidate.reason),
atoms = NonEmptyList.one(c.atom),
score = score,
inquiry = none,
open = true,
done = none
)
)(_ add c.atom)

private[report] case class SnoozeKey(snoozerId: UserId, reportId: Report.ID)
import c.*
existing.fold(
Report(
id = Id(ThreadLocalRandom nextString 8),
user = candidate.suspect.user.id,
reason = candidate.reason,
room = Room(candidate.reason),
atoms = NonEmptyList.one(c.atom),
score = score,
inquiry = none,
open = true,
done = none
)
)(_ add c.atom)

private[report] case class SnoozeKey(snoozerId: UserId, reportId: Id)
16 changes: 8 additions & 8 deletions modules/report/src/main/ReportApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -276,17 +276,17 @@ final class ReportApi(
case _ => funit
}

def byId(id: Report.ID) = coll.byId[Report](id)
def byId(id: Report.Id) = coll.byId[Report](id)

def process(mod: Mod, reportId: Report.ID): Funit =
def process(mod: Mod, reportId: Report.Id): Funit =
for {
report <- byId(reportId) orFail s"no such report $reportId"
suspect <- getSuspect(report.user) orFail s"No such suspect $report"
rooms = Set(Room(report.reason))
res <- process(mod, suspect, rooms, reportId.some)
} yield res

def process(mod: Mod, sus: Suspect, rooms: Set[Room], reportId: Option[Report.ID] = None): Funit =
def process(mod: Mod, sus: Suspect, rooms: Set[Room], reportId: Option[Report.Id] = None): Funit =
inquiries
.ofModId(mod.user.id)
.dmap(_.filter(_.user == sus.user.id))
Expand Down Expand Up @@ -348,12 +348,12 @@ final class ReportApi(
$doc("room" -> r)
}

private def selectOpenInRoom(room: Option[Room], exceptIds: Iterable[Report.ID]) =
private def selectOpenInRoom(room: Option[Room], exceptIds: Iterable[Report.Id]) =
$doc("open" -> true) ++ roomSelect(room) ++ {
exceptIds.nonEmpty ?? $doc("_id" $nin exceptIds)
}

private def selectOpenAvailableInRoom(room: Option[Room], exceptIds: Iterable[Report.ID]) =
private def selectOpenAvailableInRoom(room: Option[Room], exceptIds: Iterable[Report.Id]) =
selectOpenInRoom(room, exceptIds) ++ $doc("inquiry" $exists false)

private val maxScoreCache = cacheApi.unit[Room.Scores] {
Expand Down Expand Up @@ -467,7 +467,7 @@ final class ReportApi(
.sortBy(-_.urgency)
}

def snooze(mod: Mod, reportId: Report.ID, duration: String): Fu[Option[Report]] =
def snooze(mod: Mod, reportId: Report.Id, duration: String): Fu[Option[Report]] =
byId(reportId) flatMap {
_ ?? { report =>
snoozer.set(Report.SnoozeKey(mod.user.id, reportId), duration)
Expand Down Expand Up @@ -570,12 +570,12 @@ final class ReportApi(
* If they already are on this inquiry, cancel it.
* Returns the previous and next inquiries
*/
def toggle(mod: Mod, id: Report.ID): Fu[(Option[Report], Option[Report])] =
def toggle(mod: Mod, id: Report.Id): Fu[(Option[Report], Option[Report])] =
workQueue {
doToggle(mod, id)
}

private def doToggle(mod: Mod, id: Report.ID): Fu[(Option[Report], Option[Report])] =
private def doToggle(mod: Mod, id: Report.Id): Fu[(Option[Report], Option[Report])] =
for {
report <- coll.byId[Report](id) orElse coll.one[Report](
$doc("user" -> id, "inquiry.mod" $exists true)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@
"lint": "eslint . --ext .ts",
"lila-journal": "journalctl --user -fu lila -o cat",
"serverlog": "multitail -cT ansi -l 'pnpm lila-journal' -cT ansi -ke '^[0-9\\.]{10} ' -I .metals/metals.log",
"multilog": "multitail -cT ansi -l 'pnpm lila-journal' -cT ansi -L 'ui/build -w' -cT ansi -ke '^[0-9\\.]{10} ' -I .metals/metals.log"
"multilog": "multitail -cT ansi -l 'pnpm lila-journal' -cT ansi -L 'ui/build -w' -cT ansi -ke '^[0-9\\.]{10} ' -I .metals/metals.log -cT ansi -I logs/all.log"
}
}

0 comments on commit d48c204

Please sign in to comment.