-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreminders.go
86 lines (74 loc) · 2.14 KB
/
reminders.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"fmt"
"log"
"net/smtp"
"strconv"
"strings"
"time"
)
func sendEmail(to []string, subject string, headers string, msg string) error {
body := fmt.Sprintf("Subject: %s\n%s\n\n%s", subject, headers, msg)
auth := smtp.PlainAuth(
"",
smtpUsername,
smtpPassword,
smtpServer,
)
return smtp.SendMail(smtpServer+":"+strconv.Itoa(smtpPort), auth, reminderEmail, to, []byte(body))
}
func sendReminderEmail(t Todo) error {
user, err := GetUser(t.UserId)
if err != nil {
return err
}
subject := fmt.Sprintf("ToGoDo Reminder: %s", t.Description)
headers := "Content-Type: text/plain; charset=ISO-8859-1\n"
headers += fmt.Sprintf("To: %s <%s>", user.Name, user.Email)
body := fmt.Sprintf("Description: %s\n", t.Description)
if t.HasDueDate {
body += fmt.Sprintf("Due: %s\n", t.DueDate)
}
if len(t.Tags) > 0 {
body += fmt.Sprintf("Tags: %s\n", strings.Join(strings.Split(t.Tags, "\n"), ", "))
}
if len(t.Notes) > 0 {
body += fmt.Sprintf("Notes: %s\n", t.Notes)
}
hash := GetPostponeHash(&t, user)
body += fmt.Sprintf("\nPostpone by...\n")
body += fmt.Sprintf("An hour: %s/postpone/%d/?hash=%s&seconds=%d\n", httpAddress, t.TodoId, hash, 60*60)
body += fmt.Sprintf("A day: %s/postpone/%d/?hash=%s&seconds=%d\n", httpAddress, t.TodoId, hash, 60*60*24)
body += fmt.Sprintf("A week: %s/postpone/%d/?hash=%s&seconds=%d\n", httpAddress, t.TodoId, hash, 60*60*24*7)
body += fmt.Sprintf("A month: %s/postpone/%d/?hash=%s&seconds=%d", httpAddress, t.TodoId, hash, 60*60*24*7*30)
return sendEmail([]string{user.Email}, subject, headers, body)
}
func sendReminders(t time.Time) {
var todos []Todo
_, err := DB.Select(&todos, "SELECT * FROM todos WHERE HasReminder=? AND Completed=? AND Reminder<?", true, false, t)
if err != nil {
log.Print(err)
return
}
for i := range todos {
err = sendReminderEmail(todos[i])
if err != nil {
log.Print(err)
continue
}
err = todos[i].MarkReminded()
if err != nil {
log.Print(err)
}
}
}
func reminderRoutine(C <-chan time.Time) {
for {
t := <-C
sendReminders(t)
}
}
func startReminderRoutine() {
ticker := time.NewTicker(time.Minute)
go reminderRoutine(ticker.C)
}