Update scalafmt to 2.5.1 + scalafmtAll

This commit is contained in:
Eike Kettner
2020-05-04 23:52:53 +02:00
parent 169510aec1
commit c41cdeefec
65 changed files with 523 additions and 431 deletions

View File

@ -31,9 +31,8 @@ object Main extends IOApp {
if (!Files.exists(path)) {
logger.info(s"Not using config file '$f' because it doesn't exist")
System.clearProperty("config.file")
} else {
} else
logger.info(s"Using config file from system properties: $f")
}
case _ =>
}
}

View File

@ -23,8 +23,9 @@ object RestServer {
val templates = TemplateRoutes[F](pools.blocker, cfg)
val app = for {
restApp <- RestAppImpl
.create[F](cfg, pools.connectEC, pools.httpClientEC, pools.blocker)
restApp <-
RestAppImpl
.create[F](cfg, pools.connectEC, pools.httpClientEC, pools.blocker)
httpApp = Router(
"/api/info" -> routes.InfoRoutes(),
"/api/v1/open/" -> openRoutes(cfg, restApp),

View File

@ -34,9 +34,10 @@ object CookieData {
def fromCookie[F[_]](req: Request[F]): Either[String, String] =
for {
header <- headers.Cookie.from(req.headers).toRight("Cookie parsing error")
cookie <- header.values.toList
.find(_.name == cookieName)
.toRight("Couldn't find the authcookie")
cookie <-
header.values.toList
.find(_.name == cookieName)
.toRight("Couldn't find the authcookie")
} yield cookie.content
def fromHeader[F[_]](req: Request[F]): Either[String, String] =

View File

@ -199,10 +199,15 @@ trait Conversions {
body
.through(fs2.text.utf8Decode)
.parseJsonAs[ItemUploadMeta]
.map(_.fold(ex => {
logger.error(ex)("Reading upload metadata failed.")
throw ex
}, identity))
.map(
_.fold(
ex => {
logger.error(ex)("Reading upload metadata failed.")
throw ex
},
identity
)
)
val meta: F[(Boolean, UploadMeta)] = mp.parts
.find(_.name.exists(_.equalsIgnoreCase("meta")))
@ -452,26 +457,30 @@ trait Conversions {
BasicResult(true, "The job has been removed from the queue.")
}
def basicResult(ar: AddResult, successMsg: String): BasicResult = ar match {
case AddResult.Success => BasicResult(true, successMsg)
case AddResult.EntityExists(msg) => BasicResult(false, msg)
case AddResult.Failure(ex) => BasicResult(false, s"Internal error: ${ex.getMessage}")
}
def basicResult(ar: AddResult, successMsg: String): BasicResult =
ar match {
case AddResult.Success => BasicResult(true, successMsg)
case AddResult.EntityExists(msg) => BasicResult(false, msg)
case AddResult.Failure(ex) =>
BasicResult(false, s"Internal error: ${ex.getMessage}")
}
def basicResult(ur: OUpload.UploadResult): BasicResult = ur match {
case UploadResult.Success => BasicResult(true, "Files submitted.")
case UploadResult.NoFiles => BasicResult(false, "There were no files to submit.")
case UploadResult.NoSource => BasicResult(false, "The source id is not valid.")
}
def basicResult(ur: OUpload.UploadResult): BasicResult =
ur match {
case UploadResult.Success => BasicResult(true, "Files submitted.")
case UploadResult.NoFiles => BasicResult(false, "There were no files to submit.")
case UploadResult.NoSource => BasicResult(false, "The source id is not valid.")
}
def basicResult(cr: PassChangeResult): BasicResult = cr match {
case PassChangeResult.Success => BasicResult(true, "Password changed.")
case PassChangeResult.UpdateFailed =>
BasicResult(false, "The database update failed.")
case PassChangeResult.PasswordMismatch =>
BasicResult(false, "The current password is incorrect.")
case PassChangeResult.UserNotFound => BasicResult(false, "User not found.")
}
def basicResult(cr: PassChangeResult): BasicResult =
cr match {
case PassChangeResult.Success => BasicResult(true, "Password changed.")
case PassChangeResult.UpdateFailed =>
BasicResult(false, "The database update failed.")
case PassChangeResult.PasswordMismatch =>
BasicResult(false, "The current password is incorrect.")
case PassChangeResult.UserNotFound => BasicResult(false, "User not found.")
}
def basicResult(e: Either[Throwable, _], successMsg: String): BasicResult =
e match {

View File

@ -9,8 +9,8 @@ trait ResponseGenerator[F[_]] {
self: Http4sDsl[F] =>
implicit final class EitherResponses[A, B](e: Either[A, B]) {
def toResponse(headers: Header*)(
implicit F: Applicative[F],
def toResponse(headers: Header*)(implicit
F: Applicative[F],
w0: EntityEncoder[F, A],
w1: EntityEncoder[F, B]
): F[Response[F]] =

View File

@ -46,9 +46,10 @@ object AttachmentRoutes {
case HEAD -> Root / Ident(id) =>
for {
fileData <- backend.item.findAttachment(id, user.account.collective)
resp <- fileData
.map(data => withResponseHeaders(Ok())(data))
.getOrElse(NotFound(BasicResult(false, "Not found")))
resp <-
fileData
.map(data => withResponseHeaders(Ok())(data))
.getOrElse(NotFound(BasicResult(false, "Not found")))
} yield resp
case req @ GET -> Root / Ident(id) =>
@ -56,20 +57,22 @@ object AttachmentRoutes {
fileData <- backend.item.findAttachment(id, user.account.collective)
inm = req.headers.get(`If-None-Match`).flatMap(_.tags)
matches = matchETag(fileData.map(_.meta), inm)
resp <- fileData
.map { data =>
if (matches) withResponseHeaders(NotModified())(data)
else makeByteResp(data)
}
.getOrElse(NotFound(BasicResult(false, "Not found")))
resp <-
fileData
.map { data =>
if (matches) withResponseHeaders(NotModified())(data)
else makeByteResp(data)
}
.getOrElse(NotFound(BasicResult(false, "Not found")))
} yield resp
case HEAD -> Root / Ident(id) / "original" =>
for {
fileData <- backend.item.findAttachmentSource(id, user.account.collective)
resp <- fileData
.map(data => withResponseHeaders(Ok())(data))
.getOrElse(NotFound(BasicResult(false, "Not found")))
resp <-
fileData
.map(data => withResponseHeaders(Ok())(data))
.getOrElse(NotFound(BasicResult(false, "Not found")))
} yield resp
case req @ GET -> Root / Ident(id) / "original" =>
@ -77,20 +80,22 @@ object AttachmentRoutes {
fileData <- backend.item.findAttachmentSource(id, user.account.collective)
inm = req.headers.get(`If-None-Match`).flatMap(_.tags)
matches = matchETag(fileData.map(_.meta), inm)
resp <- fileData
.map { data =>
if (matches) withResponseHeaders(NotModified())(data)
else makeByteResp(data)
}
.getOrElse(NotFound(BasicResult(false, "Not found")))
resp <-
fileData
.map { data =>
if (matches) withResponseHeaders(NotModified())(data)
else makeByteResp(data)
}
.getOrElse(NotFound(BasicResult(false, "Not found")))
} yield resp
case HEAD -> Root / Ident(id) / "archive" =>
for {
fileData <- backend.item.findAttachmentArchive(id, user.account.collective)
resp <- fileData
.map(data => withResponseHeaders(Ok())(data))
.getOrElse(NotFound(BasicResult(false, "Not found")))
resp <-
fileData
.map(data => withResponseHeaders(Ok())(data))
.getOrElse(NotFound(BasicResult(false, "Not found")))
} yield resp
case req @ GET -> Root / Ident(id) / "archive" =>
@ -98,12 +103,13 @@ object AttachmentRoutes {
fileData <- backend.item.findAttachmentArchive(id, user.account.collective)
inm = req.headers.get(`If-None-Match`).flatMap(_.tags)
matches = matchETag(fileData.map(_.meta), inm)
resp <- fileData
.map { data =>
if (matches) withResponseHeaders(NotModified())(data)
else makeByteResp(data)
}
.getOrElse(NotFound(BasicResult(false, "Not found")))
resp <-
fileData
.map { data =>
if (matches) withResponseHeaders(NotModified())(data)
else makeByteResp(data)
}
.getOrElse(NotFound(BasicResult(false, "Not found")))
} yield resp
case GET -> Root / Ident(id) / "view" =>
@ -123,8 +129,9 @@ object AttachmentRoutes {
case DELETE -> Root / Ident(id) =>
for {
n <- backend.item.deleteAttachment(id, user.account.collective)
res = if (n == 0) BasicResult(false, "Attachment not found")
else BasicResult(true, "Attachment deleted.")
res =
if (n == 0) BasicResult(false, "Attachment not found")
else BasicResult(true, "Attachment deleted.")
resp <- Ok(res)
} yield resp
}

View File

@ -28,8 +28,9 @@ object CollectiveRoutes {
case req @ POST -> Root / "settings" =>
for {
settings <- req.as[CollectiveSettings]
res <- backend.collective
.updateLanguage(user.account.collective, settings.language)
res <-
backend.collective
.updateLanguage(user.account.collective, settings.language)
resp <- Ok(Conversions.basicResult(res, "Language updated."))
} yield resp
@ -43,11 +44,12 @@ object CollectiveRoutes {
case GET -> Root / "contacts" :? QueryParam.QueryOpt(q) +& QueryParam
.ContactKindOpt(kind) =>
for {
res <- backend.collective
.getContacts(user.account.collective, q.map(_.q), kind)
.take(50)
.compile
.toList
res <-
backend.collective
.getContacts(user.account.collective, q.map(_.q), kind)
.take(50)
.compile
.toList
resp <- Ok(ContactList(res.map(Conversions.mkContact)))
} yield resp

View File

@ -36,9 +36,10 @@ object ItemRoutes {
for {
item <- backend.item.findItem(id, user.account.collective)
result = item.map(Conversions.mkItemDetail)
resp <- result
.map(r => Ok(r))
.getOrElse(NotFound(BasicResult(false, "Not found.")))
resp <-
result
.map(r => Ok(r))
.getOrElse(NotFound(BasicResult(false, "Not found.")))
} yield resp
case POST -> Root / Ident(id) / "confirm" =>

View File

@ -40,8 +40,9 @@ object MailSendRoutes {
for {
rec <- s.recipients.traverse(MailAddress.parse)
fileIds <- s.attachmentIds.traverse(Ident.fromString)
sel = if (s.addAllAttachments) AttachSelection.All
else AttachSelection.Selected(fileIds)
sel =
if (s.addAllAttachments) AttachSelection.All
else AttachSelection.Selected(fileIds)
} yield ItemMail(item, s.subject, rec, s.body, sel)
def convertOut(res: SendResult): BasicResult =

View File

@ -31,10 +31,10 @@ object NotifyDueItemsRoutes {
for {
data <- req.as[NotificationSettings]
task = makeTask(cfg, user.account, data)
res <- ut
.executeNow(user.account, task)
.attempt
.map(Conversions.basicResult(_, "Submitted successfully."))
res <-
ut.executeNow(user.account, task)
.attempt
.map(Conversions.basicResult(_, "Submitted successfully."))
resp <- Ok(res)
} yield resp
@ -49,10 +49,10 @@ object NotifyDueItemsRoutes {
for {
data <- req.as[NotificationSettings]
task = makeTask(cfg, user.account, data)
res <- ut
.submitNotifyDueItems(user.account, task)
.attempt
.map(Conversions.basicResult(_, "Saved successfully."))
res <-
ut.submitNotifyDueItems(user.account, task)
.attempt
.map(Conversions.basicResult(_, "Saved successfully."))
resp <- Ok(res)
} yield resp
}
@ -89,12 +89,13 @@ object NotifyDueItemsRoutes {
for {
tinc <- backend.tag.loadAll(task.args.tagsInclude)
texc <- backend.tag.loadAll(task.args.tagsExclude)
conn <- backend.mail
.getSettings(account, None)
.map(
_.find(_.name == task.args.smtpConnection)
.map(_.name)
)
conn <-
backend.mail
.getSettings(account, None)
.map(
_.find(_.name == task.args.smtpConnection)
.map(_.name)
)
} yield NotificationSettings(
task.id,
task.enabled,

View File

@ -21,17 +21,16 @@ object OrganizationRoutes {
HttpRoutes.of {
case GET -> Root :? QueryParam.FullOpt(full) +& QueryParam.QueryOpt(q) =>
if (full.getOrElse(false)) {
if (full.getOrElse(false))
for {
data <- backend.organization.findAllOrg(user.account, q.map(_.q))
resp <- Ok(OrganizationList(data.map(mkOrg).toList))
} yield resp
} else {
else
for {
data <- backend.organization.findAllOrgRefs(user.account, q.map(_.q))
resp <- Ok(ReferenceList(data.map(mkIdName).toList))
} yield resp
}
case req @ POST -> Root =>
for {

View File

@ -24,17 +24,16 @@ object PersonRoutes {
HttpRoutes.of {
case GET -> Root :? QueryParam.FullOpt(full) +& QueryParam.QueryOpt(q) =>
if (full.getOrElse(false)) {
if (full.getOrElse(false))
for {
data <- backend.organization.findAllPerson(user.account, q.map(_.q))
resp <- Ok(PersonList(data.map(mkPerson).toList))
} yield resp
} else {
else
for {
data <- backend.organization.findAllPersonRefs(user.account, q.map(_.q))
resp <- Ok(ReferenceList(data.map(mkIdName).toList))
} yield resp
}
case req @ POST -> Root =>
for {

View File

@ -38,28 +38,30 @@ object RegisterRoutes {
}
}
def convert(r: NewInviteResult): InviteResult = r match {
case NewInviteResult.Success(id) =>
InviteResult(true, "New invitation created.", Some(id))
case NewInviteResult.InvitationDisabled =>
InviteResult(false, "Signing up is not enabled for invitations.", None)
case NewInviteResult.PasswordMismatch =>
InviteResult(false, "Password is invalid.", None)
}
def convert(r: NewInviteResult): InviteResult =
r match {
case NewInviteResult.Success(id) =>
InviteResult(true, "New invitation created.", Some(id))
case NewInviteResult.InvitationDisabled =>
InviteResult(false, "Signing up is not enabled for invitations.", None)
case NewInviteResult.PasswordMismatch =>
InviteResult(false, "Password is invalid.", None)
}
def convert(r: SignupResult): BasicResult = r match {
case SignupResult.CollectiveExists =>
BasicResult(false, "A collective with this name already exists.")
case SignupResult.InvalidInvitationKey =>
BasicResult(false, "Invalid invitation key.")
case SignupResult.SignupClosed =>
BasicResult(false, "Sorry, registration is closed.")
case SignupResult.Failure(ex) =>
logger.error(ex)("Error signing up")
BasicResult(false, s"Internal error: ${ex.getMessage}")
case SignupResult.Success =>
BasicResult(true, "Signup successful")
}
def convert(r: SignupResult): BasicResult =
r match {
case SignupResult.CollectiveExists =>
BasicResult(false, "A collective with this name already exists.")
case SignupResult.InvalidInvitationKey =>
BasicResult(false, "Invalid invitation key.")
case SignupResult.SignupClosed =>
BasicResult(false, "Sorry, registration is closed.")
case SignupResult.Failure(ex) =>
logger.error(ex)("Error signing up")
BasicResult(false, s"Internal error: ${ex.getMessage}")
case SignupResult.Success =>
BasicResult(true, "Signup successful")
}
def convert(r: Registration): RegisterData =
RegisterData(r.collectiveName, r.login, r.password, r.invite)

View File

@ -26,8 +26,8 @@ object TemplateRoutes {
def app: HttpRoutes[F]
}
def apply[F[_]: Effect](blocker: Blocker, cfg: Config)(
implicit C: ContextShift[F]
def apply[F[_]: Effect](blocker: Blocker, cfg: Config)(implicit
C: ContextShift[F]
): InnerRoutes[F] = {
val indexTemplate = memo(
loadResource("/index.html").flatMap(loadTemplate(_, blocker))
@ -64,8 +64,8 @@ object TemplateRoutes {
r.pure[F]
}
def loadUrl[F[_]: Sync](url: URL, blocker: Blocker)(
implicit C: ContextShift[F]
def loadUrl[F[_]: Sync](url: URL, blocker: Blocker)(implicit
C: ContextShift[F]
): F[String] =
Stream
.bracket(Sync[F].delay(url.openStream))(in => Sync[F].delay(in.close()))
@ -82,8 +82,8 @@ object TemplateRoutes {
}
}
def loadTemplate[F[_]: Sync](url: URL, blocker: Blocker)(
implicit C: ContextShift[F]
def loadTemplate[F[_]: Sync](url: URL, blocker: Blocker)(implicit
C: ContextShift[F]
): F[Template] =
loadUrl[F](url, blocker).flatMap(s => parseTemplate(s)).map { t =>
logger.info(s"Compiled template $url")