mirror of https://github.com/usememos/memos
feat: add `user_setting` model (#145)
* feat: add `user_setting` model * chore: add global store * chore: update settings in web * chore: update `i18n` examplepull/161/head
parent
dfac877957
commit
90b881502d
@ -0,0 +1,34 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
type UserSettingKey string
|
||||||
|
|
||||||
|
const (
|
||||||
|
// UserSettingLocaleKey is the key type for user locale
|
||||||
|
UserSettingLocaleKey UserSettingKey = "locale"
|
||||||
|
)
|
||||||
|
|
||||||
|
// String returns the string format of UserSettingKey type.
|
||||||
|
func (key UserSettingKey) String() string {
|
||||||
|
switch key {
|
||||||
|
case UserSettingLocaleKey:
|
||||||
|
return "locale"
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
type UserSetting struct {
|
||||||
|
UserID int
|
||||||
|
Key UserSettingKey `json:"key"`
|
||||||
|
// Value is a JSON string with basic value
|
||||||
|
Value string `json:"value"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type UserSettingUpsert struct {
|
||||||
|
UserID int
|
||||||
|
Key UserSettingKey `json:"key"`
|
||||||
|
Value string `json:"value"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type UserSettingFind struct {
|
||||||
|
UserID int
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
-- user_setting
|
||||||
|
CREATE TABLE user_setting (
|
||||||
|
user_id INTEGER NOT NULL,
|
||||||
|
key TEXT NOT NULL,
|
||||||
|
value TEXT NOT NULL,
|
||||||
|
FOREIGN KEY(user_id) REFERENCES user(id) ON DELETE CASCADE
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE UNIQUE INDEX user_setting_key_user_id_index ON user_setting(key, user_id);
|
@ -0,0 +1,122 @@
|
|||||||
|
package store
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
|
||||||
|
"github.com/usememos/memos/api"
|
||||||
|
)
|
||||||
|
|
||||||
|
type userSettingRaw struct {
|
||||||
|
UserID int
|
||||||
|
Key api.UserSettingKey
|
||||||
|
Value string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (raw *userSettingRaw) toUserSetting() *api.UserSetting {
|
||||||
|
return &api.UserSetting{
|
||||||
|
UserID: raw.UserID,
|
||||||
|
Key: raw.Key,
|
||||||
|
Value: raw.Value,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Store) UpsertUserSetting(ctx context.Context, upsert *api.UserSettingUpsert) (*api.UserSetting, error) {
|
||||||
|
tx, err := s.db.BeginTx(ctx, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, FormatError(err)
|
||||||
|
}
|
||||||
|
defer tx.Rollback()
|
||||||
|
|
||||||
|
userSettingRaw, err := upsertUserSetting(ctx, tx, upsert)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := tx.Commit(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
userSetting := userSettingRaw.toUserSetting()
|
||||||
|
|
||||||
|
return userSetting, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Store) FindUserSettingList(ctx context.Context, find *api.UserSettingFind) ([]*api.UserSetting, error) {
|
||||||
|
tx, err := s.db.BeginTx(ctx, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, FormatError(err)
|
||||||
|
}
|
||||||
|
defer tx.Rollback()
|
||||||
|
|
||||||
|
userSettingRawList, err := findUserSettingList(ctx, tx, find)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
list := []*api.UserSetting{}
|
||||||
|
for _, raw := range userSettingRawList {
|
||||||
|
list = append(list, raw.toUserSetting())
|
||||||
|
}
|
||||||
|
|
||||||
|
return list, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func upsertUserSetting(ctx context.Context, tx *sql.Tx, upsert *api.UserSettingUpsert) (*userSettingRaw, error) {
|
||||||
|
query := `
|
||||||
|
INSERT INTO user_setting (
|
||||||
|
user_id, key, value
|
||||||
|
)
|
||||||
|
VALUES (?, ?, ?)
|
||||||
|
ON CONFLICT(user_id, key) DO UPDATE
|
||||||
|
SET
|
||||||
|
value = EXCLUDED.value
|
||||||
|
RETURNING user_id, key, value
|
||||||
|
`
|
||||||
|
var userSettingRaw userSettingRaw
|
||||||
|
if err := tx.QueryRowContext(ctx, query, upsert.UserID, upsert.Key, upsert.Value).Scan(
|
||||||
|
&userSettingRaw.UserID,
|
||||||
|
&userSettingRaw.Key,
|
||||||
|
&userSettingRaw.Value,
|
||||||
|
); err != nil {
|
||||||
|
return nil, FormatError(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &userSettingRaw, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func findUserSettingList(ctx context.Context, tx *sql.Tx, find *api.UserSettingFind) ([]*userSettingRaw, error) {
|
||||||
|
query := `
|
||||||
|
SELECT
|
||||||
|
user_id,
|
||||||
|
key,
|
||||||
|
value
|
||||||
|
FROM user_setting
|
||||||
|
WHERE user_id = ?
|
||||||
|
`
|
||||||
|
rows, err := tx.QueryContext(ctx, query, find.UserID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, FormatError(err)
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
userSettingRawList := make([]*userSettingRaw, 0)
|
||||||
|
for rows.Next() {
|
||||||
|
var userSettingRaw userSettingRaw
|
||||||
|
if err := rows.Scan(
|
||||||
|
&userSettingRaw.UserID,
|
||||||
|
&userSettingRaw.Key,
|
||||||
|
&userSettingRaw.Value,
|
||||||
|
); err != nil {
|
||||||
|
return nil, FormatError(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
userSettingRawList = append(userSettingRawList, &userSettingRaw)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
return nil, FormatError(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return userSettingRawList, nil
|
||||||
|
}
|
@ -1,3 +1,8 @@
|
|||||||
{
|
{
|
||||||
"about": "About"
|
"common": {
|
||||||
|
"about": "About",
|
||||||
|
"email": "Email",
|
||||||
|
"password": "Password",
|
||||||
|
"sign-in": "Sign in"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,8 @@
|
|||||||
{
|
{
|
||||||
"about": "关于"
|
"common": {
|
||||||
|
"about": "关于",
|
||||||
|
"email": "邮箱",
|
||||||
|
"password": "密码",
|
||||||
|
"sign-in": "登录"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,41 @@
|
|||||||
|
import store from "../store";
|
||||||
|
import * as api from "../helpers/api";
|
||||||
|
import * as storage from "../helpers/storage";
|
||||||
|
import { setGlobalState, setLocale } from "../store/modules/global";
|
||||||
|
import { convertResponseModelUser } from "./userService";
|
||||||
|
|
||||||
|
const globalService = {
|
||||||
|
getState: () => {
|
||||||
|
return store.getState().global;
|
||||||
|
},
|
||||||
|
|
||||||
|
initialState: async () => {
|
||||||
|
const defaultGlobalState = {
|
||||||
|
locale: "en" as Locale,
|
||||||
|
};
|
||||||
|
|
||||||
|
const { locale: storageLocale } = storage.get(["locale"]);
|
||||||
|
if (storageLocale) {
|
||||||
|
defaultGlobalState.locale = storageLocale;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const { data } = (await api.getMyselfUser()).data;
|
||||||
|
if (data) {
|
||||||
|
const user = convertResponseModelUser(data);
|
||||||
|
if (user.setting.locale) {
|
||||||
|
defaultGlobalState.locale = user.setting.locale;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
// do nth
|
||||||
|
}
|
||||||
|
|
||||||
|
store.dispatch(setGlobalState(defaultGlobalState));
|
||||||
|
},
|
||||||
|
|
||||||
|
setLocale: (locale: Locale) => {
|
||||||
|
store.dispatch(setLocale(locale));
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export default globalService;
|
@ -0,0 +1,25 @@
|
|||||||
|
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
|
||||||
|
|
||||||
|
interface State {
|
||||||
|
locale: Locale;
|
||||||
|
}
|
||||||
|
|
||||||
|
const globalSlice = createSlice({
|
||||||
|
name: "global",
|
||||||
|
initialState: {} as State,
|
||||||
|
reducers: {
|
||||||
|
setGlobalState: (_, action: PayloadAction<State>) => {
|
||||||
|
return action.payload;
|
||||||
|
},
|
||||||
|
setLocale: (state, action: PayloadAction<Locale>) => {
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
locale: action.payload,
|
||||||
|
};
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
export const { setGlobalState, setLocale } = globalSlice.actions;
|
||||||
|
|
||||||
|
export default globalSlice.reducer;
|
@ -0,0 +1 @@
|
|||||||
|
type Locale = "en" | "zh";
|
@ -0,0 +1,15 @@
|
|||||||
|
interface Setting {
|
||||||
|
locale: "en" | "zh";
|
||||||
|
}
|
||||||
|
|
||||||
|
interface UserLocaleSetting {
|
||||||
|
key: "locale";
|
||||||
|
value: "en" | "zh";
|
||||||
|
}
|
||||||
|
|
||||||
|
type UserSetting = UserLocaleSetting;
|
||||||
|
|
||||||
|
interface UserSettingUpsert {
|
||||||
|
key: keyof Setting;
|
||||||
|
value: string;
|
||||||
|
}
|
Loading…
Reference in New Issue