2023-09-28 17:09:52 +03:00
|
|
|
package mysql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"google.golang.org/protobuf/encoding/protojson"
|
|
|
|
|
|
|
|
storepb "github.com/usememos/memos/proto/gen/store"
|
|
|
|
"github.com/usememos/memos/store"
|
|
|
|
)
|
|
|
|
|
2023-10-05 18:11:29 +03:00
|
|
|
func (d *DB) UpsertUserSetting(ctx context.Context, upsert *store.UserSetting) (*store.UserSetting, error) {
|
2023-10-07 17:56:12 +03:00
|
|
|
stmt := "INSERT INTO `user_setting` (`user_id`, `key`, `value`) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE `value` = ?"
|
2023-09-28 17:09:52 +03:00
|
|
|
if _, err := d.db.ExecContext(ctx, stmt, upsert.UserID, upsert.Key, upsert.Value, upsert.Value); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return upsert, nil
|
|
|
|
}
|
|
|
|
|
2023-10-05 18:11:29 +03:00
|
|
|
func (d *DB) ListUserSettings(ctx context.Context, find *store.FindUserSetting) ([]*store.UserSetting, error) {
|
2023-09-28 17:09:52 +03:00
|
|
|
where, args := []string{"1 = 1"}, []any{}
|
|
|
|
|
|
|
|
if v := find.Key; v != "" {
|
2023-10-07 17:56:12 +03:00
|
|
|
where, args = append(where, "`key` = ?"), append(args, v)
|
2023-09-28 17:09:52 +03:00
|
|
|
}
|
|
|
|
if v := find.UserID; v != nil {
|
2023-10-07 17:56:12 +03:00
|
|
|
where, args = append(where, "`user_id` = ?"), append(args, *find.UserID)
|
2023-09-28 17:09:52 +03:00
|
|
|
}
|
|
|
|
|
2023-10-07 17:56:12 +03:00
|
|
|
query := "SELECT `user_id`, `key`, `value` FROM `user_setting` WHERE " + strings.Join(where, " AND ")
|
2023-09-28 17:09:52 +03:00
|
|
|
rows, err := d.db.QueryContext(ctx, query, args...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
userSettingList := make([]*store.UserSetting, 0)
|
|
|
|
for rows.Next() {
|
|
|
|
var userSetting store.UserSetting
|
|
|
|
if err := rows.Scan(
|
|
|
|
&userSetting.UserID,
|
|
|
|
&userSetting.Key,
|
|
|
|
&userSetting.Value,
|
|
|
|
); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
userSettingList = append(userSettingList, &userSetting)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return userSettingList, nil
|
|
|
|
}
|
|
|
|
|
2023-10-05 18:11:29 +03:00
|
|
|
func (d *DB) UpsertUserSettingV1(ctx context.Context, upsert *storepb.UserSetting) (*storepb.UserSetting, error) {
|
2023-10-07 17:56:12 +03:00
|
|
|
stmt := "INSERT INTO `user_setting` (`user_id`, `key`, `value`) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE `value` = ?"
|
2023-09-28 17:09:52 +03: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)
|
|
|
|
} else {
|
|
|
|
return nil, errors.New("invalid user setting key")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := d.db.ExecContext(ctx, stmt, upsert.UserId, upsert.Key.String(), valueString, valueString); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return upsert, nil
|
|
|
|
}
|
|
|
|
|
2023-10-05 18:11:29 +03:00
|
|
|
func (d *DB) ListUserSettingsV1(ctx context.Context, find *store.FindUserSettingV1) ([]*storepb.UserSetting, error) {
|
2023-09-28 17:09:52 +03:00
|
|
|
where, args := []string{"1 = 1"}, []any{}
|
|
|
|
|
|
|
|
if v := find.Key; v != storepb.UserSettingKey_USER_SETTING_KEY_UNSPECIFIED {
|
2023-10-07 17:56:12 +03:00
|
|
|
where, args = append(where, "`key` = ?"), append(args, v.String())
|
2023-09-28 17:09:52 +03:00
|
|
|
}
|
|
|
|
if v := find.UserID; v != nil {
|
2023-10-07 17:56:12 +03:00
|
|
|
where, args = append(where, "`user_id` = ?"), append(args, *find.UserID)
|
2023-09-28 17:09:52 +03:00
|
|
|
}
|
|
|
|
|
2023-10-07 17:56:12 +03:00
|
|
|
query := "SELECT `user_id`, `key`, `value` FROM `user_setting` WHERE " + strings.Join(where, " AND ")
|
2023-09-28 17:09:52 +03: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,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Skip unknown user setting v1 key.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
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 {
|
2023-10-07 17:56:12 +03:00
|
|
|
stmt := "DELETE FROM `user_setting` WHERE `user_id` NOT IN (SELECT `id` FROM `user`)"
|
2023-09-28 17:09:52 +03:00
|
|
|
_, err := tx.ExecContext(ctx, stmt)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|