workspace_setting.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package store
  2. import (
  3. "context"
  4. "github.com/pkg/errors"
  5. storepb "github.com/usememos/memos/proto/gen/store"
  6. )
  7. type WorkspaceSetting struct {
  8. Name string
  9. Value string
  10. Description string
  11. }
  12. type FindWorkspaceSetting struct {
  13. Name string
  14. }
  15. type DeleteWorkspaceSetting struct {
  16. Name string
  17. }
  18. func (s *Store) UpsertWorkspaceSetting(ctx context.Context, upsert *WorkspaceSetting) (*WorkspaceSetting, error) {
  19. return s.driver.UpsertWorkspaceSetting(ctx, upsert)
  20. }
  21. func (s *Store) ListWorkspaceSettings(ctx context.Context, find *FindWorkspaceSetting) ([]*WorkspaceSetting, error) {
  22. list, err := s.driver.ListWorkspaceSettings(ctx, find)
  23. if err != nil {
  24. return nil, err
  25. }
  26. for _, systemSettingMessage := range list {
  27. s.workspaceSettingCache.Store(systemSettingMessage.Name, systemSettingMessage)
  28. }
  29. return list, nil
  30. }
  31. func (s *Store) GetWorkspaceSetting(ctx context.Context, find *FindWorkspaceSetting) (*WorkspaceSetting, error) {
  32. if find.Name != "" {
  33. if cache, ok := s.workspaceSettingCache.Load(find.Name); ok {
  34. return cache.(*WorkspaceSetting), nil
  35. }
  36. }
  37. list, err := s.ListWorkspaceSettings(ctx, find)
  38. if err != nil {
  39. return nil, err
  40. }
  41. if len(list) == 0 {
  42. return nil, nil
  43. }
  44. systemSettingMessage := list[0]
  45. s.workspaceSettingCache.Store(systemSettingMessage.Name, systemSettingMessage)
  46. return systemSettingMessage, nil
  47. }
  48. func (s *Store) DeleteWorkspaceSetting(ctx context.Context, delete *DeleteWorkspaceSetting) error {
  49. err := s.driver.DeleteWorkspaceSetting(ctx, delete)
  50. if err != nil {
  51. return errors.Wrap(err, "Failed to delete workspace setting")
  52. }
  53. s.workspaceSettingCache.Delete(delete.Name)
  54. return nil
  55. }
  56. type FindWorkspaceSettingV1 struct {
  57. Key storepb.WorkspaceSettingKey
  58. }
  59. func (s *Store) UpsertWorkspaceSettingV1(ctx context.Context, upsert *storepb.WorkspaceSetting) (*storepb.WorkspaceSetting, error) {
  60. workspaceSetting, err := s.driver.UpsertWorkspaceSettingV1(ctx, upsert)
  61. if err != nil {
  62. return nil, errors.Wrap(err, "Failed to upsert workspace setting")
  63. }
  64. s.workspaceSettingV1Cache.Store(workspaceSetting.Key.String(), workspaceSetting)
  65. return workspaceSetting, nil
  66. }
  67. func (s *Store) ListWorkspaceSettingsV1(ctx context.Context, find *FindWorkspaceSettingV1) ([]*storepb.WorkspaceSetting, error) {
  68. list, err := s.driver.ListWorkspaceSettingsV1(ctx, find)
  69. if err != nil {
  70. return nil, err
  71. }
  72. for _, workspaceSetting := range list {
  73. s.workspaceSettingV1Cache.Store(workspaceSetting.Key.String(), workspaceSetting)
  74. }
  75. return list, nil
  76. }
  77. func (s *Store) GetWorkspaceSettingV1(ctx context.Context, find *FindWorkspaceSettingV1) (*storepb.WorkspaceSetting, error) {
  78. if find.Key != storepb.WorkspaceSettingKey_WORKSPACE_SETTING_KEY_UNSPECIFIED {
  79. if cache, ok := s.workspaceSettingV1Cache.Load(find.Key.String()); ok {
  80. return cache.(*storepb.WorkspaceSetting), nil
  81. }
  82. }
  83. list, err := s.ListWorkspaceSettingsV1(ctx, find)
  84. if err != nil {
  85. return nil, err
  86. }
  87. if len(list) == 0 {
  88. return nil, nil
  89. }
  90. workspaceSetting := list[0]
  91. s.workspaceSettingV1Cache.Store(workspaceSetting.Key.String(), workspaceSetting)
  92. return workspaceSetting, nil
  93. }
  94. func (s *Store) GetWorkspaceGeneralSetting(ctx context.Context) (*storepb.WorkspaceGeneralSetting, error) {
  95. workspaceSetting, err := s.GetWorkspaceSettingV1(ctx, &FindWorkspaceSettingV1{
  96. Key: storepb.WorkspaceSettingKey_WORKSPACE_SETTING_GENERAL,
  97. })
  98. if err != nil {
  99. return nil, errors.Wrap(err, "failed to get workspace setting")
  100. }
  101. workspaceGeneralSetting := &storepb.WorkspaceGeneralSetting{}
  102. if workspaceSetting != nil {
  103. workspaceGeneralSetting = workspaceSetting.GetGeneral()
  104. }
  105. return workspaceGeneralSetting, nil
  106. }