Add undone overdue tasks reminder

This commit is contained in:
kolaente 2021-04-11 16:07:23 +02:00
parent b3c604fd2f
commit 254e7f2916
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
6 changed files with 206 additions and 9 deletions

View File

@ -346,3 +346,12 @@
index: 2
created: 2018-12-01 01:12:04
updated: 2018-12-01 01:12:04
- id: 38
title: 'task #37 done with due date'
done: true
created_by_id: 1
list_id: 22
index: 2
created: 2018-12-01 01:12:04
updated: 2018-12-01 01:12:04
due_date: 2018-10-30 22:25:24

View File

@ -93,6 +93,7 @@ func FullInit() {
// Start the cron
cron.Init()
models.RegisterReminderCron()
models.RegisterOverdueReminderCron()
// Start processing events
go func() {

View File

@ -0,0 +1,97 @@
// Vikunja is a to-do list application to facilitate your life.
// Copyright 2018-2021 Vikunja and contributors. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public Licensee as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public Licensee for more details.
//
// You should have received a copy of the GNU Affero General Public Licensee
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package models
import (
"code.vikunja.io/api/pkg/config"
"code.vikunja.io/api/pkg/cron"
"code.vikunja.io/api/pkg/db"
"code.vikunja.io/api/pkg/log"
"code.vikunja.io/api/pkg/notifications"
"code.vikunja.io/api/pkg/utils"
"time"
"xorm.io/xorm"
)
func getUndoneOverdueTasks(s *xorm.Session, now time.Time) (taskIDs []int64, err error) {
now = utils.GetTimeWithoutNanoSeconds(now)
var tasks []*Task
err = s.
Where("due_date is not null and due_date < ?", now.Format(dbTimeFormat)).
And("done = false").
Find(&tasks)
if err != nil {
return
}
for _, task := range tasks {
taskIDs = append(taskIDs, task.ID)
}
return
}
// RegisterOverdueReminderCron registers a function which checks once a day for tasks that are overdue and not done.
func RegisterOverdueReminderCron() {
if !config.ServiceEnableEmailReminders.GetBool() {
return
}
if !config.MailerEnabled.GetBool() {
log.Info("Mailer is disabled, not sending overdue per mail")
return
}
err := cron.Schedule("0 8 * * *", func() {
s := db.NewSession()
defer s.Close()
now := time.Now()
taskIDs, err := getUndoneOverdueTasks(s, now)
if err != nil {
log.Errorf("[Undone Overdue Tasks Reminder] Could not get tasks with reminders in the next minute: %s", err)
return
}
users, err := getTaskUsersForTasks(s, taskIDs)
if err != nil {
log.Errorf("[Undone Overdue Tasks Reminder] Could not get task users to send them reminders: %s", err)
return
}
log.Debugf("[Undone Overdue Tasks Reminder] Sending reminders to %d users", len(users))
for _, u := range users {
n := &ReminderDueNotification{
User: u.User,
Task: u.Task,
}
err = notifications.Notify(u.User, n)
if err != nil {
log.Errorf("[Undone Overdue Tasks Reminder] Could not notify user %d: %s", u.User.ID, err)
return
}
log.Debugf("[Undone Overdue Tasks Reminder] Sent reminder email for task %d to user %d", u.Task.ID, u.User.ID)
}
})
if err != nil {
log.Fatalf("Could not register undone overdue tasks reminder cron: %s", err)
}
}

View File

@ -0,0 +1,61 @@
// Vikunja is a to-do list application to facilitate your life.
// Copyright 2018-2021 Vikunja and contributors. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public Licensee as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public Licensee for more details.
//
// You should have received a copy of the GNU Affero General Public Licensee
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package models
import (
"code.vikunja.io/api/pkg/db"
"github.com/stretchr/testify/assert"
"testing"
"time"
)
func TestGetUndoneOverDueTasks(t *testing.T) {
t.Run("no undone tasks", func(t *testing.T) {
db.LoadAndAssertFixtures(t)
s := db.NewSession()
defer s.Close()
now, err := time.Parse(time.RFC3339Nano, "2018-01-01T01:13:00Z")
assert.NoError(t, err)
taskIDs, err := getUndoneOverdueTasks(s, now)
assert.NoError(t, err)
assert.Len(t, taskIDs, 0)
})
t.Run("undone overdue", func(t *testing.T) {
db.LoadAndAssertFixtures(t)
s := db.NewSession()
defer s.Close()
now, err := time.Parse(time.RFC3339Nano, "2018-12-01T01:13:00Z")
assert.NoError(t, err)
taskIDs, err := getUndoneOverdueTasks(s, now)
assert.NoError(t, err)
assert.Len(t, taskIDs, 1)
assert.Equal(t, int64(6), taskIDs[0])
})
t.Run("done overdue", func(t *testing.T) {
db.LoadAndAssertFixtures(t)
s := db.NewSession()
defer s.Close()
now, err := time.Parse(time.RFC3339Nano, "2018-11-01T01:13:00Z")
assert.NoError(t, err)
taskIDs, err := getUndoneOverdueTasks(s, now)
assert.NoError(t, err)
assert.Len(t, taskIDs, 0)
})
}

View File

@ -17,6 +17,7 @@
package models
import (
"code.vikunja.io/api/pkg/utils"
"time"
"code.vikunja.io/api/pkg/notifications"
@ -48,6 +49,8 @@ type taskUser struct {
User *user.User `xorm:"extends"`
}
const dbTimeFormat = `2006-01-02 15:04:05`
func getTaskUsersForTasks(s *xorm.Session, taskIDs []int64) (taskUsers []*taskUser, err error) {
if len(taskIDs) == 0 {
return
@ -103,13 +106,8 @@ func getTaskUsersForTasks(s *xorm.Session, taskIDs []int64) (taskUsers []*taskUs
}
func getTasksWithRemindersInTheNextMinute(s *xorm.Session, now time.Time) (taskIDs []int64, err error) {
now = utils.GetTimeWithoutNanoSeconds(now)
tz := config.GetTimeZone()
const dbFormat = `2006-01-02 15:04:05`
// By default, time.Now() includes nanoseconds which we don't save. That results in getting the wrong dates,
// so we make sure the time we use to get the reminders don't contain nanoseconds.
now = time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), 0, 0, now.Location()).In(tz)
nextMinute := now.Add(1 * time.Minute)
log.Debugf("[Task Reminder Cron] Looking for reminders between %s and %s to send...", now, nextMinute)
@ -117,7 +115,7 @@ func getTasksWithRemindersInTheNextMinute(s *xorm.Session, now time.Time) (taskI
reminders := []*TaskReminder{}
err = s.
Join("INNER", "tasks", "tasks.id = task_reminders.task_id").
Where("reminder >= ? and reminder < ?", now.Format(dbFormat), nextMinute.Format(dbFormat)).
Where("reminder >= ? and reminder < ?", now.Format(dbTimeFormat), nextMinute.Format(dbTimeFormat)).
And("tasks.done = false").
Find(&reminders)
if err != nil {
@ -154,9 +152,9 @@ func RegisterReminderCron() {
log.Debugf("[Task Reminder Cron] Timezone is %s", tz)
s := db.NewSession()
err := cron.Schedule("* * * * *", func() {
s := db.NewSession()
defer s.Close()
now := time.Now()
taskIDs, err := getTasksWithRemindersInTheNextMinute(s, now)

31
pkg/utils/time.go Normal file
View File

@ -0,0 +1,31 @@
// Vikunja is a to-do list application to facilitate your life.
// Copyright 2018-2021 Vikunja and contributors. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public Licensee as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public Licensee for more details.
//
// You should have received a copy of the GNU Affero General Public Licensee
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package utils
import (
"code.vikunja.io/api/pkg/config"
"time"
)
// GetTimeWithoutNanoSeconds returns a time.Time without the nanoseconds.
func GetTimeWithoutNanoSeconds(t time.Time) time.Time {
tz := config.GetTimeZone()
// By default, time.Now() includes nanoseconds which we don't save. That results in getting the wrong dates,
// so we make sure the time we use to get the reminders don't contain nanoseconds.
return time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), 0, t.Location()).In(tz)
}