Skip to content

Commit dcebcb7

Browse files
committed
Fix activity feed
Preserve actions after user/repo name change
1 parent ff2464c commit dcebcb7

File tree

6 files changed

+68
-26
lines changed

6 files changed

+68
-26
lines changed

models/action.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ type Action struct {
7474
UserID int64 `xorm:"INDEX"` // Receiver user id.
7575
OpType ActionType
7676
ActUserID int64 `xorm:"INDEX"` // Action user id.
77-
ActUserName string // Action user name.
77+
ActUserName string `xorm:"-"`
7878
ActAvatar string `xorm:"-"`
7979
RepoID int64 `xorm:"INDEX"`
80-
RepoUserName string
81-
RepoName string
80+
RepoUserName string `xorm:"-"`
81+
RepoName string `xorm:"-"`
8282
RefName string
8383
IsPrivate bool `xorm:"INDEX NOT NULL DEFAULT false"`
8484
Content string `xorm:"TEXT"`

models/consistency.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,5 @@ func (team *Team) checkForConsistency(t *testing.T) {
162162

163163
func (action *Action) checkForConsistency(t *testing.T) {
164164
repo := AssertExistsAndLoadBean(t, &Repository{ID: action.RepoID}).(*Repository)
165-
owner := AssertExistsAndLoadBean(t, &User{ID: repo.OwnerID}).(*User)
166-
actor := AssertExistsAndLoadBean(t, &User{ID: action.ActUserID}).(*User)
167-
168-
assert.Equal(t, repo.Name, action.RepoName, "action: %+v", action)
169165
assert.Equal(t, repo.IsPrivate, action.IsPrivate, "action: %+v", action)
170-
assert.Equal(t, owner.Name, action.RepoUserName, "action: %+v", action)
171-
assert.Equal(t, actor.Name, action.ActUserName, "action: %+v", action)
172166
}

models/fixtures/action.yml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,15 @@
33
user_id: 2
44
op_type: 12 # close issue
55
act_user_id: 2
6-
act_user_name: user2
76
repo_id: 2
8-
repo_user_name: user2
9-
repo_name: repo2
107
is_private: true
118

129
-
1310
id: 2
1411
user_id: 3
1512
op_type: 2 # rename repo
1613
act_user_id: 3
17-
act_user_name: user3
1814
repo_id: 3
19-
repo_user_name: user3
20-
repo_name: repo3
2115
is_private: true
2216
content: oldRepoName
2317

@@ -26,8 +20,5 @@
2620
user_id: 11
2721
op_type: 1 # create repo
2822
act_user_id: 11
29-
act_user_name: user11
3023
repo_id: 9
31-
repo_user_name: user11
32-
repo_name: repo9
3324
is_private: false

models/migrations/migrations.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ var migrations = []Migration{
114114
NewMigration("add field for login source synchronization", addLoginSourceSyncEnabledColumn),
115115
// v32 -> v33
116116
NewMigration("add units for team", addUnitsToRepoTeam),
117+
// v33 -> v34
118+
NewMigration("remove columns from action", removeActionColumns),
117119
}
118120

119121
// Migrate database to current version

models/migrations/v34.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2017 Gitea. 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+
type ActionV34 struct {
14+
ActUserName string `xorm:"-"`
15+
RepoUserName string `xorm:"-"`
16+
RepoName string `xorm:"-"`
17+
}
18+
19+
// TableName will be invoked by XORM to customize the table name
20+
func (*ActionV34) TableName() string {
21+
return "action"
22+
}
23+
24+
func removeActionColumns(x *xorm.Engine) (err error) {
25+
if err = x.Sync(new(ActionV34)); err != nil {
26+
return fmt.Errorf("Sync: %v", err)
27+
}
28+
return nil
29+
}

routers/user/home.go

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,25 +65,51 @@ func retrieveFeeds(ctx *context.Context, ctxUser *models.User, userID, offset in
6565

6666
// Check access of private repositories.
6767
feeds := make([]*models.Action, 0, len(actions))
68-
unameAvatars := map[string]string{
69-
ctxUser.Name: ctxUser.RelAvatarLink(),
70-
}
68+
userCache := map[int64]*models.User{ctxUser.ID: ctxUser}
69+
repoCache := map[int64]*models.Repository{}
7170
for _, act := range actions {
7271
// Cache results to reduce queries.
73-
_, ok := unameAvatars[act.ActUserName]
72+
u, ok := userCache[act.ActUserID]
7473
if !ok {
75-
u, err := models.GetUserByName(act.ActUserName)
74+
u, err = models.GetUserByID(act.ActUserID)
7675
if err != nil {
7776
if models.IsErrUserNotExist(err) {
7877
continue
7978
}
80-
ctx.Handle(500, "GetUserByName", err)
79+
ctx.Handle(500, "GetUserByID", err)
80+
return
81+
}
82+
userCache[act.ActUserID] = u
83+
}
84+
act.ActUserName = u.Name
85+
act.ActAvatar = u.RelAvatarLink()
86+
87+
repo, ok := repoCache[act.RepoID]
88+
if !ok {
89+
repo, err = models.GetRepositoryByID(act.RepoID)
90+
if err != nil {
91+
if models.IsErrRepoNotExist(err) {
92+
continue
93+
}
94+
ctx.Handle(500, "GetRepositoryByID", err)
95+
return
96+
}
97+
}
98+
act.RepoName = repo.Name
99+
100+
repoOwner, ok := userCache[repo.OwnerID]
101+
if !ok {
102+
repoOwner, err = models.GetUserByID(repo.OwnerID)
103+
if err != nil {
104+
if models.IsErrUserNotExist(err) {
105+
continue
106+
}
107+
ctx.Handle(500, "GetUserByID", err)
81108
return
82109
}
83-
unameAvatars[act.ActUserName] = u.RelAvatarLink()
84110
}
111+
act.RepoUserName = repoOwner.Name
85112

86-
act.ActAvatar = unameAvatars[act.ActUserName]
87113
feeds = append(feeds, act)
88114
}
89115
ctx.Data["Feeds"] = feeds

0 commit comments

Comments
 (0)