Skip to content

Commit 8414050

Browse files
authored
Merge pull request #7 from gitbucket/disable-email
Disable email notification
2 parents 42c8fd4 + 624bdcd commit 8414050

File tree

10 files changed

+109
-8
lines changed

10 files changed

+109
-8
lines changed

build.sbt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
val Organization = "io.github.gitbucket"
22
val ProjectName = "gitbucket-notifications-plugin"
3-
val ProjectVersion = "1.2.0"
3+
val ProjectVersion = "1.3.0"
44
val GitBucketVersion = Option(System.getProperty("gitbucket.version")).getOrElse("4.17.0")
55

66
name := ProjectName
77
organization := Organization
88
version := ProjectVersion
9-
scalaVersion := "2.12.2"
9+
scalaVersion := "2.12.3"
1010

1111
lazy val root = (project in file(".")).enablePlugins(SbtTwirl)
1212

project/build.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
sbt.version=0.13.15
1+
sbt.version=0.13.16
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<changeSet>
3+
<!--================================================================================================-->
4+
<!-- NOTIFICATIONS_ACCOUNT -->
5+
<!--================================================================================================-->
6+
<createTable tableName="NOTIFICATIONS_ACCOUNT">
7+
<column name="USER_NAME" type="varchar(100)" nullable="false"/>
8+
<column name="DISABLE_EMAIL" type="boolean" nullable="false"/>
9+
</createTable>
10+
11+
<addPrimaryKey constraintName="IDX_NOTIFICATIONS_ACCOUNT_PK" tableName="NOTIFICATIONS_ACCOUNT" columnNames="USER_NAME"/>
12+
<addForeignKeyConstraint constraintName="IDX_NOTIFICATIONS_ACCOUNT_FK0" baseTableName="NOTIFICATIONS_ACCOUNT" baseColumnNames="USER_NAME" referencedTableName="ACCOUNT" referencedColumnNames="USER_NAME"/>
13+
14+
</changeSet>

src/main/scala/Plugin.scala

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import gitbucket.core.controller.Context
22
import gitbucket.core.model.Issue
3+
import gitbucket.core.plugin.Link
34
import gitbucket.core.service.RepositoryService.RepositoryInfo
45
import gitbucket.core.util.Implicits.request2Session
56
import gitbucket.notifications._
@@ -22,7 +23,10 @@ class Plugin extends gitbucket.core.plugin.Plugin {
2223
new Version("1.1.0",
2324
new LiquibaseMigration("update/gitbucket-notifications_1.1.xml")
2425
),
25-
new Version("1.2.0")
26+
new Version("1.2.0"),
27+
new Version("1.3.0",
28+
new LiquibaseMigration("update/gitbucket-notifications_1.3.xml")
29+
)
2630
)
2731

2832
override val controllers = Seq(
@@ -62,4 +66,15 @@ class Plugin extends gitbucket.core.plugin.Plugin {
6266
}
6367
)
6468

69+
override val accountSettingMenus = Seq(
70+
(context: Context) =>
71+
context.loginAccount map { account =>
72+
Link(
73+
id = "notifications",
74+
label = "Notifications",
75+
path = s"${account.userName}/_notifications"
76+
)
77+
}
78+
)
79+
6580
}

src/main/scala/gitbucket/notifications/controller/NotificationsController.scala

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@ package gitbucket.notifications.controller
33
import gitbucket.core.controller.ControllerBase
44
import gitbucket.core.service.{AccountService, IssuesService, RepositoryService}
55
import gitbucket.core.util.Implicits._
6-
import gitbucket.core.util.ReadableUsersAuthenticator
6+
import gitbucket.core.util.{OneselfAuthenticator, ReadableUsersAuthenticator}
77
import gitbucket.core.util.SyntaxSugars._
88
import gitbucket.notifications.model.Watch
99
import gitbucket.notifications.service.NotificationsService
1010
import org.scalatra.Ok
1111

1212
class NotificationsController extends NotificationsControllerBase
13-
with NotificationsService with RepositoryService with AccountService with IssuesService with ReadableUsersAuthenticator
13+
with NotificationsService with RepositoryService with AccountService with IssuesService
14+
with ReadableUsersAuthenticator with OneselfAuthenticator
1415

1516
trait NotificationsControllerBase extends ControllerBase {
16-
self: NotificationsService with RepositoryService with AccountService with IssuesService with ReadableUsersAuthenticator =>
17+
self: NotificationsService with RepositoryService with AccountService with IssuesService
18+
with ReadableUsersAuthenticator with OneselfAuthenticator =>
1719

1820
ajaxPost("/:owner/:repository/watch")(readableUsersOnly { repository =>
1921
params.get("notification").flatMap(Watch.Notification.valueOf).map { notification =>
@@ -33,4 +35,19 @@ trait NotificationsControllerBase extends ControllerBase {
3335
}
3436
})
3537

38+
get("/:userName/_notifications")(oneselfOnly {
39+
val userName = params("userName")
40+
getAccountByUserName(userName).map { account =>
41+
gitbucket.notifications.html.settings(account, isDisableEmailNotification(account))
42+
} getOrElse NotFound()
43+
})
44+
45+
ajaxPost("/:userName/_notifications")(oneselfOnly {
46+
val userName = params("userName")
47+
params.getAs[Boolean]("disable").map { disable =>
48+
updateEmailNotification(userName, disable)
49+
Ok()
50+
} getOrElse NotFound()
51+
})
52+
3653
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package gitbucket.notifications.model
2+
3+
trait NotificationsAccountComponent { self: gitbucket.core.model.Profile =>
4+
import profile.api._
5+
6+
lazy val NotificationsAccounts = TableQuery[NotificationsAccounts]
7+
8+
class NotificationsAccounts(tag: Tag) extends Table[NotificationsAccount](tag, "NOTIFICATIONS_ACCOUNT") {
9+
val userName = column[String]("USER_NAME")
10+
val disableEmail = column[Boolean]("DISABLE_EMAIL")
11+
def * = (userName, disableEmail) <> (NotificationsAccount.tupled, NotificationsAccount.unapply)
12+
}
13+
}
14+
15+
case class NotificationsAccount(
16+
userName: String,
17+
disableEmail: Boolean
18+
)

src/main/scala/gitbucket/notifications/model/Profile.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ import gitbucket.core.model._
55
object Profile extends CoreProfile
66
with IssueNotificationComponent
77
with WatchComponent
8+
with NotificationsAccountComponent

src/main/scala/gitbucket/notifications/service/NotificationsHook.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ class IssueHook extends gitbucket.core.plugin.IssueHook
137137
getAccountByUserName(_)
138138
.filterNot (_.isGroupAccount)
139139
.filterNot (LDAPUtil.isDummyMailAddress)
140+
.filterNot (isDisableEmailNotification)
140141
.map (_.mailAddress)
141142
)
142143
}

src/main/scala/gitbucket/notifications/service/NotificationsService.scala

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package gitbucket.notifications.service
22

3-
import gitbucket.core.model.Issue
3+
import gitbucket.core.model.{Account, Issue}
44
import gitbucket.core.service.{AccountService, IssuesService, RepositoryService}
55
import gitbucket.notifications.model._, Profile._
66
import profile.blockingApi._
@@ -42,6 +42,15 @@ trait NotificationsService {
4242
)
4343
}
4444

45+
def isDisableEmailNotification(account: Account)(implicit s: Session): Boolean = {
46+
NotificationsAccounts.filter(_.userName === account.userName.bind).firstOption.exists(_.disableEmail)
47+
}
48+
49+
def updateEmailNotification(userName: String, disable: Boolean)(implicit s: Session): Unit = {
50+
NotificationsAccounts.filter(_.userName === userName.bind).delete
51+
if (disable) NotificationsAccounts insert NotificationsAccount(userName = userName, disableEmail = true)
52+
}
53+
4554
def autoSubscribeUsersForRepository(owner: String, repository: String)(implicit s: Session): List[String] = {
4655
// individual repository's owner
4756
owner ::
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
@(account: gitbucket.core.model.Account,
2+
disableEmail: Boolean)(implicit context: gitbucket.core.controller.Context)
3+
4+
@gitbucket.core.html.main("Notifications"){
5+
@gitbucket.core.account.html.menu("notifications", account.userName, account.isGroupAccount){
6+
<div class="panel panel-default">
7+
<div class="panel-heading strong">Email notification preferences</div>
8+
<div class="panel-body">
9+
<fieldset class="form-group">
10+
<p class="muted">If checked, never be notified of anything.</p>
11+
<label for="disable">
12+
<input type="checkbox" name="disable" id="disable" value="true" @if(disableEmail){checked}/>
13+
Disable
14+
</label>
15+
</fieldset>
16+
</div>
17+
</div>
18+
}
19+
}
20+
<script>
21+
$(function(){
22+
$('#disable').click(function(){
23+
$.post('@context.path/@account.userName/_notifications', { 'disable': this.checked });
24+
});
25+
});
26+
</script>

0 commit comments

Comments
 (0)