Skip to content

Commit f2ea31d

Browse files
authored
Enable system users for comment.LoadPoster (#28014)
System users (Ghost, ActionsUser, etc) have a negative id and may be the author of a comment, either because it was created by a now deleted user or via an action using a transient token. The GetPossibleUserByID function has special cases related to system users and will not fail if given a negative id. Refs: https://codeberg.org/forgejo/forgejo/issues/1425 (cherry picked from commit 6a2d2fa24390116d31ae2507c0a93d423f690b7b)
1 parent 0678c82 commit f2ea31d

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

models/issues/comment.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ func (c *Comment) AfterLoad(session *xorm.Session) {
350350

351351
// LoadPoster loads comment poster
352352
func (c *Comment) LoadPoster(ctx context.Context) (err error) {
353-
if c.PosterID <= 0 || c.Poster != nil {
353+
if c.Poster != nil {
354354
return nil
355355
}
356356

tests/integration/api_comment_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,43 @@ func TestAPIGetComment(t *testing.T) {
136136
assert.Equal(t, expect.Created.Unix(), apiComment.Created.Unix())
137137
}
138138

139+
func TestAPIGetSystemUserComment(t *testing.T) {
140+
defer tests.PrepareTestEnv(t)()
141+
142+
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{})
143+
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: issue.RepoID})
144+
repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
145+
146+
for _, systemUser := range []*user_model.User{
147+
user_model.NewGhostUser(),
148+
user_model.NewActionsUser(),
149+
} {
150+
body := fmt.Sprintf("Hello %s", systemUser.Name)
151+
comment, err := issues_model.CreateComment(db.DefaultContext, &issues_model.CreateCommentOptions{
152+
Type: issues_model.CommentTypeComment,
153+
Doer: systemUser,
154+
Repo: repo,
155+
Issue: issue,
156+
Content: body,
157+
})
158+
assert.NoError(t, err)
159+
160+
req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/issues/comments/%d", repoOwner.Name, repo.Name, comment.ID)
161+
resp := MakeRequest(t, req, http.StatusOK)
162+
163+
var apiComment api.Comment
164+
DecodeJSON(t, resp, &apiComment)
165+
166+
if assert.NotNil(t, apiComment.Poster) {
167+
if assert.Equal(t, systemUser.ID, apiComment.Poster.ID) {
168+
assert.NoError(t, comment.LoadPoster(db.DefaultContext))
169+
assert.Equal(t, systemUser.Name, apiComment.Poster.UserName)
170+
}
171+
}
172+
assert.Equal(t, body, apiComment.Body)
173+
}
174+
}
175+
139176
func TestAPIEditComment(t *testing.T) {
140177
defer tests.PrepareTestEnv(t)()
141178
const newCommentBody = "This is the new comment body"

0 commit comments

Comments
 (0)