-
Notifications
You must be signed in to change notification settings - Fork 2
/
notificationsv2.go
376 lines (344 loc) · 11.8 KB
/
notificationsv2.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package main
import (
"context"
"fmt"
"net/http"
"net/url"
"os"
"path"
"sort"
"strings"
"sync"
"time"
"dmitri.shuralyov.com/route/gerrit"
"dmitri.shuralyov.com/route/github"
gerritapichange "dmitri.shuralyov.com/service/change/gerritapi"
gerritapi "github.com/andygrunwald/go-gerrit"
"github.com/fsnotify/fsnotify"
githubv3 "github.com/google/go-github/github"
"github.com/gregjones/httpcache"
"github.com/shurcooL/githubv4"
"github.com/shurcooL/home/httputil"
gerritactivity "github.com/shurcooL/home/internal/exp/service/activity/gerrit"
githubactivity "github.com/shurcooL/home/internal/exp/service/activity/github"
"github.com/shurcooL/home/internal/exp/service/notification"
notificationfs "github.com/shurcooL/home/internal/exp/service/notification/fs"
"github.com/shurcooL/home/internal/exp/service/notification/httphandler"
"github.com/shurcooL/home/internal/exp/service/notification/httproute"
"github.com/shurcooL/httperror"
"github.com/shurcooL/users"
"golang.org/x/net/webdav"
"golang.org/x/oauth2"
)
// For now, the notification v2 service and app are additive.
// They co-exist with the v1 service and app side-by-side.
// Need to do more work to make it feature complete and stable,
// migrate other apps to be able to use notification service v2,
// and then remove the v1 service and app.
type router interface {
github.Router
gerrit.Router
}
func newNotificationServiceV2(
ctx context.Context,
wg *sync.WaitGroup,
fs webdav.FileSystem,
githubActivityDir string,
gerritActivityDir string,
users users.Service,
router router,
) (notification.Service, *githubactivity.Service, *gerritactivity.Service, error) {
dmitshur, err := users.Get(context.Background(), dmitshur)
if err != nil {
return nil, nil, nil, err
}
newGitHubActivity, err := newDirWatcher(ctx, githubActivityDir)
if err != nil {
return nil, nil, nil, fmt.Errorf("newDirWatcher: %v", err)
}
authTransport := &oauth2.Transport{
Source: oauth2.StaticTokenSource(&oauth2.Token{AccessToken: os.Getenv("HOME_GH_DMITSHUR_NOTIFICATIONS")}),
}
cacheTransport := &httpcache.Transport{
Transport: authTransport,
Cache: httpcache.NewMemoryCache(),
MarkCachedResponses: true,
}
githubActivity, err := githubactivity.NewService(
fs,
http.Dir(githubActivityDir), newGitHubActivity,
githubv3.NewClient(&http.Client{Transport: cacheTransport, Timeout: 10 * time.Second}),
githubv4.NewClient(&http.Client{Transport: authTransport, Timeout: 10 * time.Second}),
dmitshur, users, router,
)
if err != nil {
return nil, nil, nil, err
}
newGerritActivity, err := newDirWatcher(ctx, gerritActivityDir)
if err != nil {
return nil, nil, nil, fmt.Errorf("newDirWatcher: %v", err)
}
// TODO, THINK: reuse client from newChangeService?
gerritClient, err := gerritapi.NewClient( // TODO: Auth.
"https://go-review.googlesource.com/",
&http.Client{Transport: httpcache.NewMemoryCacheTransport()},
)
if err != nil {
panic(fmt.Errorf("internal error: gerrit.NewClient returned non-nil error: %v", err))
}
gerritChange := gerritapichange.NewService(gerritClient)
gerritActivity, err := gerritactivity.NewService(
ctx, wg, fs,
http.Dir(gerritActivityDir), newGerritActivity,
gerritChange,
dmitshur, users, router,
)
if err != nil {
return nil, nil, nil, err
}
notifService := dmitshurSeesExternalNotificationsV2{
local: notificationfs.NewService(fs, users),
dmitshurGitHubNotification: githubActivity,
dmitshurGerritNotification: gerritActivity,
users: users,
}
return notifService, githubActivity, gerritActivity, nil
}
func initNotificationsV2(
mux *http.ServeMux,
notifService notification.Service,
notifsApp httperror.Handler,
githubActivity interface{ Status() string },
gerritActivity interface{ Status() string },
users users.Service,
) {
// Register HTTP API endpoints.
notificationAPIHandler := httphandler.Notification{Notification: notifService}
mux.Handle(path.Join("/api/notificationv2", httproute.ListNotifications), headerAuth{httputil.ErrorHandler(users, notificationAPIHandler.ListNotifications)})
mux.Handle(path.Join("/api/notificationv2", httproute.StreamNotifications), headerAuth{httputil.ErrorHandler(users, notificationAPIHandler.StreamNotifications)})
mux.Handle(path.Join("/api/notificationv2", httproute.CountNotifications), headerAuth{httputil.ErrorHandler(users, notificationAPIHandler.CountNotifications)})
mux.Handle(path.Join("/api/notificationv2", httproute.MarkThreadRead), headerAuth{httputil.ErrorHandler(users, notificationAPIHandler.MarkThreadRead)})
notificationsHandler := cookieAuth{httputil.ErrorHandler(users, func(w http.ResponseWriter, req *http.Request) error {
// TODO: Keep simplifying this.
prefixLen := len("/notifications")
if prefix := req.URL.Path[:prefixLen]; req.URL.Path == prefix+"/" {
baseURL := prefix
if req.URL.RawQuery != "" {
baseURL += "?" + req.URL.RawQuery
}
return httperror.Redirect{URL: baseURL}
}
returnURL := req.URL.Path
err := notifsApp.ServeHTTP(w, req)
// TODO: Factor out this os.IsPermission(err) && u == nil check somewhere, if possible. (But this shouldn't apply for APIs.)
if s := req.Context().Value(sessionContextKey).(*session); os.IsPermission(err) && s == nil {
loginURL := (&url.URL{
Path: "/login",
RawQuery: url.Values{returnParameterName: {returnURL}}.Encode(),
}).String()
return httperror.Redirect{URL: loginURL}
}
return err
})}
mux.Handle("/notifications", notificationsHandler)
mux.Handle("/notifications/", notificationsHandler)
statusHandler := cookieAuth{httputil.ErrorHandler(users, func(w http.ResponseWriter, req *http.Request) error {
if err := httputil.AllowMethods(req, http.MethodGet, http.MethodHead); err != nil {
return err
}
if user, err := users.GetAuthenticated(req.Context()); err != nil {
return err
} else if !user.SiteAdmin {
return os.ErrPermission
}
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
if req.Method == http.MethodHead {
return nil
}
fmt.Fprintln(w, "GitHub Activity Service:", githubActivity.Status())
fmt.Fprintln(w, "Gerrit Activity Service:", gerritActivity.Status())
return nil
})}
mux.Handle("/notifications/status", statusHandler)
}
func newDirWatcher(ctx context.Context, dir string) (<-chan struct{}, error) {
var ch = make(chan struct{}, 1)
w, err := fsnotify.NewWatcher()
if err != nil {
return nil, fmt.Errorf("fsnotify.NewWatcher: %v", err)
}
go func() {
defer w.Close()
for {
select {
case <-ctx.Done():
return
case _, ok := <-w.Events:
if !ok {
return
}
select {
case ch <- struct{}{}:
default:
}
case _, ok := <-w.Errors:
if !ok {
return
}
}
}
}()
err = w.Add(dir)
if err != nil {
return nil, fmt.Errorf("watcher.Add(%q): %v", dir, err)
}
return ch, nil
}
// dmitshurSeesExternalNotificationsV2 gives dmitshur access to notifications on GitHub and Gerrit,
// in addition to local ones.
type dmitshurSeesExternalNotificationsV2 struct {
local notification.Service
dmitshurGitHubNotification notification.Service
dmitshurGerritNotification notification.Service
users users.Service
}
func (s dmitshurSeesExternalNotificationsV2) ListNotifications(ctx context.Context, opt notification.ListOptions) ([]notification.Notification, error) {
var nss []notification.Notification
var errors []error
ns, err := s.local.ListNotifications(ctx, opt)
if err != nil {
errors = append(errors, err)
}
nss = append(nss, ns...)
if opt.Namespace == "" || strings.HasPrefix(opt.Namespace, "github.com/") &&
opt.Namespace != "github.com/shurcooL/issuesapp" && opt.Namespace != "github.com/shurcooL/notificationsapp" {
currentUser, err := s.users.GetAuthenticatedSpec(ctx)
if err != nil {
return nil, err
}
if currentUser == dmitshur {
ns, err := s.dmitshurGitHubNotification.ListNotifications(ctx, opt)
if err != nil {
errors = append(errors, fmt.Errorf("dmitshurGitHubNotification.ListNotifications: %v", err))
}
nss = append(nss, ns...)
}
}
if opt.Namespace == "" || strings.HasPrefix(opt.Namespace, "go.googlesource.com/") {
currentUser, err := s.users.GetAuthenticatedSpec(ctx)
if err != nil {
return nil, err
}
if currentUser == dmitshur {
ns, err := s.dmitshurGerritNotification.ListNotifications(ctx, opt)
if err != nil {
errors = append(errors, fmt.Errorf("dmitshurGerritNotification.ListNotifications: %v", err))
}
nss = append(nss, ns...)
}
}
sort.SliceStable(nss, func(i, j int) bool { return nss[i].Time.After(nss[j].Time) })
if len(nss) > 100 {
nss = nss[:100]
}
if len(errors) > 0 {
return nss, fmt.Errorf("%d errors, including: %v", len(errors), errors[0])
}
return nss, nil
}
func (s dmitshurSeesExternalNotificationsV2) StreamNotifications(ctx context.Context, ch chan<- []notification.Notification) error {
err := s.local.StreamNotifications(ctx, ch)
if err != nil {
return err
}
currentUser, err := s.users.GetAuthenticatedSpec(ctx)
if err != nil {
return err
}
if currentUser == dmitshur {
err := s.dmitshurGitHubNotification.StreamNotifications(ctx, ch)
if err != nil {
return err
}
err = s.dmitshurGerritNotification.StreamNotifications(ctx, ch)
if err != nil {
return err
}
}
return nil
}
func (s dmitshurSeesExternalNotificationsV2) CountNotifications(ctx context.Context) (uint64, error) {
var count uint64
var errors []error
n, err := s.local.CountNotifications(ctx)
if err != nil {
errors = append(errors, err)
}
count += n
currentUser, err := s.users.GetAuthenticatedSpec(ctx)
if err != nil {
return 0, err
}
if currentUser == dmitshur {
n, err := s.dmitshurGitHubNotification.CountNotifications(ctx)
if err != nil {
errors = append(errors, fmt.Errorf("dmitshurGitHubNotification.CountNotifications: %v", err))
}
count += n
n, err = s.dmitshurGerritNotification.CountNotifications(ctx)
if err != nil {
errors = append(errors, fmt.Errorf("dmitshurGerritNotification.CountNotifications: %v", err))
}
count += n
}
if len(errors) > 0 {
return count, fmt.Errorf("%d errors, including: %v", len(errors), errors[0])
}
return count, nil
}
func (s dmitshurSeesExternalNotificationsV2) MarkThreadRead(ctx context.Context, namespace, threadType string, threadID uint64) error {
service, err := s.service(ctx, namespace)
if err != nil {
return err
}
return service.MarkThreadRead(ctx, namespace, threadType, threadID)
}
func (s dmitshurSeesExternalNotificationsV2) SubscribeThread(ctx context.Context, namespace, threadType string, threadID uint64, subscribers []users.UserSpec) error {
service, err := s.service(ctx, namespace)
if err != nil {
return err
}
return service.SubscribeThread(ctx, namespace, threadType, threadID, subscribers)
}
func (s dmitshurSeesExternalNotificationsV2) NotifyThread(ctx context.Context, namespace, threadType string, threadID uint64, nr notification.NotificationRequest) error {
service, err := s.service(ctx, namespace)
if err != nil {
return err
}
return service.NotifyThread(ctx, namespace, threadType, threadID, nr)
}
func (s dmitshurSeesExternalNotificationsV2) service(ctx context.Context, namespace string) (notification.Service, error) {
switch {
default:
return s.local, nil
case strings.HasPrefix(namespace, "github.com/") &&
namespace != "github.com/shurcooL/issuesapp" && namespace != "github.com/shurcooL/notificationsapp":
currentUser, err := s.users.GetAuthenticatedSpec(ctx)
if err != nil {
return nil, err
}
if currentUser != dmitshur {
return nil, os.ErrPermission
}
return s.dmitshurGitHubNotification, nil
case strings.HasPrefix(namespace, "go.googlesource.com/"):
currentUser, err := s.users.GetAuthenticatedSpec(ctx)
if err != nil {
return nil, err
}
if currentUser != dmitshur {
return nil, os.ErrPermission
}
return s.dmitshurGerritNotification, nil
}
}