1
+ package fr .brouillard .gitbucket .announce .controller
2
+
3
+ import fr .brouillard .gitbucket .announce .html
4
+ import gitbucket .core .controller .ControllerBase
5
+ import gitbucket .core .service .{AccountService , SystemSettingsService }
6
+ import gitbucket .core .servlet .Database
7
+ import gitbucket .core .util .AdminAuthenticator
8
+ import jp .sf .amateras .scalatra .forms ._
9
+ import org .apache .commons .mail .{DefaultAuthenticator , HtmlEmail }
10
+ import org .pegdown .PegDownProcessor
11
+ import org .slf4j .LoggerFactory
12
+
13
+ class AnnounceController extends AnnounceControllerBase
14
+ with AdminAuthenticator
15
+
16
+ trait AnnounceControllerBase extends ControllerBase with AccountService {
17
+ self : AdminAuthenticator =>
18
+
19
+ private val logger = LoggerFactory .getLogger(classOf [AnnounceController ])
20
+
21
+ case class AnnounceForm (content : String , subject : String )
22
+
23
+ private val announceForm = mapping(
24
+ " content" -> trim(label(" Announce" , text(required))),
25
+ " subject" -> trim(label(" Subject" , text(required)))
26
+ )(AnnounceForm .apply)
27
+
28
+ get(" /admin/announce" )(adminOnly {
29
+ html.announce(flash.get(" info" ))
30
+ })
31
+
32
+ post(" /admin/announce" , announceForm)(adminOnly { form =>
33
+ flash += " info" -> " Announce has been sent."
34
+
35
+ if (logger.isDebugEnabled) {
36
+ logger.debug(" sending announce: {}" , form.content)
37
+ }
38
+
39
+ val systemSettings = new SystemSettingsService {}.loadSystemSettings
40
+ if (systemSettings.notification && systemSettings.smtp.nonEmpty) {
41
+ val email = new HtmlEmail
42
+ val smtp = systemSettings.smtp.get
43
+
44
+ email.setHostName(smtp.host)
45
+ email.setSmtpPort(smtp.port.get)
46
+ smtp.user.foreach { user =>
47
+ email.setAuthenticator(new DefaultAuthenticator (user, smtp.password.getOrElse(" " )))
48
+ }
49
+ smtp.ssl.foreach { ssl =>
50
+ email.setSSLOnConnect(ssl)
51
+ }
52
+ smtp.fromAddress
53
+ .map (_ -> smtp.fromName.orNull)
54
+ .orElse (
Some (
" [email protected] " -> context.loginAccount.get.userName))
55
+ .foreach { case (address, name) =>
56
+ email.setFrom(address, name)
57
+ }
58
+ email.setCharset(" UTF-8" )
59
+ email.setSubject(form.subject)
60
+
61
+ email.setHtmlMsg(new PegDownProcessor ().markdownToHtml(form.content))
62
+
63
+ logger.info(" sending email: {}" , form.content)
64
+ val database = Database ()
65
+ database withSession { implicit session =>
66
+ getAllUsers(false ).filter(account => ! account.isGroupAccount && account.mailAddress.nonEmpty).foreach(account => email.addBcc(account.mailAddress))
67
+ }
68
+
69
+ email.send()
70
+ }
71
+
72
+ redirect(" /admin/announce" )
73
+ })
74
+ }
0 commit comments