Remove context.loginAccount.get in services

This commit is contained in:
Naoki Takezoe
2017-01-08 21:49:05 +09:00
parent ad73e1d529
commit cdeaede8bf
3 changed files with 105 additions and 98 deletions

View File

@@ -164,7 +164,8 @@ trait SystemSettingsControllerBase extends AccountManagementControllerBase {
post("/admin/system/sendmail", sendMailForm)(adminOnly { form => post("/admin/system/sendmail", sendMailForm)(adminOnly { form =>
try { try {
new Mailer(form.smtp).send(form.testAddress, new Mailer(form.smtp).send(form.testAddress,
"Test message from GitBucket", "This is a test message from GitBucket.") "Test message from GitBucket", "This is a test message from GitBucket.",
context.loginAccount.get)
"Test mail has been sent to: " + form.testAddress "Test mail has been sent to: " + form.testAddress

View File

@@ -17,9 +17,9 @@ trait HandleCommentService {
*/ */
def handleComment(issue: Issue, content: Option[String], repository: RepositoryService.RepositoryInfo, actionOpt: Option[String]) def handleComment(issue: Issue, content: Option[String], repository: RepositoryService.RepositoryInfo, actionOpt: Option[String])
(implicit context: Context, s: Session) = { (implicit context: Context, s: Session) = {
context.loginAccount.flatMap { loginAccount =>
defining(repository.owner, repository.name){ case (owner, name) => defining(repository.owner, repository.name){ case (owner, name) =>
val userName = context.loginAccount.get.userName val userName = loginAccount.userName
val (action, recordActivity) = actionOpt val (action, recordActivity) = actionOpt
.collect { .collect {
@@ -49,12 +49,12 @@ trait HandleCommentService {
// extract references and create refer comment // extract references and create refer comment
content.map { content => content.map { content =>
createReferComment(owner, name, issue, content, context.loginAccount.get) createReferComment(owner, name, issue, content, loginAccount)
} }
// call web hooks // call web hooks
action match { action match {
case None => commentId.map { commentIdSome => callIssueCommentWebHook(repository, issue, commentIdSome, context.loginAccount.get) } case None => commentId.map { commentIdSome => callIssueCommentWebHook(repository, issue, commentIdSome, loginAccount) }
case Some(act) => { case Some(act) => {
val webHookAction = act match { val webHookAction = act match {
case "open" => "opened" case "open" => "opened"
@@ -63,9 +63,9 @@ trait HandleCommentService {
case _ => act case _ => act
} }
if (issue.isPullRequest) { if (issue.isPullRequest) {
callPullRequestWebHook(webHookAction, repository, issue.issueId, context.baseUrl, context.loginAccount.get) callPullRequestWebHook(webHookAction, repository, issue.issueId, context.baseUrl, loginAccount)
} else { } else {
callIssuesWebHook(webHookAction, repository, issue, context.baseUrl, context.loginAccount.get) callIssuesWebHook(webHookAction, repository, issue, context.baseUrl, loginAccount)
} }
} }
} }
@@ -89,5 +89,6 @@ trait HandleCommentService {
commentId.map( issue -> _ ) commentId.map( issue -> _ )
} }
} }
}
} }

View File

@@ -1,7 +1,7 @@
package gitbucket.core.util package gitbucket.core.util
import gitbucket.core.model.{Session, Issue} import gitbucket.core.model.{Account, Issue, Session}
import gitbucket.core.service.{RepositoryService, AccountService, IssuesService, SystemSettingsService} import gitbucket.core.service.{AccountService, IssuesService, RepositoryService, SystemSettingsService}
import gitbucket.core.servlet.Database import gitbucket.core.servlet.Database
import gitbucket.core.view.Markdown import gitbucket.core.view.Markdown
@@ -9,16 +9,16 @@ import scala.concurrent._
import ExecutionContext.Implicits.global import ExecutionContext.Implicits.global
import org.apache.commons.mail.{DefaultAuthenticator, HtmlEmail} import org.apache.commons.mail.{DefaultAuthenticator, HtmlEmail}
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
import gitbucket.core.controller.Context import gitbucket.core.controller.Context
import SystemSettingsService.Smtp import SystemSettingsService.Smtp
import ControlUtil.defining import ControlUtil.defining
trait Notifier extends RepositoryService with AccountService with IssuesService { trait Notifier extends RepositoryService with AccountService with IssuesService {
def toNotify(r: RepositoryService.RepositoryInfo, issue: Issue, content: String) def toNotify(r: RepositoryService.RepositoryInfo, issue: Issue, content: String)
(msg: String => String)(implicit context: Context): Unit (msg: String => String)(implicit context: Context): Unit
protected def recipients(issue: Issue)(notify: String => Unit)(implicit session: Session, context: Context) = protected def recipients(issue: Issue, loginAccount: Account)(notify: String => Unit)(implicit session: Session) =
( (
// individual repository's owner // individual repository's owner
issue.userName :: issue.userName ::
@@ -31,9 +31,13 @@ trait Notifier extends RepositoryService with AccountService with IssuesService
getComments(issue.userName, issue.repositoryName, issue.issueId).map(_.commentedUserName) getComments(issue.userName, issue.repositoryName, issue.issueId).map(_.commentedUserName)
) )
.distinct .distinct
.withFilter ( _ != context.loginAccount.get.userName ) // the operation in person is excluded .withFilter ( _ != loginAccount.userName ) // the operation in person is excluded
.foreach ( getAccountByUserName(_) filterNot (_.isGroupAccount) filterNot (LDAPUtil.isDummyMailAddress(_)) foreach (x => notify(x.mailAddress)) ) .foreach (
getAccountByUserName(_)
.filterNot (_.isGroupAccount)
.filterNot (LDAPUtil.isDummyMailAddress(_))
.foreach (x => notify(x.mailAddress))
)
} }
object Notifier { object Notifier {
@@ -70,7 +74,8 @@ class Mailer(private val smtp: Smtp) extends Notifier {
private val logger = LoggerFactory.getLogger(classOf[Mailer]) private val logger = LoggerFactory.getLogger(classOf[Mailer])
def toNotify(r: RepositoryService.RepositoryInfo, issue: Issue, content: String) def toNotify(r: RepositoryService.RepositoryInfo, issue: Issue, content: String)
(msg: String => String)(implicit context: Context) = { (msg: String => String)(implicit context: Context): Unit = {
context.loginAccount.foreach { loginAccount =>
val database = Database() val database = Database()
val f = Future { val f = Future {
@@ -84,10 +89,9 @@ class Mailer(private val smtp: Smtp) extends Notifier {
enableRefsLink = true, enableRefsLink = true,
enableAnchor = false, enableAnchor = false,
enableLineBreaks = false enableLineBreaks = false
))) { case (subject, msg) => ))
recipients(issue) { to => ) { case (subject, msg) =>
send(to, subject, msg) recipients(issue, loginAccount) { to => send(to, subject, msg, loginAccount) }
}
} }
} }
"Notifications Successful." "Notifications Successful."
@@ -99,8 +103,9 @@ class Mailer(private val smtp: Smtp) extends Notifier {
case t => logger.error("Notifications Failed.", t) case t => logger.error("Notifications Failed.", t)
} }
} }
}
def send(to: String, subject: String, msg: String)(implicit context: Context): Unit = { def send(to: String, subject: String, msg: String, loginAccount: Account): Unit = {
val email = new HtmlEmail val email = new HtmlEmail
email.setHostName(smtp.host) email.setHostName(smtp.host)
email.setSmtpPort(smtp.port.get) email.setSmtpPort(smtp.port.get)
@@ -114,8 +119,8 @@ class Mailer(private val smtp: Smtp) extends Notifier {
} }
} }
smtp.fromAddress smtp.fromAddress
.map (_ -> smtp.fromName.getOrElse(context.loginAccount.get.userName)) .map (_ -> smtp.fromName.getOrElse(loginAccount.userName))
.orElse (Some("notifications@gitbucket.com" -> context.loginAccount.get.userName)) .orElse (Some("notifications@gitbucket.com" -> loginAccount.userName))
.foreach { case (address, name) => .foreach { case (address, name) =>
email.setFrom(address, name) email.setFrom(address, name)
} }