scalatags emails

v2
Thibault Duplessis 2019-04-28 21:14:16 +07:00
parent 7f26207ef5
commit 5d8ddf8f8c
8 changed files with 93 additions and 109 deletions

View File

@ -245,7 +245,7 @@ object Auth extends LilaController {
private def welcome(user: UserModel, email: EmailAddress)(implicit ctx: Context) = {
garbageCollect(user, email)
env.welcomeEmail(user, email)
env.automaticEmail.welcome(user, email)
}
private def garbageCollect(user: UserModel, email: EmailAddress)(implicit ctx: Context) =

View File

@ -1,9 +1,9 @@
package lila.security
import scalatags.Text.all.raw
import scalatags.Text.all._
import lila.common.{ Lang, EmailAddress }
import lila.common.String.html.nl2brUnsafe
import lila.i18n.I18nKeys.{ emails => trans }
import lila.user.{ User, UserRepo }
final class AutomaticEmail(
@ -11,6 +11,25 @@ final class AutomaticEmail(
baseUrl: String
) {
import Mailgun.html._
def welcome(user: User, email: EmailAddress)(implicit lang: Lang): Funit = {
val profileUrl = s"$baseUrl/@/${user.username}"
val editUrl = s"$baseUrl/account/profile"
mailgun send Mailgun.Message(
to = email,
subject = trans.welcome_subject.literalTxtTo(lang, List(user.username)),
text = s"""
${trans.welcome_text.literalTxtTo(lang, List(profileUrl, editUrl))}
${Mailgun.txt.serviceNote}
""",
htmlBody = standardEmail(
trans.welcome_text.literalTxtTo(lang, List(profileUrl, editUrl))
).some
)
}
def onTitleSet(username: String)(implicit lang: Lang): Funit = for {
user <- UserRepo named username flatten s"No such user $username"
emailOption <- UserRepo email user.id
@ -38,11 +57,7 @@ $body
${Mailgun.txt.serviceNote}
""",
htmlBody = raw(s"""
<div itemscope itemtype="http://schema.org/EmailMessage">
<p itemprop="description">${nl2brUnsafe(body)}</p>
${Mailgun.html.serviceNote}
</div>""").some
htmlBody = standardEmail(body).some
)
}
@ -67,11 +82,7 @@ $body
${Mailgun.txt.serviceNote}
""",
htmlBody = raw(s"""
<div itemscope itemtype="http://schema.org/EmailMessage">
<p itemprop="description">${nl2brUnsafe(body)}</p>
${Mailgun.html.serviceNote}
</div>""").some
htmlBody = standardEmail(body).some
)
}
}
@ -107,11 +118,7 @@ $body
${Mailgun.txt.serviceNote}
""",
htmlBody = raw(s"""
<div itemscope itemtype="http://schema.org/EmailMessage">
<p itemprop="description">${nl2brUnsafe(body)}</p>
${Mailgun.html.serviceNote}
</div>""").some
htmlBody = standardEmail(body).some
)
}
}

View File

@ -3,8 +3,8 @@ package lila.security
import scalatags.Text.all._
import lila.common.{ Lang, EmailAddress }
import lila.user.{ User, UserRepo }
import lila.i18n.I18nKeys.{ emails => trans }
import lila.user.{ User, UserRepo }
final class EmailChange(
mailgun: Mailgun,
@ -12,6 +12,8 @@ final class EmailChange(
tokenerSecret: String
) {
import Mailgun.html._
def send(user: User, email: EmailAddress)(implicit lang: Lang): Funit =
tokener make TokenPayload(user.id, email).some flatMap { token =>
lila.mon.email.types.change()
@ -30,16 +32,12 @@ ${trans.common_orPaste.literalTxtTo(lang)}
${Mailgun.txt.serviceNote}
""",
htmlBody = raw(s"""
<div itemscope itemtype="http://schema.org/EmailMessage">
<p itemprop="description">${trans.emailChange_intro.literalTo(lang).render}</p>
<p>${trans.emailChange_click.literalTo(lang).render}</p>
<div itemprop="potentialAction" itemscope itemtype="http://schema.org/ViewAction">
<meta itemprop="name" content="Change email address">
${Mailgun.html.url(url)}
</div>
${Mailgun.html.serviceNote}
</div>""").some
htmlBody = emailMessage(
pDesc(trans.emailChange_intro.literalTo(lang)),
p(trans.emailChange_click.literalTo(lang)),
potentialAction(metaName("Change email address"), Mailgun.html.url(url)),
serviceNote
).some
)
}

View File

@ -30,6 +30,8 @@ final class EmailConfirmMailgun(
tokenerSecret: String
) extends EmailConfirm {
import Mailgun.html._
def effective = true
val maxTries = 3
@ -51,20 +53,17 @@ ${trans.common_orPaste.literalTxtTo(lang)}
${Mailgun.txt.serviceNote}
${trans.emailConfirm_ignore.literalTxtTo(lang, List("https://lichess.org"))}
""",
htmlBody = raw(s"""
<div itemscope itemtype="http://schema.org/EmailMessage">
<p itemprop="description">${trans.emailConfirm_click.literalTo(lang).render}</p>
<div itemprop="potentialAction" itemscope itemtype="http://schema.org/ViewAction">
<meta itemprop="name" content="Activate account">
${Mailgun.html.url(url)}
</div>
<div itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<small>
${trans.common_note.literalTo(lang, List(Mailgun.html.noteLink)).render}
${trans.emailConfirm_ignore.literalTo(lang).render}
</small>
</div>
</div>""").some
htmlBody = emailMessage(
pDesc(trans.emailConfirm_click.literalTo(lang)),
potentialAction(metaName("Activate account"), Mailgun.html.url(url)),
publisher(
small(
trans.common_note.literalTo(lang, List(Mailgun.html.noteLink)),
" ",
trans.emailConfirm_ignore.literalTo(lang)
)
)
).some
)
}

View File

@ -138,11 +138,6 @@ final class Env(
secret = LoginTokenSecret
)
lazy val welcomeEmail = new WelcomeEmail(
mailgun = mailgun,
baseUrl = NetBaseUrl
)
lazy val automaticEmail = new AutomaticEmail(
mailgun = mailgun,
baseUrl = NetBaseUrl

View File

@ -1,6 +1,6 @@
package lila.security
import scala.concurrent.duration._
import scala.concurrent.duration.{ span => _, _ }
import akka.actor.ActorSystem
import play.api.libs.ws.{ WS, WSAuthScheme }
@ -8,6 +8,7 @@ import play.api.Play.current
import scalatags.Text.all._
import lila.common.String.html.escapeHtml
import lila.common.String.html.nl2brUnsafe
import lila.common.{ Lang, EmailAddress }
import lila.i18n.I18nKeys.{ emails => trans }
@ -67,7 +68,7 @@ object Mailgun {
object txt {
def serviceNote(implicit lang: Lang) = s"""
def serviceNote(implicit lang: Lang): String = s"""
${trans.common_note.literalTo(lang, List("https://lichess.org")).render}
${trans.common_contact.literalTo(lang, List("https://lichess.org/contact")).render}"""
@ -75,24 +76,42 @@ ${trans.common_contact.literalTo(lang, List("https://lichess.org/contact")).rend
object html {
val noteLink = raw {
"""<a itemprop="url" href="https://lichess.org/"><span itemprop="name">lichess.org</span></a>"""
}
val noteContact = raw {
"""<a itemprop="url" href="https://lichess.org/contact"><span itemprop="name">lichess.org/contact</span></a>"""
}
val itemscope = attr("itemscope").empty
val itemtype = attr("itemtype")
val itemprop = attr("itemprop")
val emailMessage = div(itemscope, itemtype := "http://schema.org/EmailMessage")
val pDesc = p(itemprop := "description")
val potentialAction = div(itemprop := "potentialAction", itemscope, itemtype := "http://schema.org/ViewAction")
def metaName(cont: String) = meta(itemprop := "name", content := cont)
val publisher = div(itemprop := "publisher", itemscope, itemtype := "http://schema.org/Organization")
val noteContact = a(itemprop := "url", href := "https://lichess.org/contact")(
span(itemprop := "name")("lichess.org/contact")
)
def serviceNote(implicit lang: Lang) = s"""
<div itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<small>${trans.common_note.literalTo(lang, List(noteLink)).render} ${trans.common_contact.literalTo(lang, List(noteContact)).render}</small>
</div>
"""
def serviceNote(implicit lang: Lang) = publisher(
small(
trans.common_note.literalTo(lang, List(Mailgun.html.noteLink)),
" ",
trans.common_contact.literalTo(lang, List(noteContact))
)
)
def url(u: String)(implicit lang: Lang) = s"""
<meta itemprop="url" content="$u">
<p><a itemprop="target" href="$u">$u</a></p>
<p>${trans.common_orPaste.literalTo(lang).render}</p>
"""
def standardEmail(body: String)(implicit lang: Lang): Frag =
emailMessage(
pDesc(nl2brUnsafe(body)),
publisher
)
val noteLink = a(
itemprop := "url",
href := "https://lichess.org/"
)(span(itemprop := "name")("lichess.org"))
def url(u: String)(implicit lang: Lang) = frag(
meta(itemprop := "url", content := u),
p(a(itemprop := "target", href := u)(u)),
p(trans.common_orPaste.literalTo(lang))
)
private[Mailgun] def wrap(subject: String, body: Frag): Frag = frag(
raw(s"""<!doctype html>

View File

@ -3,8 +3,8 @@ package lila.security
import scalatags.Text.all._
import lila.common.{ Lang, EmailAddress }
import lila.user.{ User, UserRepo }
import lila.i18n.I18nKeys.{ emails => trans }
import lila.user.{ User, UserRepo }
final class PasswordReset(
mailgun: Mailgun,
@ -12,6 +12,8 @@ final class PasswordReset(
tokenerSecret: String
) {
import Mailgun.html._
def send(user: User, email: EmailAddress)(implicit lang: Lang): Funit =
tokener make user.id flatMap { token =>
lila.mon.email.types.resetPassword()
@ -30,16 +32,12 @@ ${trans.common_orPaste.literalTxtTo(lang)}
${Mailgun.txt.serviceNote}
""",
htmlBody = raw(s"""
<div itemscope itemtype="http://schema.org/EmailMessage">
<p itemprop="description">${trans.passwordReset_intro.literalTo(lang).render}</p>
<p>${trans.passwordReset_clickOrIgnore.literalTo(lang).render}</p>
<div itemprop="potentialAction" itemscope itemtype="http://schema.org/ViewAction">
<meta itemprop="name" content="Reset password">
${Mailgun.html.url(url)}
</div>
${Mailgun.html.serviceNote}
</div>""").some
htmlBody = emailMessage(
pDesc(trans.passwordReset_intro.literalTo(lang)),
p(trans.passwordReset_clickOrIgnore.literalTo(lang)),
potentialAction(metaName("Reset password"), Mailgun.html.url(url)),
serviceNote
).some
)
}

View File

@ -1,32 +0,0 @@
package lila.security
import scalatags.Text.all._
import lila.common.{ Lang, EmailAddress }
import lila.user.User
import lila.i18n.I18nKeys.{ emails => trans }
final class WelcomeEmail(
mailgun: Mailgun,
baseUrl: String
) {
def apply(user: User, email: EmailAddress)(implicit lang: Lang): Funit = {
val profileUrl = s"$baseUrl/@/${user.username}"
val editUrl = s"$baseUrl/account/profile"
mailgun send Mailgun.Message(
to = email,
subject = trans.welcome_subject.literalTxtTo(lang, List(user.username)),
text = s"""
${trans.welcome_text.literalTxtTo(lang, List(profileUrl, editUrl))}
${Mailgun.txt.serviceNote}
""",
htmlBody = raw(s"""
<div itemscope itemtype="http://schema.org/EmailMessage">
<p itemprop="description">${trans.welcome_text.literalTo(lang, List(profileUrl, editUrl)).render}</p>
${Mailgun.html.serviceNote}
</div>""").some
)
}
}