2022-05-04 16:27:16 +02:00
|
|
|
package user
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2022-05-14 16:52:08 +02:00
|
|
|
"codeberg.org/u1f320/pronouns.cc/backend/db"
|
|
|
|
"codeberg.org/u1f320/pronouns.cc/backend/log"
|
|
|
|
"codeberg.org/u1f320/pronouns.cc/backend/server"
|
2022-05-04 16:27:16 +02:00
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"github.com/go-chi/render"
|
|
|
|
"github.com/rs/xid"
|
|
|
|
)
|
|
|
|
|
|
|
|
type GetUserResponse struct {
|
2023-01-31 00:50:17 +01:00
|
|
|
ID xid.ID `json:"id"`
|
|
|
|
Username string `json:"name"`
|
|
|
|
DisplayName *string `json:"display_name"`
|
|
|
|
Bio *string `json:"bio"`
|
2023-03-13 02:04:09 +01:00
|
|
|
Avatar *string `json:"avatar"`
|
2023-01-31 00:50:17 +01:00
|
|
|
Links []string `json:"links"`
|
|
|
|
Names []db.FieldEntry `json:"names"`
|
|
|
|
Pronouns []db.PronounEntry `json:"pronouns"`
|
|
|
|
Members []PartialMember `json:"members"`
|
|
|
|
Fields []db.Field `json:"fields"`
|
2022-05-04 16:27:16 +02:00
|
|
|
}
|
|
|
|
|
2022-05-05 16:33:44 +02:00
|
|
|
type GetMeResponse struct {
|
2022-05-10 16:33:29 +02:00
|
|
|
GetUserResponse
|
2022-05-05 16:33:44 +02:00
|
|
|
|
2023-03-23 14:54:43 +01:00
|
|
|
MaxInvites int `json:"max_invites"`
|
|
|
|
IsAdmin bool `json:"is_admin"`
|
|
|
|
|
2022-05-05 16:33:44 +02:00
|
|
|
Discord *string `json:"discord"`
|
|
|
|
DiscordUsername *string `json:"discord_username"`
|
2023-03-17 14:14:31 +01:00
|
|
|
|
|
|
|
Fediverse *string `json:"fediverse"`
|
|
|
|
FediverseUsername *string `json:"fediverse_username"`
|
|
|
|
FediverseInstance *string `json:"fediverse_instance"`
|
2022-05-05 16:33:44 +02:00
|
|
|
}
|
|
|
|
|
2022-05-04 16:27:16 +02:00
|
|
|
type PartialMember struct {
|
2023-03-12 01:31:31 +01:00
|
|
|
ID xid.ID `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
DisplayName *string `json:"display_name"`
|
2023-03-12 04:25:53 +01:00
|
|
|
Bio *string `json:"bio"`
|
2023-03-13 02:04:09 +01:00
|
|
|
Avatar *string `json:"avatar"`
|
2023-03-12 04:25:53 +01:00
|
|
|
Links []string `json:"links"`
|
2023-03-12 01:31:31 +01:00
|
|
|
Names []db.FieldEntry `json:"names"`
|
|
|
|
Pronouns []db.PronounEntry `json:"pronouns"`
|
2022-05-04 16:27:16 +02:00
|
|
|
}
|
|
|
|
|
2023-01-14 17:33:18 +01:00
|
|
|
func dbUserToResponse(u db.User, fields []db.Field, members []db.Member) GetUserResponse {
|
2022-11-21 02:38:08 +01:00
|
|
|
resp := GetUserResponse{
|
2022-05-04 16:27:16 +02:00
|
|
|
ID: u.ID,
|
|
|
|
Username: u.Username,
|
|
|
|
DisplayName: u.DisplayName,
|
|
|
|
Bio: u.Bio,
|
2023-03-13 02:04:09 +01:00
|
|
|
Avatar: u.Avatar,
|
2023-03-12 01:31:31 +01:00
|
|
|
Links: db.NotNull(u.Links),
|
|
|
|
Names: db.NotNull(u.Names),
|
|
|
|
Pronouns: db.NotNull(u.Pronouns),
|
|
|
|
Fields: db.NotNull(fields),
|
2022-05-04 16:27:16 +02:00
|
|
|
}
|
2022-11-21 02:38:08 +01:00
|
|
|
|
|
|
|
resp.Members = make([]PartialMember, len(members))
|
|
|
|
for i := range members {
|
|
|
|
resp.Members[i] = PartialMember{
|
|
|
|
ID: members[i].ID,
|
|
|
|
Name: members[i].Name,
|
|
|
|
DisplayName: members[i].DisplayName,
|
2023-03-12 04:25:53 +01:00
|
|
|
Bio: members[i].Bio,
|
2023-03-13 02:04:09 +01:00
|
|
|
Avatar: members[i].Avatar,
|
2023-03-12 04:25:53 +01:00
|
|
|
Links: db.NotNull(members[i].Links),
|
2023-03-12 01:31:31 +01:00
|
|
|
Names: db.NotNull(members[i].Names),
|
|
|
|
Pronouns: db.NotNull(members[i].Pronouns),
|
2022-11-21 02:38:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp
|
2022-05-04 16:27:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) getUser(w http.ResponseWriter, r *http.Request) error {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
userRef := chi.URLParamFromCtx(ctx, "userRef")
|
|
|
|
|
|
|
|
if id, err := xid.FromString(userRef); err == nil {
|
|
|
|
u, err := s.DB.User(ctx, id)
|
|
|
|
if err == nil {
|
2022-05-10 16:33:29 +02:00
|
|
|
fields, err := s.DB.UserFields(ctx, u.ID)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Error getting user fields: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-21 02:38:08 +01:00
|
|
|
members, err := s.DB.UserMembers(ctx, u.ID)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Error getting user members: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-14 17:33:18 +01:00
|
|
|
render.JSON(w, r, dbUserToResponse(u, fields, members))
|
2022-05-04 16:27:16 +02:00
|
|
|
return nil
|
|
|
|
} else if err != db.ErrUserNotFound {
|
|
|
|
log.Errorf("Error getting user by ID: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// otherwise, we fall back to checking usernames
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := s.DB.Username(ctx, userRef)
|
|
|
|
if err == db.ErrUserNotFound {
|
|
|
|
return server.APIError{
|
|
|
|
Code: server.ErrUserNotFound,
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if err != nil {
|
|
|
|
log.Errorf("Error getting user by username: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-08 14:04:26 +01:00
|
|
|
if u.DeletedAt != nil {
|
|
|
|
return server.APIError{Code: server.ErrUserNotFound}
|
|
|
|
}
|
|
|
|
|
2022-05-10 16:33:29 +02:00
|
|
|
fields, err := s.DB.UserFields(ctx, u.ID)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Error getting user fields: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-21 02:38:08 +01:00
|
|
|
members, err := s.DB.UserMembers(ctx, u.ID)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Error getting user members: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-14 17:33:18 +01:00
|
|
|
render.JSON(w, r, dbUserToResponse(u, fields, members))
|
2022-05-04 16:27:16 +02:00
|
|
|
return nil
|
|
|
|
}
|
2022-05-13 16:43:28 +02:00
|
|
|
|
|
|
|
func (s *Server) getMeUser(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 {
|
|
|
|
log.Errorf("Error getting user: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fields, err := s.DB.UserFields(ctx, u.ID)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Error getting user fields: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-21 02:38:08 +01:00
|
|
|
members, err := s.DB.UserMembers(ctx, u.ID)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Error getting user members: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-13 16:43:28 +02:00
|
|
|
render.JSON(w, r, GetMeResponse{
|
2023-03-17 14:14:31 +01:00
|
|
|
GetUserResponse: dbUserToResponse(u, fields, members),
|
|
|
|
MaxInvites: u.MaxInvites,
|
2023-03-23 14:54:43 +01:00
|
|
|
IsAdmin: u.IsAdmin,
|
2023-03-17 14:14:31 +01:00
|
|
|
Discord: u.Discord,
|
|
|
|
DiscordUsername: u.DiscordUsername,
|
|
|
|
Fediverse: u.Fediverse,
|
|
|
|
FediverseUsername: u.FediverseUsername,
|
2023-03-18 15:19:53 +01:00
|
|
|
FediverseInstance: u.FediverseInstance,
|
2022-05-13 16:43:28 +02:00
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|