-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathNotificationsService.scala
96 lines (84 loc) · 3.85 KB
/
NotificationsService.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package gitbucket.notifications.service
import gitbucket.core.model.{Account, Issue}
import gitbucket.core.service.{AccountService, IssuesService, RepositoryService}
import gitbucket.notifications.model._, Profile._
import profile.blockingApi._
trait NotificationsService {
self: RepositoryService with AccountService with IssuesService =>
def getWatch(owner: String, repository: String, userName: String)(implicit s: Session): Option[Watch] = {
Watches
.filter(t => t.userName === owner.bind && t.repositoryName === repository.bind && t.notificationUserName === userName.bind)
.firstOption
}
def updateWatch(owner: String, repository: String, userName: String, notification: Watch.Notification)(implicit s: Session): Unit = {
Watches
.filter(t => t.userName === owner.bind && t.repositoryName === repository.bind && t.notificationUserName === userName.bind)
.delete
Watches insert Watch(
userName = owner,
repositoryName = repository,
notificationUserName = userName,
notification = notification
)
}
def updateIssueNotification(owner: String, repository: String, issueId: Int, userName: String, subscribed: Boolean)(implicit s: Session): Unit = {
IssueNotifications
.filter { t =>
t.userName === owner.bind && t.repositoryName === repository.bind &&
t.issueId === issueId.bind && t.notificationUserName === userName.bind
}
.delete
IssueNotifications insert IssueNotification(
userName = owner,
repositoryName = repository,
issueId = issueId,
notificationUserName = userName,
subscribed = subscribed
)
}
def isDisableEmailNotification(account: Account)(implicit s: Session): Boolean = {
NotificationsAccounts.filter(_.userName === account.userName.bind).firstOption.exists(_.disableEmail)
}
def updateEmailNotification(userName: String, disable: Boolean)(implicit s: Session): Unit = {
NotificationsAccounts.filter(_.userName === userName.bind).delete
if (disable) NotificationsAccounts insert NotificationsAccount(userName = userName, disableEmail = true)
}
def autoSubscribeUsersForRepository(owner: String, repository: String)(implicit s: Session): List[String] = {
// individual repository's owner
owner ::
// group members of group repository
getGroupMembers(owner).map(_.userName) :::
// collaborators
getCollaboratorUserNames(owner, repository)
}
def getNotificationUsers(issue: Issue)(implicit s: Session): List[String] = {
val watches = Watches.filter(t =>
t.userName === issue.userName.bind && t.repositoryName === issue.repositoryName.bind
).list
val notifications = IssueNotifications.filter(t =>
t.userName === issue.userName.bind && t.repositoryName === issue.repositoryName.bind && t.issueId === issue.issueId.bind
).list
(
Seq(
// auto-subscribe users for repository
autoSubscribeUsersForRepository(issue.userName, issue.repositoryName) :::
// watching users
watches.withFilter(_.notification == Watch.Watching).map(_.notificationUserName),
// participants
issue.openedUserName ::
getComments(issue.userName, issue.repositoryName, issue.issueId).map(_.commentedUserName),
// subscribers
notifications.withFilter(_.subscribed).map(_.notificationUserName)
) zip Seq(
// not watching users
watches.withFilter(_.notification == Watch.NotWatching).map(_.notificationUserName),
// ignoring users
watches.withFilter(_.notification == Watch.Ignoring).map(_.notificationUserName),
// unsubscribers
notifications.withFilter(!_.subscribed).map(_.notificationUserName)
)
).foldLeft[List[String]](Nil){ case (res, (add, remove)) =>
(add ++ res).distinct diff remove
}
}
}