|
| 1 | +package gitbucket.notifications.service |
| 2 | + |
| 3 | +import gitbucket.core.controller.Context |
| 4 | +import gitbucket.core.model.{Account, Issue} |
| 5 | +import gitbucket.core.service.RepositoryService.RepositoryInfo |
| 6 | +import gitbucket.core.util.Notifier |
| 7 | +import gitbucket.core.view.Markdown |
| 8 | +import gitbucket.notifications.model.Profile._ |
| 9 | +import profile.blockingApi._ |
| 10 | + |
| 11 | + |
| 12 | +class AccountHook extends gitbucket.core.plugin.AccountHook { |
| 13 | + |
| 14 | + override def deleted(userName: String)(implicit session: Session): Unit = { |
| 15 | + IssueNotifications.filter(_.notificationUserName === userName.bind).delete |
| 16 | + Watches.filter(_.notificationUserName === userName.bind).delete |
| 17 | + } |
| 18 | + |
| 19 | +} |
| 20 | + |
| 21 | +class RepositoryHook extends gitbucket.core.plugin.RepositoryHook { |
| 22 | + |
| 23 | + override def deleted(owner: String, repository: String)(implicit session: Session): Unit = { |
| 24 | + IssueNotifications.filter(t => t.userName === owner.bind && t.repositoryName === repository.bind).delete |
| 25 | + Watches.filter(t => t.userName === owner.bind && t.repositoryName === repository.bind).delete |
| 26 | + } |
| 27 | + |
| 28 | + override def renamed(owner: String, repository: String, newRepository: String)(implicit session: Session): Unit = { |
| 29 | + rename(owner, repository, owner, newRepository) |
| 30 | + } |
| 31 | + |
| 32 | + override def transferred(owner: String, newOwner: String, repository: String)(implicit session: Session): Unit = { |
| 33 | + rename(owner, repository, newOwner, repository) |
| 34 | + } |
| 35 | + |
| 36 | + // TODO select - insert |
| 37 | + private def rename(owner: String, repository: String, newOwner: String, newRepository: String)(implicit session: Session) = { |
| 38 | + val n = IssueNotifications.filter(t => t.userName === owner.bind && t.repositoryName === repository.bind).list |
| 39 | + val w = Watches.filter(t => t.userName === owner.bind && t.repositoryName === repository.bind).list |
| 40 | + |
| 41 | + deleted(owner, repository) |
| 42 | + |
| 43 | + IssueNotifications.insertAll(n.map(_.copy(userName = newOwner, repositoryName = newRepository)) :_*) |
| 44 | + Watches.insertAll(w.map(_.copy(userName = newOwner, repositoryName = newRepository)) :_*) |
| 45 | + } |
| 46 | + |
| 47 | +} |
| 48 | + |
| 49 | +class IssueHook extends gitbucket.core.plugin.IssueHook { |
| 50 | + |
| 51 | + override def created(issue: Issue, r: RepositoryInfo)(implicit context: Context): Unit = { |
| 52 | + Notifier().toNotify( |
| 53 | + subject(issue, r), |
| 54 | + message(issue.content getOrElse "", r)(content => s""" |
| 55 | + |$content<br/> |
| 56 | + |--<br/> |
| 57 | + |<a href="${s"${context.baseUrl}/${r.owner}/${r.name}/issues/${issue.issueId}"}">View it on GitBucket</a> |
| 58 | + """.stripMargin) |
| 59 | + )(recipients(issue)) |
| 60 | + } |
| 61 | + |
| 62 | + override def addedComment(commentId: Int, content: String, issue: Issue, r: RepositoryInfo)(implicit context: Context): Unit = { |
| 63 | + Notifier().toNotify( |
| 64 | + subject(issue, r), |
| 65 | + message(content, r)(content => s""" |
| 66 | + |$content<br/> |
| 67 | + |--<br/> |
| 68 | + |<a href="${s"${context.baseUrl}/${r.owner}/${r.name}/issues/${issue.issueId}#comment-$commentId"}">View it on GitBucket</a> |
| 69 | + """.stripMargin) |
| 70 | + )(recipients(issue)) |
| 71 | + } |
| 72 | + |
| 73 | + override def closed(issue: Issue, r: RepositoryInfo)(implicit context: Context): Unit = { |
| 74 | + Notifier().toNotify( |
| 75 | + subject(issue, r), |
| 76 | + message("close", r)(content => s""" |
| 77 | + |$content <a href="${s"${context.baseUrl}/${r.owner}/${r.name}/issues/${issue.issueId}"}">#${issue.issueId}</a> |
| 78 | + """.stripMargin) |
| 79 | + )(recipients(issue)) |
| 80 | + } |
| 81 | + |
| 82 | + override def reopened(issue: Issue, r: RepositoryInfo)(implicit context: Context): Unit = { |
| 83 | + Notifier().toNotify( |
| 84 | + subject(issue, r), |
| 85 | + message("reopen", r)(content => s""" |
| 86 | + |$content <a href="${s"${context.baseUrl}/${r.owner}/${r.name}/issues/${issue.issueId}"}">#${issue.issueId}</a> |
| 87 | + """.stripMargin) |
| 88 | + )(recipients(issue)) |
| 89 | + } |
| 90 | + |
| 91 | + |
| 92 | + protected val subject: (Issue, RepositoryInfo) => String = |
| 93 | + (issue, r) => s"[${r.owner}/${r.name}] ${issue.title} (#${issue.issueId})" |
| 94 | + |
| 95 | + protected val message: (String, RepositoryInfo) => (String => String) => String = |
| 96 | + (content, r) => msg => msg(Markdown.toHtml( |
| 97 | + markdown = content, |
| 98 | + repository = r, |
| 99 | + enableWikiLink = false, |
| 100 | + enableRefsLink = true, |
| 101 | + enableAnchor = false, |
| 102 | + enableLineBreaks = false |
| 103 | + )) |
| 104 | + |
| 105 | + // TODO |
| 106 | + protected val recipients: Issue => Account => Session => Seq[String] = { |
| 107 | + issue => loginAccount => implicit session => |
| 108 | + Seq("") |
| 109 | + } |
| 110 | + |
| 111 | +} |
| 112 | + |
| 113 | +class PullRequestHook extends IssueHook with gitbucket.core.plugin.PullRequestHook { |
| 114 | + |
| 115 | + override def created(issue: Issue, r: RepositoryInfo)(implicit context: Context): Unit = { |
| 116 | + val url = s"${context.baseUrl}/${r.owner}/${r.name}/pull/${issue.issueId}" |
| 117 | + Notifier().toNotify( |
| 118 | + subject(issue, r), |
| 119 | + message(issue.content getOrElse "", r)(content => s""" |
| 120 | + |$content<hr/> |
| 121 | + |View, comment on, or merge it at:<br/> |
| 122 | + |<a href="$url">$url</a> |
| 123 | + """.stripMargin) |
| 124 | + )(recipients(issue)) |
| 125 | + } |
| 126 | + |
| 127 | + override def addedComment(commentId: Int, content: String, issue: Issue, r: RepositoryInfo)(implicit context: Context): Unit = { |
| 128 | + Notifier().toNotify( |
| 129 | + subject(issue, r), |
| 130 | + message(content, r)(content => s""" |
| 131 | + |$content<br/> |
| 132 | + |--<br/> |
| 133 | + |<a href="${s"${context.baseUrl}/${r.owner}/${r.name}/pull/${issue.issueId}#comment-$commentId"}">View it on GitBucket</a> |
| 134 | + """.stripMargin) |
| 135 | + )(recipients(issue)) |
| 136 | + } |
| 137 | + |
| 138 | + override def merged(issue: Issue, r: RepositoryInfo)(implicit context: Context): Unit = { |
| 139 | + Notifier().toNotify( |
| 140 | + subject(issue, r), |
| 141 | + message("merge", r)(content => s""" |
| 142 | + |$content <a href="${s"${context.baseUrl}/${r.owner}/${r.name}/pull/${issue.issueId}"}">#${issue.issueId}</a> |
| 143 | + """.stripMargin) |
| 144 | + )(recipients(issue)) |
| 145 | + } |
| 146 | + |
| 147 | +} |
0 commit comments