73 lines
1.6 KiB
Go
73 lines
1.6 KiB
Go
package core
|
|
|
|
import (
|
|
"bytes"
|
|
"time"
|
|
|
|
"git.wntrmute.dev/kyle/goutils/log"
|
|
pb "git.wntrmute.dev/kyle/overpush/proto"
|
|
"github.com/gregdel/pushover"
|
|
)
|
|
|
|
var priorityMap = map[pb.Priority]int{
|
|
pb.Priority_PriorityInvalid: pushover.PriorityNormal,
|
|
pb.Priority_PriorityLowest: pushover.PriorityLowest,
|
|
pb.Priority_PriorityLow: pushover.PriorityLow,
|
|
pb.Priority_PriorityNormal: pushover.PriorityNormal,
|
|
pb.Priority_PriorityHigh: pushover.PriorityHigh,
|
|
pb.Priority_PriorityEmergency: pushover.PriorityEmergency,
|
|
}
|
|
|
|
type Message struct {
|
|
m *pushover.Message
|
|
r []string
|
|
buf *bytes.Buffer
|
|
}
|
|
|
|
func (m *Message) Close() error {
|
|
if m.buf != nil {
|
|
m.buf.Reset()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func NewMessage(pbmsg *pb.Message) *Message {
|
|
msg := &Message{
|
|
m: &pushover.Message{
|
|
Message: pbmsg.Text,
|
|
},
|
|
}
|
|
|
|
if t := pbmsg.GetTimestamp(); t != 0 {
|
|
msg.m.Timestamp = t
|
|
} else {
|
|
msg.m.Timestamp = time.Now().Unix()
|
|
}
|
|
|
|
priority, ok := priorityMap[pbmsg.Priority]
|
|
if !ok {
|
|
log.Warningln("core: message has an unknown priority; marking it as normal priority")
|
|
priority = pushover.PriorityNormal
|
|
}
|
|
msg.m.Priority = priority
|
|
|
|
if url := pbmsg.GetUrl(); url != nil {
|
|
msg.m.URL = url.GetLink()
|
|
msg.m.URLTitle = url.GetTitle()
|
|
}
|
|
|
|
if attachment := pbmsg.GetAttachment(); attachment != nil {
|
|
msg.buf = bytes.NewBuffer(attachment)
|
|
if err := msg.m.AddAttachment(msg.buf); err != nil {
|
|
log.Warningf("core: adding attachment to message failed: %s", err)
|
|
if pbmsg.Priority == pb.Priority_PriorityLowest {
|
|
return nil
|
|
}
|
|
log.Warningln("core: continuing without attachment")
|
|
}
|
|
}
|
|
|
|
msg.r = pbmsg.GetRecipients()
|
|
return msg
|
|
}
|