Signed-off-by: brookjeynes me@brookjeynes.dev
+36
-27
internal/server/handlers/comment.go
+36
-27
internal/server/handlers/comment.go
···
1
package handlers
2
3
import (
4
-
"log"
5
"net/http"
6
"strings"
7
"time"
···
23
)
24
25
func (h *Handler) HandleNewComment(w http.ResponseWriter, r *http.Request) {
26
client, err := h.Oauth.AuthorizedClient(r)
27
if err != nil {
28
-
log.Println("failed to get authorized client:", err)
29
htmx.HxRedirect(w, "/login")
30
return
31
}
32
33
user, err := bsky.GetUserWithBskyProfile(h.Oauth, r)
34
if err != nil {
35
-
log.Println("failed to get logged-in user:", err)
36
htmx.HxRedirect(w, "/login")
37
return
38
}
39
40
profile, err := db.GetProfile(h.Db, user.Did)
41
if err != nil {
42
-
log.Println("failed to get logged-in user:", err)
43
htmx.HxRedirect(w, "/login")
44
return
45
}
46
47
err = r.ParseForm()
48
if err != nil {
49
-
log.Println("invalid comment form:", err)
50
htmx.HxError(w, http.StatusBadRequest, "Unable to process comment, please try again later.")
51
return
52
}
53
54
commentBody := r.FormValue("comment")
55
if len(strings.TrimSpace(commentBody)) == 0 {
56
-
log.Println("invalid comment form: missing comment body")
57
htmx.HxError(w, http.StatusBadRequest, "Comment cannot be empty.")
58
return
59
}
60
61
studySessionUri := r.FormValue("study_session_uri")
62
if len(studySessionUri) == 0 {
63
-
log.Println("invalid comment form: missing study session Uri")
64
htmx.HxError(w, http.StatusBadRequest, "Unable to create comment, please try again later.")
65
return
66
}
···
100
},
101
})
102
if err != nil {
103
-
log.Println("failed to create comment record:", err)
104
htmx.HxError(w, http.StatusInternalServerError, "Failed to create comment, try again later.")
105
return
106
}
···
121
122
err = h.Posthog.Enqueue(event)
123
if err != nil {
124
-
log.Println("failed to enqueue posthog event:", err)
125
}
126
}
127
···
153
}
154
155
func (h *Handler) HandleDeleteComment(w http.ResponseWriter, r *http.Request) {
156
user := h.Oauth.GetUser(r)
157
if user == nil {
158
-
log.Println("failed to get logged-in user")
159
htmx.HxRedirect(w, "/login")
160
return
161
}
162
client, err := h.Oauth.AuthorizedClient(r)
163
if err != nil {
164
-
log.Println("failed to get authorized client:", err)
165
htmx.HxRedirect(w, "/login")
166
return
167
}
···
171
rkey := chi.URLParam(r, "rkey")
172
comment, err := db.GetCommentByRkey(h.Db, user.Did, rkey)
173
if err != nil {
174
-
log.Println("failed to get comment from db:", err)
175
htmx.HxError(w, http.StatusInternalServerError, "Failed to delete comment, try again later.")
176
return
177
}
178
179
if user.Did != comment.Did {
180
-
log.Printf("user '%s' does not own record '%s'", user.Did, rkey)
181
htmx.HxError(w, http.StatusUnauthorized, "You do not have permissions to delete this comment.")
182
return
183
}
···
188
Rkey: comment.Rkey,
189
})
190
if err != nil {
191
-
log.Println("failed to delete comment from PDS:", err)
192
htmx.HxError(w, http.StatusInternalServerError, "Failed to delete comment, try again later.")
193
return
194
}
···
209
210
err = h.Posthog.Enqueue(event)
211
if err != nil {
212
-
log.Println("failed to enqueue posthog event:", err)
213
}
214
}
215
···
218
}
219
220
func (h *Handler) HandleEditCommentPage(w http.ResponseWriter, r *http.Request) {
221
user, err := bsky.GetUserWithBskyProfile(h.Oauth, r)
222
if err != nil {
223
-
log.Println("failed to get logged-in user:", err)
224
htmx.HxRedirect(w, "/login")
225
return
226
}
···
228
rkey := chi.URLParam(r, "rkey")
229
comment, err := db.GetCommentByRkey(h.Db, user.Did, rkey)
230
if err != nil {
231
-
log.Println("failed to get comment from db:", err)
232
htmx.HxError(w, http.StatusInternalServerError, "Failed to update comment, try again later.")
233
return
234
}
235
236
if user.Did != comment.Did {
237
-
log.Printf("user '%s' does not own record '%s'", user.Did, rkey)
238
htmx.HxError(w, http.StatusUnauthorized, "You do not have permissions to edit this comment.")
239
return
240
}
···
245
case http.MethodPost:
246
client, err := h.Oauth.AuthorizedClient(r)
247
if err != nil {
248
-
log.Println("failed to get authorized client:", err)
249
htmx.HxRedirect(w, "/login")
250
return
251
}
252
253
err = r.ParseForm()
254
if err != nil {
255
-
log.Println("invalid comment form:", err)
256
htmx.HxError(w, http.StatusBadRequest, "Unable to process comment, please try again later.")
257
return
258
}
259
260
commentBody := r.FormValue("comment")
261
if len(strings.TrimSpace(commentBody)) == 0 {
262
-
log.Println("invalid comment form: missing comment body")
263
htmx.HxError(w, http.StatusBadRequest, "Comment cannot be empty.")
264
return
265
}
···
303
SwapRecord: cid,
304
})
305
if err != nil {
306
-
log.Println("failed to update study session record:", err)
307
htmx.HxError(w, http.StatusInternalServerError, "Failed to update comment, try again later.")
308
return
309
}
···
324
325
err = h.Posthog.Enqueue(event)
326
if err != nil {
327
-
log.Println("failed to enqueue posthog event:", err)
328
}
329
}
330
···
387
}
388
389
func (h *Handler) HandleReply(w http.ResponseWriter, r *http.Request) {
390
user := h.Oauth.GetUser(r)
391
if user == nil {
392
-
log.Println("failed to get logged-in user")
393
htmx.HxRedirect(w, "/login")
394
return
395
}
···
397
studySessionUri := r.URL.Query().Get("root")
398
parentCommentUri := r.URL.Query().Get("parent")
399
if len(studySessionUri) == 0 || len(parentCommentUri) == 0 {
400
-
log.Println("invalid reply form: study session uri or parent comment uri is empty")
401
htmx.HxError(w, http.StatusBadRequest, "Unable to process comment, please try again later.")
402
return
403
}
···
409
}
410
411
func (h *Handler) HandleCancelCommentEdit(w http.ResponseWriter, r *http.Request) {
412
user, err := bsky.GetUserWithBskyProfile(h.Oauth, r)
413
if err != nil {
414
-
log.Println("failed to get logged-in user:", err)
415
htmx.HxRedirect(w, "/login")
416
return
417
}
···
419
rkey := chi.URLParam(r, "rkey")
420
comment, err := db.GetCommentByRkey(h.Db, user.Did, rkey)
421
if err != nil {
422
-
log.Println("failed to get comment from db:", err)
423
htmx.HxError(w, http.StatusInternalServerError, "Failed to update comment, try again later.")
424
return
425
}
···
1
package handlers
2
3
import (
4
"net/http"
5
"strings"
6
"time"
···
22
)
23
24
func (h *Handler) HandleNewComment(w http.ResponseWriter, r *http.Request) {
25
+
l := h.Logger.With("handler", "HandleNewComment")
26
+
27
client, err := h.Oauth.AuthorizedClient(r)
28
if err != nil {
29
+
l.Error("failed to get authorized client", "err", err)
30
htmx.HxRedirect(w, "/login")
31
return
32
}
33
34
user, err := bsky.GetUserWithBskyProfile(h.Oauth, r)
35
if err != nil {
36
+
l.Error("failed to get logged-in user", "err", err)
37
htmx.HxRedirect(w, "/login")
38
return
39
}
40
41
profile, err := db.GetProfile(h.Db, user.Did)
42
if err != nil {
43
+
l.Error("failed to get logged-in user", "err", err)
44
htmx.HxRedirect(w, "/login")
45
return
46
}
47
48
err = r.ParseForm()
49
if err != nil {
50
+
l.Error("invalid comment form", "err", err)
51
htmx.HxError(w, http.StatusBadRequest, "Unable to process comment, please try again later.")
52
return
53
}
54
55
commentBody := r.FormValue("comment")
56
if len(strings.TrimSpace(commentBody)) == 0 {
57
+
l.Error("invalid comment form: missing comment body")
58
htmx.HxError(w, http.StatusBadRequest, "Comment cannot be empty.")
59
return
60
}
61
62
studySessionUri := r.FormValue("study_session_uri")
63
if len(studySessionUri) == 0 {
64
+
l.Error("invalid comment form: missing study session Uri")
65
htmx.HxError(w, http.StatusBadRequest, "Unable to create comment, please try again later.")
66
return
67
}
···
101
},
102
})
103
if err != nil {
104
+
l.Error("failed to create comment record", "err", err)
105
htmx.HxError(w, http.StatusInternalServerError, "Failed to create comment, try again later.")
106
return
107
}
···
122
123
err = h.Posthog.Enqueue(event)
124
if err != nil {
125
+
l.Error("failed to enqueue posthog event", "err", err)
126
}
127
}
128
···
154
}
155
156
func (h *Handler) HandleDeleteComment(w http.ResponseWriter, r *http.Request) {
157
+
l := h.Logger.With("handler", "HandleDeleteComment")
158
+
159
user := h.Oauth.GetUser(r)
160
if user == nil {
161
+
l.Error("failed to get logged-in user")
162
htmx.HxRedirect(w, "/login")
163
return
164
}
165
client, err := h.Oauth.AuthorizedClient(r)
166
if err != nil {
167
+
l.Error("failed to get authorized client", "err", err)
168
htmx.HxRedirect(w, "/login")
169
return
170
}
···
174
rkey := chi.URLParam(r, "rkey")
175
comment, err := db.GetCommentByRkey(h.Db, user.Did, rkey)
176
if err != nil {
177
+
l.Error("failed to get comment from db", "err", err)
178
htmx.HxError(w, http.StatusInternalServerError, "Failed to delete comment, try again later.")
179
return
180
}
181
182
if user.Did != comment.Did {
183
+
l.Error("user does not own record", "did", user.Did, "commentDid", comment.Did)
184
htmx.HxError(w, http.StatusUnauthorized, "You do not have permissions to delete this comment.")
185
return
186
}
···
191
Rkey: comment.Rkey,
192
})
193
if err != nil {
194
+
l.Error("failed to delete comment from PDS", "err", err)
195
htmx.HxError(w, http.StatusInternalServerError, "Failed to delete comment, try again later.")
196
return
197
}
···
212
213
err = h.Posthog.Enqueue(event)
214
if err != nil {
215
+
l.Error("failed to enqueue posthog event", "err", err)
216
}
217
}
218
···
221
}
222
223
func (h *Handler) HandleEditCommentPage(w http.ResponseWriter, r *http.Request) {
224
+
l := h.Logger.With("handler", "HandleEditCommentPage")
225
+
226
user, err := bsky.GetUserWithBskyProfile(h.Oauth, r)
227
if err != nil {
228
+
l.Error("failed to get logged-in user", "err", err)
229
htmx.HxRedirect(w, "/login")
230
return
231
}
···
233
rkey := chi.URLParam(r, "rkey")
234
comment, err := db.GetCommentByRkey(h.Db, user.Did, rkey)
235
if err != nil {
236
+
l.Error("failed to get comment from db", "err", err)
237
htmx.HxError(w, http.StatusInternalServerError, "Failed to update comment, try again later.")
238
return
239
}
240
241
if user.Did != comment.Did {
242
+
l.Error("user does not own record", "did", user.Did, "commentDid", comment.Did)
243
htmx.HxError(w, http.StatusUnauthorized, "You do not have permissions to edit this comment.")
244
return
245
}
···
250
case http.MethodPost:
251
client, err := h.Oauth.AuthorizedClient(r)
252
if err != nil {
253
+
l.Error("failed to get authorized client", "err", err)
254
htmx.HxRedirect(w, "/login")
255
return
256
}
257
258
err = r.ParseForm()
259
if err != nil {
260
+
l.Error("invalid comment form", "err", err)
261
htmx.HxError(w, http.StatusBadRequest, "Unable to process comment, please try again later.")
262
return
263
}
264
265
commentBody := r.FormValue("comment")
266
if len(strings.TrimSpace(commentBody)) == 0 {
267
+
l.Error("invalid comment form: missing comment body")
268
htmx.HxError(w, http.StatusBadRequest, "Comment cannot be empty.")
269
return
270
}
···
308
SwapRecord: cid,
309
})
310
if err != nil {
311
+
l.Error("failed to update study session record", "err", err)
312
htmx.HxError(w, http.StatusInternalServerError, "Failed to update comment, try again later.")
313
return
314
}
···
329
330
err = h.Posthog.Enqueue(event)
331
if err != nil {
332
+
l.Error("failed to enqueue posthog event", "err", err)
333
}
334
}
335
···
392
}
393
394
func (h *Handler) HandleReply(w http.ResponseWriter, r *http.Request) {
395
+
l := h.Logger.With("handler", "HandleReply")
396
+
397
user := h.Oauth.GetUser(r)
398
if user == nil {
399
+
l.Error("failed to get logged-in user")
400
htmx.HxRedirect(w, "/login")
401
return
402
}
···
404
studySessionUri := r.URL.Query().Get("root")
405
parentCommentUri := r.URL.Query().Get("parent")
406
if len(studySessionUri) == 0 || len(parentCommentUri) == 0 {
407
+
l.Error("invalid reply form: study session uri or parent comment uri is empty")
408
htmx.HxError(w, http.StatusBadRequest, "Unable to process comment, please try again later.")
409
return
410
}
···
416
}
417
418
func (h *Handler) HandleCancelCommentEdit(w http.ResponseWriter, r *http.Request) {
419
+
l := h.Logger.With("handler", "HandleCancelCommentEdit")
420
+
421
user, err := bsky.GetUserWithBskyProfile(h.Oauth, r)
422
if err != nil {
423
+
l.Error("failed to get logged-in user", "err", err)
424
htmx.HxRedirect(w, "/login")
425
return
426
}
···
428
rkey := chi.URLParam(r, "rkey")
429
comment, err := db.GetCommentByRkey(h.Db, user.Did, rkey)
430
if err != nil {
431
+
l.Error("failed to get comment from db", "err", err)
432
htmx.HxError(w, http.StatusInternalServerError, "Failed to update comment, try again later.")
433
return
434
}
History
1 round
0 comments
brookjeynes.dev
submitted
#0
1 commit
expand
collapse
feat(handlers/comment): use slogger
Signed-off-by: brookjeynes <me@brookjeynes.dev>
expand 0 comments
pull request successfully merged