Skip to content

Commit

Permalink
refactor: improve notification rendering and scheduling
Browse files Browse the repository at this point in the history
  • Loading branch information
revelaction committed Aug 28, 2024
1 parent 9679ceb commit d2f62b9
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 20 deletions.
24 changes: 16 additions & 8 deletions notify/desktop/desktop.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/revelaction/ical-git/config"
"github.com/revelaction/ical-git/notify"
"html/template"
//"time"
"time"
"bytes"
)

Expand All @@ -23,7 +23,7 @@ func New(conf config.Config) *Desktop {
// icon https://specifications.freedesktop.org/icon-theme-spec/icon-theme-spec-latest.html#directory_layout
func (d *Desktop) Notify(n notify.Notification) error {

body, err := renderNotification(n)
body, err := d.renderNotification(n)
if err != nil {
return err
}
Expand All @@ -32,15 +32,24 @@ func (d *Desktop) Notify(n notify.Notification) error {
return nil
}

func renderNotification(n notify.Notification) (string, error) {
func (d *Desktop) renderNotification(n notify.Notification) (string, error) {

type NotificationWrapper struct {
notify.Notification
EventTimeZone *time.Location
}
wrapper := struct {
notify.Notification
EventTimeZone string
EventTimeConf time.Time
EventTimeZoneConf string
}{

Notification: n,
EventTimeZone: n.EventTimeTz(),
EventTimeConf: n.EventTimeConf(d.config.Location.Location),
EventTimeZoneConf: d.config.Location.Location.String(),
}

const tpl = `
📅 <b>{{.EventTime.Format "Monday, 2006-01-02"}}</b> <b>{{.EventTime.Format "🕒 15:04"}}</b> 🌍 {{.EventTimeZone}}
📅 <i>{{.EventTimeConf.Format "Monday, 2006-01-02"}}</i> <i>{{.EventTimeConf.Format "🕒 15:04"}}</i> 🌍 <i>{{.EventTimeZoneConf}}</i>
{{if .Duration}}
⏳ Duration: <b>{{.Duration}}</b>
Expand All @@ -67,7 +76,6 @@ Attendees:
return "", err
}

wrapper := NotificationWrapper{Notification: n}

var buf bytes.Buffer
if err := t.Execute(&buf, wrapper); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion notify/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type Notification struct {
}

// EventTimeConf returns the EventTime in the configured location
func (n Notification) EventTimeConf(loc time.Location) time.Time {
func (n Notification) EventTimeConf(loc *time.Location) time.Time {
return n.EventTime.In(loc)
}

Expand Down
2 changes: 1 addition & 1 deletion notify/schedule/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (s *Scheduler) Schedule(notifications []notify.Notification) error {
f := s.getNotifyFunc(n)
dur := n.Time.Sub(s.start)
slog.Info("⏰ Notification", "time", n.Time, "event_time", n.EventTime, "trigger_in", dur, "DurIso8601", n.DurIso8601, "type", n.Type, "summary", n.Summary)
dur = 10 * time.Second // Hack
dur = 3 * time.Second // Hack
timer := time.AfterFunc(dur, f)
s.timers = append(s.timers, timer)
}
Expand Down
31 changes: 21 additions & 10 deletions notify/telegram/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ import (
"time"
)

type Notifier interface {
Notify(message string, level string) error
}

type message struct {
msg string
Expand All @@ -21,7 +18,7 @@ type message struct {
// Telegram
type Telegram struct {
bot *tg.BotAPI
conf config.Config
config config.Config
}

func New(conf config.Config) *Telegram {
Expand All @@ -33,20 +30,20 @@ func New(conf config.Config) *Telegram {

return &Telegram{
bot: bot,
conf: conf,
config: conf,
}
}

func (t *Telegram) Notify(n notify.Notification) error {

message, err := renderNotification(n)
message, err := t.renderNotification(n)

if err != nil {
return err
}
//message = "YOLO"

msg := tg.NewMessage(t.conf.Telegram.ChatId, message)
msg := tg.NewMessage(t.config.Telegram.ChatId, message)
msg.ParseMode = "html"
_, err = t.bot.Send(msg)
if err != nil {
Expand All @@ -57,11 +54,25 @@ func (t *Telegram) Notify(n notify.Notification) error {
}

// https://core.telegram.org/bots/api#html-style
func renderNotification(n notify.Notification) (string, error) {
func (t *Telegram) renderNotification(n notify.Notification) (string, error) {

wrapper := struct {
notify.Notification
EventTimeZone string
EventTimeConf time.Time
EventTimeZoneConf string
}{

Notification: n,
EventTimeZone: n.EventTimeTz(),
EventTimeConf: n.EventTimeConf(t.config.Location.Location),
EventTimeZoneConf: t.config.Location.Location.String(),
}
const tpl = `
<b>{{.Summary}}</b>
📅 <b>{{.EventTime.Format "Monday, 2006-01-02"}}</b> <b>{{.EventTime.Format "🕒 15:04"}}</b> 🌍 {{.EventTimeZone}}
📅 <i>{{.EventTimeConf.Format "Monday, 2006-01-02"}}</i> <i>{{.EventTimeConf.Format "🕒 15:04"}}</i> 🌍 <i>{{.EventTimeZoneConf}}</i>
{{if .Duration}}
⏳ Duration: <b>{{.Duration}}</b>
Expand All @@ -83,13 +94,13 @@ Attendees:
{{end}}
`
// Confirmed: ✅, Postponed: 🔄Cancelled: ❌Pending: ⌛Tentative: 🤔Not Attending: 🚫
t, err := template.New("notification").Parse(tpl)
tmpl, err := template.New("notification").Parse(tpl)
if err != nil {
return "", err
}

var buf bytes.Buffer
if err := t.Execute(&buf, n); err != nil {
if err := tmpl.Execute(&buf, wrapper); err != nil {
return "", err
}

Expand Down

0 comments on commit d2f62b9

Please sign in to comment.