2022-05-04 07:27:16 -07:00
|
|
|
package user
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2023-05-18 18:13:46 -07:00
|
|
|
"time"
|
2022-05-04 07:27:16 -07:00
|
|
|
|
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-05-04 07:27:16 -07:00
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"github.com/go-chi/render"
|
|
|
|
"github.com/rs/xid"
|
|
|
|
)
|
|
|
|
|
|
|
|
type GetUserResponse struct {
|
2023-04-19 02:05:01 -07:00
|
|
|
ID xid.ID `json:"id"`
|
2023-06-02 18:06:26 -07:00
|
|
|
SID string `json:"sid"`
|
2023-04-19 02:05:01 -07:00
|
|
|
Username string `json:"name"`
|
|
|
|
DisplayName *string `json:"display_name"`
|
|
|
|
Bio *string `json:"bio"`
|
|
|
|
MemberTitle *string `json:"member_title"`
|
|
|
|
Avatar *string `json:"avatar"`
|
|
|
|
Links []string `json:"links"`
|
|
|
|
Names []db.FieldEntry `json:"names"`
|
|
|
|
Pronouns []db.PronounEntry `json:"pronouns"`
|
|
|
|
Members []PartialMember `json:"members"`
|
|
|
|
Fields []db.Field `json:"fields"`
|
|
|
|
CustomPreferences db.CustomPreferences `json:"custom_preferences"`
|
2023-05-25 04:40:15 -07:00
|
|
|
Flags []db.UserFlag `json:"flags"`
|
2023-06-05 07:29:18 -07:00
|
|
|
Badges db.Badge `json:"badges"`
|
2022-05-04 07:27:16 -07:00
|
|
|
}
|
|
|
|
|
2022-05-05 07:33:44 -07:00
|
|
|
type GetMeResponse struct {
|
2022-05-10 07:33:29 -07:00
|
|
|
GetUserResponse
|
2022-05-05 07:33:44 -07:00
|
|
|
|
2023-05-18 18:13:46 -07:00
|
|
|
CreatedAt time.Time `json:"created_at"`
|
|
|
|
|
2023-06-02 18:06:26 -07:00
|
|
|
MaxInvites int `json:"max_invites"`
|
|
|
|
IsAdmin bool `json:"is_admin"`
|
|
|
|
ListPrivate bool `json:"list_private"`
|
|
|
|
LastSIDReroll time.Time `json:"last_sid_reroll"`
|
2023-03-23 06:54:43 -07:00
|
|
|
|
2022-05-05 07:33:44 -07:00
|
|
|
Discord *string `json:"discord"`
|
|
|
|
DiscordUsername *string `json:"discord_username"`
|
2023-03-17 06:14:31 -07:00
|
|
|
|
2023-04-17 18:49:37 -07:00
|
|
|
Tumblr *string `json:"tumblr"`
|
|
|
|
TumblrUsername *string `json:"tumblr_username"`
|
|
|
|
|
2023-04-18 13:52:58 -07:00
|
|
|
Google *string `json:"google"`
|
|
|
|
GoogleUsername *string `json:"google_username"`
|
|
|
|
|
2023-03-17 06:14:31 -07:00
|
|
|
Fediverse *string `json:"fediverse"`
|
|
|
|
FediverseUsername *string `json:"fediverse_username"`
|
|
|
|
FediverseInstance *string `json:"fediverse_instance"`
|
2022-05-05 07:33:44 -07:00
|
|
|
}
|
|
|
|
|
2022-05-04 07:27:16 -07:00
|
|
|
type PartialMember 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"`
|
2023-03-11 19:25:53 -08:00
|
|
|
Bio *string `json:"bio"`
|
2023-03-12 18:04:09 -07:00
|
|
|
Avatar *string `json:"avatar"`
|
2023-03-11 19:25:53 -08:00
|
|
|
Links []string `json:"links"`
|
2023-03-11 16:31:31 -08:00
|
|
|
Names []db.FieldEntry `json:"names"`
|
|
|
|
Pronouns []db.PronounEntry `json:"pronouns"`
|
2022-05-04 07:27:16 -07:00
|
|
|
}
|
|
|
|
|
2023-05-25 04:40:15 -07:00
|
|
|
func dbUserToResponse(u db.User, fields []db.Field, members []db.Member, flags []db.UserFlag) GetUserResponse {
|
2022-11-20 17:38:08 -08:00
|
|
|
resp := GetUserResponse{
|
2023-04-19 02:05:01 -07:00
|
|
|
ID: u.ID,
|
2023-06-02 18:06:26 -07:00
|
|
|
SID: u.SID,
|
2023-04-19 02:05:01 -07:00
|
|
|
Username: u.Username,
|
|
|
|
DisplayName: u.DisplayName,
|
|
|
|
Bio: u.Bio,
|
|
|
|
MemberTitle: u.MemberTitle,
|
|
|
|
Avatar: u.Avatar,
|
|
|
|
Links: db.NotNull(u.Links),
|
|
|
|
Names: db.NotNull(u.Names),
|
|
|
|
Pronouns: db.NotNull(u.Pronouns),
|
|
|
|
Fields: db.NotNull(fields),
|
|
|
|
CustomPreferences: u.CustomPreferences,
|
2023-05-25 04:40:15 -07:00
|
|
|
Flags: flags,
|
2022-05-04 07:27:16 -07:00
|
|
|
}
|
2022-11-20 17:38:08 -08:00
|
|
|
|
2023-06-05 07:29:18 -07:00
|
|
|
if u.IsAdmin {
|
|
|
|
resp.Badges |= db.BadgeAdmin
|
|
|
|
}
|
|
|
|
|
2022-11-20 17:38:08 -08:00
|
|
|
resp.Members = make([]PartialMember, len(members))
|
|
|
|
for i := range members {
|
|
|
|
resp.Members[i] = PartialMember{
|
|
|
|
ID: members[i].ID,
|
2023-06-02 18:06:26 -07:00
|
|
|
SID: members[i].SID,
|
2022-11-20 17:38:08 -08:00
|
|
|
Name: members[i].Name,
|
|
|
|
DisplayName: members[i].DisplayName,
|
2023-03-11 19:25:53 -08:00
|
|
|
Bio: members[i].Bio,
|
2023-03-12 18:04:09 -07:00
|
|
|
Avatar: members[i].Avatar,
|
2023-03-11 19:25:53 -08:00
|
|
|
Links: db.NotNull(members[i].Links),
|
2023-03-11 16:31:31 -08:00
|
|
|
Names: db.NotNull(members[i].Names),
|
|
|
|
Pronouns: db.NotNull(members[i].Pronouns),
|
2022-11-20 17:38:08 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp
|
2022-05-04 07:27:16 -07:00
|
|
|
}
|
|
|
|
|
2023-05-25 04:40:15 -07:00
|
|
|
func (s *Server) getUser(w http.ResponseWriter, r *http.Request) (err error) {
|
2022-05-04 07:27:16 -07:00
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
userRef := chi.URLParamFromCtx(ctx, "userRef")
|
|
|
|
|
2023-05-25 04:40:15 -07:00
|
|
|
var u db.User
|
2022-05-04 07:27:16 -07:00
|
|
|
if id, err := xid.FromString(userRef); err == nil {
|
2023-05-25 04:40:15 -07:00
|
|
|
u, err = s.DB.User(ctx, id)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("getting user by ID: %v", err)
|
2022-05-04 07:27:16 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-25 04:40:15 -07:00
|
|
|
if u.ID.IsNil() {
|
|
|
|
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
|
2022-05-04 07:27:16 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-05-10 07:33:29 -07:00
|
|
|
fields, err := s.DB.UserFields(ctx, u.ID)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Error getting user fields: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-25 04:40:15 -07:00
|
|
|
flags, err := s.DB.UserFlags(ctx, u.ID)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("getting user flags: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-01 08:20:59 -07:00
|
|
|
var members []db.Member
|
|
|
|
if !u.ListPrivate || isSelf {
|
|
|
|
members, err = s.DB.UserMembers(ctx, u.ID, isSelf)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Error getting user members: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
2022-11-20 17:38:08 -08:00
|
|
|
}
|
|
|
|
|
2023-05-25 04:40:15 -07:00
|
|
|
render.JSON(w, r, dbUserToResponse(u, fields, members, flags))
|
2022-05-04 07:27:16 -07:00
|
|
|
return nil
|
|
|
|
}
|
2022-05-13 07:43:28 -07: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
|
|
|
|
}
|
|
|
|
|
2023-04-01 08:20:59 -07:00
|
|
|
members, err := s.DB.UserMembers(ctx, u.ID, true)
|
2022-11-20 17:38:08 -08:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Error getting user members: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-25 04:40:15 -07:00
|
|
|
flags, err := s.DB.UserFlags(ctx, u.ID)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("getting user flags: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-13 07:43:28 -07:00
|
|
|
render.JSON(w, r, GetMeResponse{
|
2023-05-25 04:40:15 -07:00
|
|
|
GetUserResponse: dbUserToResponse(u, fields, members, flags),
|
2023-05-18 18:13:46 -07:00
|
|
|
CreatedAt: u.ID.Time(),
|
2023-03-17 06:14:31 -07:00
|
|
|
MaxInvites: u.MaxInvites,
|
2023-03-23 06:54:43 -07:00
|
|
|
IsAdmin: u.IsAdmin,
|
2023-04-01 08:20:59 -07:00
|
|
|
ListPrivate: u.ListPrivate,
|
2023-06-02 18:06:26 -07:00
|
|
|
LastSIDReroll: u.LastSIDReroll,
|
2023-03-17 06:14:31 -07:00
|
|
|
Discord: u.Discord,
|
|
|
|
DiscordUsername: u.DiscordUsername,
|
2023-04-17 18:49:37 -07:00
|
|
|
Tumblr: u.Tumblr,
|
|
|
|
TumblrUsername: u.TumblrUsername,
|
2023-04-18 13:52:58 -07:00
|
|
|
Google: u.Google,
|
|
|
|
GoogleUsername: u.GoogleUsername,
|
2023-03-17 06:14:31 -07:00
|
|
|
Fediverse: u.Fediverse,
|
|
|
|
FediverseUsername: u.FediverseUsername,
|
2023-03-18 07:19:53 -07:00
|
|
|
FediverseInstance: u.FediverseInstance,
|
2022-05-13 07:43:28 -07:00
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|