Skip to content

Commit

Permalink
Merge pull request #538 from lenguyenthanh/rewrite-avoid-infix
Browse files Browse the repository at this point in the history
Rewrite AvoidInfix
  • Loading branch information
ornicar authored Mar 2, 2024
2 parents 2526743 + efa3151 commit 960a42f
Show file tree
Hide file tree
Showing 64 changed files with 733 additions and 647 deletions.
4 changes: 4 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@

# Scala Steward: Reformat with scalafmt 3.7.14
9f8712ec23ee7538e81c9b10b507eb4ad455af60

# Reformat with AvoidInfix rule
b98d6a989a0e1e1704f690b5baff6285f764c60e
036f81ec8ad580cd179b460744e57bfa480f2fc5
2 changes: 1 addition & 1 deletion .scalafmt.conf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ runner.dialect = scala3
align.preset = more
maxColumn = 110
spaces.inImportCurlyBraces = true
rewrite.rules = [SortImports, RedundantParens, SortModifiers]
rewrite.rules = [SortImports, RedundantParens, SortModifiers, AvoidInfix]
rewrite.redundantBraces.stringInterpolation = true

rewrite.scala3.convertToNewSyntax = yes
Expand Down
35 changes: 19 additions & 16 deletions src/main/scala/Auth.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,22 @@ final class Auth(mongo: Mongo, seenAt: SeenAtUpdate, config: Config)(using Execu
import Mongo.given

def apply(req: RequestHeader): Future[Option[Success]] =
if req.flag contains Flag.api then Future successful None
if req.flag contains Flag.api then Future.successful(None)
else
sessionIdFromReq(req) match
case Some(sid) if sid startsWith appealPrefix => Future successful None
case Some(sid) => sessionAuth(sid)
case Some(sid) if sid.startsWith(appealPrefix) => Future.successful(None)
case Some(sid) => sessionAuth(sid)
case None =>
bearerFromHeader(req) match
case Some(bearer) => bearerAuth(bearer)
case None => Future successful None
case None => Future.successful(None)

def sidFromReq(req: RequestHeader): Option[String] =
req cookie cookieName flatMap:
case sidRegex(id) => Some(id)
case _ => None
req
.cookie(cookieName)
.flatMap:
case sidRegex(id) => Some(id)
case _ => None

private def sessionAuth(sid: String): Future[Option[Success.Cookie]] =
mongo
Expand All @@ -34,7 +36,7 @@ final class Auth(mongo: Mongo, seenAt: SeenAtUpdate, config: Config)(using Execu
Some(BSONDocument("_id" -> false, "user" -> true))
).one[BSONDocument]
.map:
_ flatMap { _.getAsOpt[User.Id]("user") }
_.flatMap { _.getAsOpt[User.Id]("user") }
.map:
_.map: user =>
Success.Cookie:
Expand All @@ -46,15 +48,15 @@ final class Auth(mongo: Mongo, seenAt: SeenAtUpdate, config: Config)(using Execu

private val cookieName = config.getString("cookie.name")

private val bearerSigner = Algo hmac config.getString("oauth.secret")
private val bearerSigner = Algo.hmac(config.getString("oauth.secret"))

private def bearerFromHeader(req: RequestHeader): Option[Auth.Bearer] =
req.header("Authorization").flatMap { authorization =>
val prefix = "Bearer "
if authorization.startsWith(prefix) then
authorization.stripPrefix(prefix).split(':') match
case Array(bearer, signed) if bearerSigner.sha1(bearer) hash_= signed => Some(Bearer(bearer))
case _ => None
case Array(bearer, signed) if bearerSigner.sha1(bearer).hash_=(signed) => Some(Bearer(bearer))
case _ => None
else None
}

Expand All @@ -70,11 +72,12 @@ final class Auth(mongo: Mongo, seenAt: SeenAtUpdate, config: Config)(using Execu
_.getAsOpt[User.Id]("userId").map(Success.OAuth(_))

private def sessionIdFromReq(req: RequestHeader): Option[String] =
req cookie cookieName flatMap {
case sessionIdRegex(id) => Some(id)
case _ => None
} orElse
req.queryParameter(sessionIdKey)
req
.cookie(cookieName)
.flatMap:
case sessionIdRegex(id) => Some(id)
case _ => None
.orElse(req.queryParameter(sessionIdKey))

object Auth:
private val sessionIdKey = "sessionId"
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/Bus.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ object Bus:
Msg(event, chan(channel))

def size = impl.size
def sizeOf(chan: ChanSelect) = impl sizeOf chan(channel)
def sizeOf(chan: ChanSelect) = impl.sizeOf(chan(channel))

// distinct bus for internal events
val internal = util.EventBus[Matchable, Chan, PartialFunction[Matchable, Unit]](
initialCapacity = 16,
publish = (listener, event) => listener lift event
publish = (listener, event) => listener.lift(event)
)
20 changes: 11 additions & 9 deletions src/main/scala/Chess.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,18 @@ object Chess:
,
opening =
if Variant.list.openingSensibleVariants(req.variant)
then OpeningDb findByEpdFen req.fen
then OpeningDb.findByEpdFen(req.fen)
else None,
chapterId = req.chapterId
)

def apply(req: ClientOut.Opening): Option[ClientIn.OpeningMsg] =
if Variant.list.openingSensibleVariants(req.variant)
then
OpeningDb findByEpdFen req.fen map:
ClientIn.OpeningMsg(req.path, _)
OpeningDb
.findByEpdFen(req.fen)
.map:
ClientIn.OpeningMsg(req.path, _)
else None

private def makeNode(
Expand All @@ -58,8 +60,8 @@ object Chess:
path: UciPath,
chapterId: Option[ChapterId]
): ClientIn.Node =
val movable = game.situation playable false
val fen = chess.format.Fen write game
val movable = game.situation.playable(false)
val fen = chess.format.Fen.write(game)
ClientIn.Node(
path = path,
id = UciCharPair(move.uci),
Expand All @@ -70,7 +72,7 @@ object Chess:
dests = if movable then game.situation.destinations else Map.empty,
opening =
if game.ply <= 30 && Variant.list.openingSensibleVariants(game.board.variant)
then OpeningDb findByEpdFen fen
then OpeningDb.findByEpdFen(fen)
else None,
drops = if movable then game.situation.drops else Some(Nil),
crazyData = game.situation.board.crazyData,
Expand All @@ -96,9 +98,9 @@ object Chess:
var first = true
dests.foreach: (orig, dests) =>
if first then first = false
else sb append " "
sb append orig.asChar
dests foreach { sb append _.asChar }
else sb.append(" ")
sb.append(orig.asChar)
dests.foreach(d => sb.append(d.asChar))
sb.toString

given OWrites[Crazyhouse.Pocket] = OWrites: v =>
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/Clients.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ object Clients:
Behaviors.receive[Control]: (ctx, msg) =>
msg match
case Control.Start(behavior, promise) =>
promise success ctx.spawnAnonymous(behavior)
promise.success(ctx.spawnAnonymous(behavior))
Behaviors.same
case Control.Stop(client) =>
ctx.stop(client)
Expand Down
Loading

0 comments on commit 960a42f

Please sign in to comment.