mirror of
https://github.com/usememos/memos.git
synced 2025-06-05 22:09:59 +02:00
feat: add user setting field (#2054)
This commit is contained in:
@ -23,7 +23,6 @@ func NewUserService(store *store.Store) *UserService {
|
||||
}
|
||||
|
||||
func (s *UserService) GetUser(ctx context.Context, request *apiv2pb.GetUserRequest) (*apiv2pb.GetUserResponse, error) {
|
||||
println("GetUser", request.Name)
|
||||
user, err := s.Store.GetUser(ctx, &store.FindUser{
|
||||
Username: &request.Name,
|
||||
})
|
||||
@ -38,6 +37,18 @@ func (s *UserService) GetUser(ctx context.Context, request *apiv2pb.GetUserReque
|
||||
// Data desensitization.
|
||||
userMessage.OpenId = ""
|
||||
|
||||
userUID := int(userMessage.Id)
|
||||
userSettings, err := s.Store.ListUserSettings(ctx, &store.FindUserSetting{
|
||||
UserID: &userUID,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "failed to list user settings: %v", err)
|
||||
}
|
||||
// TODO: check the access permission for user settings.
|
||||
for _, userSetting := range userSettings {
|
||||
userMessage.Settings = append(userMessage.Settings, convertUserSettingFromStore(userSetting))
|
||||
}
|
||||
|
||||
response := &apiv2pb.GetUserResponse{
|
||||
User: userMessage,
|
||||
}
|
||||
@ -56,6 +67,7 @@ func convertUserFromStore(user *store.User) *apiv2pb.User {
|
||||
Nickname: user.Nickname,
|
||||
OpenId: user.OpenID,
|
||||
AvatarUrl: user.AvatarURL,
|
||||
Settings: []*apiv2pb.UserSetting{},
|
||||
}
|
||||
}
|
||||
|
||||
@ -71,3 +83,46 @@ func convertUserRoleFromStore(role store.Role) apiv2pb.Role {
|
||||
return apiv2pb.Role_ROLE_UNSPECIFIED
|
||||
}
|
||||
}
|
||||
|
||||
func convertUserSettingFromStore(userSetting *store.UserSetting) *apiv2pb.UserSetting {
|
||||
userSettingKey := apiv2pb.UserSetting_KEY_UNSPECIFIED
|
||||
userSettingValue := &apiv2pb.UserSettingValue{}
|
||||
switch userSetting.Key {
|
||||
case "locale":
|
||||
userSettingKey = apiv2pb.UserSetting_LOCALE
|
||||
userSettingValue.Value = &apiv2pb.UserSettingValue_StringValue{
|
||||
StringValue: userSetting.Value,
|
||||
}
|
||||
case "appearance":
|
||||
userSettingKey = apiv2pb.UserSetting_APPEARANCE
|
||||
userSettingValue.Value = &apiv2pb.UserSettingValue_StringValue{
|
||||
StringValue: userSetting.Value,
|
||||
}
|
||||
case "memo-visibility":
|
||||
userSettingKey = apiv2pb.UserSetting_MEMO_VISIBILITY
|
||||
userSettingValue.Value = &apiv2pb.UserSettingValue_VisibilityValue{
|
||||
VisibilityValue: convertVisibilityFromString(userSetting.Value),
|
||||
}
|
||||
case "telegram-user-id":
|
||||
userSettingKey = apiv2pb.UserSetting_TELEGRAM_USER_ID
|
||||
userSettingValue.Value = &apiv2pb.UserSettingValue_StringValue{
|
||||
StringValue: userSetting.Value,
|
||||
}
|
||||
}
|
||||
return &apiv2pb.UserSetting{
|
||||
UserId: int32(userSetting.UserID),
|
||||
Key: userSettingKey,
|
||||
Value: userSettingValue,
|
||||
}
|
||||
}
|
||||
|
||||
func convertVisibilityFromString(visibility string) apiv2pb.Visibility {
|
||||
switch visibility {
|
||||
case "public":
|
||||
return apiv2pb.Visibility_PUBLIC
|
||||
case "private":
|
||||
return apiv2pb.Visibility_PRIVATE
|
||||
default:
|
||||
return apiv2pb.Visibility_VISIBILITY_UNSPECIFIED
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user