diff --git a/pkg/models/task_comments.go b/pkg/models/task_comments.go index f6f40e34e..804a66d9a 100644 --- a/pkg/models/task_comments.go +++ b/pkg/models/task_comments.go @@ -255,7 +255,9 @@ func (tc *TaskComment) ReadAll(s *xorm.Session, auth web.Auth, search string, pa } func addCommentsToTasks(s *xorm.Session, taskIDs []int64, taskMap map[int64]*Task) (err error) { - comments, _, _, err := getAllCommentsForTasksWithoutPermissionCheck(s, taskIDs, "", 0, 50) + // Only fetch the first page of comments when expanding tasks to avoid + // loading all comments for tasks with many comments. + comments, _, _, err := getAllCommentsForTasksWithoutPermissionCheck(s, taskIDs, "", 1, 50) if err != nil { return err } diff --git a/pkg/models/task_comments_test.go b/pkg/models/task_comments_test.go index b6eef137c..e12cd641d 100644 --- a/pkg/models/task_comments_test.go +++ b/pkg/models/task_comments_test.go @@ -17,6 +17,7 @@ package models import ( + "fmt" "testing" "code.vikunja.io/api/pkg/db" @@ -270,3 +271,24 @@ func TestTaskComment_ReadAll(t *testing.T) { assert.Equal(t, int64(15), resultComment[0].ID) }) } + +func TestAddCommentsToTasksLimit(t *testing.T) { + db.LoadAndAssertFixtures(t) + s := db.NewSession() + defer s.Close() + + taskID := int64(1) + + // Add a bunch of comments to exceed the pagination limit + for i := 0; i < 60; i++ { + _, err := s.Insert(&TaskComment{Comment: fmt.Sprintf("bulk %d", i), TaskID: taskID, AuthorID: 1}) + require.NoError(t, err) + } + + task := &Task{ID: taskID} + taskMap := map[int64]*Task{taskID: task} + + err := addCommentsToTasks(s, []int64{taskID}, taskMap) + require.NoError(t, err) + assert.Len(t, task.Comments, 50) +}