2022-09-20 03:55:00 -07:00
|
|
|
package member
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2023-06-03 07:18:47 -07:00
|
|
|
"codeberg.org/pronounscc/pronouns.cc/backend/db"
|
|
|
|
"codeberg.org/pronounscc/pronouns.cc/backend/server"
|
2022-09-20 03:55:00 -07:00
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"github.com/go-chi/render"
|
2022-10-03 01:59:30 -07:00
|
|
|
"github.com/rs/xid"
|
2022-09-20 03:55:00 -07:00
|
|
|
)
|
|
|
|
|
2022-10-03 01:59:30 -07:00
|
|
|
type memberListResponse struct {
|
2023-03-11 16:31:31 -08:00
|
|
|
ID xid.ID `json:"id"`
|
2023-06-02 18:06:26 -07:00
|
|
|
SID string `json:"sid"`
|
2023-03-11 16:31:31 -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"`
|
2023-03-11 16:31:31 -08:00
|
|
|
Links []string `json:"links"`
|
|
|
|
Names []db.FieldEntry `json:"names"`
|
|
|
|
Pronouns []db.PronounEntry `json:"pronouns"`
|
2023-04-02 14:08:44 -07:00
|
|
|
Unlisted bool `json:"unlisted"`
|
2022-10-03 01:59:30 -07:00
|
|
|
}
|
|
|
|
|
2023-04-02 14:08:44 -07:00
|
|
|
func membersToMemberList(ms []db.Member, isSelf bool) []memberListResponse {
|
2022-10-03 01:59:30 -07:00
|
|
|
resps := make([]memberListResponse, len(ms))
|
|
|
|
for i := range ms {
|
|
|
|
resps[i] = memberListResponse{
|
2023-05-09 15:46:25 -07:00
|
|
|
ID: ms[i].ID,
|
2023-06-02 18:06:26 -07:00
|
|
|
SID: ms[i].SID,
|
2023-05-09 15:46:25 -07:00
|
|
|
Name: ms[i].Name,
|
|
|
|
DisplayName: ms[i].DisplayName,
|
|
|
|
Bio: ms[i].Bio,
|
|
|
|
Avatar: ms[i].Avatar,
|
|
|
|
Links: db.NotNull(ms[i].Links),
|
|
|
|
Names: db.NotNull(ms[i].Names),
|
|
|
|
Pronouns: db.NotNull(ms[i].Pronouns),
|
2022-10-03 01:59:30 -07:00
|
|
|
}
|
2023-04-02 14:08:44 -07:00
|
|
|
|
|
|
|
if isSelf {
|
|
|
|
resps[i].Unlisted = ms[i].Unlisted
|
|
|
|
}
|
2022-10-03 01:59:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return resps
|
|
|
|
}
|
|
|
|
|
2022-09-20 03:55:00 -07:00
|
|
|
func (s *Server) getUserMembers(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
|
|
|
isSelf := false
|
|
|
|
if claims, ok := server.ClaimsFromContext(ctx); ok && claims.UserID == u.ID {
|
|
|
|
isSelf = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.ListPrivate && !isSelf {
|
|
|
|
return server.APIError{Code: server.ErrMemberListPrivate}
|
|
|
|
}
|
|
|
|
|
|
|
|
ms, err := s.DB.UserMembers(ctx, u.ID, isSelf)
|
2022-09-20 03:55:00 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-02 14:08:44 -07:00
|
|
|
render.JSON(w, r, membersToMemberList(ms, isSelf))
|
2022-09-20 03:55:00 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) getMeMembers(w http.ResponseWriter, r *http.Request) error {
|
|
|
|
ctx := r.Context()
|
|
|
|
claims, _ := server.ClaimsFromContext(ctx)
|
|
|
|
|
2023-04-01 08:20:59 -07:00
|
|
|
ms, err := s.DB.UserMembers(ctx, claims.UserID, true)
|
2022-09-20 03:55:00 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-02 14:08:44 -07:00
|
|
|
render.JSON(w, r, membersToMemberList(ms, true))
|
2022-09-20 03:55:00 -07:00
|
|
|
return nil
|
|
|
|
}
|