feat(profile): support chatwoot settings updates

This commit is contained in:
2026-06-05 01:24:38 +08:00
parent a88b49bb04
commit 7aa3362185
3 changed files with 225 additions and 6 deletions
+67 -2
View File
@@ -1,7 +1,9 @@
package v1
import (
"encoding/json"
"net/http"
"strings"
"github.com/gin-gonic/gin"
@@ -50,8 +52,8 @@ func (h *ProfileHandler) Update(c *gin.Context) {
return
}
var req service.ProfileUpdatePayload
if err := c.ShouldBindJSON(&req); err != nil {
req, err := bindProfileUpdate(c)
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, err.Error())
return
}
@@ -67,6 +69,69 @@ func (h *ProfileHandler) Update(c *gin.Context) {
c.JSON(http.StatusOK, user)
}
func bindProfileUpdate(c *gin.Context) (service.ProfileUpdatePayload, error) {
contentType := c.GetHeader("Content-Type")
if strings.Contains(contentType, "multipart/form-data") || strings.Contains(contentType, "application/x-www-form-urlencoded") {
return bindProfileUpdateForm(c)
}
var req service.ProfileUpdatePayload
if err := c.ShouldBindJSON(&req); err != nil {
return req, err
}
return req, nil
}
func bindProfileUpdateForm(c *gin.Context) (service.ProfileUpdatePayload, error) {
var req service.ProfileUpdatePayload
if err := c.Request.ParseMultipartForm(32 << 20); err != nil && !strings.Contains(err.Error(), "request Content-Type isn't multipart/form-data") {
return req, err
}
form := c.Request.Form
profile := &req.Profile
if value := form.Get("profile[name]"); value != "" {
profile.Name = value
}
if value := form.Get("profile[email]"); value != "" {
profile.Email = value
}
if _, ok := form["profile[display_name]"]; ok {
value := form.Get("profile[display_name]")
profile.DisplayName = &value
}
if _, ok := form["profile[message_signature]"]; ok {
value := form.Get("profile[message_signature]")
profile.MessageSignature = &value
}
if _, ok := form["profile[phone_number]"]; ok {
value := form.Get("profile[phone_number]")
profile.PhoneNumber = &value
}
if value := form.Get("profile[avatar_url]"); value != "" {
profile.AvatarURL = value
}
if file, err := c.FormFile("profile[avatar]"); err == nil && file != nil {
profile.AvatarURL = file.Filename
}
uiSettings := map[string]any{}
for key, values := range form {
if !strings.HasPrefix(key, "profile[ui_settings][") || len(values) == 0 {
continue
}
settingKey := strings.TrimSuffix(strings.TrimPrefix(key, "profile[ui_settings]["), "]")
uiSettings[settingKey] = values[0]
}
if raw := form.Get("profile[ui_settings]"); raw != "" {
var parsed map[string]any
if err := json.Unmarshal([]byte(raw), &parsed); err == nil {
uiSettings = parsed
}
}
if len(uiSettings) > 0 {
profile.UISettings = uiSettings
}
return req, nil
}
// UpdateAvatar updates the current user's avatar.
// PUT /api/v1/profile/avatar
func (h *ProfileHandler) UpdateAvatar(c *gin.Context) {