From 45defebcf435cade4b72763236e1e2dfdac770cc Mon Sep 17 00:00:00 2001 From: kolaente Date: Fri, 30 Sep 2022 18:35:40 +0200 Subject: [PATCH] fix: tasks with the same assignee as doer should not appear twice in overdue task mails --- pkg/models/notifications.go | 14 ++++++++++++-- pkg/models/task_overdue_reminder.go | 6 +++--- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/pkg/models/notifications.go b/pkg/models/notifications.go index 02688b8b4d3..8867d0dfde9 100644 --- a/pkg/models/notifications.go +++ b/pkg/models/notifications.go @@ -18,6 +18,7 @@ package models import ( "bufio" + "sort" "strconv" "strings" "time" @@ -230,14 +231,23 @@ func (n *UndoneTaskOverdueNotification) Name() string { // UndoneTasksOverdueNotification represents a UndoneTasksOverdueNotification notification type UndoneTasksOverdueNotification struct { User *user.User - Tasks []*Task + Tasks map[int64]*Task } // ToMail returns the mail notification for UndoneTasksOverdueNotification func (n *UndoneTasksOverdueNotification) ToMail() *notifications.Mail { - overdueLine := "" + sortedTasks := make([]*Task, 0, len(n.Tasks)) for _, task := range n.Tasks { + sortedTasks = append(sortedTasks, task) + } + + sort.Slice(sortedTasks, func(i, j int) bool { + return sortedTasks[i].DueDate.Before(sortedTasks[j].DueDate) + }) + + overdueLine := "" + for _, task := range sortedTasks { until := time.Until(task.DueDate).Round(1*time.Hour) * -1 overdueLine += `* [` + task.Title + `](` + config.ServiceFrontendurl.GetString() + "tasks/" + strconv.FormatInt(task.ID, 10) + `), overdue since ` + utils.HumanizeDuration(until) + "\n" } diff --git a/pkg/models/task_overdue_reminder.go b/pkg/models/task_overdue_reminder.go index a96db9585bb..0feff61a1c7 100644 --- a/pkg/models/task_overdue_reminder.go +++ b/pkg/models/task_overdue_reminder.go @@ -92,10 +92,10 @@ func getUndoneOverdueTasks(s *xorm.Session, now time.Time) (usersWithTasks map[i if !exists { uts[t.User.ID] = &userWithTasks{ user: t.User, - tasks: []*Task{}, + tasks: make(map[int64]*Task), } } - uts[t.User.ID].tasks = append(uts[t.User.ID].tasks, t.Task) + uts[t.User.ID].tasks[t.Task.ID] = t.Task } } @@ -104,7 +104,7 @@ func getUndoneOverdueTasks(s *xorm.Session, now time.Time) (usersWithTasks map[i type userWithTasks struct { user *user.User - tasks []*Task + tasks map[int64]*Task } // RegisterOverdueReminderCron registers a function which checks once a day for tasks that are overdue and not done.