Skip to content

Commit

Permalink
/email
Browse files Browse the repository at this point in the history
  • Loading branch information
JG1VPP committed Jul 15, 2024
1 parent a925afb commit 7a6465d
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 16 deletions.
29 changes: 29 additions & 0 deletions app/controllers/controllers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,35 @@ class Shell @Inject()(implicit in: Injections) extends IC {
}


/**
* 管理者権限を伴って、メール送信のGETまたはPOSTメソッドを処理するコントローラです。
*
*
* @param in 依存性注入
*/
@Singleton
class Email @Inject()(implicit in: Injections) extends IC {
/**
* 管理者権限を表す真偽値です。
*/
implicit val admin: Boolean = true

/**
* メール送信のページのビューを返します。
*
* @return メール送信のページ
*/
def email = Action(implicit r => Ok(pages.email(new MessageForm().fill(MessageFormData.data))))

/**
* メール送信のリクエストを処理します。
*
* @return メール送信のページ
*/
def shoot = Action(implicit r => Ok(new MailerTask().send(new MessageForm().bindFromRequest().get)))
}


/**
* ブラウザに返信するレスポンスのキャッシュ設定を編集するフィルタです。
*
Expand Down
55 changes: 55 additions & 0 deletions app/models/models.scala
Original file line number Diff line number Diff line change
Expand Up @@ -494,3 +494,58 @@ class DevelopForm extends Form[DevelopFormData](
(DevelopFormData.apply)
(DevelopFormData.unapply), Map.empty, Nil, None
)


/**
* メール送信のフォームに入力されたデータです。
*
*
* @param to 宛先
* @param sub 件名
* @param body 本文
*
* @since 2024/07/06
*/
case class MessageFormData(
to: String,
sub: String,
body: String,
)


/**
* メール送信のフォームに入力されたデータです。
*
*
* @since 2024/07/06
*/
object MessageFormData {
/**
* メールのテンプレートを返します。
*
* @param in 依存性注入
* @return フォームの内容
*/
def data(implicit in: Injections) = MessageFormData(
to = in.rule.mail,
sub = "",
body = views.txt.mails.mailer().body.trim,
)
}


/**
* メール送信のフォームとデータの関連付けと検証を実装します。
*
*
* @since 2024/07/06
*/
class MessageForm extends Form[MessageFormData](
Forms.mapping(
"to" -> Forms.email,
"sub" -> Forms.nonEmptyText,
"body" -> Forms.nonEmptyText,
)
(MessageFormData.apply)
(MessageFormData.unapply), Map.empty, Nil, None
)
40 changes: 33 additions & 7 deletions app/tasks/tasks.scala
Original file line number Diff line number Diff line change
Expand Up @@ -231,29 +231,55 @@ class FileDLTask(call: String, file: String)(implicit in: Injections) {
* 書類提出を受理した内容のメールを参加局に送信します。
*
*
* @param req リクエストヘッダ
* @param in 依存性注入
* @param admin 管理者権限
*/
class NotifyTask(implicit in: Injections) {
class NotifyTask(implicit req: RequestHeader, in: Injections, admin: Boolean) {
/**
* 指定された参加局に対してメールを送信します。
*
* @param station 参加局の登録情報
*/
def send(station: StationData): Unit = util.Try {
def send(station: StationData): Unit = {
val text = views.txt.mails.accept(station.call).body.trim
new MailerTask().send(new MessageFormData(
to = "%s <%s>".format(station.call, station.mail),
sub = text.linesIterator.toSeq.head.split(";").head.trim,
body = text.linesWithSeparators.toSeq.tail.mkString.trim
))
}
}


/**
* 指定された宛先・件名・本文のメールを送信します。
*
*
* @param req リクエストヘッダ
* @param in 依存性注入
* @param admin 管理者権限
*/
class MailerTask(implicit req: RequestHeader, in: Injections, admin: Boolean) {
/**
* 指定された内容のメールを送信します。
*
* @param message 内容
*/
def send(message: MessageFormData): Html = util.Try {
val mail = new Email
val user = in.cf.get[String]("play.mailer.user")
val host = in.cf.get[String]("play.mailer.host")
val text = views.txt.mails.accept(station.call).body.trim
mail.setFrom("%s <%s@%s>".format(in.rule.host, user, host))
mail.addTo("%s <%s>".format(station.call, station.mail))
mail.addTo(message.to)
mail.addBcc(in.rule.mail)
mail.addReplyTo(in.rule.mail)
mail.setSubject(text.linesIterator.toSeq.head.split(";").head.trim)
mail.setBodyText(text.linesWithSeparators.toSeq.tail.mkString.trim)
mail.setSubject(message.sub)
mail.setBodyText(message.body)
in.mc.send(mail)
}.recover {
case ex: EmailException => Logger("mail").error("MAIL ERROR!", ex)
}
}.map(_ => pages.lists()).get
}


Expand Down
1 change: 1 addition & 0 deletions app/views/comps/page.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
@navi(href = routes.Admin.index.url, label = "集計結果", active = nav == "lists", icon = "fas fa-trophy")
@navi(href = routes.Admin.entry.url, label = "書類提出", active = nav == "entry", icon = "fas fa-upload")
@navi(href = routes.Shell.shell.url, label = "規約開発", active = nav == "shell", icon = "fas fa-terminal")
@navi(href = routes.Email.email.url, label = "連絡送信", active = nav == "email", icon = "fas fa-envelope")
} else {
@navi(href = routes.Index.entry.url, label = "書類提出", active = nav == "entry", icon = "fas fa-upload")
@navi(href = routes.Index.lists.url, label = "集計結果", active = nav == "lists", icon = "fas fa-trophy")
Expand Down
11 changes: 2 additions & 9 deletions app/views/mails/accept.scala.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@(call: String)(implicit in: Injections)

@{in.rule.name}書類提出の完了 ; 空白を除く先頭行はメールの件名
@{in.rule.name}書類提出の完了 ; 件名

@{call}様

Expand All @@ -12,11 +12,4 @@
にて受理いたしました。
今後とも当コンテストをよろしくお願いいたします。

---
このメールに心当たりがない場合はお手数ですが下記の連絡先までご連絡ください。

---
@{in.rule.host()}
@{in.rule.name()}受付担当係
@{in.rule.link()}
@{in.rule.mail()}
@mails.footer()
10 changes: 10 additions & 0 deletions app/views/mails/footer.scala.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@()(implicit in: Injections)

---
このメールに心当たりがない場合はお手数ですが下記の連絡先までご連絡ください。

---
@{in.rule.host()}
@{in.rule.name()}受付担当係
@{in.rule.link()}
@{in.rule.mail()}
3 changes: 3 additions & 0 deletions app/views/mails/mailer.scala.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@()(implicit in: Injections)

@mails.footer()
20 changes: 20 additions & 0 deletions app/views/pages/email.scala.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
@(form: Form[MessageFormData])(implicit r: RequestHeader, in: Injections, admin: Boolean)

@comps.page(title = "連絡送信", nav = "email") {
@helper.form(action = routes.Email.shoot) {
@helper.CSRF.formField
<fieldset>
@comps.text(form("to"), label = "To")
@comps.text(form("sub"), label = "Sub")
@comps.area(form("body"), label = "Body")
</fieldset>
@comps.warn(id = "submit", title = "確認", button = "送信する", icon = "fas fa-paper-plane", submit = true) {
<p>メールを送信します。</p>
}
}
<script>
$('form').submit(function() {
$(this).find(':submit').prop('disabled', 'true');
});
</script>
}
4 changes: 4 additions & 0 deletions conf/routes
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ DELETE /admin/proof/:call controllers.Force.drop(call: String)
POST /admin/entry controllers.Force.edit
POST /admin/amend controllers.Force.redo

# Mailer pages
GET /admin/email controllers.Email.email
POST /admin/email controllers.Email.shoot

# System pages
GET /admin/shell controllers.Shell.shell
POST /admin/shell controllers.Shell.debug
Expand Down

0 comments on commit 7a6465d

Please sign in to comment.