feat(views): return position when retriving tasks

This commit is contained in:
kolaente 2024-03-16 11:48:50 +01:00
parent 786e67f692
commit f364f3bec8
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
7 changed files with 35 additions and 11 deletions

View File

@ -158,7 +158,7 @@ func exportProjectsAndTasks(s *xorm.Session, u *user.User, wr *zip.Writer) (task
tasks, _, _, err := getTasksForProjects(s, rawProjects, u, &taskSearchOptions{
page: 0,
perPage: -1,
})
}, nil)
if err != nil {
return taskIDs, err
}

View File

@ -274,7 +274,7 @@ func GetTasksInBucketsForView(s *xorm.Session, view *ProjectView, opts *taskSear
taskMap[t.ID] = t
}
err = addMoreInfoToTasks(s, taskMap, auth)
err = addMoreInfoToTasks(s, taskMap, auth, view)
if err != nil {
return nil, err
}

View File

@ -223,7 +223,7 @@ func duplicateProjectBackground(s *xorm.Session, pd *ProjectDuplicate, doer web.
func duplicateTasks(s *xorm.Session, doer web.Auth, ld *ProjectDuplicate, bucketMap map[int64]int64) (err error) {
// Get all tasks + all task details
tasks, _, _, err := getTasksForProjects(s, []*Project{{ID: ld.ProjectID}}, doer, &taskSearchOptions{})
tasks, _, _, err := getTasksForProjects(s, []*Project{{ID: ld.ProjectID}}, doer, &taskSearchOptions{}, nil)
if err != nil {
return err
}

View File

@ -213,7 +213,7 @@ func (tf *TaskCollection) ReadAll(s *xorm.Session, a web.Auth, search string, pa
if err != nil {
return nil, 0, 0, err
}
return getTasksForProjects(s, []*Project{project}, a, opts)
return getTasksForProjects(s, []*Project{project}, a, opts, view)
}
// If the project ID is not set, we get all tasks for the user.
@ -253,5 +253,5 @@ func (tf *TaskCollection) ReadAll(s *xorm.Session, a web.Auth, search string, pa
}
}
return getTasksForProjects(s, projects, a, opts)
return getTasksForProjects(s, projects, a, opts, view)
}

View File

@ -134,3 +134,11 @@ func RecalculateTaskPositions(s *xorm.Session, view *ProjectView) (err error) {
_, err = s.Insert(newPositions)
return
}
func getPositionsForView(s *xorm.Session, view *ProjectView) (positions []*TaskPosition, err error) {
positions = []*TaskPosition{}
err = s.
Where("project_view_id = ?", view.ID).
Find(&positions)
return
}

View File

@ -303,7 +303,7 @@ func getRawTasksForProjects(s *xorm.Session, projects []*Project, a web.Auth, op
return tasks, len(tasks), totalItems, err
}
func getTasksForProjects(s *xorm.Session, projects []*Project, a web.Auth, opts *taskSearchOptions) (tasks []*Task, resultCount int, totalItems int64, err error) {
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 {
@ -315,7 +315,7 @@ func getTasksForProjects(s *xorm.Session, projects []*Project, a web.Auth, opts
taskMap[t.ID] = t
}
err = addMoreInfoToTasks(s, taskMap, a)
err = addMoreInfoToTasks(s, taskMap, a, view)
if err != nil {
return nil, 0, 0, err
}
@ -392,7 +392,7 @@ func GetTasksByUIDs(s *xorm.Session, uids []string, a web.Auth) (tasks []*Task,
taskMap[t.ID] = t
}
err = addMoreInfoToTasks(s, taskMap, a)
err = addMoreInfoToTasks(s, taskMap, a, nil)
return
}
@ -533,7 +533,7 @@ func addRelatedTasksToTasks(s *xorm.Session, taskIDs []int64, taskMap map[int64]
// This function takes a map with pointers and returns a slice with pointers to tasks
// It adds more stuff like assignees/labels/etc to a bunch of tasks
func addMoreInfoToTasks(s *xorm.Session, taskMap map[int64]*Task, a web.Auth) (err error) {
func addMoreInfoToTasks(s *xorm.Session, taskMap map[int64]*Task, a web.Auth, view *ProjectView) (err error) {
// No need to iterate over users and stuff if the project doesn't have tasks
if len(taskMap) == 0 {
@ -591,6 +591,17 @@ func addMoreInfoToTasks(s *xorm.Session, taskMap map[int64]*Task, a web.Auth) (e
return
}
var positionsMap = make(map[int64]*TaskPosition)
if view != nil {
positions, err := getPositionsForView(s, view)
if err != nil {
return err
}
for _, position := range positions {
positionsMap[position.TaskID] = position
}
}
// Add all objects to their tasks
for _, task := range taskMap {
@ -612,6 +623,11 @@ func addMoreInfoToTasks(s *xorm.Session, taskMap map[int64]*Task, a web.Auth) (e
if has {
task.Reactions = r
}
p, has := positionsMap[task.ID]
if has {
task.Position = p.Position
}
}
// Get all related tasks
@ -1487,7 +1503,7 @@ func (t *Task) ReadOne(s *xorm.Session, a web.Auth) (err error) {
taskMap := make(map[int64]*Task, 1)
taskMap[t.ID] = t
err = addMoreInfoToTasks(s, taskMap, a)
err = addMoreInfoToTasks(s, taskMap, a, nil)
if err != nil {
return
}

View File

@ -291,7 +291,7 @@ func reindexTasksInTypesense(s *xorm.Session, tasks map[int64]*Task) (err error)
return
}
err = addMoreInfoToTasks(s, tasks, &user.User{ID: 1})
err = addMoreInfoToTasks(s, tasks, &user.User{ID: 1}, nil)
if err != nil {
return fmt.Errorf("could not fetch more task info: %s", err.Error())
}