2022-09-20 03:55:00 -07:00
|
|
|
package member
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
|
2023-06-03 07:18:47 -07:00
|
|
|
"codeberg.org/pronounscc/pronouns.cc/backend/db"
|
|
|
|
"codeberg.org/pronounscc/pronouns.cc/backend/server"
|
2023-05-23 07:32:02 -07:00
|
|
|
"emperror.dev/errors"
|
2022-09-20 03:55:00 -07:00
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"github.com/go-chi/render"
|
|
|
|
"github.com/rs/xid"
|
|
|
|
)
|
|
|
|
|
|
|
|
type GetMemberResponse struct {
|
2022-11-20 12:09:29 -08:00
|
|
|
ID xid.ID `json:"id"`
|
2023-06-02 18:06:26 -07:00
|
|
|
SID string `json:"sid"`
|
2022-11-20 12:09:29 -08:00
|
|
|
Name string `json:"name"`
|
|
|
|
DisplayName *string `json:"display_name"`
|
|
|
|
Bio *string `json:"bio"`
|
2023-03-12 18:04:09 -07:00
|
|
|
Avatar *string `json:"avatar"`
|
2022-11-20 12:09:29 -08:00
|
|
|
Links []string `json:"links"`
|
2022-09-20 03:55:00 -07:00
|
|
|
|
2023-01-30 15:50:17 -08:00
|
|
|
Names []db.FieldEntry `json:"names"`
|
|
|
|
Pronouns []db.PronounEntry `json:"pronouns"`
|
|
|
|
Fields []db.Field `json:"fields"`
|
2023-05-25 04:40:15 -07:00
|
|
|
Flags []db.MemberFlag `json:"flags"`
|
2022-09-20 03:55:00 -07:00
|
|
|
|
|
|
|
User PartialUser `json:"user"`
|
2023-04-01 08:20:59 -07:00
|
|
|
|
|
|
|
Unlisted *bool `json:"unlisted,omitempty"`
|
2022-09-20 03:55:00 -07:00
|
|
|
}
|
|
|
|
|
2023-05-25 04:40:15 -07:00
|
|
|
func dbMemberToMember(u db.User, m db.Member, fields []db.Field, flags []db.MemberFlag, isOwnMember bool) GetMemberResponse {
|
2023-04-01 08:20:59 -07:00
|
|
|
r := GetMemberResponse{
|
2022-11-20 12:09:29 -08:00
|
|
|
ID: m.ID,
|
2023-06-02 18:06:26 -07:00
|
|
|
SID: m.SID,
|
2022-11-20 12:09:29 -08:00
|
|
|
Name: m.Name,
|
|
|
|
DisplayName: m.DisplayName,
|
|
|
|
Bio: m.Bio,
|
2023-03-12 18:04:09 -07:00
|
|
|
Avatar: m.Avatar,
|
2023-03-11 16:31:31 -08:00
|
|
|
Links: db.NotNull(m.Links),
|
2022-09-20 03:55:00 -07:00
|
|
|
|
2023-03-11 16:31:31 -08:00
|
|
|
Names: db.NotNull(m.Names),
|
|
|
|
Pronouns: db.NotNull(m.Pronouns),
|
|
|
|
Fields: db.NotNull(fields),
|
2023-05-25 04:40:15 -07:00
|
|
|
Flags: flags,
|
2022-09-20 03:55:00 -07:00
|
|
|
|
|
|
|
User: PartialUser{
|
2023-04-19 02:05:01 -07:00
|
|
|
ID: u.ID,
|
|
|
|
Username: u.Username,
|
|
|
|
DisplayName: u.DisplayName,
|
|
|
|
Avatar: u.Avatar,
|
|
|
|
CustomPreferences: u.CustomPreferences,
|
2022-09-20 03:55:00 -07:00
|
|
|
},
|
|
|
|
}
|
2023-04-01 08:20:59 -07:00
|
|
|
|
|
|
|
if isOwnMember {
|
|
|
|
r.Unlisted = &m.Unlisted
|
|
|
|
}
|
|
|
|
|
|
|
|
return r
|
2022-09-20 03:55:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type PartialUser struct {
|
2023-04-19 02:05:01 -07:00
|
|
|
ID xid.ID `json:"id"`
|
|
|
|
Username string `json:"name"`
|
|
|
|
DisplayName *string `json:"display_name"`
|
|
|
|
Avatar *string `json:"avatar"`
|
|
|
|
CustomPreferences db.CustomPreferences `json:"custom_preferences"`
|
2022-09-20 03:55:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) getMember(w http.ResponseWriter, r *http.Request) error {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
id, err := xid.FromString(chi.URLParam(r, "memberRef"))
|
|
|
|
if err != nil {
|
|
|
|
return server.APIError{
|
|
|
|
Code: server.ErrMemberNotFound,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m, err := s.DB.Member(ctx, id)
|
|
|
|
if err != nil {
|
|
|
|
return server.APIError{
|
|
|
|
Code: server.ErrMemberNotFound,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := s.DB.User(ctx, m.UserID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-08 05:04:26 -08:00
|
|
|
if u.DeletedAt != nil {
|
|
|
|
return server.APIError{Code: server.ErrMemberNotFound}
|
|
|
|
}
|
|
|
|
|
2023-04-01 08:20:59 -07:00
|
|
|
isOwnMember := false
|
|
|
|
if claims, ok := server.ClaimsFromContext(ctx); ok && claims.UserID == u.ID {
|
|
|
|
isOwnMember = true
|
|
|
|
}
|
|
|
|
|
2022-09-20 03:55:00 -07:00
|
|
|
fields, err := s.DB.MemberFields(ctx, m.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-25 04:40:15 -07:00
|
|
|
flags, err := s.DB.MemberFlags(ctx, m.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
render.JSON(w, r, dbMemberToMember(u, m, fields, flags, isOwnMember))
|
2022-09-20 03:55:00 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) getUserMember(w http.ResponseWriter, r *http.Request) error {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
u, err := s.parseUser(ctx, chi.URLParam(r, "userRef"))
|
|
|
|
if err != nil {
|
|
|
|
return server.APIError{
|
|
|
|
Code: server.ErrUserNotFound,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-08 05:04:26 -08:00
|
|
|
if u.DeletedAt != nil {
|
|
|
|
return server.APIError{Code: server.ErrUserNotFound}
|
|
|
|
}
|
|
|
|
|
2023-04-01 08:20:59 -07:00
|
|
|
isOwnMember := false
|
|
|
|
if claims, ok := server.ClaimsFromContext(ctx); ok && claims.UserID == u.ID {
|
|
|
|
isOwnMember = true
|
|
|
|
}
|
|
|
|
|
2022-09-20 03:55:00 -07:00
|
|
|
m, err := s.DB.UserMember(ctx, u.ID, chi.URLParam(r, "memberRef"))
|
|
|
|
if err != nil {
|
|
|
|
return server.APIError{
|
|
|
|
Code: server.ErrMemberNotFound,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fields, err := s.DB.MemberFields(ctx, m.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-25 04:40:15 -07:00
|
|
|
flags, err := s.DB.MemberFlags(ctx, m.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
render.JSON(w, r, dbMemberToMember(u, m, fields, flags, isOwnMember))
|
2022-09-20 03:55:00 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-23 07:32:02 -07:00
|
|
|
func (s *Server) getMeMember(w http.ResponseWriter, r *http.Request) error {
|
|
|
|
ctx := r.Context()
|
|
|
|
claims, _ := server.ClaimsFromContext(ctx)
|
|
|
|
|
|
|
|
u, err := s.DB.User(ctx, claims.UserID)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "getting me user")
|
|
|
|
}
|
|
|
|
|
|
|
|
m, err := s.DB.UserMember(ctx, claims.UserID, chi.URLParam(r, "memberRef"))
|
|
|
|
if err != nil {
|
|
|
|
return server.APIError{
|
|
|
|
Code: server.ErrMemberNotFound,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fields, err := s.DB.MemberFields(ctx, m.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-28 19:04:24 -07:00
|
|
|
flags, err := s.DB.MemberFlags(ctx, m.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
render.JSON(w, r, dbMemberToMember(u, m, fields, flags, true))
|
2023-05-23 07:32:02 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-20 03:55:00 -07:00
|
|
|
func (s *Server) parseUser(ctx context.Context, userRef string) (u db.User, err error) {
|
|
|
|
if id, err := xid.FromString(userRef); err != nil {
|
|
|
|
u, err := s.DB.User(ctx, id)
|
|
|
|
if err == nil {
|
|
|
|
return u, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.DB.Username(ctx, userRef)
|
|
|
|
}
|