Skip to content

Commit 0c332f0

Browse files
ethantkoeniglunny
authored andcommitted
Fix activity feed (#1779)
* Fix activity feed Preserve actions after user/repo name change * Add missing comment * Fix migration, and remove fields completely * Tests
1 parent 03912ce commit 0c332f0

File tree

11 files changed

+238
-165
lines changed

11 files changed

+238
-165
lines changed

integrations/mysql.ini

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ DB_TYPE = mysql
66
HOST = 127.0.0.1:3306
77
NAME = testgitea
88
USER = root
9-
PASSWD =
9+
PASSWD =
1010
SSL_MODE = disable
1111
PATH = data/gitea.db
1212

@@ -26,14 +26,14 @@ OFFLINE_MODE = false
2626
ENABLED = false
2727

2828
[service]
29-
REGISTER_EMAIL_CONFIRM = false
30-
ENABLE_NOTIFY_MAIL = false
31-
DISABLE_REGISTRATION = false
32-
ENABLE_CAPTCHA = false
33-
REQUIRE_SIGNIN_VIEW = false
34-
DEFAULT_KEEP_EMAIL_PRIVATE = false
29+
REGISTER_EMAIL_CONFIRM = false
30+
ENABLE_NOTIFY_MAIL = false
31+
DISABLE_REGISTRATION = false
32+
ENABLE_CAPTCHA = false
33+
REQUIRE_SIGNIN_VIEW = false
34+
DEFAULT_KEEP_EMAIL_PRIVATE = false
3535
DEFAULT_ALLOW_CREATE_ORGANIZATION = true
36-
NO_REPLY_ADDRESS = noreply.example.org
36+
NO_REPLY_ADDRESS = noreply.example.org
3737

3838
[picture]
3939
DISABLE_GRAVATAR = false
@@ -53,5 +53,7 @@ LEVEL = Warn
5353
LEVEL = Info
5454

5555
[security]
56-
INSTALL_LOCK = true
57-
SECRET_KEY = 9pCviYTWSb
56+
INSTALL_LOCK = true
57+
SECRET_KEY = 9pCviYTWSb
58+
INTERNAL_TOKEN = eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOjE0OTU1NTE2MTh9.hhSVGOANkaKk3vfCd2jDOIww4pUk0xtg9JRde5UogyQ
59+

models/action.go

Lines changed: 83 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -70,20 +70,18 @@ func init() {
7070
// repository. It implemented interface base.Actioner so that can be
7171
// used in template render.
7272
type Action struct {
73-
ID int64 `xorm:"pk autoincr"`
74-
UserID int64 `xorm:"INDEX"` // Receiver user id.
75-
OpType ActionType
76-
ActUserID int64 `xorm:"INDEX"` // Action user id.
77-
ActUserName string // Action user name.
78-
ActAvatar string `xorm:"-"`
79-
RepoID int64 `xorm:"INDEX"`
80-
RepoUserName string
81-
RepoName string
82-
RefName string
83-
IsPrivate bool `xorm:"INDEX NOT NULL DEFAULT false"`
84-
Content string `xorm:"TEXT"`
85-
Created time.Time `xorm:"-"`
86-
CreatedUnix int64 `xorm:"INDEX"`
73+
ID int64 `xorm:"pk autoincr"`
74+
UserID int64 `xorm:"INDEX"` // Receiver user id.
75+
OpType ActionType
76+
ActUserID int64 `xorm:"INDEX"` // Action user id.
77+
ActUser *User `xorm:"-"`
78+
RepoID int64 `xorm:"INDEX"`
79+
Repo *Repository `xorm:"-"`
80+
RefName string
81+
IsPrivate bool `xorm:"INDEX NOT NULL DEFAULT false"`
82+
Content string `xorm:"TEXT"`
83+
Created time.Time `xorm:"-"`
84+
CreatedUnix int64 `xorm:"INDEX"`
8785
}
8886

8987
// BeforeInsert will be invoked by XORM before inserting a record
@@ -106,42 +104,71 @@ func (a *Action) GetOpType() int {
106104
return int(a.OpType)
107105
}
108106

107+
func (a *Action) loadActUser() {
108+
if a.ActUser != nil {
109+
return
110+
}
111+
var err error
112+
a.ActUser, err = GetUserByID(a.ActUserID)
113+
if err == nil {
114+
return
115+
} else if IsErrUserNotExist(err) {
116+
a.ActUser = NewGhostUser()
117+
} else {
118+
log.Error(4, "GetUserByID(%d): %v", a.ActUserID, err)
119+
}
120+
}
121+
122+
func (a *Action) loadRepo() {
123+
if a.ActUser != nil {
124+
return
125+
}
126+
var err error
127+
a.Repo, err = GetRepositoryByID(a.RepoID)
128+
if err != nil {
129+
log.Error(4, "GetRepositoryByID(%d): %v", a.RepoID, err)
130+
}
131+
}
132+
109133
// GetActUserName gets the action's user name.
110134
func (a *Action) GetActUserName() string {
111-
return a.ActUserName
135+
a.loadActUser()
136+
return a.ActUser.Name
112137
}
113138

114139
// ShortActUserName gets the action's user name trimmed to max 20
115140
// chars.
116141
func (a *Action) ShortActUserName() string {
117-
return base.EllipsisString(a.ActUserName, 20)
142+
return base.EllipsisString(a.GetActUserName(), 20)
118143
}
119144

120145
// GetRepoUserName returns the name of the action repository owner.
121146
func (a *Action) GetRepoUserName() string {
122-
return a.RepoUserName
147+
a.loadRepo()
148+
return a.Repo.MustOwner().Name
123149
}
124150

125151
// ShortRepoUserName returns the name of the action repository owner
126152
// trimmed to max 20 chars.
127153
func (a *Action) ShortRepoUserName() string {
128-
return base.EllipsisString(a.RepoUserName, 20)
154+
return base.EllipsisString(a.GetRepoUserName(), 20)
129155
}
130156

131157
// GetRepoName returns the name of the action repository.
132158
func (a *Action) GetRepoName() string {
133-
return a.RepoName
159+
a.loadRepo()
160+
return a.Repo.Name
134161
}
135162

136163
// ShortRepoName returns the name of the action repository
137164
// trimmed to max 33 chars.
138165
func (a *Action) ShortRepoName() string {
139-
return base.EllipsisString(a.RepoName, 33)
166+
return base.EllipsisString(a.GetRepoName(), 33)
140167
}
141168

142169
// GetRepoPath returns the virtual path to the action repository.
143170
func (a *Action) GetRepoPath() string {
144-
return path.Join(a.RepoUserName, a.RepoName)
171+
return path.Join(a.GetRepoUserName(), a.GetRepoName())
145172
}
146173

147174
// ShortRepoPath returns the virtual path to the action repository
@@ -205,13 +232,12 @@ func (a *Action) GetIssueContent() string {
205232

206233
func newRepoAction(e Engine, u *User, repo *Repository) (err error) {
207234
if err = notifyWatchers(e, &Action{
208-
ActUserID: u.ID,
209-
ActUserName: u.Name,
210-
OpType: ActionCreateRepo,
211-
RepoID: repo.ID,
212-
RepoUserName: repo.Owner.Name,
213-
RepoName: repo.Name,
214-
IsPrivate: repo.IsPrivate,
235+
ActUserID: u.ID,
236+
ActUser: u,
237+
OpType: ActionCreateRepo,
238+
RepoID: repo.ID,
239+
Repo: repo,
240+
IsPrivate: repo.IsPrivate,
215241
}); err != nil {
216242
return fmt.Errorf("notify watchers '%d/%d': %v", u.ID, repo.ID, err)
217243
}
@@ -227,14 +253,13 @@ func NewRepoAction(u *User, repo *Repository) (err error) {
227253

228254
func renameRepoAction(e Engine, actUser *User, oldRepoName string, repo *Repository) (err error) {
229255
if err = notifyWatchers(e, &Action{
230-
ActUserID: actUser.ID,
231-
ActUserName: actUser.Name,
232-
OpType: ActionRenameRepo,
233-
RepoID: repo.ID,
234-
RepoUserName: repo.Owner.Name,
235-
RepoName: repo.Name,
236-
IsPrivate: repo.IsPrivate,
237-
Content: oldRepoName,
256+
ActUserID: actUser.ID,
257+
ActUser: actUser,
258+
OpType: ActionRenameRepo,
259+
RepoID: repo.ID,
260+
Repo: repo,
261+
IsPrivate: repo.IsPrivate,
262+
Content: oldRepoName,
238263
}); err != nil {
239264
return fmt.Errorf("notify watchers: %v", err)
240265
}
@@ -521,15 +546,14 @@ func CommitRepoAction(opts CommitRepoActionOptions) error {
521546

522547
refName := git.RefEndName(opts.RefFullName)
523548
if err = NotifyWatchers(&Action{
524-
ActUserID: pusher.ID,
525-
ActUserName: pusher.Name,
526-
OpType: opType,
527-
Content: string(data),
528-
RepoID: repo.ID,
529-
RepoUserName: repo.MustOwner().Name,
530-
RepoName: repo.Name,
531-
RefName: refName,
532-
IsPrivate: repo.IsPrivate,
549+
ActUserID: pusher.ID,
550+
ActUser: pusher,
551+
OpType: opType,
552+
Content: string(data),
553+
RepoID: repo.ID,
554+
Repo: repo,
555+
RefName: refName,
556+
IsPrivate: repo.IsPrivate,
533557
}); err != nil {
534558
return fmt.Errorf("NotifyWatchers: %v", err)
535559
}
@@ -598,14 +622,13 @@ func CommitRepoAction(opts CommitRepoActionOptions) error {
598622

599623
func transferRepoAction(e Engine, doer, oldOwner *User, repo *Repository) (err error) {
600624
if err = notifyWatchers(e, &Action{
601-
ActUserID: doer.ID,
602-
ActUserName: doer.Name,
603-
OpType: ActionTransferRepo,
604-
RepoID: repo.ID,
605-
RepoUserName: repo.Owner.Name,
606-
RepoName: repo.Name,
607-
IsPrivate: repo.IsPrivate,
608-
Content: path.Join(oldOwner.Name, repo.Name),
625+
ActUserID: doer.ID,
626+
ActUser: doer,
627+
OpType: ActionTransferRepo,
628+
RepoID: repo.ID,
629+
Repo: repo,
630+
IsPrivate: repo.IsPrivate,
631+
Content: path.Join(oldOwner.Name, repo.Name),
609632
}); err != nil {
610633
return fmt.Errorf("notifyWatchers: %v", err)
611634
}
@@ -628,14 +651,13 @@ func TransferRepoAction(doer, oldOwner *User, repo *Repository) error {
628651

629652
func mergePullRequestAction(e Engine, doer *User, repo *Repository, issue *Issue) error {
630653
return notifyWatchers(e, &Action{
631-
ActUserID: doer.ID,
632-
ActUserName: doer.Name,
633-
OpType: ActionMergePullRequest,
634-
Content: fmt.Sprintf("%d|%s", issue.Index, issue.Title),
635-
RepoID: repo.ID,
636-
RepoUserName: repo.Owner.Name,
637-
RepoName: repo.Name,
638-
IsPrivate: repo.IsPrivate,
654+
ActUserID: doer.ID,
655+
ActUser: doer,
656+
OpType: ActionMergePullRequest,
657+
Content: fmt.Sprintf("%d|%s", issue.Index, issue.Title),
658+
RepoID: repo.ID,
659+
Repo: repo,
660+
IsPrivate: repo.IsPrivate,
639661
})
640662
}
641663

0 commit comments

Comments
 (0)