From c203d73b33f4424ca4de079bb0beac5cda3a795c Mon Sep 17 00:00:00 2001 From: kolaente Date: Sun, 3 Nov 2019 23:08:26 +0100 Subject: [PATCH] Fixed a bug where adding assignees or reminders via an update would re-create them and not respect already inserted ones, leaving a lot of garbage --- pkg/models/task_assignees.go | 13 ++++++++++++- pkg/models/tasks.go | 20 ++++++++++++++++++-- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/pkg/models/task_assignees.go b/pkg/models/task_assignees.go index 1b0f28be..386070a7 100644 --- a/pkg/models/task_assignees.go +++ b/pkg/models/task_assignees.go @@ -43,7 +43,7 @@ type TaskAssigneeWithUser struct { } func getRawTaskAssigneesForTasks(taskIDs []int64) (taskAssignees []*TaskAssigneeWithUser, err error) { - taskAssignees = []*TaskAssigneeWithUser{nil} + taskAssignees = []*TaskAssigneeWithUser{} err = x.Table("task_assignees"). Select("task_id, users.*"). In("task_id", taskIDs). @@ -55,6 +55,17 @@ func getRawTaskAssigneesForTasks(taskIDs []int64) (taskAssignees []*TaskAssignee // Create or update a bunch of task assignees func (t *Task) updateTaskAssignees(assignees []*User) (err error) { + // Load the current assignees + currentAssignees, err := getRawTaskAssigneesForTasks([]int64{t.ID}) + if err != nil { + return err + } + + t.Assignees = make([]*User, 0, len(currentAssignees)) + for _, assignee := range currentAssignees { + t.Assignees = append(t.Assignees, &assignee.User) + } + // If we don't have any new assignees, delete everything right away. Saves us some hassle. if len(assignees) == 0 && len(t.Assignees) > 0 { _, err = x.Where("task_id = ?", t.ID). diff --git a/pkg/models/tasks.go b/pkg/models/tasks.go index 137df073..88d69f6e 100644 --- a/pkg/models/tasks.go +++ b/pkg/models/tasks.go @@ -369,6 +369,12 @@ func GetTasksByUIDs(uids []string) (tasks []*Task, err error) { return } +func getRemindersForTasks(taskIDs []int64) (reminders []*TaskReminder, err error) { + reminders = []*TaskReminder{} + err = x.Table("task_reminders").In("task_id", taskIDs).Find(&reminders) + return +} + // 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(taskMap map[int64]*Task) (tasks []*Task, err error) { @@ -456,8 +462,7 @@ func addMoreInfoToTasks(taskMap map[int64]*Task) (tasks []*Task, err error) { } // Get all reminders and put them in a map to have it easier later - reminders := []*TaskReminder{} - err = x.Table("task_reminders").In("task_id", taskIDs).Find(&reminders) + reminders, err := getRemindersForTasks(taskIDs) if err != nil { return } @@ -730,6 +735,17 @@ func updateDone(oldTask *Task, newTask *Task) { // The parameter is a slice with unix dates which holds the new reminders. func (t *Task) updateReminders(reminders []int64) (err error) { + // Load the current reminders + taskReminders, err := getRemindersForTasks([]int64{t.ID}) + if err != nil { + return err + } + + t.RemindersUnix = make([]int64, 0, len(taskReminders)) + for _, reminder := range taskReminders { + t.RemindersUnix = append(t.RemindersUnix, reminder.ReminderUnix) + } + // If we're removing everything, delete all reminders right away if len(reminders) == 0 && len(t.RemindersUnix) > 0 { _, err = x.Where("task_id = ?", t.ID).