Browse Source

Fix "Dashboard shows deleted comments" (#1995)

Jonas Franz 3 years ago
parent
commit
441986a473

+ 40 - 0
models/action.go

@@ -9,6 +9,7 @@ import (
9 9
 	"fmt"
10 10
 	"path"
11 11
 	"regexp"
12
+	"strconv"
12 13
 	"strings"
13 14
 	"time"
14 15
 	"unicode"
@@ -77,6 +78,9 @@ type Action struct {
77 78
 	ActUser     *User       `xorm:"-"`
78 79
 	RepoID      int64       `xorm:"INDEX"`
79 80
 	Repo        *Repository `xorm:"-"`
81
+	CommentID   int64       `xorm:"INDEX"`
82
+	Comment     *Comment    `xorm:"-"`
83
+	IsDeleted   bool        `xorm:"INDEX NOT NULL DEFAULT false"`
80 84
 	RefName     string
81 85
 	IsPrivate   bool      `xorm:"INDEX NOT NULL DEFAULT false"`
82 86
 	Content     string    `xorm:"TEXT"`
@@ -191,6 +195,35 @@ func (a *Action) GetRepoLink() string {
191 195
 	return "/" + a.GetRepoPath()
192 196
 }
193 197
 
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
+
194 227
 // GetBranch returns the action's repository branch.
195 228
 func (a *Action) GetBranch() string {
196 229
 	return a.RefName
@@ -678,6 +711,7 @@ type GetFeedsOptions struct {
678 711
 	RequestingUserID int64
679 712
 	IncludePrivate   bool // include private actions
680 713
 	OnlyPerformedBy  bool // only actions performed by requested user
714
+	IncludeDeleted   bool // include deleted actions
681 715
 }
682 716
 
683 717
 // GetFeeds returns actions according to the provided options
@@ -706,5 +740,11 @@ func GetFeeds(opts GetFeedsOptions) ([]*Action, error) {
706 740
 	if opts.RequestedUser.IsOrganization() {
707 741
 		sess.In("repo_id", repoIDs)
708 742
 	}
743
+
744
+	if !opts.IncludeDeleted {
745
+		sess.And("is_deleted = ?", false)
746
+
747
+	}
748
+
709 749
 	return actions, sess.Find(&actions)
710 750
 }

+ 3 - 0
models/action_test.go

@@ -310,6 +310,7 @@ func TestGetFeeds(t *testing.T) {
310 310
 		RequestingUserID: user.ID,
311 311
 		IncludePrivate:   true,
312 312
 		OnlyPerformedBy:  false,
313
+		IncludeDeleted:   true,
313 314
 	})
314 315
 	assert.NoError(t, err)
315 316
 	assert.Len(t, actions, 1)
@@ -337,6 +338,7 @@ func TestGetFeeds2(t *testing.T) {
337 338
 		RequestingUserID: userID,
338 339
 		IncludePrivate:   true,
339 340
 		OnlyPerformedBy:  false,
341
+		IncludeDeleted:   true,
340 342
 	})
341 343
 	assert.NoError(t, err)
342 344
 	assert.Len(t, actions, 1)
@@ -348,6 +350,7 @@ func TestGetFeeds2(t *testing.T) {
348 350
 		RequestingUserID: userID,
349 351
 		IncludePrivate:   false,
350 352
 		OnlyPerformedBy:  false,
353
+		IncludeDeleted:   true,
351 354
 	})
352 355
 	assert.NoError(t, err)
353 356
 	assert.Len(t, actions, 0)

+ 3 - 0
models/issue_comment.go

@@ -334,6 +334,8 @@ func createComment(e *xorm.Session, opts *CreateCommentOptions) (_ *Comment, err
334 334
 		Content:   fmt.Sprintf("%d|%s", opts.Issue.Index, strings.Split(opts.Content, "\n")[0]),
335 335
 		RepoID:    opts.Repo.ID,
336 336
 		Repo:      opts.Repo,
337
+		Comment:   comment,
338
+		CommentID: comment.ID,
337 339
 		IsPrivate: opts.Repo.IsPrivate,
338 340
 	}
339 341
 
@@ -666,6 +668,7 @@ func DeleteComment(comment *Comment) error {
666 668
 			return err
667 669
 		}
668 670
 	}
671
+	sess.Where("comment_id = ?", comment.ID).Cols("is_deleted").Update(&Action{IsDeleted: true})
669 672
 
670 673
 	return sess.Commit()
671 674
 }

+ 2 - 0
models/migrations/migrations.go

@@ -118,6 +118,8 @@ var migrations = []Migration{
118 118
 	NewMigration("remove columns from action", removeActionColumns),
119 119
 	// v34 -> v35
120 120
 	NewMigration("give all units to owner teams", giveAllUnitsToOwnerTeams),
121
+	// v35 -> v36
122
+	NewMigration("adds comment to an action", addCommentIDToAction),
121 123
 }
122 124
 
123 125
 // Migrate database to current version

+ 25 - 0
models/migrations/v35.go

@@ -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
+}

+ 3 - 2
routers/user/home.go

@@ -54,7 +54,7 @@ func getDashboardContextUser(ctx *context.Context) *models.User {
54 54
 }
55 55
 
56 56
 // 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) {
58 58
 	var requestingID int64
59 59
 	if ctx.User != nil {
60 60
 		requestingID = ctx.User.ID
@@ -64,6 +64,7 @@ func retrieveFeeds(ctx *context.Context, user *models.User, includePrivate, isPr
64 64
 		RequestingUserID: requestingID,
65 65
 		IncludePrivate:   includePrivate,
66 66
 		OnlyPerformedBy:  isProfile,
67
+		IncludeDeleted:   includeDeletedComments,
67 68
 	})
68 69
 	if err != nil {
69 70
 		ctx.Handle(500, "GetFeeds", err)
@@ -186,7 +187,7 @@ func Dashboard(ctx *context.Context) {
186 187
 	ctx.Data["MirrorCount"] = len(mirrors)
187 188
 	ctx.Data["Mirrors"] = mirrors
188 189
 
189
-	retrieveFeeds(ctx, ctxUser, true, false)
190
+	retrieveFeeds(ctx, ctxUser, true, false, false)
190 191
 	if ctx.Written() {
191 192
 		return
192 193
 	}

+ 1 - 1
routers/user/profile.go

@@ -138,7 +138,7 @@ func Profile(ctx *context.Context) {
138 138
 	ctx.Data["Keyword"] = keyword
139 139
 	switch tab {
140 140
 	case "activity":
141
-		retrieveFeeds(ctx, ctxUser, showPrivate, true)
141
+		retrieveFeeds(ctx, ctxUser, showPrivate, true, false)
142 142
 		if ctx.Written() {
143 143
 			return
144 144
 		}

+ 1 - 1
templates/user/dashboard/feeds.tmpl

@@ -63,7 +63,7 @@
63 63
 					{{else if eq .GetOpType 7}}
64 64
 						<span class="text truncate issue title has-emoji">{{index .GetIssueInfos 1}}</span>
65 65
 					{{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>
67 67
 						<p class="text light grey has-emoji">{{index .GetIssueInfos 1}}</p>
68 68
 					{{else if eq .GetOpType 11}}
69 69
 						<p class="text light grey has-emoji">{{index .GetIssueInfos 1}}</p>