2022-11-22 04:31:42 -08:00
|
|
|
package member
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"emperror.dev/errors"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"github.com/go-chi/render"
|
|
|
|
"github.com/rs/xid"
|
|
|
|
|
2023-09-06 16:43:05 -07:00
|
|
|
"codeberg.org/pronounscc/pronouns.cc/backend/common"
|
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"
|
2022-11-22 04:31:42 -08:00
|
|
|
)
|
|
|
|
|
2023-09-06 16:43:05 -07:00
|
|
|
func (s *Server) deleteMember(w http.ResponseWriter, r *http.Request) (err error) {
|
2022-11-22 04:31:42 -08:00
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
claims, _ := server.ClaimsFromContext(ctx)
|
|
|
|
|
2023-03-30 07:58:35 -07:00
|
|
|
if !claims.TokenWrite {
|
|
|
|
return server.APIError{Code: server.ErrMissingPermissions, Details: "this token is read-only"}
|
|
|
|
}
|
|
|
|
|
2023-09-06 16:43:05 -07:00
|
|
|
var m db.Member
|
|
|
|
if id, err := xid.FromString(chi.URLParam(r, "memberRef")); err == nil {
|
|
|
|
m, err = s.DB.Member(ctx, id)
|
|
|
|
if err != nil {
|
|
|
|
if err == db.ErrMemberNotFound {
|
|
|
|
return server.APIError{Code: server.ErrMemberNotFound}
|
|
|
|
}
|
2022-11-22 04:31:42 -08:00
|
|
|
|
2023-09-06 16:43:05 -07:00
|
|
|
return errors.Wrap(err, "getting member")
|
2022-11-22 04:31:42 -08:00
|
|
|
}
|
2023-09-06 16:43:05 -07:00
|
|
|
} else if id, err := common.ParseSnowflake(chi.URLParam(r, "memberRef")); err == nil {
|
|
|
|
m, err = s.DB.MemberBySnowflake(ctx, common.MemberID(id))
|
|
|
|
if err != nil {
|
|
|
|
if err == db.ErrMemberNotFound {
|
|
|
|
return server.APIError{Code: server.ErrMemberNotFound}
|
|
|
|
}
|
2022-11-22 04:31:42 -08:00
|
|
|
|
2023-09-06 16:43:05 -07:00
|
|
|
return errors.Wrap(err, "getting member")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return server.APIError{Code: server.ErrMemberNotFound}
|
2022-11-22 04:31:42 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if m.UserID != claims.UserID {
|
|
|
|
return server.APIError{Code: server.ErrNotOwnMember}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = s.DB.DeleteMember(ctx, m.ID)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "deleting member")
|
|
|
|
}
|
|
|
|
|
2023-03-12 18:19:03 -07:00
|
|
|
if m.Avatar != nil {
|
|
|
|
err = s.DB.DeleteMemberAvatar(ctx, m.ID, *m.Avatar)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "deleting member avatar")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-01 17:54:08 -07:00
|
|
|
// update last active time
|
|
|
|
err = s.DB.UpdateActiveTime(ctx, s.DB, claims.UserID)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("updating last active time for user %v: %v", claims.UserID, err)
|
2023-09-19 17:39:14 -07:00
|
|
|
return errors.Wrap(err, "updating last active time")
|
2023-05-01 17:54:08 -07:00
|
|
|
}
|
|
|
|
|
2023-03-30 07:05:40 -07:00
|
|
|
render.NoContent(w, r)
|
2022-11-22 04:31:42 -08:00
|
|
|
return nil
|
|
|
|
}
|