Skip to content

Commit 76ca258

Browse files
committed
chore: simplify update user settings
1 parent d86756f commit 76ca258

File tree

2 files changed

+38
-218
lines changed

2 files changed

+38
-218
lines changed

server/router/api/v1/user_service.go

Lines changed: 38 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -385,11 +385,46 @@ func (s *APIV1Service) UpdateUserSetting(ctx context.Context, request *v1pb.Upda
385385
return nil, status.Errorf(codes.NotFound, "%s not found", storeKey.String())
386386
}
387387

388-
// merge only the fields specified by UpdateMask
389-
merged := mergeUserSettingWithMask(existingUserSetting, request.Setting, storeKey, request.UpdateMask.Paths)
388+
// Only GENERAL settings are supported via UpdateUserSetting
389+
// Other setting types have dedicated service methods
390+
if storeKey != storepb.UserSetting_GENERAL {
391+
return nil, status.Errorf(codes.InvalidArgument, "setting type %s should not be updated via UpdateUserSetting", storeKey.String())
392+
}
393+
394+
// Start with existing general setting values
395+
existingGeneral := existingUserSetting.GetGeneral()
396+
updatedGeneral := &v1pb.UserSetting_GeneralSetting{
397+
Appearance: existingGeneral.GetAppearance(),
398+
MemoVisibility: existingGeneral.GetMemoVisibility(),
399+
Locale: existingGeneral.GetLocale(),
400+
Theme: existingGeneral.GetTheme(),
401+
}
402+
403+
// Apply updates for fields specified in the update mask
404+
incomingGeneral := request.Setting.GetGeneralSetting()
405+
for _, field := range request.UpdateMask.Paths {
406+
switch field {
407+
case "appearance":
408+
updatedGeneral.Appearance = incomingGeneral.Appearance
409+
case "memoVisibility":
410+
updatedGeneral.MemoVisibility = incomingGeneral.MemoVisibility
411+
case "theme":
412+
updatedGeneral.Theme = incomingGeneral.Theme
413+
case "locale":
414+
updatedGeneral.Locale = incomingGeneral.Locale
415+
}
416+
}
417+
418+
// Create the updated setting
419+
updatedSetting := &v1pb.UserSetting{
420+
Name: request.Setting.Name,
421+
Value: &v1pb.UserSetting_GeneralSetting_{
422+
GeneralSetting: updatedGeneral,
423+
},
424+
}
390425

391426
// Convert API setting to store setting
392-
storeSetting, err := convertUserSettingToStore(merged, userID, storeKey)
427+
storeSetting, err := convertUserSettingToStore(updatedSetting, userID, storeKey)
393428
if err != nil {
394429
return nil, status.Errorf(codes.InvalidArgument, "failed to convert setting: %v", err)
395430
}
@@ -1335,72 +1370,3 @@ func (s *APIV1Service) validateUserFilter(_ context.Context, filterStr string) e
13351370
}
13361371
return nil
13371372
}
1338-
1339-
func mergeUserSettingWithMask(existing *storepb.UserSetting, incoming *v1pb.UserSetting, key storepb.UserSetting_Key, paths []string) *v1pb.UserSetting {
1340-
if incoming == nil {
1341-
return &v1pb.UserSetting{}
1342-
}
1343-
1344-
switch key {
1345-
case storepb.UserSetting_GENERAL:
1346-
var gs *v1pb.UserSetting_GeneralSetting
1347-
1348-
if existing == nil {
1349-
gs = &v1pb.UserSetting_GeneralSetting{
1350-
Locale: "en",
1351-
Appearance: "system",
1352-
MemoVisibility: "PRIVATE",
1353-
Theme: "",
1354-
}
1355-
} else {
1356-
gs = &v1pb.UserSetting_GeneralSetting{
1357-
Appearance: existing.GetGeneral().GetAppearance(),
1358-
MemoVisibility: existing.GetGeneral().GetMemoVisibility(),
1359-
Locale: existing.GetGeneral().GetLocale(),
1360-
Theme: existing.GetGeneral().GetTheme(),
1361-
}
1362-
}
1363-
1364-
for _, field := range paths {
1365-
switch field {
1366-
case "appearance":
1367-
gs.Appearance = incoming.GetGeneralSetting().Appearance
1368-
case "memoVisibility":
1369-
gs.MemoVisibility = incoming.GetGeneralSetting().MemoVisibility
1370-
case "theme":
1371-
gs.Theme = incoming.GetGeneralSetting().Theme
1372-
case "locale":
1373-
gs.Locale = incoming.GetGeneralSetting().Locale
1374-
}
1375-
}
1376-
1377-
return &v1pb.UserSetting{
1378-
Name: incoming.Name,
1379-
Value: &v1pb.UserSetting_GeneralSetting_{
1380-
GeneralSetting: gs,
1381-
},
1382-
}
1383-
1384-
case storepb.UserSetting_SHORTCUTS:
1385-
// handled by the FE calling shortcut_service.CreateShortcut
1386-
// if the FE wants to modify shortcuts by calling the user_service we need to handle below
1387-
1388-
return incoming
1389-
case storepb.UserSetting_WEBHOOKS:
1390-
// handled by the FE calling user_service.CreateUserWebhook
1391-
// if the FE wants to modify webhooks by calling the user_service we need to handle below
1392-
1393-
return incoming
1394-
case storepb.UserSetting_ACCESS_TOKENS:
1395-
// handled by the FE calling user_service.CreateUserAccessToken
1396-
// if the FE wants to modify access tokens by calling the user_service we need to handle below
1397-
1398-
return incoming
1399-
case storepb.UserSetting_SESSIONS:
1400-
// handled by the FE calling auth_service.CreateSession
1401-
// if the FE wants to modify sessions by calling the user_service we need to handle below
1402-
return incoming
1403-
default:
1404-
return incoming
1405-
}
1406-
}

server/router/api/v1/user_service_test.go

Lines changed: 0 additions & 146 deletions
This file was deleted.

0 commit comments

Comments
 (0)