@@ -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,106 +58,124 @@ 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 = {
56- Notifier ().toNotify(
57- subject(issue, r),
58- message(issue.content getOrElse " " , r)(content => s """
59- | $content<br/>
60- |--<br/>
61- |<a href=" ${s " ${context.baseUrl}/ ${r.owner}/ ${r.name}/issues/ ${issue.issueId}" }">View it on GitBucket</a>
62- """ .stripMargin)
63- )(recipients(issue))
61+ private val logger = LoggerFactory .getLogger(classOf [IssueHook ])
62+
63+ override def created (issue : Issue , r : RepositoryInfo )(implicit session : Session , context : Context ): Unit = {
64+ val markdown =
65+ s """ | ${issue.content getOrElse " " }
66+ |
67+ |----
68+ |[View it on GitBucket]( ${s " ${context.baseUrl}/ ${r.owner}/ ${r.name}/issues/ ${issue.issueId}" })
69+ | """ .stripMargin
70+
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 = {
67- Notifier ().toNotify(
68- subject(issue, r),
69- message(content, r)(content => s """
70- | $content<br/>
71- |--<br/>
72- |<a href=" ${s " ${context.baseUrl}/ ${r.owner}/ ${r.name}/issues/ ${issue.issueId}#comment- $commentId" }">View it on GitBucket</a>
73- """ .stripMargin)
74- )(recipients(issue))
74+ override def addedComment (commentId : Int , content : String , issue : Issue , r : RepositoryInfo )
75+ (implicit session : Session , context : Context ): Unit = {
76+ val markdown =
77+ s """ | ${content}
78+ |
79+ |----
80+ |[View it on GitBucket]( ${s " ${context.baseUrl}/ ${r.owner}/ ${r.name}/issues/ ${issue.issueId}#comment- $commentId" })
81+ | """ .stripMargin
82+
83+ sendAsync(issue, r, subject(issue, r), markdown)
7584 }
7685
77- override def closed (issue : Issue , r : RepositoryInfo )(implicit context : Context ): Unit = {
78- Notifier ().toNotify(
79- subject(issue, r),
80- message(" close" , r)(content => s """
81- | $content <a href=" ${s " ${context.baseUrl}/ ${r.owner}/ ${r.name}/issues/ ${issue.issueId}" }"># ${issue.issueId}</a>
82- """ .stripMargin)
83- )(recipients(issue))
86+ override def closed (issue : Issue , r : RepositoryInfo )(implicit session : Session , context : Context ): Unit = {
87+ val markdown =
88+ s """ |close #[ ${issue.issueId}]( ${s " ${context.baseUrl}/ ${r.owner}/ ${r.name}/issues/ ${issue.issueId}" })
89+ | """ .stripMargin
90+
91+ sendAsync(issue, r, subject(issue, r), markdown)
8492 }
8593
86- override def reopened (issue : Issue , r : RepositoryInfo )(implicit context : Context ): Unit = {
87- Notifier ().toNotify(
88- subject(issue, r),
89- message(" reopen" , r)(content => s """
90- | $content <a href=" ${s " ${context.baseUrl}/ ${r.owner}/ ${r.name}/issues/ ${issue.issueId}" }"># ${issue.issueId}</a>
91- """ .stripMargin)
92- )(recipients(issue))
94+ override def reopened (issue : Issue , r : RepositoryInfo )(implicit session : Session , context : Context ): Unit = {
95+ val markdown =
96+ s """ |reopen #[ ${issue.issueId}]( ${s " ${context.baseUrl}/ ${r.owner}/ ${r.name}/issues/ ${issue.issueId}" })
97+ | """ .stripMargin
98+
99+ sendAsync(issue, r, subject(issue, r), markdown)
93100 }
94101
95102
96- 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+ }
97106
98- protected def message ( content : String , r : RepositoryInfo )( msg : String => String )(implicit context : Context ): String =
99- msg( Markdown .toHtml(
100- markdown = content ,
107+ protected def toHtml ( markdown : String , r : RepositoryInfo )(implicit context : Context ): String =
108+ Markdown .toHtml(
109+ markdown = markdown ,
101110 repository = r,
102111 enableWikiLink = false ,
103112 enableRefsLink = true ,
104113 enableAnchor = false ,
105114 enableLineBreaks = false
106- ))
107-
108- protected val recipients : Issue => Account => Session => Seq [String ] = {
109- issue => loginAccount => implicit session =>
110- getNotificationUsers(issue)
111- .withFilter ( _ != loginAccount.userName ) // the operation in person is excluded
112- .flatMap (
113- getAccountByUserName(_)
114- .filterNot (_.isGroupAccount)
115- .filterNot (LDAPUtil .isDummyMailAddress)
116- .filterNot (isDisableEmailNotification)
117- .map (_.mailAddress)
118- )
115+ )
116+
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+ .filterNot (isDisableEmailNotification)
141+ .map (_.mailAddress)
142+ )
119143 }
120144
121145}
122146
123147class PullRequestHook extends IssueHook with gitbucket.core.plugin.PullRequestHook {
124148
125- override def created (issue : Issue , r : RepositoryInfo )(implicit context : Context ): Unit = {
126- val url = s " ${context.baseUrl} / ${r.owner} / ${r.name} /pull/ ${issue.issueId} "
127- Notifier ().toNotify(
128- subject(issue, r),
129- message(issue.content getOrElse " " , r)(content => s """
130- | $content <hr/>
131- |View, comment on, or merge it at:<br/>
132- |<a href=" $url "> $url </a>
133- """ .stripMargin)
134- )(recipients (issue) )
149+ override def created (issue : Issue , r : RepositoryInfo )(implicit session : Session , context : Context ): Unit = {
150+ val markdown =
151+ s """ | ${issue.content getOrElse " " }
152+ |
153+ |----
154+ |View, comment on, or merge it at:
155+ | ${context.baseUrl} / ${r.owner} / ${r.name} /pull/ ${issue.issueId}
156+ | """ .stripMargin
157+
158+ sendAsync(issue, r, subject (issue, r), markdown )
135159 }
136160
137- override def addedComment (commentId : Int , content : String , issue : Issue , r : RepositoryInfo )(implicit context : Context ): Unit = {
138- Notifier ().toNotify(
139- subject(issue, r),
140- message(content, r)(content => s """
141- | $content<br/>
142- |--<br/>
143- |<a href=" ${s " ${context.baseUrl}/ ${r.owner}/ ${r.name}/pull/ ${issue.issueId}#comment- $commentId" }">View it on GitBucket</a>
144- """ .stripMargin)
145- )(recipients(issue))
161+ override def addedComment (commentId : Int , content : String , issue : Issue , r : RepositoryInfo )
162+ (implicit session : Session , context : Context ): Unit = {
163+ val markdown =
164+ s """ | $content
165+ |
166+ |----
167+ |[View it on GitBucket]( ${s " ${context.baseUrl}/ ${r.owner}/ ${r.name}/pull/ ${issue.issueId}#comment- $commentId" })
168+ | """ .stripMargin
169+
170+ sendAsync(issue, r, subject(issue, r), markdown)
146171 }
147172
148- override def merged (issue : Issue , r : RepositoryInfo )(implicit context : Context ): Unit = {
149- Notifier ().toNotify(
150- subject(issue, r),
151- message(" merge" , r)(content => s """
152- | $content <a href=" ${s " ${context.baseUrl}/ ${r.owner}/ ${r.name}/pull/ ${issue.issueId}" }"># ${issue.issueId}</a>
153- """ .stripMargin)
154- )(recipients(issue))
173+ override def merged (issue : Issue , r : RepositoryInfo )(implicit session : Session , context : Context ): Unit = {
174+ val markdown =
175+ s """ |merge #[ ${issue.issueId}]( ${s " ${context.baseUrl}/ ${r.owner}/ ${r.name}/pull/ ${issue.issueId}" })
176+ | """ .stripMargin
177+
178+ sendAsync(issue, r, subject(issue, r), markdown)
155179 }
156180
157181}
0 commit comments