-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
274 lines (244 loc) · 7.01 KB
/
main.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package main
import (
"context"
"fmt"
"os"
"os/signal"
"regexp"
"strings"
"syscall"
"time"
"github.com/getlantern/systray"
"github.com/koltyakov/github-notify/icon"
)
var (
appname = "github-notify"
version string
)
var (
appConf = &settings{}
menu = map[string]*systray.MenuItem{}
appCtx, appCtxCancel = context.WithCancel(context.Background())
tray = &Tray{} // Tray state cache
autoStart = &Autostart{}
ghClient *GitHubClient
repoEvents map[string]int
)
// Init systray applications
func main() {
// Graceful shutdown signalling
grace := make(chan os.Signal, 1)
signal.Notify(grace, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
// Lock session to prevent multiple simultaneous application instances
if err := lockSession(); err != nil {
fmt.Printf("error: %s\n", err)
return
}
defer unlockSession()
// Systray exit handler
onExit := func() {
appCtxCancel()
}
// Graceful shutdown action
go func() {
<-grace
systray.Quit()
}()
// Initiate systray application
systray.Run(onReady, onExit)
}
// onReady bootstraps system tray menu logic
func onReady() {
tray.SetIcon(icon.Base)
tray.SetTitle("Loading...")
// Get app settings
c, err := getSettings()
if err != nil {
onError(err)
}
appConf = &c
// Initiate GitHub client
ghClient = NewGitHubClient(context.Background(), appConf.GithubToken)
// Check autostart settings
if err := autoStart.Configure(appConf.AutoStart); err != nil {
fmt.Println(err)
}
// Menu items
menu["notifications"] = systray.AddMenuItem("Notifications", "Open notifications on GitHub")
menu["markAsRead"] = systray.AddMenuItem("Mark as read", "Marks current notifications as read")
menu["filter"] = systray.AddMenuItem("Filter mode", "") // "Notifications filter mode switch")
menu["filter:all"] = menu["filter"].AddSubMenuItem("All notifications", "Show all notifications")
menu["filter:favorite"] = menu["filter"].AddSubMenuItem("Favorite repos", "Show notification only from favorite repositories")
menu["getToken"] = systray.AddMenuItem("Get Token", "Open token creation page")
menu["settings"] = systray.AddMenuItem("Settings", "Open applications settings")
systray.AddSeparator()
menu["about"] = systray.AddMenuItem("About", "About GitHub Notify")
menu["quit"] = systray.AddMenuItem("Quit", "Quit GitHub Notify")
// Default states for menu items
menu["markAsRead"].Disable()
menu["getToken"].Hide()
// Notification filters
checkNotificationMode(appConf.FiltersMode)
// Menu actions
go menuActions()
// Show get token item only when no token is provided
if appConf.GithubToken == "" {
onEmptyToken()
}
// Infinite service loop
for {
<-time.After(run(1*time.Second, appConf))
}
}
// menuActions watch to menu actions channels
// must be started in a goroutine, otherwise blocks the loop
func menuActions() {
for {
select {
case <-menu["notifications"].ClickedCh:
onOpenLinkHandler("https://github.com/notifications?query=is%3Aunread")
case <-menu["markAsRead"].ClickedCh:
onMarkAsRead("")
case <-menu["filter:all"].ClickedCh:
onNotificationModeChange("all")
case <-menu["filter:favorite"].ClickedCh:
onNotificationModeChange("favorite")
case <-menu["getToken"].ClickedCh:
onOpenLinkHandler("https://github.com/settings/tokens/new?scopes=notifications&description=GitHub%20Notify%20App")
case <-menu["settings"].ClickedCh:
openSettingsHandler()
case <-menu["about"].ClickedCh:
onOpenLinkHandler("https://github.com/koltyakov/github-notify")
case <-menu["quit"].ClickedCh:
systray.Quit()
return
}
}
}
// run executes notification checks logic
func run(timeout time.Duration, cnfg *settings) time.Duration {
// Get notification only when having access token
if cnfg.GithubToken != "" {
// Request GitHub API
notifications, err := ghClient.GetNotifications()
if err != nil {
if onError(err); strings.Contains(err.Error(), "401 Bad credentials") {
menu["getToken"].Show()
cnfg.GithubToken = ""
return 0 // continue
}
} else {
re := map[string]int{}
for _, n := range notifications {
re[*n.Repository.FullName] = re[*n.Repository.FullName] + 1
}
repoEvents = re
onNotification(len(notifications), re, cnfg)
}
// Timeout duration from settings
d, err := time.ParseDuration(cnfg.UpdateFrequency)
if err != nil {
fmt.Printf("error parsing update frequency: %s\n", err)
d = 30 * time.Second
}
timeout = d
}
return timeout
}
// openSettingsHandler handler
func openSettingsHandler() {
menu["settings"].Disable()
go func() {
newCnfg, upd, err := openSettings(appCtx)
if err != nil {
onError(err)
}
menu["settings"].Enable()
if upd && err == nil {
appConf = &newCnfg
ghClient.SetToken(appConf.GithubToken)
menu["getToken"].Hide()
if appConf.GithubToken == "" {
onEmptyToken()
}
// Configure autostart settings
if err := autoStart.Configure(appConf.AutoStart); err != nil {
fmt.Println(err)
}
// check updates immediately after settings change
go func() { _ = run(0, appConf) }()
}
}()
}
// onError system tray menu on error event handler
func onError(err error) {
fmt.Printf("error: %s\n", err)
tray.SetTitle("Error")
tray.SetTooltip(fmt.Sprintf("Error: %s", err))
tray.SetIcon(icon.Err)
}
// onEmptyToken system tray menu on empty token event handler
func onEmptyToken() {
menu["getToken"].Show()
tray.SetTitle("No Token")
tray.SetTooltip("Error: no access token has been provided")
tray.SetIcon(icon.Err)
}
// onNotificationModeChange notification mode (all, favorite) change handler
func onNotificationModeChange(mode string) {
c, err := setFilterMode(mode)
if err != nil {
fmt.Printf("error: %s\n", err)
return
}
appConf = &c
checkNotificationMode(mode)
go func() { _ = run(0, appConf) }()
}
// onMarkAsRead mark notifications as read handler
func onMarkAsRead(repo string) {
if repo == "" {
// Mark all repositories as read when a specific repo is not provided
for rn := range repoEvents {
markAsRead := true
if appConf.FiltersMode == "favorite" {
markAsRead = false
for _, fr := range appConf.FavoriteRepos {
if matched, _ := regexp.MatchString(fr, rn); matched {
markAsRead = true
}
}
}
if markAsRead {
if err := ghClient.MarkRepositoryNotificationsRead(rn); err != nil {
fmt.Printf("error: %s\n", err)
}
}
}
} else {
// Mark a specific repository events as read
if err := ghClient.MarkRepositoryNotificationsRead(repo); err != nil {
fmt.Printf("error: %s\n", err)
}
}
go func() { _ = run(0, appConf) }()
}
// checkNotificationMode sets check mark to a notification mode item
// unchecks other selected modes
func checkNotificationMode(mode string) {
for mKey, mItem := range menu {
if strings.Index(mKey, "filter:") != -1 {
if mKey == "filter:"+mode {
mItem.Check()
continue
}
mItem.Uncheck()
}
}
}
// onOpenLinkHandler on open link handler
func onOpenLinkHandler(url string) {
if err := openBrowser(url); err != nil {
fmt.Printf("error opening browser: %s\n", err)
}
}