@@ -2,12 +2,18 @@ package gitbucket.notifications.service
22
33import gitbucket .core .controller .Context
44import gitbucket .core .model .{Account , Issue }
5- import gitbucket .core .service ._ , RepositoryService .RepositoryInfo
6- import gitbucket .core .util .{LDAPUtil , Notifier }
5+ import gitbucket .core .service ._
6+ import RepositoryService .RepositoryInfo
7+ import gitbucket .core .util .{LDAPUtil , Mailer }
78import gitbucket .core .view .Markdown
89import gitbucket .notifications .model .Profile ._
10+ import org .slf4j .LoggerFactory
911import profile .blockingApi ._
1012
13+ import scala .concurrent .Future
14+ import scala .concurrent .ExecutionContext .Implicits .global
15+ import scala .util .{Success , Failure }
16+
1117
1218class AccountHook extends gitbucket.core.plugin.AccountHook {
1319
@@ -52,46 +58,51 @@ class IssueHook extends gitbucket.core.plugin.IssueHook
5258 with AccountService
5359 with IssuesService {
5460
55- override def created (issue : Issue , r : RepositoryInfo )(implicit context : Context ): Unit = {
61+ private val logger = LoggerFactory .getLogger(classOf [IssueHook ])
62+
63+ override def created (issue : Issue , r : RepositoryInfo )(implicit session : Session , context : Context ): Unit = {
5664 val markdown =
5765 s """ | ${issue.content getOrElse " " }
5866 |
5967 |----
6068 |[View it on GitBucket]( ${s " ${context.baseUrl}/ ${r.owner}/ ${r.name}/issues/ ${issue.issueId}" })
6169 | """ .stripMargin
6270
63- Notifier ().toNotify(subject( issue, r), markdown, Some (toHtml(markdown , r)))(recipients(issue) )
71+ sendAsync( issue, r, subject(issue , r), markdown )
6472 }
6573
66- override def addedComment (commentId : Int , content : String , issue : Issue , r : RepositoryInfo )(implicit context : Context ): Unit = {
74+ override def addedComment (commentId : Int , content : String , issue : Issue , r : RepositoryInfo )
75+ (implicit session : Session , context : Context ): Unit = {
6776 val markdown =
6877 s """ | ${content}
6978 |
7079 |----
7180 |[View it on GitBucket]( ${s " ${context.baseUrl}/ ${r.owner}/ ${r.name}/issues/ ${issue.issueId}#comment- $commentId" })
7281 | """ .stripMargin
7382
74- Notifier ().toNotify(subject( issue, r), markdown, Some (toHtml(markdown , r)))(recipients(issue) )
83+ sendAsync( issue, r, subject(issue , r), markdown )
7584 }
7685
77- override def closed (issue : Issue , r : RepositoryInfo )(implicit context : Context ): Unit = {
86+ override def closed (issue : Issue , r : RepositoryInfo )(implicit session : Session , context : Context ): Unit = {
7887 val markdown =
7988 s """ |close #[ ${issue.issueId}]( ${s " ${context.baseUrl}/ ${r.owner}/ ${r.name}/issues/ ${issue.issueId}" })
8089 | """ .stripMargin
8190
82- Notifier ().toNotify(subject( issue, r), markdown, Some (toHtml(markdown , r)))(recipients(issue) )
91+ sendAsync( issue, r, subject(issue , r), markdown )
8392 }
8493
85- override def reopened (issue : Issue , r : RepositoryInfo )(implicit context : Context ): Unit = {
94+ override def reopened (issue : Issue , r : RepositoryInfo )(implicit session : Session , context : Context ): Unit = {
8695 val markdown =
8796 s """ |reopen #[ ${issue.issueId}]( ${s " ${context.baseUrl}/ ${r.owner}/ ${r.name}/issues/ ${issue.issueId}" })
8897 | """ .stripMargin
8998
90- Notifier ().toNotify(subject( issue, r), markdown, Some (toHtml(markdown , r)))(recipients(issue) )
99+ sendAsync( issue, r, subject(issue , r), markdown )
91100 }
92101
93102
94- protected def subject (issue : Issue , r : RepositoryInfo ): String = s " [ ${r.owner}/ ${r.name}] ${issue.title} (# ${issue.issueId}) "
103+ protected def subject (issue : Issue , r : RepositoryInfo ): String = {
104+ s " [ ${r.owner}/ ${r.name}] ${issue.title} (# ${issue.issueId}) "
105+ }
95106
96107 protected def toHtml (markdown : String , r : RepositoryInfo )(implicit context : Context ): String =
97108 Markdown .toHtml(
@@ -103,23 +114,38 @@ class IssueHook extends gitbucket.core.plugin.IssueHook
103114 enableLineBreaks = false
104115 )
105116
106- protected val recipients : Issue => Account => Session => Seq [String ] = {
107- issue => loginAccount => implicit session =>
108- getNotificationUsers(issue)
109- .withFilter ( _ != loginAccount.userName ) // the operation in person is excluded
110- .flatMap (
111- getAccountByUserName(_)
112- .filterNot (_.isGroupAccount)
113- .filterNot (LDAPUtil .isDummyMailAddress)
114- .map (_.mailAddress)
115- )
117+ protected def sendAsync (issue : Issue , repository : RepositoryInfo , subject : String , markdown : String )
118+ (implicit session : Session , context : Context ): Unit = {
119+ val recipients = getRecipients(issue, context.loginAccount.get)
120+ val mailer = new Mailer (context.settings)
121+ val f = Future {
122+ recipients.foreach { address =>
123+ mailer.send(address, subject, markdown, Some (toHtml(markdown, repository)), context.loginAccount)
124+ }
125+ " Notifications Successful."
126+ }
127+ f.onComplete {
128+ case Success (s) => logger.debug(s)
129+ case Failure (t) => logger.error(" Notifications Failed." , t)
130+ }
131+ }
132+
133+ protected def getRecipients (issue : Issue , loginAccount : Account )(implicit session : Session ): Seq [String ] = {
134+ getNotificationUsers(issue)
135+ .withFilter ( _ != loginAccount.userName ) // the operation in person is excluded
136+ .flatMap (
137+ getAccountByUserName(_)
138+ .filterNot (_.isGroupAccount)
139+ .filterNot (LDAPUtil .isDummyMailAddress)
140+ .map (_.mailAddress)
141+ )
116142 }
117143
118144}
119145
120146class PullRequestHook extends IssueHook with gitbucket.core.plugin.PullRequestHook {
121147
122- override def created (issue : Issue , r : RepositoryInfo )(implicit context : Context ): Unit = {
148+ override def created (issue : Issue , r : RepositoryInfo )(implicit session : Session , context : Context ): Unit = {
123149 val markdown =
124150 s """ | ${issue.content getOrElse " " }
125151 |
@@ -128,26 +154,27 @@ class PullRequestHook extends IssueHook with gitbucket.core.plugin.PullRequestHo
128154 | ${context.baseUrl}/ ${r.owner}/ ${r.name}/pull/ ${issue.issueId}
129155 | """ .stripMargin
130156
131- Notifier ().toNotify(subject( issue, r), markdown, Some (toHtml(markdown , r)))(recipients(issue) )
157+ sendAsync( issue, r, subject(issue , r), markdown )
132158 }
133159
134- override def addedComment (commentId : Int , content : String , issue : Issue , r : RepositoryInfo )(implicit context : Context ): Unit = {
160+ override def addedComment (commentId : Int , content : String , issue : Issue , r : RepositoryInfo )
161+ (implicit session : Session , context : Context ): Unit = {
135162 val markdown =
136163 s """ | $content
137164 |
138165 |----
139166 |[View it on GitBucket]( ${s " ${context.baseUrl}/ ${r.owner}/ ${r.name}/pull/ ${issue.issueId}#comment- $commentId" })
140167 | """ .stripMargin
141168
142- Notifier ().toNotify(subject( issue, r), markdown, Some (toHtml(markdown , r)))(recipients(issue) )
169+ sendAsync( issue, r, subject(issue , r), markdown )
143170 }
144171
145- override def merged (issue : Issue , r : RepositoryInfo )(implicit context : Context ): Unit = {
172+ override def merged (issue : Issue , r : RepositoryInfo )(implicit session : Session , context : Context ): Unit = {
146173 val markdown =
147174 s """ |merge #[ ${issue.issueId}]( ${s " ${context.baseUrl}/ ${r.owner}/ ${r.name}/pull/ ${issue.issueId}" })
148175 | """ .stripMargin
149176
150- Notifier ().toNotify(subject( issue, r), markdown, Some (toHtml(markdown , r)))(recipients(issue) )
177+ sendAsync( issue, r, subject(issue , r), markdown )
151178 }
152179
153180}
0 commit comments