-
Notifications
You must be signed in to change notification settings - Fork 0
/
ajax-spec-subspec.go
330 lines (274 loc) · 9.79 KB
/
ajax-spec-subspec.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
package main
import (
"database/sql"
"fmt"
"net/http"
"strings"
"time"
)
func ajaxSubspec(db *sql.DB, userID *uint, w http.ResponseWriter, r *http.Request) (interface{}, int) {
// GET
query := r.URL.Query()
specID, err := AtoInt64(query.Get("specId"))
if err != nil {
logError(r, userID, fmt.Errorf("parsing specId: %w", err))
return nil, http.StatusBadRequest
}
subspecID, err := AtoInt64(query.Get("subspecId"))
if err != nil {
logError(r, userID, fmt.Errorf("parsing subspecId: %w", err))
return nil, http.StatusBadRequest
}
if access, status := verifyReadTarget(r, db, userID, CommunityTargetSubspec, subspecID); !access {
return nil, status
}
s := &SpecSubspec{
ID: subspecID,
SpecID: specID,
RenderTime: time.Now(),
}
var args = []interface{}{specID, subspecID}
var lastUpdatedField string
if userID == nil {
lastUpdatedField = `GREATEST(spec_subspec.updated_at, spec_subspec.blocks_updated_at) AS last_updated`
} else {
lastUpdatedField = `CASE
-- when editor
WHEN spec.owner_type = ` + argPlaceholder(OwnerTypeUser, &args) + `
AND spec.owner_id = ` + argPlaceholder(*userID, &args) + `
THEN spec_subspec.updated_at
-- when visitor
ELSE GREATEST(spec_subspec.updated_at, spec_subspec.blocks_updated_at)
END AS last_updated`
}
var unreadCountField string
if userID == nil {
unreadCountField = `0 AS unread_count`
} else {
unreadCountField = `(SELECT COUNT(*) FROM spec_community_comment AS c
LEFT JOIN spec_community_read AS r
ON r.user_id = ` + argPlaceholder(*userID, &args) + `
AND r.target_type = ` + argPlaceholder(CommunityTargetComment, &args) + `
AND r.target_id = c.id
WHERE c.target_type = ` + argPlaceholder(CommunityTargetSubspec, &args) + `
AND c.target_id = spec_subspec.id
AND r.user_id IS NULL
) AS unread_count`
}
err = db.QueryRow(`SELECT spec_subspec.created_at,
spec_subspec.subspec_name, spec_subspec.subspec_desc, spec_subspec.is_private,
-- spec.spec_name, spec.owner_type, spec.owner_id,
-- select last updated
`+lastUpdatedField+`,
-- select number of unread comments
`+unreadCountField+`,
-- select total number of comments
(SELECT COUNT(*) FROM spec_community_comment AS c
WHERE c.target_type = `+argPlaceholder(CommunityTargetSubspec, &args)+`
AND c.target_id = spec_subspec.id
) AS comments_count
FROM spec_subspec
INNER JOIN spec ON spec.id = $1
WHERE spec_subspec.id = $2
AND spec_subspec.spec_id = $1`,
args...,
).Scan(&s.Created, &s.Name, &s.Desc, &s.Private,
&s.Updated, &s.UnreadCount, &s.CommentsCount)
if err != nil {
logError(r, userID, fmt.Errorf("reading subspec: %w", err))
return nil, http.StatusInternalServerError
}
if AtoBool(query.Get("loadBlocks")) {
s.Blocks, err = loadContextBlocks(db, userID, specID, &subspecID)
if err != nil {
logError(r, userID, fmt.Errorf("loading subspec blocks: %w", err))
return nil, http.StatusInternalServerError
}
}
return s, http.StatusOK
}
// load a list of subspecs for nav or block ref editing
func ajaxSubspecs(db *sql.DB, userID *uint, w http.ResponseWriter, r *http.Request) (interface{}, int) {
// GET
query := r.URL.Query()
specID, err := AtoInt64(query.Get("specId"))
if err != nil {
logError(r, userID, fmt.Errorf("parsing specId: %w", err))
return nil, http.StatusBadRequest
}
if access, status := verifyReadTarget(r, db, userID, CommunityTargetSpec, specID); !access {
return nil, status
}
var args []interface{}
var publicSubspecCond string
if userID == nil {
// The public may not be allowed to view this subspec.
publicSubspecCond = `(NOT spec_subspec.is_private)`
} else {
// Only the owner may view private subspecs
publicSubspecCond = `((NOT spec_subspec.is_private) OR (
spec.owner_type = ` + argPlaceholder(OwnerTypeUser, &args) + `
AND spec.owner_id = ` + argPlaceholder(*userID, &args) + `
))`
}
rows, err := db.Query(
`SELECT spec_subspec.id, spec_subspec.created_at, spec_subspec.updated_at,
spec_subspec.subspec_name, spec_subspec.subspec_desc, spec_subspec.is_private
FROM spec_subspec
INNER JOIN spec ON spec.id = spec_subspec.spec_id
WHERE spec.id = `+argPlaceholder(specID, &args)+`
AND `+publicSubspecCond+`
ORDER BY spec_subspec.is_private, spec_subspec.subspec_name`,
args...,
)
if err != nil {
logError(r, userID, fmt.Errorf("querying subspecs: %w", err))
return nil, http.StatusInternalServerError
}
subspecs := []*SpecSubspec{}
for rows.Next() {
s := &SpecSubspec{
SpecID: specID,
}
err = rows.Scan(&s.ID, &s.Created, &s.Updated, &s.Name, &s.Desc, &s.Private)
if err != nil {
if err2 := rows.Close(); err2 != nil {
logError(r, userID, fmt.Errorf("closing rows: %s; on scan error: %w", err2, err))
return nil, http.StatusInternalServerError
}
logError(r, userID, fmt.Errorf("scanning subspec: %w", err))
return nil, http.StatusInternalServerError
}
subspecs = append(subspecs, s)
}
return subspecs, http.StatusOK
}
func ajaxSpecCreateSubspec(db *sql.DB, userID uint, w http.ResponseWriter, r *http.Request) (interface{}, int) {
// POST
err := r.ParseForm()
if err != nil {
logError(r, &userID, err)
return nil, http.StatusInternalServerError
}
specID, err := AtoInt64(r.Form.Get("specId"))
if err != nil {
logError(r, &userID, fmt.Errorf("parsing specId: %w", err))
return nil, http.StatusBadRequest
}
if access, status := verifyCreateSubspec(r, db, userID, specID); !access {
return nil, status
}
name := Substr(strings.TrimSpace(r.Form.Get("name")), subspecNameMaxLen)
if name == "" {
logError(r, &userID, fmt.Errorf("subspec name required"))
return nil, http.StatusBadRequest
}
desc := AtoPointerNilIfEmpty(strings.TrimSpace(r.Form.Get("desc")))
private := AtoBool(strings.TrimSpace(r.Form.Get("private")))
return handleInTransaction(r, db, &userID, func(tx *sql.Tx) (interface{}, int) {
var subspec = &SpecSubspec{}
err = tx.QueryRow(
`INSERT INTO spec_subspec (
spec_id, created_at, updated_at, subspec_name, subspec_desc, is_private
) VALUES (
$1, $2, $2, $3, $4, $5
) RETURNING id, spec_id, created_at, updated_at,
subspec_name, subspec_desc, is_private`,
specID, time.Now(), name, desc, private,
).Scan(&subspec.ID, &subspec.SpecID, &subspec.Created, &subspec.Updated,
&subspec.Name, &subspec.Desc, &subspec.Private)
if err != nil {
logError(r, &userID, fmt.Errorf("creating subspec: %w", err))
return nil, http.StatusInternalServerError
}
return subspec, http.StatusCreated
})
}
func ajaxSpecSaveSubspec(db *sql.DB, userID uint, w http.ResponseWriter, r *http.Request) (interface{}, int) {
// POST
err := r.ParseForm()
if err != nil {
logError(r, &userID, err)
return nil, http.StatusInternalServerError
}
subspecID, err := AtoInt64(r.Form.Get("subspecId"))
if err != nil {
logError(r, &userID, fmt.Errorf("parsing subspecId: %w", err))
return nil, http.StatusBadRequest
}
if access, status := verifyWriteTarget(r, db, userID, CommunityTargetSubspec, subspecID); !access {
return nil, status
}
name := Substr(strings.TrimSpace(r.Form.Get("name")), subspecNameMaxLen)
if name == "" {
logError(r, &userID, fmt.Errorf("subspec name required"))
return nil, http.StatusBadRequest
}
desc := AtoPointerNilIfEmpty(strings.TrimSpace(r.Form.Get("desc")))
private := AtoBool(strings.TrimSpace(r.Form.Get("private")))
return handleInTransaction(r, db, &userID, func(tx *sql.Tx) (interface{}, int) {
s := &SpecSubspec{
ID: subspecID,
}
var args = []interface{}{}
// Scan new values as represented in DB for return
err = tx.QueryRow(`UPDATE spec_subspec
SET updated_at = `+argPlaceholder(time.Now(), &args)+`,
subspec_name = `+argPlaceholder(name, &args)+`,
subspec_desc = `+argPlaceholder(desc, &args)+`,
is_private = `+argPlaceholder(private, &args)+`
WHERE id = `+argPlaceholder(subspecID, &args)+`
RETURNING spec_id, created_at, updated_at,
subspec_name, subspec_desc, is_private,
-- select number of unread comments
(SELECT COUNT(*) FROM spec_community_comment AS c
LEFT JOIN spec_community_read AS r
ON r.user_id = `+argPlaceholder(userID, &args)+`
AND r.target_type = `+argPlaceholder(CommunityTargetComment, &args)+`
AND r.target_id = c.id
WHERE c.target_type = `+argPlaceholder(CommunityTargetSubspec, &args)+`
AND c.target_id = spec_subspec.id
AND r.user_id IS NULL
) AS unread_count,
-- select total number of comments
(SELECT COUNT(*)
FROM spec_community_comment AS c
WHERE c.target_type = `+argPlaceholder(CommunityTargetSubspec, &args)+`
AND c.target_id = spec_subspec.id
) AS comments_count`,
args...,
).Scan(&s.SpecID, &s.Created, &s.Updated, &s.Name, &s.Desc, &s.Private,
&s.UnreadCount, &s.CommentsCount)
if err != nil {
logError(r, &userID, fmt.Errorf("updating subspec: %w", err))
return nil, http.StatusInternalServerError
}
return s, http.StatusOK
})
}
func ajaxSpecDeleteSubspec(db *sql.DB, userID uint, w http.ResponseWriter, r *http.Request) (interface{}, int) {
// POST
err := r.ParseForm()
if err != nil {
logError(r, &userID, err)
return nil, http.StatusInternalServerError
}
subspecID, err := AtoInt64(r.Form.Get("subspecId"))
if err != nil {
logError(r, &userID, fmt.Errorf("parsing subspecId: %w", err))
return nil, http.StatusBadRequest
}
if access, status := verifyDeleteTarget(r, db, userID, CommunityTargetSubspec, subspecID); !access {
return nil, status
}
return handleInTransaction(r, db, &userID, func(tx *sql.Tx) (interface{}, int) {
// Blocks in subspec are deleted on cascade
// Don't clear references from blocks - display "content unavailable" message
_, err := tx.Exec(`DELETE FROM spec_subspec WHERE id=$1`, subspecID)
if err != nil {
logError(r, &userID, fmt.Errorf("deleting subspec: %w", err))
return nil, http.StatusInternalServerError
}
return nil, http.StatusOK
})
}