From 1049b27d373f9cd2b683e93553e41bc804f52d62 Mon Sep 17 00:00:00 2001 From: kolaente Date: Fri, 13 Sep 2024 19:45:48 +0200 Subject: [PATCH] fix(view): correctly resolve bucket filter when paginating (cherry picked from commit 45ff5907e69b24f00926841790d0f4f3d11e530a) --- pkg/models/task_collection.go | 31 +++++++++++++++++++++++++++++++ pkg/models/tasks.go | 1 - 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/pkg/models/task_collection.go b/pkg/models/task_collection.go index 434a41bce..f56ec49cc 100644 --- a/pkg/models/task_collection.go +++ b/pkg/models/task_collection.go @@ -17,6 +17,8 @@ package models import ( + "regexp" + "strconv" "strings" "code.vikunja.io/api/pkg/user" @@ -174,6 +176,28 @@ func getRelevantProjectsFromCollection(s *xorm.Session, a web.Auth, tf *TaskColl return []*Project{{ID: tf.ProjectID}}, nil } +func getFilterValueForBucketFilter(filter string, view *ProjectView) (newFilter string, err error) { + re := regexp.MustCompile(`bucket_id\s*=\s*(\d+)`) + + match := re.FindStringSubmatch(filter) + if len(match) < 2 { + return filter, nil + } + + bucketID, err := strconv.Atoi(match[1]) + if err != nil { + return "", err + } + + for id, bucket := range view.BucketConfiguration { + if id == bucketID { + return re.ReplaceAllString(filter, `(`+bucket.Filter+`)`), nil + } + } + + return filter, nil +} + // ReadAll gets all tasks for a collection // @Summary Get tasks in a project // @Description Returns all tasks for the current project. @@ -251,6 +275,13 @@ func (tf *TaskCollection) ReadAll(s *xorm.Session, a web.Auth, search string, pa tf.Filter = view.Filter } } + + if view.BucketConfigurationMode == BucketConfigurationModeFilter && strings.Contains(tf.Filter, "bucket_id") { + tf.Filter, err = getFilterValueForBucketFilter(tf.Filter, view) + if err != nil { + return nil, 0, 0, err + } + } } opts, err := getTaskFilterOptsFromCollection(tf, view) diff --git a/pkg/models/tasks.go b/pkg/models/tasks.go index 1d3bb25e3..a09c68807 100644 --- a/pkg/models/tasks.go +++ b/pkg/models/tasks.go @@ -325,7 +325,6 @@ func getRawTasksForProjects(s *xorm.Session, projects []*Project, a web.Auth, op } func getTasksForProjects(s *xorm.Session, projects []*Project, a web.Auth, opts *taskSearchOptions, view *ProjectView) (tasks []*Task, resultCount int, totalItems int64, err error) { - tasks, resultCount, totalItems, err = getRawTasksForProjects(s, projects, a, opts) if err != nil { return nil, 0, 0, err