-
Notifications
You must be signed in to change notification settings - Fork 0
/
static.go
133 lines (106 loc) · 3.25 KB
/
static.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package static
import (
"fmt"
"github.com/ethanbaker/horus/utils/types"
"github.com/robfig/cron/v3"
)
/* ---- INIT ---- */
// Run init functions
func Init() error {
if err := NotionInit(); err != nil {
return err
}
return nil
}
/* ---- CONSTANTS ---- */
// A map of enabled functions in the submodule. New messages can be
// called using a key in this map
var enabledFunctions = map[string]func() string{
"ping": Ping,
"notion-daily-digest": NotionDailyDigest,
"notion-night-affirmations": NotionNightAffirmations,
"notion-morning-affirmations": NotionMorningAffirmations,
}
/* ---- MESSAGE ---- */
// StaticOutreachMessage represents a statically sent message to the user. Static messages
// repeat on a given interval supplied by a CRON string by calling a custom-built
// function
type StaticOutreachMessage struct {
Function func() string // The function that will be called repeatedly
Channels []chan string // Channels that Horus will send the response to
Repeat string // The cron string used to repeat calls
cronjob *cron.Cron `gorm:"-"` // The cronjob representing the repeating function (in case we want to end the cronjob)
stopped bool `gorm:"-"` // Whether or not the task is stopped
}
func (m *StaticOutreachMessage) GetContent() string {
return m.Function()
}
func (m *StaticOutreachMessage) GetChannels() []chan string {
return m.Channels
}
func (m *StaticOutreachMessage) Start() error {
// Make sure the message is stopped
if m.stopped {
return fmt.Errorf("message is still active")
}
// Make sure the cronjob is not nil
if m.cronjob == nil {
return fmt.Errorf("cron is nil")
}
m.cronjob.Start()
return nil
}
func (m *StaticOutreachMessage) Stop() error {
// Make sure the message has not already been stopped
if m.stopped {
return fmt.Errorf("message is already stopped")
}
// Make sure the cronjob is not nil
if m.cronjob == nil {
return fmt.Errorf("cron is nil")
}
// Stop the message
return m.cronjob.Stop().Err()
}
func (m *StaticOutreachMessage) Delete() error {
// Make sure the cronjob is not nil
if m.cronjob == nil {
return fmt.Errorf("cron is nil")
}
// Stop the message
if err := m.cronjob.Stop().Err(); err != nil {
return err
}
m.cronjob = nil
return nil
}
// New creates a new static message (we don't save these to a DB because they're statically called in implementations)
func New(services *types.OutreachServices, channels []chan string, raw any) (types.OutreachMessage, error) {
// Cast the raw input data
data, ok := raw.(types.StaticOutreach)
if !ok {
return nil, fmt.Errorf("cannot properly cast input data to StaticOutreach type")
}
// Find the function key we want to run
f, ok := enabledFunctions[data.Function]
if !ok {
return nil, fmt.Errorf("function with key %v is not present", data.Function)
}
// Create the message
m := StaticOutreachMessage{
Function: f,
Repeat: data.Repeat,
Channels: channels,
}
// Create a new cron job to send the message every interval
m.cronjob = services.Cron
_, err := m.cronjob.AddFunc(m.Repeat, func() {
// Get the content of the message once
content := m.GetContent()
// Send the content through to each channel
for _, c := range m.GetChannels() {
c <- content
}
})
return &m, err
}