@@ -2,12 +2,18 @@ package gitbucket.notifications.service
2
2
3
3
import gitbucket .core .controller .Context
4
4
import 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 }
7
8
import gitbucket .core .view .Markdown
8
9
import gitbucket .notifications .model .Profile ._
10
+ import org .slf4j .LoggerFactory
9
11
import profile .blockingApi ._
10
12
13
+ import scala .concurrent .Future
14
+ import scala .concurrent .ExecutionContext .Implicits .global
15
+ import scala .util .{Success , Failure }
16
+
11
17
12
18
class AccountHook extends gitbucket.core.plugin.AccountHook {
13
19
@@ -52,46 +58,51 @@ class IssueHook extends gitbucket.core.plugin.IssueHook
52
58
with AccountService
53
59
with IssuesService {
54
60
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 = {
56
64
val markdown =
57
65
s """ | ${issue.content getOrElse " " }
58
66
|
59
67
|----
60
68
|[View it on GitBucket]( ${s " ${context.baseUrl}/ ${r.owner}/ ${r.name}/issues/ ${issue.issueId}" })
61
69
| """ .stripMargin
62
70
63
- Notifier ().toNotify(subject( issue, r), markdown, Some (toHtml(markdown , r)))(recipients(issue) )
71
+ sendAsync( issue, r, subject(issue , r), markdown )
64
72
}
65
73
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 = {
67
76
val markdown =
68
77
s """ | ${content}
69
78
|
70
79
|----
71
80
|[View it on GitBucket]( ${s " ${context.baseUrl}/ ${r.owner}/ ${r.name}/issues/ ${issue.issueId}#comment- $commentId" })
72
81
| """ .stripMargin
73
82
74
- Notifier ().toNotify(subject( issue, r), markdown, Some (toHtml(markdown , r)))(recipients(issue) )
83
+ sendAsync( issue, r, subject(issue , r), markdown )
75
84
}
76
85
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 = {
78
87
val markdown =
79
88
s """ |close #[ ${issue.issueId}]( ${s " ${context.baseUrl}/ ${r.owner}/ ${r.name}/issues/ ${issue.issueId}" })
80
89
| """ .stripMargin
81
90
82
- Notifier ().toNotify(subject( issue, r), markdown, Some (toHtml(markdown , r)))(recipients(issue) )
91
+ sendAsync( issue, r, subject(issue , r), markdown )
83
92
}
84
93
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 = {
86
95
val markdown =
87
96
s """ |reopen #[ ${issue.issueId}]( ${s " ${context.baseUrl}/ ${r.owner}/ ${r.name}/issues/ ${issue.issueId}" })
88
97
| """ .stripMargin
89
98
90
- Notifier ().toNotify(subject( issue, r), markdown, Some (toHtml(markdown , r)))(recipients(issue) )
99
+ sendAsync( issue, r, subject(issue , r), markdown )
91
100
}
92
101
93
102
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
+ }
95
106
96
107
protected def toHtml (markdown : String , r : RepositoryInfo )(implicit context : Context ): String =
97
108
Markdown .toHtml(
@@ -103,23 +114,38 @@ class IssueHook extends gitbucket.core.plugin.IssueHook
103
114
enableLineBreaks = false
104
115
)
105
116
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
+ )
116
142
}
117
143
118
144
}
119
145
120
146
class PullRequestHook extends IssueHook with gitbucket.core.plugin.PullRequestHook {
121
147
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 = {
123
149
val markdown =
124
150
s """ | ${issue.content getOrElse " " }
125
151
|
@@ -128,26 +154,27 @@ class PullRequestHook extends IssueHook with gitbucket.core.plugin.PullRequestHo
128
154
| ${context.baseUrl}/ ${r.owner}/ ${r.name}/pull/ ${issue.issueId}
129
155
| """ .stripMargin
130
156
131
- Notifier ().toNotify(subject( issue, r), markdown, Some (toHtml(markdown , r)))(recipients(issue) )
157
+ sendAsync( issue, r, subject(issue , r), markdown )
132
158
}
133
159
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 = {
135
162
val markdown =
136
163
s """ | $content
137
164
|
138
165
|----
139
166
|[View it on GitBucket]( ${s " ${context.baseUrl}/ ${r.owner}/ ${r.name}/pull/ ${issue.issueId}#comment- $commentId" })
140
167
| """ .stripMargin
141
168
142
- Notifier ().toNotify(subject( issue, r), markdown, Some (toHtml(markdown , r)))(recipients(issue) )
169
+ sendAsync( issue, r, subject(issue , r), markdown )
143
170
}
144
171
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 = {
146
173
val markdown =
147
174
s """ |merge #[ ${issue.issueId}]( ${s " ${context.baseUrl}/ ${r.owner}/ ${r.name}/pull/ ${issue.issueId}" })
148
175
| """ .stripMargin
149
176
150
- Notifier ().toNotify(subject( issue, r), markdown, Some (toHtml(markdown , r)))(recipients(issue) )
177
+ sendAsync( issue, r, subject(issue , r), markdown )
151
178
}
152
179
153
180
}
0 commit comments