user_setting.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package mysql
  2. import (
  3. "context"
  4. "database/sql"
  5. "strings"
  6. "github.com/pkg/errors"
  7. "google.golang.org/protobuf/encoding/protojson"
  8. storepb "github.com/usememos/memos/proto/gen/store"
  9. "github.com/usememos/memos/store"
  10. )
  11. func (d *DB) UpsertUserSetting(ctx context.Context, upsert *storepb.UserSetting) (*storepb.UserSetting, error) {
  12. stmt := "INSERT INTO `user_setting` (`user_id`, `key`, `value`) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE `value` = ?"
  13. var valueString string
  14. if upsert.Key == storepb.UserSettingKey_USER_SETTING_ACCESS_TOKENS {
  15. valueBytes, err := protojson.Marshal(upsert.GetAccessTokens())
  16. if err != nil {
  17. return nil, err
  18. }
  19. valueString = string(valueBytes)
  20. } else if upsert.Key == storepb.UserSettingKey_USER_SETTING_LOCALE {
  21. valueString = upsert.GetLocale()
  22. } else if upsert.Key == storepb.UserSettingKey_USER_SETTING_APPEARANCE {
  23. valueString = upsert.GetAppearance()
  24. } else if upsert.Key == storepb.UserSettingKey_USER_SETTING_MEMO_VISIBILITY {
  25. valueString = upsert.GetMemoVisibility()
  26. } else if upsert.Key == storepb.UserSettingKey_USER_SETTING_TELEGRAM_USER_ID {
  27. valueString = upsert.GetTelegramUserId()
  28. } else {
  29. return nil, errors.Errorf("unknown user setting key: %s", upsert.Key.String())
  30. }
  31. if _, err := d.db.ExecContext(ctx, stmt, upsert.UserId, upsert.Key.String(), valueString, valueString); err != nil {
  32. return nil, err
  33. }
  34. return upsert, nil
  35. }
  36. func (d *DB) ListUserSettings(ctx context.Context, find *store.FindUserSetting) ([]*storepb.UserSetting, error) {
  37. where, args := []string{"1 = 1"}, []any{}
  38. if v := find.Key; v != storepb.UserSettingKey_USER_SETTING_KEY_UNSPECIFIED {
  39. where, args = append(where, "`key` = ?"), append(args, v.String())
  40. }
  41. if v := find.UserID; v != nil {
  42. where, args = append(where, "`user_id` = ?"), append(args, *find.UserID)
  43. }
  44. query := "SELECT `user_id`, `key`, `value` FROM `user_setting` WHERE " + strings.Join(where, " AND ")
  45. rows, err := d.db.QueryContext(ctx, query, args...)
  46. if err != nil {
  47. return nil, err
  48. }
  49. defer rows.Close()
  50. userSettingList := make([]*storepb.UserSetting, 0)
  51. for rows.Next() {
  52. userSetting := &storepb.UserSetting{}
  53. var keyString, valueString string
  54. if err := rows.Scan(
  55. &userSetting.UserId,
  56. &keyString,
  57. &valueString,
  58. ); err != nil {
  59. return nil, err
  60. }
  61. userSetting.Key = storepb.UserSettingKey(storepb.UserSettingKey_value[keyString])
  62. if userSetting.Key == storepb.UserSettingKey_USER_SETTING_ACCESS_TOKENS {
  63. accessTokensUserSetting := &storepb.AccessTokensUserSetting{}
  64. if err := protojson.Unmarshal([]byte(valueString), accessTokensUserSetting); err != nil {
  65. return nil, err
  66. }
  67. userSetting.Value = &storepb.UserSetting_AccessTokens{
  68. AccessTokens: accessTokensUserSetting,
  69. }
  70. } else if userSetting.Key == storepb.UserSettingKey_USER_SETTING_LOCALE {
  71. userSetting.Value = &storepb.UserSetting_Locale{
  72. Locale: valueString,
  73. }
  74. } else if userSetting.Key == storepb.UserSettingKey_USER_SETTING_APPEARANCE {
  75. userSetting.Value = &storepb.UserSetting_Appearance{
  76. Appearance: valueString,
  77. }
  78. } else if userSetting.Key == storepb.UserSettingKey_USER_SETTING_MEMO_VISIBILITY {
  79. userSetting.Value = &storepb.UserSetting_MemoVisibility{
  80. MemoVisibility: valueString,
  81. }
  82. } else if userSetting.Key == storepb.UserSettingKey_USER_SETTING_TELEGRAM_USER_ID {
  83. userSetting.Value = &storepb.UserSetting_TelegramUserId{
  84. TelegramUserId: valueString,
  85. }
  86. } else {
  87. // Skip unknown user setting key.
  88. continue
  89. }
  90. userSettingList = append(userSettingList, userSetting)
  91. }
  92. if err := rows.Err(); err != nil {
  93. return nil, err
  94. }
  95. return userSettingList, nil
  96. }
  97. func vacuumUserSetting(ctx context.Context, tx *sql.Tx) error {
  98. stmt := "DELETE FROM `user_setting` WHERE `user_id` NOT IN (SELECT `id` FROM `user`)"
  99. _, err := tx.ExecContext(ctx, stmt)
  100. if err != nil {
  101. return err
  102. }
  103. return nil
  104. }