Skip to content

Commit 441986a

Browse files
jonasfranzbkcsoft
authored andcommitted
Fix "Dashboard shows deleted comments" (#1995)
1 parent 7356762 commit 441986a

File tree

8 files changed

+78
-4
lines changed

8 files changed

+78
-4
lines changed

models/action.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"fmt"
1010
"path"
1111
"regexp"
12+
"strconv"
1213
"strings"
1314
"time"
1415
"unicode"
@@ -77,6 +78,9 @@ type Action struct {
7778
ActUser *User `xorm:"-"`
7879
RepoID int64 `xorm:"INDEX"`
7980
Repo *Repository `xorm:"-"`
81+
CommentID int64 `xorm:"INDEX"`
82+
Comment *Comment `xorm:"-"`
83+
IsDeleted bool `xorm:"INDEX NOT NULL DEFAULT false"`
8084
RefName string
8185
IsPrivate bool `xorm:"INDEX NOT NULL DEFAULT false"`
8286
Content string `xorm:"TEXT"`
@@ -191,6 +195,35 @@ func (a *Action) GetRepoLink() string {
191195
return "/" + a.GetRepoPath()
192196
}
193197

198+
// GetCommentLink returns link to action comment.
199+
func (a *Action) GetCommentLink() string {
200+
if a == nil {
201+
return "#"
202+
}
203+
if a.Comment == nil && a.CommentID != 0 {
204+
a.Comment, _ = GetCommentByID(a.CommentID)
205+
}
206+
if a.Comment != nil {
207+
return a.Comment.HTMLURL()
208+
}
209+
if len(a.GetIssueInfos()) == 0 {
210+
return "#"
211+
}
212+
//Return link to issue
213+
issueIDString := a.GetIssueInfos()[0]
214+
issueID, err := strconv.ParseInt(issueIDString, 10, 64)
215+
if err != nil {
216+
return "#"
217+
}
218+
219+
issue, err := GetIssueByID(issueID)
220+
if err != nil {
221+
return "#"
222+
}
223+
224+
return issue.HTMLURL()
225+
}
226+
194227
// GetBranch returns the action's repository branch.
195228
func (a *Action) GetBranch() string {
196229
return a.RefName
@@ -678,6 +711,7 @@ type GetFeedsOptions struct {
678711
RequestingUserID int64
679712
IncludePrivate bool // include private actions
680713
OnlyPerformedBy bool // only actions performed by requested user
714+
IncludeDeleted bool // include deleted actions
681715
}
682716

683717
// GetFeeds returns actions according to the provided options
@@ -706,5 +740,11 @@ func GetFeeds(opts GetFeedsOptions) ([]*Action, error) {
706740
if opts.RequestedUser.IsOrganization() {
707741
sess.In("repo_id", repoIDs)
708742
}
743+
744+
if !opts.IncludeDeleted {
745+
sess.And("is_deleted = ?", false)
746+
747+
}
748+
709749
return actions, sess.Find(&actions)
710750
}

models/action_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ func TestGetFeeds(t *testing.T) {
310310
RequestingUserID: user.ID,
311311
IncludePrivate: true,
312312
OnlyPerformedBy: false,
313+
IncludeDeleted: true,
313314
})
314315
assert.NoError(t, err)
315316
assert.Len(t, actions, 1)
@@ -337,6 +338,7 @@ func TestGetFeeds2(t *testing.T) {
337338
RequestingUserID: userID,
338339
IncludePrivate: true,
339340
OnlyPerformedBy: false,
341+
IncludeDeleted: true,
340342
})
341343
assert.NoError(t, err)
342344
assert.Len(t, actions, 1)
@@ -348,6 +350,7 @@ func TestGetFeeds2(t *testing.T) {
348350
RequestingUserID: userID,
349351
IncludePrivate: false,
350352
OnlyPerformedBy: false,
353+
IncludeDeleted: true,
351354
})
352355
assert.NoError(t, err)
353356
assert.Len(t, actions, 0)

models/issue_comment.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,8 @@ func createComment(e *xorm.Session, opts *CreateCommentOptions) (_ *Comment, err
334334
Content: fmt.Sprintf("%d|%s", opts.Issue.Index, strings.Split(opts.Content, "\n")[0]),
335335
RepoID: opts.Repo.ID,
336336
Repo: opts.Repo,
337+
Comment: comment,
338+
CommentID: comment.ID,
337339
IsPrivate: opts.Repo.IsPrivate,
338340
}
339341

@@ -666,6 +668,7 @@ func DeleteComment(comment *Comment) error {
666668
return err
667669
}
668670
}
671+
sess.Where("comment_id = ?", comment.ID).Cols("is_deleted").Update(&Action{IsDeleted: true})
669672

670673
return sess.Commit()
671674
}

models/migrations/migrations.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ var migrations = []Migration{
118118
NewMigration("remove columns from action", removeActionColumns),
119119
// v34 -> v35
120120
NewMigration("give all units to owner teams", giveAllUnitsToOwnerTeams),
121+
// v35 -> v36
122+
NewMigration("adds comment to an action", addCommentIDToAction),
121123
}
122124

123125
// Migrate database to current version

models/migrations/v35.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2017 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package migrations
6+
7+
import (
8+
"fmt"
9+
10+
"github.com/go-xorm/xorm"
11+
)
12+
13+
func addCommentIDToAction(x *xorm.Engine) error {
14+
// Action see models/action.go
15+
type Action struct {
16+
CommentID int64 `xorm:"INDEX"`
17+
IsDeleted bool `xorm:"INDEX NOT NULL DEFAULT false"`
18+
}
19+
20+
if err := x.Sync2(new(Action)); err != nil {
21+
return fmt.Errorf("Sync2: %v", err)
22+
}
23+
24+
return nil
25+
}

routers/user/home.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func getDashboardContextUser(ctx *context.Context) *models.User {
5454
}
5555

5656
// retrieveFeeds loads feeds for the specified user
57-
func retrieveFeeds(ctx *context.Context, user *models.User, includePrivate, isProfile bool) {
57+
func retrieveFeeds(ctx *context.Context, user *models.User, includePrivate, isProfile bool, includeDeletedComments bool) {
5858
var requestingID int64
5959
if ctx.User != nil {
6060
requestingID = ctx.User.ID
@@ -64,6 +64,7 @@ func retrieveFeeds(ctx *context.Context, user *models.User, includePrivate, isPr
6464
RequestingUserID: requestingID,
6565
IncludePrivate: includePrivate,
6666
OnlyPerformedBy: isProfile,
67+
IncludeDeleted: includeDeletedComments,
6768
})
6869
if err != nil {
6970
ctx.Handle(500, "GetFeeds", err)
@@ -186,7 +187,7 @@ func Dashboard(ctx *context.Context) {
186187
ctx.Data["MirrorCount"] = len(mirrors)
187188
ctx.Data["Mirrors"] = mirrors
188189

189-
retrieveFeeds(ctx, ctxUser, true, false)
190+
retrieveFeeds(ctx, ctxUser, true, false, false)
190191
if ctx.Written() {
191192
return
192193
}

routers/user/profile.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func Profile(ctx *context.Context) {
138138
ctx.Data["Keyword"] = keyword
139139
switch tab {
140140
case "activity":
141-
retrieveFeeds(ctx, ctxUser, showPrivate, true)
141+
retrieveFeeds(ctx, ctxUser, showPrivate, true, false)
142142
if ctx.Written() {
143143
return
144144
}

templates/user/dashboard/feeds.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
{{else if eq .GetOpType 7}}
6464
<span class="text truncate issue title has-emoji">{{index .GetIssueInfos 1}}</span>
6565
{{else if eq .GetOpType 10}}
66-
<span class="text truncate issue title has-emoji">{{.GetIssueTitle}}</span>
66+
<a href="{{.GetCommentLink}}" class="text truncate issue title has-emoji">{{.GetIssueTitle}}</a>
6767
<p class="text light grey has-emoji">{{index .GetIssueInfos 1}}</p>
6868
{{else if eq .GetOpType 11}}
6969
<p class="text light grey has-emoji">{{index .GetIssueInfos 1}}</p>

0 commit comments

Comments
 (0)