2023-03-23 06:54:43 -07:00
|
|
|
package mod
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
2023-06-03 07:18:47 -07:00
|
|
|
"codeberg.org/pronounscc/pronouns.cc/backend/db"
|
|
|
|
"codeberg.org/pronounscc/pronouns.cc/backend/log"
|
|
|
|
"codeberg.org/pronounscc/pronouns.cc/backend/server"
|
2023-03-23 06:54:43 -07:00
|
|
|
"emperror.dev/errors"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"github.com/go-chi/render"
|
|
|
|
)
|
|
|
|
|
|
|
|
type warning struct {
|
|
|
|
db.Warning
|
|
|
|
Read bool `json:"read"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func dbWarningsToResponse(ws []db.Warning) []warning {
|
|
|
|
out := make([]warning, len(ws))
|
|
|
|
for i := range ws {
|
|
|
|
out[i] = warning{ws[i], ws[i].ReadAt != nil}
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) getWarnings(w http.ResponseWriter, r *http.Request) (err error) {
|
|
|
|
ctx := r.Context()
|
|
|
|
claims, _ := server.ClaimsFromContext(ctx)
|
|
|
|
showAll := r.FormValue("all") == "true"
|
|
|
|
|
|
|
|
warnings, err := s.DB.Warnings(ctx, claims.UserID, !showAll)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("getting warnings: %v", err)
|
|
|
|
return errors.Wrap(err, "getting warnings from database")
|
|
|
|
}
|
|
|
|
|
|
|
|
render.JSON(w, r, dbWarningsToResponse(warnings))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) ackWarning(w http.ResponseWriter, r *http.Request) (err error) {
|
|
|
|
ctx := r.Context()
|
|
|
|
claims, _ := server.ClaimsFromContext(ctx)
|
|
|
|
|
2023-03-30 07:58:35 -07:00
|
|
|
if !claims.APIToken {
|
|
|
|
return server.APIError{Code: server.ErrMissingPermissions, Details: "This endpoint cannot be used by API tokens"}
|
|
|
|
}
|
|
|
|
|
2023-03-23 06:54:43 -07:00
|
|
|
id, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return server.APIError{Code: server.ErrBadRequest}
|
|
|
|
}
|
|
|
|
|
|
|
|
ok, err := s.DB.AckWarning(ctx, claims.UserID, id)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("acknowledging warning: %v", err)
|
|
|
|
return errors.Wrap(err, "acknowledging warning")
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
return server.APIError{Code: server.ErrNotFound}
|
|
|
|
}
|
|
|
|
|
2023-03-30 07:05:40 -07:00
|
|
|
render.NoContent(w, r)
|
2023-03-23 06:54:43 -07:00
|
|
|
return nil
|
|
|
|
}
|