2023-12-03 13:31:29 +08:00
|
|
|
package postgres
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
2024-01-06 17:12:10 +08:00
|
|
|
"strings"
|
2023-12-03 13:31:29 +08:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"google.golang.org/protobuf/encoding/protojson"
|
|
|
|
|
|
|
|
storepb "github.com/usememos/memos/proto/gen/store"
|
|
|
|
"github.com/usememos/memos/store"
|
|
|
|
)
|
|
|
|
|
2023-12-08 22:41:47 +08:00
|
|
|
func (d *DB) UpsertUserSetting(ctx context.Context, upsert *storepb.UserSetting) (*storepb.UserSetting, error) {
|
2024-01-06 17:12:10 +08:00
|
|
|
stmt := `
|
|
|
|
INSERT INTO user_setting (
|
|
|
|
user_id, key, value
|
|
|
|
)
|
|
|
|
VALUES ($1, $2, $3)
|
|
|
|
ON CONFLICT(user_id, key) DO UPDATE
|
|
|
|
SET value = EXCLUDED.value
|
|
|
|
`
|
2023-12-03 13:31:29 +08:00
|
|
|
var valueString string
|
|
|
|
if upsert.Key == storepb.UserSettingKey_USER_SETTING_ACCESS_TOKENS {
|
|
|
|
valueBytes, err := protojson.Marshal(upsert.GetAccessTokens())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
valueString = string(valueBytes)
|
2023-12-08 09:54:32 +08:00
|
|
|
} else if upsert.Key == storepb.UserSettingKey_USER_SETTING_LOCALE {
|
|
|
|
valueString = upsert.GetLocale()
|
|
|
|
} else if upsert.Key == storepb.UserSettingKey_USER_SETTING_APPEARANCE {
|
|
|
|
valueString = upsert.GetAppearance()
|
|
|
|
} else if upsert.Key == storepb.UserSettingKey_USER_SETTING_MEMO_VISIBILITY {
|
|
|
|
valueString = upsert.GetMemoVisibility()
|
|
|
|
} else if upsert.Key == storepb.UserSettingKey_USER_SETTING_TELEGRAM_USER_ID {
|
|
|
|
valueString = upsert.GetTelegramUserId()
|
2023-12-03 13:31:29 +08:00
|
|
|
} else {
|
2023-12-08 09:54:32 +08:00
|
|
|
return nil, errors.Errorf("unknown user setting key: %s", upsert.Key.String())
|
2023-12-03 13:31:29 +08:00
|
|
|
}
|
|
|
|
|
2024-01-06 17:12:10 +08:00
|
|
|
if _, err := d.db.ExecContext(ctx, stmt, upsert.UserId, upsert.Key.String(), valueString); err != nil {
|
2023-12-03 13:31:29 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return upsert, nil
|
|
|
|
}
|
|
|
|
|
2023-12-08 22:41:47 +08:00
|
|
|
func (d *DB) ListUserSettings(ctx context.Context, find *store.FindUserSetting) ([]*storepb.UserSetting, error) {
|
2024-01-06 17:12:10 +08:00
|
|
|
where, args := []string{"1 = 1"}, []any{}
|
2023-12-03 13:31:29 +08:00
|
|
|
|
|
|
|
if v := find.Key; v != storepb.UserSettingKey_USER_SETTING_KEY_UNSPECIFIED {
|
2024-01-06 17:12:10 +08:00
|
|
|
where, args = append(where, "key = "+placeholder(len(args)+1)), append(args, v.String())
|
2023-12-03 13:31:29 +08:00
|
|
|
}
|
|
|
|
if v := find.UserID; v != nil {
|
2024-01-06 17:12:10 +08:00
|
|
|
where, args = append(where, "user_id = "+placeholder(len(args)+1)), append(args, *find.UserID)
|
2023-12-03 13:31:29 +08:00
|
|
|
}
|
|
|
|
|
2024-01-06 17:12:10 +08:00
|
|
|
query := `
|
|
|
|
SELECT
|
|
|
|
user_id,
|
|
|
|
key,
|
|
|
|
value
|
|
|
|
FROM user_setting
|
|
|
|
WHERE ` + strings.Join(where, " AND ")
|
2023-12-03 13:31:29 +08:00
|
|
|
rows, err := d.db.QueryContext(ctx, query, args...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
userSettingList := make([]*storepb.UserSetting, 0)
|
|
|
|
for rows.Next() {
|
|
|
|
userSetting := &storepb.UserSetting{}
|
|
|
|
var keyString, valueString string
|
|
|
|
if err := rows.Scan(
|
|
|
|
&userSetting.UserId,
|
|
|
|
&keyString,
|
|
|
|
&valueString,
|
|
|
|
); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
userSetting.Key = storepb.UserSettingKey(storepb.UserSettingKey_value[keyString])
|
|
|
|
if userSetting.Key == storepb.UserSettingKey_USER_SETTING_ACCESS_TOKENS {
|
|
|
|
accessTokensUserSetting := &storepb.AccessTokensUserSetting{}
|
|
|
|
if err := protojson.Unmarshal([]byte(valueString), accessTokensUserSetting); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
userSetting.Value = &storepb.UserSetting_AccessTokens{
|
|
|
|
AccessTokens: accessTokensUserSetting,
|
|
|
|
}
|
2023-12-08 09:54:32 +08:00
|
|
|
} else if userSetting.Key == storepb.UserSettingKey_USER_SETTING_LOCALE {
|
|
|
|
userSetting.Value = &storepb.UserSetting_Locale{
|
|
|
|
Locale: valueString,
|
|
|
|
}
|
|
|
|
} else if userSetting.Key == storepb.UserSettingKey_USER_SETTING_APPEARANCE {
|
|
|
|
userSetting.Value = &storepb.UserSetting_Appearance{
|
|
|
|
Appearance: valueString,
|
|
|
|
}
|
|
|
|
} else if userSetting.Key == storepb.UserSettingKey_USER_SETTING_MEMO_VISIBILITY {
|
|
|
|
userSetting.Value = &storepb.UserSetting_MemoVisibility{
|
|
|
|
MemoVisibility: valueString,
|
|
|
|
}
|
|
|
|
} else if userSetting.Key == storepb.UserSettingKey_USER_SETTING_TELEGRAM_USER_ID {
|
|
|
|
userSetting.Value = &storepb.UserSetting_TelegramUserId{
|
|
|
|
TelegramUserId: valueString,
|
|
|
|
}
|
2023-12-03 13:31:29 +08:00
|
|
|
} else {
|
2023-12-08 22:05:43 +08:00
|
|
|
// Skip unknown user setting key.
|
|
|
|
continue
|
2023-12-03 13:31:29 +08:00
|
|
|
}
|
|
|
|
userSettingList = append(userSettingList, userSetting)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return userSettingList, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func vacuumUserSetting(ctx context.Context, tx *sql.Tx) error {
|
2024-01-06 17:12:10 +08:00
|
|
|
stmt := `DELETE FROM user_setting WHERE user_id NOT IN (SELECT id FROM "user")`
|
2024-01-05 21:27:16 +08:00
|
|
|
_, err := tx.ExecContext(ctx, stmt)
|
2023-12-03 13:31:29 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-05 21:27:16 +08:00
|
|
|
return nil
|
2023-12-03 13:31:29 +08:00
|
|
|
}
|