Handle no avatar, add delete avatar support

This commit is contained in:
blankhex committed 2026-10-22 23:08:23 +03:00
1 parent 35bff424f1
commit 3864a4c1cc
9 files changed
+110 -11

No files matched your search

+31 -2
View File
@@ -20,6 +20,7 @@ type userResponse struct {
FullName *string `json:"fullName"`
Email *string `json:"email"`
IsAdmin bool `json:"isAdmin"`
HasAvatar bool `json:"hasAvatar"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
ActivatedAt *time.Time `json:"activatedAt"`
@@ -38,7 +39,12 @@ func (h *Handler) listUsers(w http.ResponseWriter, r *http.Request) {
result := make([]userResponse, 0, len(users))
for _, u := range users {
result = append(result, newUserResponse(u))
hasAvatar, err := h.users.HasAvatar(r.Context(), u)
if err != nil {
h.fail(w, err)
return
}
result = append(result, newUserResponse(u, hasAvatar))
}
writeJson(w, http.StatusOK, result)
@@ -131,7 +137,18 @@ func (h *Handler) getUserAvatar(w http.ResponseWriter, r *http.Request) {
// avatarURL builds the absolute URL of a user's avatar, preferring the
// configured PublicBaseURL and falling back to the request's scheme and Host.
// It returns an empty string when the user has no avatar.
func (h *Handler) avatarURL(r *http.Request, id int64) string {
user, err := h.users.UserById(r.Context(), id)
if err != nil {
return ""
}
hasAvatar, err := h.users.HasAvatar(r.Context(), user)
if err != nil || !hasAvatar {
return ""
}
base := h.cfg.PublicBaseURL
if base == "" {
scheme := "http"
@@ -146,13 +163,25 @@ func (h *Handler) avatarURL(r *http.Request, id int64) string {
return fmt.Sprintf("%s/api/users/%d/avatar", base, id)
}
func newUserResponse(u *user.User) userResponse {
// respondUser writes a user response that includes whether the user has an
// avatar stored.
func (h *Handler) respondUser(w http.ResponseWriter, r *http.Request, u *user.User, status int) {
hasAvatar, err := h.users.HasAvatar(r.Context(), u)
if err != nil {
h.fail(w, err)
return
}
writeJson(w, status, newUserResponse(u, hasAvatar))
}
func newUserResponse(u *user.User, hasAvatar bool) userResponse {
return userResponse{
Id: u.Id,
Login: u.Login,
FullName: u.FullName,
Email: u.Email,
IsAdmin: u.IsAdmin,
HasAvatar: hasAvatar,
CreatedAt: u.CreatedAt,
UpdatedAt: u.UpdatedAt,
ActivatedAt: u.ActivatedAt,