1
0

feat(views): sort by position

This commit is contained in:
kolaente 2024-03-15 19:23:59 +01:00
parent 43f24661d7
commit ee6ea03506
No known key found for this signature in database
GPG Key ID: F40E70337AB24C9B
3 changed files with 17 additions and 57 deletions

View File

@ -19,8 +19,6 @@ package models
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"github.com/typesense/typesense-go/typesense/api"
"github.com/typesense/typesense-go/typesense/api/pointer"
"strconv" "strconv"
"time" "time"
@ -536,15 +534,6 @@ func (l *AddTaskToTypesense) Handle(msg *message.Message) (err error) {
return err return err
} }
_, err = typesenseClient.Collection("tasks").
Documents().
Delete(context.Background(), &api.DeleteDocumentsParams{
FilterBy: pointer.String("task_id:" + strconv.FormatInt(event.Task.ID, 10)),
})
if err != nil {
return err
}
_, err = typesenseClient.Collection("tasks"). _, err = typesenseClient.Collection("tasks").
Documents(). Documents().
Create(context.Background(), ttask) Create(context.Background(), ttask)

View File

@ -444,8 +444,8 @@ func (t *typesenseTaskSearcher) Search(opts *taskSearchOptions) (tasks []*Task,
} }
if param.sortBy == taskPropertyPosition { if param.sortBy == taskPropertyPosition {
filterBy = append(filterBy, "project_view_id: "+strconv.FormatInt(param.projectViewID, 10)) param.sortBy = "positions.view_" + strconv.FormatInt(param.projectViewID, 10)
break continue
} }
sortbyFields = append(sortbyFields, param.sortBy+"(missing_values:last):"+param.orderBy.String()) sortbyFields = append(sortbyFields, param.sortBy+"(missing_values:last):"+param.orderBy.String())

View File

@ -20,7 +20,6 @@ import (
"context" "context"
"fmt" "fmt"
"strconv" "strconv"
"strings"
"time" "time"
"code.vikunja.io/api/pkg/config" "code.vikunja.io/api/pkg/config"
@ -249,17 +248,14 @@ func ReindexAllTasks() (err error) {
return return
} }
func getTypesenseTaskForTask(s *xorm.Session, task *Task, projectsCache map[int64]*Project) (ttasks []*typesenseTask, err error) { func getTypesenseTaskForTask(s *xorm.Session, task *Task, projectsCache map[int64]*Project) (ttask *typesenseTask, err error) {
positions := []*TaskPosition{} positions := []*TaskPosition{}
err = s.Where("task_id = ?", task.ID).Find(&positions) err = s.Where("task_id = ?", task.ID).Find(&positions)
if err != nil { if err != nil {
return return
} }
for _, position := range positions { ttask = convertTaskToTypesenseTask(task, positions)
ttask := convertTaskToTypesenseTask(task, position)
ttasks = append(ttasks, ttask)
}
var p *Project var p *Project
if projectsCache == nil { if projectsCache == nil {
@ -280,15 +276,11 @@ func getTypesenseTaskForTask(s *xorm.Session, task *Task, projectsCache map[int6
} }
comment := &TaskComment{TaskID: task.ID} comment := &TaskComment{TaskID: task.ID}
comments, _, _, err := comment.ReadAll(s, &user.User{ID: p.OwnerID}, "", -1, -1) ttask.Comments, _, _, err = comment.ReadAll(s, &user.User{ID: p.OwnerID}, "", -1, -1)
if err != nil { if err != nil {
return nil, fmt.Errorf("could not fetch comments for task %d: %s", task.ID, err.Error()) return nil, fmt.Errorf("could not fetch comments for task %d: %s", task.ID, err.Error())
} }
for _, t := range ttasks {
t.Comments = comments
}
return return
} }
@ -306,30 +298,15 @@ func reindexTasksInTypesense(s *xorm.Session, tasks map[int64]*Task) (err error)
projects := make(map[int64]*Project) projects := make(map[int64]*Project)
typesenseTasks := []interface{}{} typesenseTasks := []interface{}{}
taskIDs := []string{}
for _, task := range tasks { for _, task := range tasks {
ttasks, err := getTypesenseTaskForTask(s, task, projects) ttask, err := getTypesenseTaskForTask(s, task, projects)
if err != nil { if err != nil {
return err return err
} }
for _, ttask := range ttasks { typesenseTasks = append(typesenseTasks, ttask)
typesenseTasks = append(typesenseTasks, ttask)
}
taskIDs = append(taskIDs, strconv.FormatInt(task.ID, 10))
}
_, err = typesenseClient.Collection("tasks").
Documents().
Delete(context.Background(), &api.DeleteDocumentsParams{
FilterBy: pointer.String("task_id:[" + strings.Join(taskIDs, ",") + "]"),
})
if err != nil {
log.Errorf("Could not delete old tasks in Typesense", err)
return err
} }
_, err = typesenseClient.Collection("tasks"). _, err = typesenseClient.Collection("tasks").
@ -426,7 +403,6 @@ func indexDummyTask() (err error) {
type typesenseTask struct { type typesenseTask struct {
ID string `json:"id"` ID string `json:"id"`
TaskID string `json:"task_id"`
Title string `json:"title"` Title string `json:"title"`
Description string `json:"description"` Description string `json:"description"`
Done bool `json:"done"` Done bool `json:"done"`
@ -451,22 +427,15 @@ type typesenseTask struct {
Assignees interface{} `json:"assignees"` Assignees interface{} `json:"assignees"`
Labels interface{} `json:"labels"` Labels interface{} `json:"labels"`
//RelatedTasks interface{} `json:"related_tasks"` // TODO //RelatedTasks interface{} `json:"related_tasks"` // TODO
Attachments interface{} `json:"attachments"` Attachments interface{} `json:"attachments"`
Comments interface{} `json:"comments"` Comments interface{} `json:"comments"`
Position float64 `json:"position"` Positions map[string]float64 `json:"positions"`
ProjectViewID int64 `json:"project_view_id"`
} }
func convertTaskToTypesenseTask(task *Task, position *TaskPosition) *typesenseTask { func convertTaskToTypesenseTask(task *Task, positions []*TaskPosition) *typesenseTask {
var projectViewID int64
if position != nil {
projectViewID = position.ProjectViewID
}
tt := &typesenseTask{ tt := &typesenseTask{
ID: fmt.Sprintf("%d_%d", task.ID, projectViewID), ID: fmt.Sprintf("%d", task.ID),
TaskID: fmt.Sprintf("%d", task.ID),
Title: task.Title, Title: task.Title,
Description: task.Description, Description: task.Description,
Done: task.Done, Done: task.Done,
@ -491,9 +460,7 @@ func convertTaskToTypesenseTask(task *Task, position *TaskPosition) *typesenseTa
Assignees: task.Assignees, Assignees: task.Assignees,
Labels: task.Labels, Labels: task.Labels,
//RelatedTasks: task.RelatedTasks, //RelatedTasks: task.RelatedTasks,
Attachments: task.Attachments, Attachments: task.Attachments,
Position: position.Position,
ProjectViewID: projectViewID,
} }
if task.DoneAt.IsZero() { if task.DoneAt.IsZero() {
@ -509,6 +476,10 @@ func convertTaskToTypesenseTask(task *Task, position *TaskPosition) *typesenseTa
tt.EndDate = nil tt.EndDate = nil
} }
for _, position := range positions {
tt.Positions["view_"+strconv.FormatInt(position.ProjectViewID, 10)] = position.Position
}
return tt return tt
} }