-
Notifications
You must be signed in to change notification settings - Fork 0
/
rchooks.go
191 lines (162 loc) · 6.19 KB
/
rchooks.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
package rchooks
import (
"context"
"encoding/json"
"fmt"
"strings"
rc "github.com/grokify/go-ringcentral-client/office/v1/client"
clientutil "github.com/grokify/go-ringcentral-client/office/v1/util"
"github.com/grokify/mogo/errors/errorsutil"
"github.com/grokify/mogo/net/http/httputilmore"
"github.com/grokify/mogo/type/stringsutil"
)
const (
WebhookStatusBlacklisted = "Blacklisted"
RingCentralAPIResponseFormat = `RingCentral_API_Status_Code [%v]`
ExpiresMax = 499999999 // 15 years
HeaderValidationToken = "Validation-Token"
TransportTypePubNub = "PubNub"
TransportTypeWebHook = "WebHook"
)
func ParseCreateSubscriptionRequest(data []byte) (rc.CreateSubscriptionRequest, error) {
var req rc.CreateSubscriptionRequest
err := json.Unmarshal(data, &req)
return req, err
}
type RcHooks struct {
Client *rc.APIClient
}
func (util *RcHooks) GetSubscriptions(ctx context.Context) (rc.RecordsCollectionResourceSubscriptionResponse, error) {
info, resp, err := util.Client.PushNotificationsApi.GetSubscriptions(ctx)
if err != nil && resp.StatusCode >= 300 {
err = errorsutil.Wrap(err, string(clientutil.ApiResponseErrorBody(err)))
}
return info, httputilmore.CondenseResponseNot2xxToError(resp, err, "ERROR - Get Subscriptions API")
}
func (util *RcHooks) CreateSubscription(ctx context.Context, req rc.CreateSubscriptionRequest) (rc.SubscriptionInfo, error) {
info, resp, err := util.Client.PushNotificationsApi.CreateSubscription(ctx, req)
if err != nil && resp.StatusCode >= 300 {
err = errorsutil.Wrap(err, string(clientutil.ApiResponseErrorBody(err)))
}
return info, httputilmore.CondenseResponseNot2xxToError(resp, err, "ERROR - Create Subscription API")
}
func (util *RcHooks) DeleteSubscription(ctx context.Context, subscriptionID string) error {
resp, err := util.Client.PushNotificationsApi.DeleteSubscription(ctx, subscriptionID)
return httputilmore.CondenseResponseNot2xxToError(resp, err,
fmt.Sprintf("ERROR - Dete Subscription API id [%v]", subscriptionID))
}
func (util *RcHooks) DeleteBlacklisted(ctx context.Context, matches []rc.SubscriptionResponse) ([]rc.SubscriptionResponse, error) {
goodMatches := []rc.SubscriptionResponse{}
for _, match := range matches {
if match.Status == WebhookStatusBlacklisted {
err := util.DeleteSubscription(ctx, match.Id)
if err != nil {
return goodMatches, err
}
} else {
goodMatches = append(goodMatches, match)
}
}
return goodMatches, nil
}
func FilterSubscriptionsForRequest(ress []rc.SubscriptionResponse, req rc.CreateSubscriptionRequest) []rc.SubscriptionResponse {
reqFiltersStringSorted := stringsutil.JoinStringsTrimSpaceToLowerSort(req.EventFilters, ",")
reqWebhookURL := strings.TrimSpace(req.DeliveryMode.Address)
matches := []rc.SubscriptionResponse{}
for _, res := range ress {
resFiltersStringSorted := stringsutil.JoinStringsTrimSpaceToLowerSort(res.EventFilters, ",")
resWebhookURL := strings.TrimSpace(res.DeliveryMode.Address)
if reqWebhookURL == resWebhookURL &&
reqFiltersStringSorted == resFiltersStringSorted {
matches = append(matches, res)
}
}
return matches
}
func (util *RcHooks) RecreateSubscriptionIDOrURL(ctx context.Context, subIDOrURL string) ([]rc.SubscriptionInfo, error) {
recreated := []rc.SubscriptionInfo{}
subs, err := util.GetSubscriptions(ctx)
if err != nil {
return recreated, err
}
matches := []rc.SubscriptionResponse{}
for _, sub := range subs.Records {
if subIDOrURL == sub.Id || subIDOrURL == sub.DeliveryMode.Address {
matches = append(matches, sub)
}
}
if len(matches) == 0 {
return recreated, fmt.Errorf("no matches found for [%v]", subIDOrURL)
}
for _, sub := range matches {
newSubReq := NewCreateSubscriptionRequestPermahook(sub.EventFilters, sub.DeliveryMode.Address)
if err := util.DeleteSubscription(ctx, sub.Id); err != nil {
return recreated, err
}
if newSubInfo, err := util.CreateSubscription(ctx, newSubReq); err != nil {
return recreated, err
} else {
recreated = append(recreated, newSubInfo)
}
}
return recreated, nil
}
func (util *RcHooks) CheckAndFixSubscription(ctx context.Context, req rc.CreateSubscriptionRequest) (rc.SubscriptionInfo, error) {
recreated := rc.SubscriptionInfo{}
subs, err := util.GetSubscriptions(ctx)
if err != nil {
return recreated, err
}
matches := FilterSubscriptionsForRequest(subs.Records, req)
if remaining, err := util.DeleteBlacklisted(ctx, matches); err != nil {
return recreated, err
} else if len(remaining) == 0 {
return util.CreateSubscription(ctx, req)
}
return recreated, nil
}
func (util *RcHooks) DeleteByIDOrURL(ctx context.Context, idOrURLToDelete string) ([]rc.SubscriptionResponse, error) {
deleted := []rc.SubscriptionResponse{}
info, err := util.GetSubscriptions(ctx)
if err != nil {
return deleted, err
}
for _, subscription := range info.Records {
if idOrURLToDelete == subscription.Id ||
idOrURLToDelete == subscription.DeliveryMode.Address {
resp, err := util.Client.PushNotificationsApi.DeleteSubscription(
ctx, subscription.Id)
err = httputilmore.CondenseResponseNot2xxToError(
resp, err,
fmt.Sprintf("ERROR - Delete Subscription API Id [%v]", subscription.Id))
if err != nil {
return deleted, err
}
deleted = append(deleted, subscription)
}
}
return deleted, nil
}
func NewCreateSubscriptionRequestPermahook(eventFilters []string, hookURL string) rc.CreateSubscriptionRequest {
return rc.CreateSubscriptionRequest{
EventFilters: eventFilters,
DeliveryMode: rc.NotificationDeliveryModeRequest{
TransportType: TransportTypeWebHook,
Address: hookURL},
ExpiresIn: int32(ExpiresMax)}
}
func NewCreateSubscriptionRequestPermahookBotSimple(hookURL string) rc.CreateSubscriptionRequest {
return NewCreateSubscriptionRequestPermahook([]string{EventFilterMessagePosts}, hookURL)
}
type WebhookDefinitionThin struct {
URL string `json:"url"`
EventFilters []string `json:"eventFilters"`
}
func (thin *WebhookDefinitionThin) Full() rc.CreateSubscriptionRequest {
return rc.CreateSubscriptionRequest{
EventFilters: thin.EventFilters,
DeliveryMode: rc.NotificationDeliveryModeRequest{
TransportType: TransportTypeWebHook,
Address: thin.URL},
ExpiresIn: int32(ExpiresMax)}
}