2023-06-26 23:06:53 +08:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2023-07-02 18:56:25 +08:00
|
|
|
"net/http"
|
2023-06-26 23:06:53 +08:00
|
|
|
|
2023-07-02 18:56:25 +08:00
|
|
|
"github.com/labstack/echo/v4"
|
2023-09-17 22:55:13 +08:00
|
|
|
"github.com/pkg/errors"
|
2023-06-26 23:06:53 +08:00
|
|
|
"golang.org/x/exp/slices"
|
2023-09-17 22:55:13 +08:00
|
|
|
|
|
|
|
"github.com/usememos/memos/store"
|
2023-06-26 23:06:53 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type UserSettingKey string
|
|
|
|
|
|
|
|
const (
|
|
|
|
// UserSettingLocaleKey is the key type for user locale.
|
|
|
|
UserSettingLocaleKey UserSettingKey = "locale"
|
|
|
|
// UserSettingAppearanceKey is the key type for user appearance.
|
|
|
|
UserSettingAppearanceKey UserSettingKey = "appearance"
|
|
|
|
// UserSettingMemoVisibilityKey is the key type for user preference memo default visibility.
|
|
|
|
UserSettingMemoVisibilityKey UserSettingKey = "memo-visibility"
|
2023-08-11 22:34:08 +08:00
|
|
|
// UserSettingTelegramUserIDKey is the key type for telegram UserID of memos user.
|
2023-06-26 23:06:53 +08:00
|
|
|
UserSettingTelegramUserIDKey UserSettingKey = "telegram-user-id"
|
|
|
|
)
|
|
|
|
|
|
|
|
// String returns the string format of UserSettingKey type.
|
|
|
|
func (key UserSettingKey) String() string {
|
|
|
|
switch key {
|
|
|
|
case UserSettingLocaleKey:
|
|
|
|
return "locale"
|
|
|
|
case UserSettingAppearanceKey:
|
|
|
|
return "appearance"
|
|
|
|
case UserSettingMemoVisibilityKey:
|
|
|
|
return "memo-visibility"
|
|
|
|
case UserSettingTelegramUserIDKey:
|
|
|
|
return "telegram-user-id"
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
UserSettingLocaleValue = []string{
|
|
|
|
"de",
|
|
|
|
"en",
|
|
|
|
"es",
|
|
|
|
"fr",
|
2023-07-22 09:38:54 +08:00
|
|
|
"hi",
|
2023-06-26 23:06:53 +08:00
|
|
|
"hr",
|
|
|
|
"it",
|
|
|
|
"ja",
|
|
|
|
"ko",
|
|
|
|
"nl",
|
|
|
|
"pl",
|
|
|
|
"pt-BR",
|
|
|
|
"ru",
|
|
|
|
"sl",
|
|
|
|
"sv",
|
|
|
|
"tr",
|
|
|
|
"uk",
|
|
|
|
"vi",
|
|
|
|
"zh-Hans",
|
|
|
|
"zh-Hant",
|
|
|
|
}
|
|
|
|
UserSettingAppearanceValue = []string{"system", "light", "dark"}
|
|
|
|
UserSettingMemoVisibilityValue = []Visibility{Private, Protected, Public}
|
|
|
|
)
|
|
|
|
|
|
|
|
type UserSetting struct {
|
2023-08-04 21:55:07 +08:00
|
|
|
UserID int32 `json:"userId"`
|
2023-06-26 23:06:53 +08:00
|
|
|
Key UserSettingKey `json:"key"`
|
2023-07-02 18:56:25 +08:00
|
|
|
Value string `json:"value"`
|
2023-06-26 23:06:53 +08:00
|
|
|
}
|
|
|
|
|
2023-07-02 18:56:25 +08:00
|
|
|
type UpsertUserSettingRequest struct {
|
2023-08-04 21:55:07 +08:00
|
|
|
UserID int32 `json:"-"`
|
2023-06-26 23:06:53 +08:00
|
|
|
Key UserSettingKey `json:"key"`
|
|
|
|
Value string `json:"value"`
|
|
|
|
}
|
|
|
|
|
2023-08-09 21:53:06 +08:00
|
|
|
func (s *APIV1Service) registerUserSettingRoutes(g *echo.Group) {
|
2023-08-09 22:30:27 +08:00
|
|
|
g.POST("/user/setting", s.UpsertUserSetting)
|
2023-08-09 21:53:06 +08:00
|
|
|
}
|
|
|
|
|
2023-08-09 22:30:27 +08:00
|
|
|
// UpsertUserSetting godoc
|
2023-08-09 21:53:06 +08:00
|
|
|
//
|
2023-08-09 22:30:27 +08:00
|
|
|
// @Summary Upsert user setting
|
2023-08-09 21:53:06 +08:00
|
|
|
// @Tags user-setting
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param body body UpsertUserSettingRequest true "Request object."
|
|
|
|
// @Success 200 {object} store.UserSetting "Created user setting"
|
|
|
|
// @Failure 400 {object} nil "Malformatted post user setting upsert request | Invalid user setting format"
|
|
|
|
// @Failure 401 {object} nil "Missing auth session"
|
|
|
|
// @Failure 500 {object} nil "Failed to upsert user setting"
|
|
|
|
// @Router /api/v1/user/setting [POST]
|
2023-08-09 22:30:27 +08:00
|
|
|
func (s *APIV1Service) UpsertUserSetting(c echo.Context) error {
|
2023-08-09 21:53:06 +08:00
|
|
|
ctx := c.Request().Context()
|
2023-09-14 20:16:17 +08:00
|
|
|
userID, ok := c.Get(userIDContextKey).(int32)
|
2023-08-09 21:53:06 +08:00
|
|
|
if !ok {
|
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "Missing auth session")
|
|
|
|
}
|
|
|
|
|
|
|
|
userSettingUpsert := &UpsertUserSettingRequest{}
|
|
|
|
if err := json.NewDecoder(c.Request().Body).Decode(userSettingUpsert); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post user setting upsert request").SetInternal(err)
|
|
|
|
}
|
|
|
|
if err := userSettingUpsert.Validate(); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Invalid user setting format").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
userSettingUpsert.UserID = userID
|
|
|
|
userSetting, err := s.Store.UpsertUserSetting(ctx, &store.UserSetting{
|
|
|
|
UserID: userID,
|
|
|
|
Key: userSettingUpsert.Key.String(),
|
|
|
|
Value: userSettingUpsert.Value,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to upsert user setting").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
userSettingMessage := convertUserSettingFromStore(userSetting)
|
|
|
|
return c.JSON(http.StatusOK, userSettingMessage)
|
|
|
|
}
|
|
|
|
|
2023-07-02 18:56:25 +08:00
|
|
|
func (upsert UpsertUserSettingRequest) Validate() error {
|
2023-06-26 23:06:53 +08:00
|
|
|
if upsert.Key == UserSettingLocaleKey {
|
|
|
|
localeValue := "en"
|
|
|
|
err := json.Unmarshal([]byte(upsert.Value), &localeValue)
|
|
|
|
if err != nil {
|
2023-09-29 13:04:54 +08:00
|
|
|
return errors.New("failed to unmarshal user setting locale value")
|
2023-06-26 23:06:53 +08:00
|
|
|
}
|
|
|
|
if !slices.Contains(UserSettingLocaleValue, localeValue) {
|
2023-09-29 13:04:54 +08:00
|
|
|
return errors.New("invalid user setting locale value")
|
2023-06-26 23:06:53 +08:00
|
|
|
}
|
|
|
|
} else if upsert.Key == UserSettingAppearanceKey {
|
|
|
|
appearanceValue := "system"
|
|
|
|
err := json.Unmarshal([]byte(upsert.Value), &appearanceValue)
|
|
|
|
if err != nil {
|
2023-09-29 13:04:54 +08:00
|
|
|
return errors.New("failed to unmarshal user setting appearance value")
|
2023-06-26 23:06:53 +08:00
|
|
|
}
|
|
|
|
if !slices.Contains(UserSettingAppearanceValue, appearanceValue) {
|
2023-09-29 13:04:54 +08:00
|
|
|
return errors.New("invalid user setting appearance value")
|
2023-06-26 23:06:53 +08:00
|
|
|
}
|
|
|
|
} else if upsert.Key == UserSettingMemoVisibilityKey {
|
|
|
|
memoVisibilityValue := Private
|
|
|
|
err := json.Unmarshal([]byte(upsert.Value), &memoVisibilityValue)
|
|
|
|
if err != nil {
|
2023-09-29 13:04:54 +08:00
|
|
|
return errors.New("failed to unmarshal user setting memo visibility value")
|
2023-06-26 23:06:53 +08:00
|
|
|
}
|
|
|
|
if !slices.Contains(UserSettingMemoVisibilityValue, memoVisibilityValue) {
|
2023-09-29 13:04:54 +08:00
|
|
|
return errors.New("invalid user setting memo visibility value")
|
2023-06-26 23:06:53 +08:00
|
|
|
}
|
|
|
|
} else if upsert.Key == UserSettingTelegramUserIDKey {
|
2023-07-02 18:56:25 +08:00
|
|
|
var key string
|
|
|
|
err := json.Unmarshal([]byte(upsert.Value), &key)
|
2023-06-26 23:06:53 +08:00
|
|
|
if err != nil {
|
2023-09-29 13:04:54 +08:00
|
|
|
return errors.New("invalid user setting telegram user id value")
|
2023-06-26 23:06:53 +08:00
|
|
|
}
|
|
|
|
} else {
|
2023-09-29 13:04:54 +08:00
|
|
|
return errors.New("invalid user setting key")
|
2023-06-26 23:06:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-02 18:56:25 +08:00
|
|
|
func convertUserSettingFromStore(userSetting *store.UserSetting) *UserSetting {
|
|
|
|
return &UserSetting{
|
|
|
|
UserID: userSetting.UserID,
|
|
|
|
Key: UserSettingKey(userSetting.Key),
|
|
|
|
Value: userSetting.Value,
|
|
|
|
}
|
2023-06-26 23:06:53 +08:00
|
|
|
}
|