Implemented reminder
This commit is contained in:
parent
d89d63803f
commit
c352176417
@ -44,6 +44,8 @@ func (ntfy NTFY) Send(uuid string, msg []byte) (err error) {
|
|||||||
|
|
||||||
ackURL := fmt.Sprintf("http, OK, %s/notification/ack?uuid=%s", ntfy.AcknowledgeURL, uuid)
|
ackURL := fmt.Sprintf("http, OK, %s/notification/ack?uuid=%s", ntfy.AcknowledgeURL, uuid)
|
||||||
req.Header.Add("X-Actions", ackURL)
|
req.Header.Add("X-Actions", ackURL)
|
||||||
|
req.Header.Add("X-Priority", "5")
|
||||||
|
req.Header.Add("X-Tags", "calendar")
|
||||||
|
|
||||||
res, err = http.DefaultClient.Do(req)
|
res, err = http.DefaultClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
72
schedule.go
72
schedule.go
@ -8,6 +8,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@ -17,19 +18,20 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Schedule struct {
|
type Schedule struct {
|
||||||
ID int
|
ID int
|
||||||
UserID int `json:"user_id" db:"user_id"`
|
UserID int `json:"user_id" db:"user_id"`
|
||||||
Node Node
|
Node Node
|
||||||
ScheduleUUID string `db:"schedule_uuid"`
|
ScheduleUUID string `db:"schedule_uuid"`
|
||||||
Time time.Time
|
Time time.Time
|
||||||
Description string
|
RemindMinutes int `db:"remind_minutes"`
|
||||||
Acknowledged bool
|
Description string
|
||||||
|
Acknowledged bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func ScanForSchedules(timeOffset int, content string) (schedules []Schedule) {// {{{
|
func ScanForSchedules(timeOffset int, content string) (schedules []Schedule) { // {{{
|
||||||
schedules = []Schedule{}
|
schedules = []Schedule{}
|
||||||
|
|
||||||
rxp := regexp.MustCompile(`\{\s*([0-9]{4}-[0-9]{2}-[0-9]{2}\s+[0-9]{2}:[0-9]{2}(?::[0-9]{2})?)\s*\,\s*([^\]]+?)\s*\}`)
|
rxp := regexp.MustCompile(`\{\s*([0-9]{4}-[0-9]{2}-[0-9]{2}\s+[0-9]{2}:[0-9]{2}(?::[0-9]{2})?)\s*\,\s*(?:(\d+)\s*(h|min)\s*,)?\s*([^\]]+?)\s*\}`)
|
||||||
foundSchedules := rxp.FindAllStringSubmatch(content, -1)
|
foundSchedules := rxp.FindAllStringSubmatch(content, -1)
|
||||||
|
|
||||||
for _, data := range foundSchedules {
|
for _, data := range foundSchedules {
|
||||||
@ -38,6 +40,7 @@ func ScanForSchedules(timeOffset int, content string) (schedules []Schedule) {//
|
|||||||
data[1] = data[1] + ":00"
|
data[1] = data[1] + ":00"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Timezone calculations
|
||||||
var timeTZ string
|
var timeTZ string
|
||||||
if timeOffset < 0 {
|
if timeOffset < 0 {
|
||||||
hours := (-timeOffset) / 60
|
hours := (-timeOffset) / 60
|
||||||
@ -54,16 +57,30 @@ func ScanForSchedules(timeOffset int, content string) (schedules []Schedule) {//
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Reminder
|
||||||
|
var remindMinutes int
|
||||||
|
if data[2] != "" && data[3] != "" {
|
||||||
|
value, _ := strconv.Atoi(data[2])
|
||||||
|
unit := strings.ToLower(data[3])
|
||||||
|
switch unit {
|
||||||
|
case "min":
|
||||||
|
remindMinutes = value
|
||||||
|
case "h":
|
||||||
|
remindMinutes = value * 60
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
schedule := Schedule{
|
schedule := Schedule{
|
||||||
Time: timestamp,
|
Time: timestamp,
|
||||||
Description: data[2],
|
RemindMinutes: remindMinutes,
|
||||||
|
Description: data[4],
|
||||||
}
|
}
|
||||||
schedules = append(schedules, schedule)
|
schedules = append(schedules, schedule)
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}// }}}
|
} // }}}
|
||||||
func RetrieveSchedules(userID int, nodeID int) (schedules []Schedule, err error) {// {{{
|
func RetrieveSchedules(userID int, nodeID int) (schedules []Schedule, err error) { // {{{
|
||||||
schedules = []Schedule{}
|
schedules = []Schedule{}
|
||||||
|
|
||||||
res := service.Db.Conn.QueryRow(`
|
res := service.Db.Conn.QueryRow(`
|
||||||
@ -73,7 +90,7 @@ func RetrieveSchedules(userID int, nodeID int) (schedules []Schedule, err error)
|
|||||||
user_id,
|
user_id,
|
||||||
json_build_object('id', node_id) AS node,
|
json_build_object('id', node_id) AS node,
|
||||||
schedule_uuid,
|
schedule_uuid,
|
||||||
time,
|
time - MAKE_INTERVAL(mins => remind_minutes) AS time,
|
||||||
description,
|
description,
|
||||||
acknowledged
|
acknowledged
|
||||||
FROM schedule
|
FROM schedule
|
||||||
@ -100,29 +117,30 @@ func RetrieveSchedules(userID int, nodeID int) (schedules []Schedule, err error)
|
|||||||
|
|
||||||
err = json.Unmarshal(data, &schedules)
|
err = json.Unmarshal(data, &schedules)
|
||||||
return
|
return
|
||||||
}// }}}
|
} // }}}
|
||||||
|
|
||||||
func (a Schedule) IsEqual(b Schedule) bool {// {{{
|
func (a Schedule) IsEqual(b Schedule) bool { // {{{
|
||||||
return a.UserID == b.UserID &&
|
return a.UserID == b.UserID &&
|
||||||
a.Node.ID == b.Node.ID &&
|
a.Node.ID == b.Node.ID &&
|
||||||
a.Time.Equal(b.Time) &&
|
a.Time.Equal(b.Time) &&
|
||||||
a.Description == b.Description
|
a.Description == b.Description
|
||||||
}// }}}
|
} // }}}
|
||||||
func (s *Schedule) Insert(queryable Queryable) error {// {{{
|
func (s *Schedule) Insert(queryable Queryable) error { // {{{
|
||||||
res := queryable.QueryRow(`
|
res := queryable.QueryRow(`
|
||||||
INSERT INTO schedule(user_id, node_id, time, description)
|
INSERT INTO schedule(user_id, node_id, time, remind_minutes, description)
|
||||||
VALUES($1, $2, $3, $4)
|
VALUES($1, $2, $3, $4, $5)
|
||||||
RETURNING id
|
RETURNING id
|
||||||
`,
|
`,
|
||||||
s.UserID,
|
s.UserID,
|
||||||
s.Node.ID,
|
s.Node.ID,
|
||||||
s.Time,
|
s.Time,
|
||||||
|
s.RemindMinutes,
|
||||||
s.Description,
|
s.Description,
|
||||||
)
|
)
|
||||||
|
|
||||||
return res.Scan(&s.ID)
|
return res.Scan(&s.ID)
|
||||||
}// }}}
|
} // }}}
|
||||||
func (s *Schedule) Delete(queryable Queryable) error {// {{{
|
func (s *Schedule) Delete(queryable Queryable) error { // {{{
|
||||||
_, err := queryable.Exec(`
|
_, err := queryable.Exec(`
|
||||||
DELETE FROM schedule
|
DELETE FROM schedule
|
||||||
WHERE
|
WHERE
|
||||||
@ -133,9 +151,9 @@ func (s *Schedule) Delete(queryable Queryable) error {// {{{
|
|||||||
s.ID,
|
s.ID,
|
||||||
)
|
)
|
||||||
return err
|
return err
|
||||||
}// }}}
|
} // }}}
|
||||||
|
|
||||||
func ExpiredSchedules() (schedules []Schedule) {// {{{
|
func ExpiredSchedules() (schedules []Schedule) { // {{{
|
||||||
schedules = []Schedule{}
|
schedules = []Schedule{}
|
||||||
|
|
||||||
res, err := service.Db.Conn.Queryx(`
|
res, err := service.Db.Conn.Queryx(`
|
||||||
@ -144,11 +162,11 @@ func ExpiredSchedules() (schedules []Schedule) {// {{{
|
|||||||
user_id,
|
user_id,
|
||||||
node_id,
|
node_id,
|
||||||
schedule_uuid,
|
schedule_uuid,
|
||||||
time,
|
time - MAKE_INTERVAL(mins => remind_minutes) AS time,
|
||||||
description
|
description
|
||||||
FROM schedule
|
FROM schedule
|
||||||
WHERE
|
WHERE
|
||||||
time < NOW() AND
|
(time - MAKE_INTERVAL(mins => remind_minutes)) < NOW() AND
|
||||||
NOT acknowledged
|
NOT acknowledged
|
||||||
ORDER BY
|
ORDER BY
|
||||||
time ASC
|
time ASC
|
||||||
@ -168,4 +186,4 @@ func ExpiredSchedules() (schedules []Schedule) {// {{{
|
|||||||
schedules = append(schedules, s)
|
schedules = append(schedules, s)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}// }}}
|
} // }}}
|
||||||
|
1
sql/00019.sql
Normal file
1
sql/00019.sql
Normal file
@ -0,0 +1 @@
|
|||||||
|
ALTER TABLE public.schedule ADD COLUMN remind_minutes int NOT NULL DEFAULT 0;
|
Loading…
Reference in New Issue
Block a user