-
Notifications
You must be signed in to change notification settings - Fork 124
/
subscription.go
137 lines (111 loc) · 3.81 KB
/
subscription.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
package route
import (
"context"
"net/http"
dbModel "github.com/evergreen-ci/evergreen/model"
"github.com/evergreen-ci/evergreen/model/event"
"github.com/evergreen-ci/evergreen/rest/data"
"github.com/evergreen-ci/evergreen/rest/model"
"github.com/evergreen-ci/gimlet"
"github.com/evergreen-ci/utility"
"github.com/pkg/errors"
)
////////////////////////////////////////////////////////////////////////
//
// POST /rest/v2/subscriptions
type subscriptionPostHandler struct {
Subscriptions *[]model.APISubscription `json:"subscriptions"`
}
func makeSetSubscription() gimlet.RouteHandler {
return &subscriptionPostHandler{}
}
func (s *subscriptionPostHandler) Factory() gimlet.RouteHandler {
return &subscriptionPostHandler{}
}
func (s *subscriptionPostHandler) Parse(ctx context.Context, r *http.Request) error {
s.Subscriptions = &[]model.APISubscription{}
if err := utility.ReadJSON(r.Body, s.Subscriptions); err != nil {
return errors.Wrap(err, "reading subscriptions from JSON request body")
}
return nil
}
func (s *subscriptionPostHandler) Run(ctx context.Context) gimlet.Responder {
u := MustHaveUser(ctx)
err := data.SaveSubscriptions(u.Username(), *s.Subscriptions, false)
if err != nil {
return gimlet.MakeJSONInternalErrorResponder(errors.Wrapf(err, "saving subscriptions for user '%s'", u.Username()))
}
return gimlet.NewJSONResponse(struct{}{})
}
////////////////////////////////////////////////////////////////////////
//
// GET /rest/v2/subscriptions
type subscriptionGetHandler struct {
owner string
ownerType string
}
func makeFetchSubscription() gimlet.RouteHandler {
return &subscriptionGetHandler{}
}
func (s *subscriptionGetHandler) Factory() gimlet.RouteHandler {
return &subscriptionGetHandler{}
}
func (s *subscriptionGetHandler) Parse(ctx context.Context, r *http.Request) error {
u := MustHaveUser(ctx)
s.owner = r.FormValue("owner")
s.ownerType = r.FormValue("type")
if !event.IsValidOwnerType(s.ownerType) {
return errors.Errorf("invalid owner type '%s'", s.ownerType)
}
if s.owner == "" {
return errors.New("owner must be specified")
}
if s.ownerType == string(event.OwnerTypePerson) && s.owner != u.Username() {
return gimlet.ErrorResponse{
StatusCode: http.StatusUnauthorized,
Message: "cannot get subscriptions for someone other than yourself",
}
}
if s.ownerType == string(event.OwnerTypeProject) {
id, err := dbModel.GetIdForProject(s.owner)
if err != nil {
return errors.Wrapf(err, "getting ID for project '%s'", s.owner)
}
s.owner = id
}
return nil
}
func (s *subscriptionGetHandler) Run(ctx context.Context) gimlet.Responder {
subs, err := data.GetSubscriptions(s.owner, event.OwnerType(s.ownerType))
if err != nil {
return gimlet.MakeJSONInternalErrorResponder(errors.Wrapf(err, "getting subscriptions for owner '%s' and owner type '%s'", s.owner, s.ownerType))
}
return gimlet.NewJSONResponse(subs)
}
////////////////////////////////////////////////////////////////////////
//
// DELETE /rest/v2/subscriptions
type subscriptionDeleteHandler struct {
id string
}
func makeDeleteSubscription() gimlet.RouteHandler {
return &subscriptionDeleteHandler{}
}
func (s *subscriptionDeleteHandler) Factory() gimlet.RouteHandler {
return &subscriptionDeleteHandler{}
}
func (s *subscriptionDeleteHandler) Parse(ctx context.Context, r *http.Request) error {
idString := r.FormValue("id")
if idString == "" {
return errors.New("must specify a subscription ID to delete")
}
s.id = idString
return nil
}
func (s *subscriptionDeleteHandler) Run(ctx context.Context) gimlet.Responder {
u := MustHaveUser(ctx)
if err := data.DeleteSubscriptions(u.Username(), []string{s.id}); err != nil {
return gimlet.MakeJSONInternalErrorResponder(errors.Wrapf(err, "deleting subscription '%s' for user '%s'", s.id, u.Username()))
}
return gimlet.NewJSONResponse(struct{}{})
}