fix: remove 500 error responses from upload endpoints; fix: return validation error strings instead of error lists; fix: handle invalid avatar upload IDs with 400 Bad Request response; fix: add missing S3Controller to controller initialization; fix: change avatar_upload_id to string type and update validation rules; chore: add license header to smtp.go; refactor: replace manual proxy implementation with httputil.ReverseProxy; fix: inject S3Service dependency into ProfileService; fix: set color and color_grad fields during profile update; fix: correct DTO mapping for profile and settings; fix: check object existence before copying in SaveUpload; fix: adjust profile DTO mapping function for proper pointer handling
198 lines
5.2 KiB
Go
198 lines
5.2 KiB
Go
// Copyright (c) 2025 Nikolai Papin
|
|
//
|
|
// This file is part of Easywish
|
|
//
|
|
// This program is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// This program is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
|
|
// the GNU General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
package controllers
|
|
|
|
import (
|
|
"easywish/internal/dto"
|
|
errs "easywish/internal/errors"
|
|
"easywish/internal/services"
|
|
"easywish/internal/utils/enums"
|
|
"errors"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
type ProfileController struct {
|
|
log *zap.Logger
|
|
ps services.ProfileService
|
|
}
|
|
|
|
func NewProfileController(_log *zap.Logger, _ps services.ProfileService) Controller {
|
|
|
|
ctrl := ProfileController{log: _log, ps: _ps}
|
|
|
|
return &controllerImpl{
|
|
Path: "/profile",
|
|
Middleware: []gin.HandlerFunc{},
|
|
Methods: []ControllerMethod{
|
|
{
|
|
HttpMethod: GET,
|
|
Path: "",
|
|
Authorization: enums.UserRole,
|
|
Middleware: []gin.HandlerFunc{},
|
|
Function: ctrl.getMyProfile,
|
|
},
|
|
{
|
|
HttpMethod: GET,
|
|
Path: "/:username",
|
|
Authorization: enums.GuestRole,
|
|
Middleware: []gin.HandlerFunc{},
|
|
Function: ctrl.getProfileByUsername,
|
|
},
|
|
{
|
|
HttpMethod: GET,
|
|
Path: "/settings",
|
|
Authorization: enums.UserRole,
|
|
Middleware: []gin.HandlerFunc{},
|
|
Function: ctrl.getProfileSettings,
|
|
},
|
|
{
|
|
HttpMethod: PUT,
|
|
Path: "",
|
|
Authorization: enums.UserRole,
|
|
Middleware: []gin.HandlerFunc{},
|
|
Function: ctrl.updateProfile,
|
|
},
|
|
{
|
|
HttpMethod: PUT,
|
|
Path: "/settings",
|
|
Authorization: enums.UserRole,
|
|
Middleware: []gin.HandlerFunc{},
|
|
Function: ctrl.updateProfileSettings,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
// @Summary Get your profile
|
|
// @Tags Profile
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security JWT
|
|
// @Success 200 {object} dto.ProfileDto " "
|
|
// @Router /profile [get]
|
|
func (ctrl *ProfileController) getMyProfile(c *gin.Context) {
|
|
cinfo := GetClientInfo(c)
|
|
|
|
response, err := ctrl.ps.GetMyProfile(cinfo); if err != nil {
|
|
c.Status(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, response)
|
|
}
|
|
|
|
// @Summary Get profile by username
|
|
// @Tags Profile
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security JWT
|
|
// @Param username path string true " "
|
|
// @Success 200 {object} dto.ProfileDto " "
|
|
// @Failure 404 "Profile not found"
|
|
// @Failure 403 "Restricted profile"
|
|
// @Router /profile/{username} [get]
|
|
func (ctrl *ProfileController) getProfileByUsername(c *gin.Context) {
|
|
cinfo := GetClientInfo(c)
|
|
|
|
username := c.Param("username"); if username == "" {
|
|
c.Status(http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
response, err := ctrl.ps.GetProfileByUsername(cinfo, username); if err != nil {
|
|
if errors.Is(err, errs.ErrNotFound) {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "Profile not found"})
|
|
} else if errors.Is(err, errs.ErrForbidden) {
|
|
c.JSON(http.StatusForbidden, gin.H{"error": "Access restricted by profile's privacy settings"})
|
|
} else {
|
|
c.Status(http.StatusInternalServerError)
|
|
}
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, response)
|
|
}
|
|
|
|
// @Summary Get your profile settings
|
|
// @Tags Profile
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security JWT
|
|
// @Success 200 {object} dto.ProfileSettingsDto " "
|
|
// @Router /profile/settings [get]
|
|
func (ctrl *ProfileController) getProfileSettings(c *gin.Context) {
|
|
cinfo := GetClientInfo(c)
|
|
|
|
response, err := ctrl.ps.GetProfileSettings(cinfo); if err != nil {
|
|
c.Status(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, response)
|
|
}
|
|
|
|
// @Summary Update your profile
|
|
// @Tags Profile
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security JWT
|
|
// @Param request body dto.NewProfileDto true " "
|
|
// @Success 200 {object} bool " "
|
|
// @Router /profile [put]
|
|
func (ctrl *ProfileController) updateProfile(c *gin.Context) {
|
|
request, err := GetRequest[dto.NewProfileDto](c); if err != nil {
|
|
return
|
|
}
|
|
|
|
response, err := ctrl.ps.UpdateProfile(request.User, request.Body); if err != nil {
|
|
|
|
if errors.Is(err, errs.ErrFileNotFound) {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid upload ID. Make sure file was uploaded and is not expired."})
|
|
}
|
|
|
|
c.Status(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, response)
|
|
}
|
|
|
|
// @Summary Update your profile's settings
|
|
// @Tags Profile
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security JWT
|
|
// @Param request body dto.ProfileSettingsDto true " "
|
|
// @Success 200 {object} bool " "
|
|
// @Router /profile/settings [put]
|
|
func (ctrl *ProfileController) updateProfileSettings(c *gin.Context) {
|
|
request, err := GetRequest[dto.ProfileSettingsDto](c); if err != nil {
|
|
return
|
|
}
|
|
|
|
response, err := ctrl.ps.UpdateProfileSettings(request.User, request.Body); if err != nil || !response {
|
|
c.Status(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, response)
|
|
}
|