-
Notifications
You must be signed in to change notification settings - Fork 33
/
incoming-webhooks.go
217 lines (192 loc) · 5.18 KB
/
incoming-webhooks.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
package postgres
import (
"database/sql"
"errors"
"strings"
sq "github.com/Masterminds/squirrel"
"github.com/ncarlier/readflow/pkg/model"
)
var inboundServiceColumns = []string{
"id",
"user_id",
"alias",
"token",
"script",
"last_usage_at",
"created_at",
"updated_at",
}
func mapRowToIncomingWebhook(row *sql.Row) (*model.IncomingWebhook, error) {
result := model.IncomingWebhook{}
err := row.Scan(
&result.ID,
&result.UserID,
&result.Alias,
&result.Token,
&result.Script,
&result.LastUsageAt,
&result.CreatedAt,
&result.UpdatedAt,
)
if err == sql.ErrNoRows {
return nil, nil
} else if err != nil {
return nil, mapError(err)
}
return &result, nil
}
// CreateIncomingWebhookForUser creates an incoming webhook for a user
func (pg *DB) CreateIncomingWebhookForUser(uid uint, form model.IncomingWebhookCreateForm) (*model.IncomingWebhook, error) {
query, args, _ := pg.psql.Insert(
"incoming_webhooks",
).Columns(
"user_id", "alias", "token", "script",
).Values(
uid,
form.Alias,
form.Token,
form.Script,
).Suffix(
"RETURNING " + strings.Join(inboundServiceColumns, ","),
).ToSql()
row := pg.db.QueryRow(query, args...)
return mapRowToIncomingWebhook(row)
}
// UpdateIncomingWebhookForUser updates an incoming webhook for a user
func (pg *DB) UpdateIncomingWebhookForUser(uid uint, form model.IncomingWebhookUpdateForm) (*model.IncomingWebhook, error) {
update := map[string]interface{}{
"updated_at": "NOW()",
}
if form.Alias != nil {
update["alias"] = *form.Alias
}
if form.Script != nil {
update["script"] = *form.Script
}
query, args, _ := pg.psql.Update(
"incoming_webhooks",
).SetMap(update).Where(
sq.Eq{"id": form.ID},
).Where(
sq.Eq{"user_id": uid},
).Suffix(
"RETURNING " + strings.Join(inboundServiceColumns, ","),
).ToSql()
row := pg.db.QueryRow(query, args...)
return mapRowToIncomingWebhook(row)
}
// GetIncomingWebhookByID get an incoming webhook from the DB
func (pg *DB) GetIncomingWebhookByID(id uint) (*model.IncomingWebhook, error) {
query, args, _ := pg.psql.Select(inboundServiceColumns...).From(
"incoming_webhooks",
).Where(
sq.Eq{"id": id},
).ToSql()
row := pg.db.QueryRow(query, args...)
return mapRowToIncomingWebhook(row)
}
// GetIncomingWebhookByToken find an incoming webhook by token form the DB (last usage is updated!)
func (pg *DB) GetIncomingWebhookByToken(token string) (*model.IncomingWebhook, error) {
query, args, _ := pg.psql.Update(
"incoming_webhooks",
).Set("last_usage_at", "NOW()").Where(
sq.Eq{"token": token},
).Suffix(
"RETURNING " + strings.Join(inboundServiceColumns, ","),
).ToSql()
row := pg.db.QueryRow(query, args...)
return mapRowToIncomingWebhook(row)
}
// GetIncomingWebhookByUserAndAlias returns incoming webhook of an user by its alias
func (pg *DB) GetIncomingWebhookByUserAndAlias(uid uint, alias string) (*model.IncomingWebhook, error) {
query, args, _ := pg.psql.Select(inboundServiceColumns...).From(
"incoming_webhooks",
).Where(
sq.Eq{"user_id": uid},
).Where(
sq.Eq{"alias": alias},
).ToSql()
row := pg.db.QueryRow(query, args...)
return mapRowToIncomingWebhook(row)
}
// GetIncomingWebhooksByUser returns incoming webhooks of an user from DB
func (pg *DB) GetIncomingWebhooksByUser(uid uint) ([]model.IncomingWebhook, error) {
query, args, _ := pg.psql.Select(inboundServiceColumns...).From(
"incoming_webhooks",
).Where(
sq.Eq{"user_id": uid},
).OrderBy("alias ASC").ToSql()
rows, err := pg.db.Query(query, args...)
if err != nil {
return nil, err
}
defer rows.Close()
var result []model.IncomingWebhook
for rows.Next() {
item := model.IncomingWebhook{}
err = rows.Scan(
&item.ID,
&item.UserID,
&item.Alias,
&item.Token,
&item.Script,
&item.LastUsageAt,
&item.CreatedAt,
&item.UpdatedAt,
)
if err != nil {
return nil, err
}
result = append(result, item)
}
err = rows.Err()
if err != nil {
return nil, err
}
return result, nil
}
// CountIncomingWebhooksByUser returns total nb of incoming webhooks of an user from the DB
func (pg *DB) CountIncomingWebhooksByUser(uid uint) (uint, error) {
counter := pg.psql.Select("count(*)").From(
"incoming_webhooks",
).Where(sq.Eq{"user_id": uid})
query, args, _ := counter.ToSql()
var count uint
if err := pg.db.QueryRow(query, args...).Scan(&count); err != nil {
return 0, err
}
return count, nil
}
// DeleteIncomingWebhookByUser removes an inboundService from the DB
func (pg *DB) DeleteIncomingWebhookByUser(uid, id uint) error {
query, args, _ := pg.psql.Delete("incoming_webhooks").Where(
sq.Eq{"id": id},
).Where(
sq.Eq{"user_id": uid},
).ToSql()
result, err := pg.db.Exec(query, args...)
if err != nil {
return err
}
count, err := result.RowsAffected()
if err != nil {
return err
}
if count == 0 {
return errors.New("no incoming webhook been removed")
}
return nil
}
// DeleteIncomingWebhooksByUser removes incoming webhooks from the DB
func (pg *DB) DeleteIncomingWebhooksByUser(uid uint, ids []uint) (int64, error) {
query, args, _ := pg.psql.Delete("incoming_webhooks").Where(
sq.Eq{"user_id": uid},
).Where(
sq.Eq{"id": ids},
).ToSql()
result, err := pg.db.Exec(query, args...)
if err != nil {
return 0, err
}
return result.RowsAffected()
}