1
0

feat(views): recalculate all positions when updating

This commit is contained in:
kolaente
2024-03-15 13:45:30 +01:00
parent 8ce476491e
commit ca4e3e01c5
5 changed files with 42 additions and 47 deletions

View File

@ -1111,14 +1111,6 @@ func (t *Task) Update(s *xorm.Session, a web.Auth) (err error) {
return err
}
// Update all positions if the newly saved position is < 0.1
if ot.Position < 0.1 {
err = recalculateTaskPositions(s, t.ProjectID)
if err != nil {
return err
}
}
// Get the task updated timestamp in a new struct - if we'd just try to put it into t which we already have, it
// would still contain the old updated date.
nt := &Task{}
@ -1141,39 +1133,6 @@ func (t *Task) Update(s *xorm.Session, a web.Auth) (err error) {
return updateProjectLastUpdated(s, &Project{ID: t.ProjectID})
}
func recalculateTaskPositions(s *xorm.Session, projectID int64) (err error) {
allTasks := []*Task{}
err = s.
Where("project_id = ?", projectID).
OrderBy("position asc").
Find(&allTasks)
if err != nil {
return
}
maxPosition := math.Pow(2, 32)
for i, task := range allTasks {
currentPosition := maxPosition / float64(len(allTasks)) * (float64(i + 1))
// Here we use "NoAutoTime() to prevent the ORM from updating column "updated" automatically.
// Otherwise, this signals to CalDAV clients that the task has changed, which is not the case.
// Consequence: when synchronizing a list of tasks, the first one immediately changes the date of all the
// following ones from the same batch, which are then unable to be updated.
_, err = s.Cols("position").
Where("id = ?", task.ID).
NoAutoTime().
Update(&Task{Position: currentPosition})
if err != nil {
return
}
}
return
}
func addOneMonthToDate(d time.Time) time.Time {
return time.Date(d.Year(), d.Month()+1, d.Day(), d.Hour(), d.Minute(), d.Second(), d.Nanosecond(), config.GetTimeZone())
}