-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcustom_integration.go
346 lines (303 loc) · 9.25 KB
/
custom_integration.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
package database
import (
"context"
"github.com/jackc/pgtype"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool"
)
type CustomIntegrationTable struct {
*pgxpool.Pool
}
type CustomIntegration struct {
Id int `json:"id"`
OwnerId uint64 `json:"owner_id"`
HttpMethod string `json:"http_method"`
WebhookUrl string `json:"webhook_url"`
ValidationUrl *string `json:"validation_url"`
Name string `json:"name"`
Description string `json:"description"`
ImageUrl *string `json:"image_url"`
PrivacyPolicyUrl *string `json:"privacy_policy_url"`
Public bool `json:"public"`
Approved bool `json:"approved"`
}
type CustomIntegrationWithGuildCount struct {
CustomIntegration
GuildCount int `json:"guild_count"`
}
type CustomIntegrationWithActive struct {
CustomIntegrationWithGuildCount
Active bool `json:"active"`
}
func newCustomIntegrationTable(db *pgxpool.Pool) *CustomIntegrationTable {
return &CustomIntegrationTable{
db,
}
}
func (i CustomIntegrationTable) Schema() string {
return `
CREATE TABLE IF NOT EXISTS custom_integrations(
"id" SERIAL NOT NULL UNIQUE,
"owner_id" int8 NOT NULL,
"webhook_url" VARCHAR(255) NOT NULL,
"validation_url" VARCHAR(255) NULL,
"http_method" VARCHAR(4) NOT NULL,
"name" VARCHAR(32) NOT NULL,
"description" VARCHAR(255) NOT NULL,
"image_url" VARCHAR(255) NULL,
"privacy_policy_url" VARCHAR(255) NULL,
"public" BOOL NOT NULL DEFAULT 'f',
"approved" BOOL NOT NULL DEFAULT 'f',
PRIMARY KEY("id")
);
CREATE INDEX IF NOT EXISTS custom_integrations_owner_id ON custom_integrations("owner_id");
`
}
func (i *CustomIntegrationTable) Get(ctx context.Context, id int) (CustomIntegration, bool, error) {
query := `SELECT "id", "owner_id", "webhook_url", "validation_url", "http_method", "name", "description", "image_url", "privacy_policy_url", "public", "approved" FROM custom_integrations WHERE "id" = $1;`
var integration CustomIntegration
err := i.QueryRow(ctx, query, id).Scan(
&integration.Id,
&integration.OwnerId,
&integration.WebhookUrl,
&integration.ValidationUrl,
&integration.HttpMethod,
&integration.Name,
&integration.Description,
&integration.ImageUrl,
&integration.PrivacyPolicyUrl,
&integration.Public,
&integration.Approved,
)
if err != nil {
if err == pgx.ErrNoRows {
return integration, false, nil
} else {
return CustomIntegration{}, false, err
}
}
return integration, true, nil
}
func (i *CustomIntegrationTable) GetAll(ctx context.Context, ids []int) ([]CustomIntegration, error) {
query := `
SELECT "id", "owner_id", "webhook_url", "validation_url", "http_method", "name", "description", "image_url", "privacy_policy_url", "public", "approved"
FROM custom_integrations
WHERE "id" = ANY($1);`
idArray := &pgtype.Int4Array{}
if err := idArray.Set(ids); err != nil {
return nil, err
}
rows, err := i.Query(ctx, query, idArray)
if err != nil {
return nil, err
}
var integrations []CustomIntegration
for rows.Next() {
var integration CustomIntegration
err := rows.Scan(
&integration.Id,
&integration.OwnerId,
&integration.WebhookUrl,
&integration.ValidationUrl,
&integration.HttpMethod,
&integration.Name,
&integration.Description,
&integration.ImageUrl,
&integration.PrivacyPolicyUrl,
&integration.Public,
&integration.Approved,
)
if err != nil {
return nil, err
}
integrations = append(integrations, integration)
}
return integrations, nil
}
func (i *CustomIntegrationTable) GetOwnedCount(ctx context.Context, userId uint64) (count int, err error) {
query := `SELECT COUNT(*) FROM custom_integrations WHERE "owner_id" = $1;`
err = i.QueryRow(ctx, query, userId).Scan(&count)
return
}
func (i *CustomIntegrationTable) GetAllOwned(ctx context.Context, ownerId uint64) ([]CustomIntegrationWithGuildCount, error) {
query := `
SELECT
integrations.id,
integrations.owner_id,
integrations.webhook_url,
integrations.validation_url,
integrations.http_method,
integrations.name,
integrations.description,
integrations.image_url,
integrations.privacy_policy_url,
integrations.public,
integrations.approved,
COALESCE(counts.count, 0) AS guild_count
FROM custom_integrations AS integrations
LEFT OUTER JOIN custom_integration_guild_counts counts ON integrations.id = counts.integration_id
WHERE "owner_id" = $1;`
rows, err := i.Query(ctx, query, ownerId)
if err != nil {
return nil, err
}
var integrations []CustomIntegrationWithGuildCount
for rows.Next() {
var integration CustomIntegrationWithGuildCount
err := rows.Scan(
&integration.Id,
&integration.OwnerId,
&integration.WebhookUrl,
&integration.ValidationUrl,
&integration.HttpMethod,
&integration.Name,
&integration.Description,
&integration.ImageUrl,
&integration.PrivacyPolicyUrl,
&integration.Public,
&integration.Approved,
&integration.GuildCount,
)
if err != nil {
return nil, err
}
integrations = append(integrations, integration)
}
return integrations, nil
}
func (i *CustomIntegrationGuildsTable) GetAvailableIntegrationsWithActive(ctx context.Context, guildId, userId uint64, limit, offset int) ([]CustomIntegrationWithActive, error) {
query := `
WITH active AS (
SELECT integration_id
FROM custom_integration_guilds
WHERE guild_id=$1
)
SELECT
integrations.id,
integrations.owner_id,
integrations.webhook_url,
integrations.validation_url,
integrations.http_method,
integrations.name,
integrations.description,
integrations.image_url,
integrations.privacy_policy_url,
integrations.public,
integrations.approved,
COALESCE(counts.count, 0) AS guild_count,
CASE WHEN active.integration_id IS NOT NULL THEN TRUE ELSE FALSE END AS added
FROM custom_integrations as integrations
LEFT OUTER JOIN active ON active.integration_id = integrations.id
LEFT OUTER JOIN custom_integration_guild_counts counts ON integrations.id = counts.integration_id
WHERE active.integration_id IS NOT NULL OR
((integrations.public = 't' AND integrations.approved = 't') OR integrations.owner_id = $2)
ORDER BY active.integration_id NULLS LAST, guild_count DESC
LIMIT $3 OFFSET $4;
`
rows, err := i.Query(ctx, query, guildId, userId, limit, offset)
if err != nil {
return nil, err
}
var integrations []CustomIntegrationWithActive
for rows.Next() {
var integration CustomIntegrationWithActive
err := rows.Scan(
&integration.Id,
&integration.OwnerId,
&integration.WebhookUrl,
&integration.ValidationUrl,
&integration.HttpMethod,
&integration.Name,
&integration.Description,
&integration.ImageUrl,
&integration.PrivacyPolicyUrl,
&integration.Public,
&integration.Approved,
&integration.GuildCount,
&integration.Active,
)
if err != nil {
return nil, err
}
integrations = append(integrations, integration)
}
return integrations, nil
}
func (i *CustomIntegrationGuildsTable) CanActivate(ctx context.Context, integrationId int, userId uint64) (canActivate bool, err error) {
query := `
SELECT EXISTS(
SELECT 1
FROM custom_integrations AS integrations
WHERE integrations.id = $1 AND ((integrations.public = 't' AND integrations.approved = 't') OR integrations.owner_id = $2)
);
`
err = i.QueryRow(ctx, query, integrationId, userId).Scan(&canActivate)
return
}
func (i *CustomIntegrationTable) Create(ctx context.Context, ownerId uint64, webhookUrl string, validationUrl *string, httpMethod, name, description string, imageUrl, privacyPolicyUrl *string) (CustomIntegration, error) {
query := `
INSERT INTO custom_integrations("owner_id", "webhook_url", "validation_url", "http_method", "name", "description", "image_url", "privacy_policy_url", "public", "approved")
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, 'f', 'f')
RETURNING "id";
;`
integration := CustomIntegration{
OwnerId: ownerId,
Name: name,
WebhookUrl: webhookUrl,
ValidationUrl: validationUrl,
HttpMethod: httpMethod,
Description: description,
ImageUrl: imageUrl,
PrivacyPolicyUrl: privacyPolicyUrl,
Public: false,
Approved: false,
}
if err := i.QueryRow(ctx, query, ownerId, webhookUrl, validationUrl, httpMethod, name, description, imageUrl, privacyPolicyUrl).Scan(&integration.Id); err != nil {
return CustomIntegration{}, err
}
return integration, nil
}
func (i *CustomIntegrationTable) SetPublic(ctx context.Context, integrationId int) (err error) {
query := `UPDATE custom_integrations SET "public" = TRUE WHERE "id" = $1;`
_, err = i.Exec(ctx, query, integrationId)
return
}
func (i *CustomIntegrationTable) Update(ctx context.Context, integration CustomIntegration) (err error) {
query := `
UPDATE custom_integrations
SET
"webhook_url" = $2,
"validation_url" = $3,
"http_method" = $4,
"name" = $5,
"description" = $6,
"image_url" = $7,
"privacy_policy_url" = $8,
"public" = $9,
"approved" = $10
WHERE "id" = $1;
`
_, err = i.Exec(
ctx,
query,
integration.Id,
integration.WebhookUrl,
integration.ValidationUrl,
integration.HttpMethod,
integration.Name,
integration.Description,
integration.ImageUrl,
integration.PrivacyPolicyUrl,
integration.Public,
integration.Approved,
)
return
}
func (i *CustomIntegrationTable) Delete(ctx context.Context, id int) (err error) {
query := `
DELETE FROM custom_integrations
WHERE "id" = $1;
`
_, err = i.Exec(ctx, query, id)
return
}