aboutsummaryrefslogtreecommitdiffstats
path: root/models/activities
diff options
context:
space:
mode:
Diffstat (limited to 'models/activities')
-rw-r--r--models/activities/action.go51
-rw-r--r--models/activities/action_test.go2
-rw-r--r--models/activities/notification.go16
3 files changed, 63 insertions, 6 deletions
diff --git a/models/activities/action.go b/models/activities/action.go
index 4baedbfe12..8e7492c008 100644
--- a/models/activities/action.go
+++ b/models/activities/action.go
@@ -223,6 +223,49 @@ func (a *Action) GetRepoAbsoluteLink() string {
return setting.AppURL + url.PathEscape(a.GetRepoUserName()) + "/" + url.PathEscape(a.GetRepoName())
}
+// GetCommentHTMLURL returns link to action comment.
+func (a *Action) GetCommentHTMLURL() string {
+ return a.getCommentHTMLURL(db.DefaultContext)
+}
+
+func (a *Action) loadComment(ctx context.Context) (err error) {
+ if a.CommentID == 0 || a.Comment != nil {
+ return nil
+ }
+ a.Comment, err = issues_model.GetCommentByID(ctx, a.CommentID)
+ return err
+}
+
+func (a *Action) getCommentHTMLURL(ctx context.Context) string {
+ if a == nil {
+ return "#"
+ }
+ _ = a.loadComment(ctx)
+ if a.Comment != nil {
+ return a.Comment.HTMLURL()
+ }
+ if len(a.GetIssueInfos()) == 0 {
+ return "#"
+ }
+ // Return link to issue
+ issueIDString := a.GetIssueInfos()[0]
+ issueID, err := strconv.ParseInt(issueIDString, 10, 64)
+ if err != nil {
+ return "#"
+ }
+
+ issue, err := issues_model.GetIssueByID(ctx, issueID)
+ if err != nil {
+ return "#"
+ }
+
+ if err = issue.LoadRepo(ctx); err != nil {
+ return "#"
+ }
+
+ return issue.HTMLURL()
+}
+
// GetCommentLink returns link to action comment.
func (a *Action) GetCommentLink() string {
return a.getCommentLink(db.DefaultContext)
@@ -232,11 +275,9 @@ func (a *Action) getCommentLink(ctx context.Context) string {
if a == nil {
return "#"
}
- if a.Comment == nil && a.CommentID != 0 {
- a.Comment, _ = issues_model.GetCommentByID(ctx, a.CommentID)
- }
+ _ = a.loadComment(ctx)
if a.Comment != nil {
- return a.Comment.HTMLURL()
+ return a.Comment.Link()
}
if len(a.GetIssueInfos()) == 0 {
return "#"
@@ -257,7 +298,7 @@ func (a *Action) getCommentLink(ctx context.Context) string {
return "#"
}
- return issue.HTMLURL()
+ return issue.Link()
}
// GetBranch returns the action's repository branch.
diff --git a/models/activities/action_test.go b/models/activities/action_test.go
index 29312bd482..f37e58f685 100644
--- a/models/activities/action_test.go
+++ b/models/activities/action_test.go
@@ -36,7 +36,7 @@ func TestAction_GetRepoLink(t *testing.T) {
expected := path.Join(setting.AppSubURL, owner.Name, repo.Name)
assert.Equal(t, expected, action.GetRepoLink())
assert.Equal(t, repo.HTMLURL(), action.GetRepoAbsoluteLink())
- assert.Equal(t, comment.HTMLURL(), action.GetCommentLink())
+ assert.Equal(t, comment.HTMLURL(), action.GetCommentHTMLURL())
}
func TestGetFeeds(t *testing.T) {
diff --git a/models/activities/notification.go b/models/activities/notification.go
index f153eb0589..75276a0443 100644
--- a/models/activities/notification.go
+++ b/models/activities/notification.go
@@ -459,6 +459,22 @@ func (n *Notification) HTMLURL() string {
return ""
}
+// Link formats a relative URL-string to the notification
+func (n *Notification) Link() string {
+ switch n.Source {
+ case NotificationSourceIssue, NotificationSourcePullRequest:
+ if n.Comment != nil {
+ return n.Comment.Link()
+ }
+ return n.Issue.Link()
+ case NotificationSourceCommit:
+ return n.Repository.Link() + "/commit/" + url.PathEscape(n.CommitID)
+ case NotificationSourceRepository:
+ return n.Repository.Link()
+ }
+ return ""
+}
+
// APIURL formats a URL-string to the notification
func (n *Notification) APIURL() string {
return setting.AppURL + "api/v1/notifications/threads/" + strconv.FormatInt(n.ID, 10)