diff --git a/src/main/scala/gitbucket/core/controller/SystemSettingsController.scala b/src/main/scala/gitbucket/core/controller/SystemSettingsController.scala index 46b403c..1485d56 100644 --- a/src/main/scala/gitbucket/core/controller/SystemSettingsController.scala +++ b/src/main/scala/gitbucket/core/controller/SystemSettingsController.scala @@ -174,11 +174,12 @@ post("/admin/system/sendmail", sendMailForm)(adminOnly { form => try { - new Mailer(form.smtp).send(form.testAddress, - "Test message from GitBucket", - "This is a test message from GitBucket.", - None, - context.loginAccount + new Mailer(context.settings.copy(smtp = Some(form.smtp), notification = true)).send( + to = form.testAddress, + subject = "Test message from GitBucket", + textMsg = "This is a test message from GitBucket.", + htmlMsg = None, + loginAccount = context.loginAccount ) "Test mail has been sent to: " + form.testAddress diff --git a/src/main/scala/gitbucket/core/plugin/IssueHook.scala b/src/main/scala/gitbucket/core/plugin/IssueHook.scala index 8bed047..56e7294 100644 --- a/src/main/scala/gitbucket/core/plugin/IssueHook.scala +++ b/src/main/scala/gitbucket/core/plugin/IssueHook.scala @@ -3,18 +3,20 @@ import gitbucket.core.controller.Context import gitbucket.core.model.Issue import gitbucket.core.service.RepositoryService.RepositoryInfo +import gitbucket.core.model.Profile._ +import profile.api._ trait IssueHook { - def created(issue: Issue, repository: RepositoryInfo)(implicit context: Context): Unit = () - def addedComment(commentId: Int, content: String, issue: Issue, repository: RepositoryInfo)(implicit context: Context): Unit = () - def closed(issue: Issue, repository: RepositoryInfo)(implicit context: Context): Unit = () - def reopened(issue: Issue, repository: RepositoryInfo)(implicit context: Context): Unit = () + def created(issue: Issue, repository: RepositoryInfo)(implicit session: Session, context: Context): Unit = () + def addedComment(commentId: Int, content: String, issue: Issue, repository: RepositoryInfo)(implicit session: Session, context: Context): Unit = () + def closed(issue: Issue, repository: RepositoryInfo)(implicit session: Session, context: Context): Unit = () + def reopened(issue: Issue, repository: RepositoryInfo)(implicit session: Session, context: Context): Unit = () } trait PullRequestHook extends IssueHook { - def merged(issue: Issue, repository: RepositoryInfo)(implicit context: Context): Unit = () + def merged(issue: Issue, repository: RepositoryInfo)(implicit session: Session, context: Context): Unit = () } diff --git a/src/main/scala/gitbucket/core/util/Mailer.scala b/src/main/scala/gitbucket/core/util/Mailer.scala new file mode 100644 index 0000000..703f4b4 --- /dev/null +++ b/src/main/scala/gitbucket/core/util/Mailer.scala @@ -0,0 +1,68 @@ +package gitbucket.core.util + +import gitbucket.core.model.Account +import gitbucket.core.service.SystemSettingsService + +import org.apache.commons.mail.{DefaultAuthenticator, HtmlEmail} +import SystemSettingsService.SystemSettings + +class Mailer(settings: SystemSettings){ + + def send(to: String, subject: String, textMsg: String, htmlMsg: Option[String] = None, loginAccount: Option[Account] = None): Unit = { + createMail(subject, textMsg, htmlMsg, loginAccount).foreach { email => + email.addTo(to).send + } + } + + def sendBcc(bcc: Seq[String], subject: String, textMsg: String, htmlMsg: Option[String] = None, loginAccount: Option[Account] = None): Unit = { + createMail(subject, textMsg, htmlMsg, loginAccount).foreach { email => + bcc.foreach { address => + email.addBcc(address) + } + email.send() + } + } + + def createMail(subject: String, textMsg: String, htmlMsg: Option[String] = None, loginAccount: Option[Account] = None): Option[HtmlEmail] = { + if(settings.notification == true){ + settings.smtp.map { smtp => + val email = new HtmlEmail + email.setHostName(smtp.host) + email.setSmtpPort(smtp.port.get) + smtp.user.foreach { user => + email.setAuthenticator(new DefaultAuthenticator(user, smtp.password.getOrElse(""))) + } + smtp.ssl.foreach { ssl => + email.setSSLOnConnect(ssl) + if(ssl == true) { + email.setSslSmtpPort(smtp.port.get.toString) + } + } + smtp.starttls.foreach { starttls => + email.setStartTLSEnabled(starttls) + email.setStartTLSRequired(starttls) + } + smtp.fromAddress + .map (_ -> smtp.fromName.getOrElse(loginAccount.map(_.userName).getOrElse("GitBucket"))) + .orElse (Some("notifications@gitbucket.com" -> loginAccount.map(_.userName).getOrElse("GitBucket"))) + .foreach { case (address, name) => + email.setFrom(address, name) + } + email.setCharset("UTF-8") + email.setSubject(subject) + email.setTextMsg(textMsg) + htmlMsg.foreach { msg => + email.setHtmlMsg(msg) + } + + email + } + } else None + } + +} + +//class MockMailer extends Notifier { +// def toNotify(subject: String, textMsg: String, htmlMsg: Option[String] = None) +// (recipients: Account => Session => Seq[String])(implicit context: Context): Unit = () +//} diff --git a/src/main/scala/gitbucket/core/util/Notifier.scala b/src/main/scala/gitbucket/core/util/Notifier.scala deleted file mode 100644 index d3ee034..0000000 --- a/src/main/scala/gitbucket/core/util/Notifier.scala +++ /dev/null @@ -1,99 +0,0 @@ -package gitbucket.core.util - -import gitbucket.core.model.{Session, Account} -import gitbucket.core.model.Profile.profile.blockingApi._ -import gitbucket.core.service.SystemSettingsService -import gitbucket.core.servlet.Database - -import scala.concurrent._ -import scala.util.{Success, Failure} -import ExecutionContext.Implicits.global -import org.apache.commons.mail.{DefaultAuthenticator, HtmlEmail} -import org.slf4j.LoggerFactory -import gitbucket.core.controller.Context -import SystemSettingsService.Smtp - -/** - * The trait for notifications. - * This is used by notifications plugin, which provides notifications feature on GitBucket. - * Please see the plugin for details. - */ -trait Notifier { - def toNotify(subject: String, textMsg: String) - (recipients: Account => Session => Seq[String])(implicit context: Context): Unit = { - toNotify(subject, textMsg, None)(recipients) - } - - def toNotify(subject: String, textMsg: String, htmlMsg: Option[String]) - (recipients: Account => Session => Seq[String])(implicit context: Context): Unit - -} - -object Notifier { - def apply(): Notifier = new SystemSettingsService {}.loadSystemSettings match { - case settings if (settings.notification && settings.useSMTP) => new Mailer(settings.smtp.get) - case _ => new MockMailer - } -} - -class Mailer(private val smtp: Smtp) extends Notifier { - private val logger = LoggerFactory.getLogger(classOf[Mailer]) - - def toNotify(subject: String, textMsg: String, htmlMsg: Option[String] = None) - (recipients: Account => Session => Seq[String])(implicit context: Context): Unit = { - context.loginAccount.foreach { loginAccount => - val database = Database() - - val f = Future { - database withSession { session => - recipients(loginAccount)(session) foreach { to => - send(to, subject, textMsg, htmlMsg, Some(loginAccount)) - } - } - "Notifications Successful." - } - f.onComplete { - case Success(s) => logger.debug(s) - case Failure(t) => logger.error("Notifications Failed.", t) - } - } - } - - def send(to: String, subject: String, textMsg: String, htmlMsg: Option[String] = None, loginAccount: Option[Account] = None): Unit = { - val email = new HtmlEmail - email.setHostName(smtp.host) - email.setSmtpPort(smtp.port.get) - smtp.user.foreach { user => - email.setAuthenticator(new DefaultAuthenticator(user, smtp.password.getOrElse(""))) - } - smtp.ssl.foreach { ssl => - email.setSSLOnConnect(ssl) - if(ssl == true) { - email.setSslSmtpPort(smtp.port.get.toString) - } - } - smtp.starttls.foreach { starttls => - email.setStartTLSEnabled(starttls) - email.setStartTLSRequired(starttls) - } - smtp.fromAddress - .map (_ -> smtp.fromName.getOrElse(loginAccount.map(_.userName).getOrElse("GitBucket"))) - .orElse (Some("notifications@gitbucket.com" -> loginAccount.map(_.userName).getOrElse("GitBucket"))) - .foreach { case (address, name) => - email.setFrom(address, name) - } - email.setCharset("UTF-8") - email.setSubject(subject) - email.setTextMsg(textMsg) - htmlMsg.foreach { msg => - email.setHtmlMsg(msg) - } - - email.addTo(to).send - } - -} -class MockMailer extends Notifier { - def toNotify(subject: String, textMsg: String, htmlMsg: Option[String] = None) - (recipients: Account => Session => Seq[String])(implicit context: Context): Unit = () -}