-
Notifications
You must be signed in to change notification settings - Fork 31
/
app.go
348 lines (294 loc) · 12.8 KB
/
app.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
package stream_chat
import (
"context"
"net/http"
"time"
)
type AppSettings struct {
Name string `json:"name"`
OrganizationName string `json:"organization"`
Suspended bool `json:"suspended"`
SuspendedExplanation string `json:"suspended_explanation"`
ConfigNameMap map[string]*ChannelConfig `json:"channel_configs"`
RevokeTokensIssuedBefore *time.Time `json:"revoke_tokens_issued_before"`
DisableAuth *bool `json:"disable_auth_checks,omitempty"`
DisablePermissions *bool `json:"disable_permissions_checks,omitempty"`
PushNotifications PushNotificationFields `json:"push_notifications"`
PushConfig *PushConfigRequest `json:"push_config,omitempty"`
APNConfig *APNConfig `json:"apn_config,omitempty"`
FirebaseConfig *FirebaseConfigRequest `json:"firebase_config,omitempty"`
XiaomiConfig *XiaomiConfigRequest `json:"xiaomi_config,omitempty"`
HuaweiConfig *HuaweiConfigRequest `json:"huawei_config,omitempty"`
WebhookURL *string `json:"webhook_url,omitempty"`
WebhookEvents []string `json:"webhook_events,omitempty"`
SqsURL *string `json:"sqs_url,omitempty"`
SqsKey *string `json:"sqs_key,omitempty"`
SqsSecret *string `json:"sqs_secret,omitempty"`
SnsTopicArn *string `json:"sns_topic_arn,omitempty"`
SnsKey *string `json:"sns_key,omitempty"`
SnsSecret *string `json:"sns_secret,omitempty"`
BeforeMessageSendHookURL *string `json:"before_message_send_hook_url,omitempty"`
CustomActionHandlerURL *string `json:"custom_action_handler_url,omitempty"`
FileUploadConfig *FileUploadConfig `json:"file_upload_config,omitempty"`
ImageUploadConfig *FileUploadConfig `json:"image_upload_config,omitempty"`
ImageModerationLabels []string `json:"image_moderation_labels,omitempty"`
ImageModerationEnabled *bool `json:"image_moderation_enabled,omitempty"`
PermissionVersion *string `json:"permission_version,omitempty"`
MigratePermissionsToV2 *bool `json:"migrate_permissions_to_v2,omitempty"`
Policies map[string][]Policy `json:"policies"`
Grants map[string][]string `json:"grants,omitempty"`
MultiTenantEnabled *bool `json:"multi_tenant_enabled,omitempty"`
AsyncURLEnrichEnabled *bool `json:"async_url_enrich_enabled,omitempty"`
AutoTranslationEnabled *bool `json:"auto_translation_enabled,omitempty"`
RemindersInterval int `json:"reminders_interval,omitempty"`
UserSearchDisallowedRoles []string `json:"user_search_disallowed_roles,omitempty"`
EnforceUniqueUsernames *string `json:"enforce_unique_usernames,omitempty"`
ChannelHideMembersOnly *bool `json:"channel_hide_members_only,omitempty"`
AsyncModerationConfig *AsyncModerationConfiguration `json:"async_moderation_config,omitempty"`
}
func (a *AppSettings) SetDisableAuth(b bool) *AppSettings {
a.DisableAuth = &b
return a
}
func (a *AppSettings) SetDisablePermissions(b bool) *AppSettings {
a.DisablePermissions = &b
return a
}
func (a *AppSettings) SetAPNConfig(c APNConfig) *AppSettings {
a.APNConfig = &c
return a
}
func (a *AppSettings) SetFirebaseConfig(c FirebaseConfigRequest) *AppSettings {
a.FirebaseConfig = &c
return a
}
func (a *AppSettings) SetWebhookURL(s string) *AppSettings {
a.WebhookURL = &s
return a
}
func (a *AppSettings) SetMultiTenant(b bool) *AppSettings {
a.MultiTenantEnabled = &b
return a
}
func (a *AppSettings) SetGrants(g map[string][]string) *AppSettings {
a.Grants = g
return a
}
func (a *AppSettings) SetAsyncModerationConfig(c AsyncModerationConfiguration) *AppSettings {
a.AsyncModerationConfig = &c
return a
}
func NewAppSettings() *AppSettings {
return &AppSettings{}
}
type AsyncModerationCallback struct {
Mode string `json:"mode"`
ServerURL string `json:"server_url"`
}
type AsyncModerationConfiguration struct {
Callback *AsyncModerationCallback `json:"callback,omitempty"`
Timeout int `json:"timeout_ms,omitempty"`
}
type FileUploadConfig struct {
AllowedFileExtensions []string `json:"allowed_file_extensions,omitempty"`
BlockedFileExtensions []string `json:"blocked_file_extensions,omitempty"`
AllowedMimeTypes []string `json:"allowed_mime_types,omitempty"`
BlockedMimeTypes []string `json:"blocked_mime_types,omitempty"`
}
type APNConfig struct {
Enabled bool `json:"enabled"`
Development bool `json:"development"`
AuthType string `json:"auth_type,omitempty"`
AuthKey string `json:"auth_key,omitempty"`
NotificationTemplate string `json:"notification_template"`
Host string `json:"host,omitempty"`
BundleID string `json:"bundle_id,omitempty"`
TeamID string `json:"team_id,omitempty"`
KeyID string `json:"key_id,omitempty"`
}
type PushNotificationFields struct {
Version string `json:"version"`
OfflineOnly bool `json:"offline_only"`
APNConfig APNConfig `json:"apn"`
FirebaseConfig FirebaseConfig `json:"firebase"`
HuaweiConfig HuaweiConfig `json:"huawei"`
XiaomiConfig XiaomiConfig `json:"xiaomi"`
Providers []PushProvider `json:"providers,omitempty"`
}
type FirebaseConfigRequest struct {
ServerKey string `json:"server_key"`
NotificationTemplate string `json:"notification_template,omitempty"`
DataTemplate string `json:"data_template,omitempty"`
APNTemplate *string `json:"apn_template,omitempty"`
CredentialsJSON string `json:"credentials_json,omitempty"`
}
type FirebaseConfig struct {
Enabled bool `json:"enabled"`
NotificationTemplate string `json:"notification_template"`
DataTemplate string `json:"data_template"`
}
type XiaomiConfigRequest struct {
PackageName string `json:"package_name"`
Secret string `json:"secret"`
}
type XiaomiConfig struct {
Enabled bool `json:"enabled"`
}
type HuaweiConfigRequest struct {
ID string `json:"id"`
Secret string `json:"secret"`
}
type HuaweiConfig struct {
Enabled bool `json:"enabled"`
}
type PushConfigRequest struct {
Version string `json:"version,omitempty"`
OfflineOnly bool `json:"offline_only,omitempty"`
}
type Policy struct {
Name string `json:"name"`
Resources []string `json:"resources"`
Roles []string `json:"roles"`
Action int `json:"action"` // allow: 1, deny: 0
Owner bool `json:"owner"`
Priority int `json:"priority"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type AppResponse struct {
App *AppSettings `json:"app"`
Response
}
// GetAppSettings returns app settings.
func (c *Client) GetAppSettings(ctx context.Context) (*AppResponse, error) {
var resp AppResponse
err := c.makeRequest(ctx, http.MethodGet, "app", nil, nil, &resp)
return &resp, err
}
// UpdateAppSettings makes request to update app settings
// Example of usage:
//
// settings := NewAppSettings().SetDisableAuth(true)
// err := client.UpdateAppSettings(settings)
func (c *Client) UpdateAppSettings(ctx context.Context, settings *AppSettings) (*Response, error) {
var resp Response
err := c.makeRequest(ctx, http.MethodPatch, "app", nil, settings, &resp)
return &resp, err
}
type CheckSQSRequest struct {
SqsURL string `json:"sqs_url"`
SqsKey string `json:"sqs_key"`
SqsSecret string `json:"sqs_secret"`
}
type CheckSQSResponse struct {
Status string `json:"status"`
Error string `json:"error"`
Data map[string]interface{} `json:"data"`
Response
}
// CheckSqs checks whether the AWS credentials are valid for the SQS queue access.
func (c *Client) CheckSqs(ctx context.Context, req *CheckSQSRequest) (*CheckSQSResponse, error) {
var resp CheckSQSResponse
err := c.makeRequest(ctx, http.MethodPost, "check_sqs", nil, req, &resp)
return &resp, err
}
type CheckSNSRequest struct {
SnsTopicARN string `json:"sns_topic_arn"`
SnsKey string `json:"sns_key"`
SnsSecret string `json:"sns_secret"`
}
type CheckSNSResponse struct {
Status string `json:"status"`
Error string `json:"error"`
Data map[string]interface{} `json:"data"`
Response
}
func (c *Client) CheckSns(ctx context.Context, req *CheckSNSRequest) (*CheckSNSResponse, error) {
var resp CheckSNSResponse
err := c.makeRequest(ctx, http.MethodPost, "check_sns", nil, req, &resp)
return &resp, err
}
type CheckPushRequest struct {
MessageID string `json:"message_id,omitempty"`
ApnTemplate string `json:"apn_template,omitempty"`
FirebaseTemplate string `json:"firebase_template,omitempty"`
FirebaseDataTemplate string `json:"firebase_data_template,omitempty"`
SkipDevices *bool `json:"skip_devices,omitempty"`
PushProviderName string `json:"push_provider_name,omitempty"`
PushProviderType string `json:"push_provider_type,omitempty"`
UserID string `json:"user_id,omitempty"`
User *User `json:"user,omitempty"`
}
type DeviceError struct {
Provider string `json:"provider"`
ProviderName string `json:"provider_name"`
ErrorMessage string `json:"error_message"`
}
type CheckPushResponse struct {
DeviceErrors map[string]DeviceError `json:"device_errors"`
GeneralErrors []string `json:"general_errors"`
SkipDevices *bool `json:"skip_devices"`
RenderedApnTemplate string `json:"rendered_apn_template"`
RenderedFirebaseTemplate string `json:"rendered_firebase_template"`
RenderedMessage map[string]string `json:"rendered_message"`
Response
}
// CheckPush initiates a push test.
func (c *Client) CheckPush(ctx context.Context, req *CheckPushRequest) (*CheckPushResponse, error) {
var resp CheckPushResponse
err := c.makeRequest(ctx, http.MethodPost, "check_push", nil, req, &resp)
return &resp, err
}
// RevokeTokens revokes all tokens for an application issued before given time.
func (c *Client) RevokeTokens(ctx context.Context, before *time.Time) (*Response, error) {
setting := make(map[string]interface{})
if before == nil {
setting["revoke_tokens_issued_before"] = nil
} else {
setting["revoke_tokens_issued_before"] = before.Format(time.RFC3339)
}
var resp Response
err := c.makeRequest(ctx, http.MethodPatch, "app", nil, setting, &resp)
return &resp, err
}
type PushProvider struct {
Type PushProviderType `json:"type"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
DisabledAt *time.Time `json:"disabled_at,omitempty"`
DisabledReason string `json:"disabled_reason,omitempty"`
APNAuthKey string `json:"apn_auth_key,omitempty"`
APNKeyID string `json:"apn_key_id,omitempty"`
APNTeamID string `json:"apn_team_id,omitempty"`
APNTopic string `json:"apn_topic,omitempty"`
FirebaseCredentials string `json:"firebase_credentials,omitempty"`
FirebaseNotificationTemplate *string `json:"firebase_notification_template,omitempty"`
FirebaseAPNTemplate *string `json:"firebase_apn_template,omitempty"`
HuaweiAppID string `json:"huawei_app_id,omitempty"`
HuaweiAppSecret string `json:"huawei_app_secret,omitempty"`
XiaomiPackageName string `json:"xiaomi_package_name,omitempty"`
XiaomiAppSecret string `json:"xiaomi_app_secret,omitempty"`
}
// UpsertPushProvider inserts or updates a push provider.
func (c *Client) UpsertPushProvider(ctx context.Context, provider *PushProvider) (*Response, error) {
body := map[string]PushProvider{"push_provider": *provider}
var resp Response
err := c.makeRequest(ctx, http.MethodPost, "push_providers", nil, body, &resp)
return &resp, err
}
// DeletePushProvider deletes a push provider.
func (c *Client) DeletePushProvider(ctx context.Context, providerType, name string) (*Response, error) {
var resp Response
err := c.makeRequest(ctx, http.MethodDelete, "push_providers/"+providerType+"/"+name, nil, nil, &resp)
return &resp, err
}
type PushProviderListResponse struct {
Response
PushProviders []PushProvider `json:"push_providers"`
}
// ListPushProviders returns the list of push providers.
func (c *Client) ListPushProviders(ctx context.Context) (*PushProviderListResponse, error) {
var providers PushProviderListResponse
err := c.makeRequest(ctx, http.MethodGet, "push_providers", nil, nil, &providers)
return &providers, err
}