Y艒ten: A social tracker for your language learning journey built on the atproto.

feat(handlers/comment): use slogger #10

merged opened by brookjeynes.dev targeting master from push-trrpxxyxxmot
Labels

None yet.

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

History

1 round 0 comments
sign up or login to add to the discussion
brookjeynes.dev submitted #0
1 commit
expand
feat(handlers/comment): use slogger
expand 0 comments
pull request successfully merged