From 737991827897c88e2d6d0dbd3ffeb399059569cb Mon Sep 17 00:00:00 2001 From: Stefan Pletka <124689083+Eisei24@users.noreply.github.com> Date: Tue, 22 Oct 2024 13:44:29 +0200 Subject: [PATCH 1/3] Changed device identifier to id --- backend/pkg/api/data_access/dummy.go | 4 +-- backend/pkg/api/data_access/notifications.go | 30 ++++++++++---------- backend/pkg/api/handlers/public.go | 4 +-- backend/pkg/api/types/notifications.go | 2 +- frontend/types/api/notifications.ts | 2 +- 5 files changed, 21 insertions(+), 21 deletions(-) diff --git a/backend/pkg/api/data_access/dummy.go b/backend/pkg/api/data_access/dummy.go index 24472343a..cf123aec6 100644 --- a/backend/pkg/api/data_access/dummy.go +++ b/backend/pkg/api/data_access/dummy.go @@ -576,10 +576,10 @@ func (d *DummyService) UpdateNotificationSettingsGeneral(ctx context.Context, us func (d *DummyService) UpdateNotificationSettingsNetworks(ctx context.Context, userId uint64, chainId uint64, settings t.NotificationSettingsNetwork) error { return nil } -func (d *DummyService) UpdateNotificationSettingsPairedDevice(ctx context.Context, userId uint64, pairedDeviceId string, name string, IsNotificationsEnabled bool) error { +func (d *DummyService) UpdateNotificationSettingsPairedDevice(ctx context.Context, userId uint64, pairedDeviceId uint64, name string, IsNotificationsEnabled bool) error { return nil } -func (d *DummyService) DeleteNotificationSettingsPairedDevice(ctx context.Context, userId uint64, pairedDeviceId string) error { +func (d *DummyService) DeleteNotificationSettingsPairedDevice(ctx context.Context, userId uint64, pairedDeviceId uint64) error { return nil } diff --git a/backend/pkg/api/data_access/notifications.go b/backend/pkg/api/data_access/notifications.go index 01d54b860..106b4b61f 100644 --- a/backend/pkg/api/data_access/notifications.go +++ b/backend/pkg/api/data_access/notifications.go @@ -50,8 +50,8 @@ type NotificationsRepository interface { GetNotificationSettingsDefaultValues(ctx context.Context) (*t.NotificationSettingsDefaultValues, error) UpdateNotificationSettingsGeneral(ctx context.Context, userId uint64, settings t.NotificationSettingsGeneral) error UpdateNotificationSettingsNetworks(ctx context.Context, userId uint64, chainId uint64, settings t.NotificationSettingsNetwork) error - UpdateNotificationSettingsPairedDevice(ctx context.Context, userId uint64, pairedDeviceId string, name string, IsNotificationsEnabled bool) error - DeleteNotificationSettingsPairedDevice(ctx context.Context, userId uint64, pairedDeviceId string) error + UpdateNotificationSettingsPairedDevice(ctx context.Context, userId uint64, pairedDeviceId uint64, name string, IsNotificationsEnabled bool) error + DeleteNotificationSettingsPairedDevice(ctx context.Context, userId uint64, pairedDeviceId uint64) error UpdateNotificationSettingsClients(ctx context.Context, userId uint64, clientId uint64, IsSubscribed bool) (*t.NotificationSettingsClient, error) GetNotificationSettingsDashboards(ctx context.Context, userId uint64, cursor string, colSort t.Sort[enums.NotificationSettingsDashboardColumn], search string, limit uint64) ([]t.NotificationSettingsDashboardsTableRow, *t.Paging, error) UpdateNotificationSettingsValidatorDashboard(ctx context.Context, userId uint64, dashboardId t.VDBIdPrimary, groupId uint64, settings t.NotificationSettingsValidatorDashboard) error @@ -1325,20 +1325,20 @@ func (d *DataAccessService) GetNotificationSettings(ctx context.Context, userId // ------------------------------------- // Get the paired devices pairedDevices := []struct { - DeviceIdentifier sql.NullString `db:"device_identifier"` - CreatedTs time.Time `db:"created_ts"` - DeviceName string `db:"device_name"` - NotifyEnabled bool `db:"notify_enabled"` + DeviceId uint64 `db:"id"` + CreatedTs time.Time `db:"created_ts"` + DeviceName string `db:"device_name"` + NotifyEnabled bool `db:"notify_enabled"` }{} wg.Go(func() error { err := d.userReader.SelectContext(ctx, &pairedDevices, ` SELECT - device_identifier, + id, created_ts, device_name, COALESCE(notify_enabled, false) AS notify_enabled FROM users_devices - WHERE user_id = $1 AND device_identifier IS NOT NULL`, userId) + WHERE user_id = $1`, userId) if err != nil { return fmt.Errorf(`error retrieving data for notifications paired devices: %w`, err) } @@ -1429,7 +1429,7 @@ func (d *DataAccessService) GetNotificationSettings(ctx context.Context, userId for _, device := range pairedDevices { result.PairedDevices = append(result.PairedDevices, t.NotificationPairedDevice{ - Id: device.DeviceIdentifier.String, + Id: device.DeviceId, PairedTimestamp: device.CreatedTs.Unix(), Name: device.DeviceName, IsNotificationsEnabled: device.NotifyEnabled, @@ -1642,13 +1642,13 @@ func (d *DataAccessService) UpdateNotificationSettingsNetworks(ctx context.Conte } return nil } -func (d *DataAccessService) UpdateNotificationSettingsPairedDevice(ctx context.Context, userId uint64, pairedDeviceId string, name string, IsNotificationsEnabled bool) error { +func (d *DataAccessService) UpdateNotificationSettingsPairedDevice(ctx context.Context, userId uint64, pairedDeviceId uint64, name string, IsNotificationsEnabled bool) error { result, err := d.userWriter.ExecContext(ctx, ` UPDATE users_devices SET device_name = $1, notify_enabled = $2 - WHERE user_id = $3 AND device_identifier = $4`, + WHERE user_id = $3 AND id = $4`, name, IsNotificationsEnabled, userId, pairedDeviceId) if err != nil { return err @@ -1660,14 +1660,14 @@ func (d *DataAccessService) UpdateNotificationSettingsPairedDevice(ctx context.C return err } if rowsAffected == 0 { - return fmt.Errorf("device with id %s to update notification settings not found", pairedDeviceId) + return fmt.Errorf("device with id %v to update notification settings not found", pairedDeviceId) } return nil } -func (d *DataAccessService) DeleteNotificationSettingsPairedDevice(ctx context.Context, userId uint64, pairedDeviceId string) error { +func (d *DataAccessService) DeleteNotificationSettingsPairedDevice(ctx context.Context, userId uint64, pairedDeviceId uint64) error { result, err := d.userWriter.ExecContext(ctx, ` DELETE FROM users_devices - WHERE user_id = $1 AND device_identifier = $2`, + WHERE user_id = $1 AND id = $2`, userId, pairedDeviceId) if err != nil { return err @@ -1679,7 +1679,7 @@ func (d *DataAccessService) DeleteNotificationSettingsPairedDevice(ctx context.C return err } if rowsAffected == 0 { - return fmt.Errorf("device with id %s to delete not found", pairedDeviceId) + return fmt.Errorf("device with id %v to delete not found", pairedDeviceId) } return nil } diff --git a/backend/pkg/api/handlers/public.go b/backend/pkg/api/handlers/public.go index 98d1b8b55..f3792d7e5 100644 --- a/backend/pkg/api/handlers/public.go +++ b/backend/pkg/api/handlers/public.go @@ -2345,7 +2345,7 @@ func (h *HandlerService) PublicPutUserNotificationSettingsPairedDevices(w http.R return } // TODO use a better way to validate the paired device id - pairedDeviceId := v.checkRegex(reNonEmpty, mux.Vars(r)["paired_device_id"], "paired_device_id") + pairedDeviceId := v.checkUint(mux.Vars(r)["paired_device_id"], "paired_device_id") name := v.checkNameNotEmpty(req.Name) if v.hasErrors() { handleErr(w, r, v) @@ -2386,7 +2386,7 @@ func (h *HandlerService) PublicDeleteUserNotificationSettingsPairedDevices(w htt return } // TODO use a better way to validate the paired device id - pairedDeviceId := v.checkRegex(reNonEmpty, mux.Vars(r)["paired_device_id"], "paired_device_id") + pairedDeviceId := v.checkUint(mux.Vars(r)["paired_device_id"], "paired_device_id") if v.hasErrors() { handleErr(w, r, v) return diff --git a/backend/pkg/api/types/notifications.go b/backend/pkg/api/types/notifications.go index 36ca1b1b6..e2baeb32c 100644 --- a/backend/pkg/api/types/notifications.go +++ b/backend/pkg/api/types/notifications.go @@ -160,7 +160,7 @@ type NotificationNetwork struct { type InternalPutUserNotificationSettingsNetworksResponse ApiDataResponse[NotificationNetwork] type NotificationPairedDevice struct { - Id string `json:"id"` + Id uint64 `json:"id"` PairedTimestamp int64 `json:"paired_timestamp"` Name string `json:"name,omitempty"` IsNotificationsEnabled bool `json:"is_notifications_enabled"` diff --git a/frontend/types/api/notifications.ts b/frontend/types/api/notifications.ts index 2dc1c96ee..7abe5c7f9 100644 --- a/frontend/types/api/notifications.ts +++ b/frontend/types/api/notifications.ts @@ -152,7 +152,7 @@ export interface NotificationNetwork { } export type InternalPutUserNotificationSettingsNetworksResponse = ApiDataResponse; export interface NotificationPairedDevice { - id: string; + id: number /* uint64 */; paired_timestamp: number /* int64 */; name?: string; is_notifications_enabled: boolean; From 707cfcba6d1f49a7aca1717e719ae7d243038fec Mon Sep 17 00:00:00 2001 From: Stefan Pletka <124689083+Eisei24@users.noreply.github.com> Date: Tue, 22 Oct 2024 14:13:11 +0200 Subject: [PATCH 2/3] Fix linter issue --- backend/pkg/api/handlers/input_validation.go | 1 - 1 file changed, 1 deletion(-) diff --git a/backend/pkg/api/handlers/input_validation.go b/backend/pkg/api/handlers/input_validation.go index 16478449a..eca791aa8 100644 --- a/backend/pkg/api/handlers/input_validation.go +++ b/backend/pkg/api/handlers/input_validation.go @@ -33,7 +33,6 @@ var ( reEthereumAddress = regexp.MustCompile(`^(0x)?[0-9a-fA-F]{40}$`) reWithdrawalCredential = regexp.MustCompile(`^(0x0[01])?[0-9a-fA-F]{62}$`) reEnsName = regexp.MustCompile(`^.+\.eth$`) - reNonEmpty = regexp.MustCompile(`^\s*\S.*$`) reGraffiti = regexp.MustCompile(`^.{2,}$`) // at least 2 characters, so that queries won't time out reCursor = regexp.MustCompile(`^[A-Za-z0-9-_]+$`) // has to be base64 reEmail = regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$") From 18aecaa98e7e595e04e4ce9eee10e69cfd00bc10 Mon Sep 17 00:00:00 2001 From: Stefan Pletka <124689083+Eisei24@users.noreply.github.com> Date: Tue, 22 Oct 2024 14:42:13 +0200 Subject: [PATCH 3/3] refactor(notifications): Use number id instead of string device_identifier as unique device identifier --- .../NotificationsManagementPairedDeviceModalContent.vue | 4 ++-- .../management/NotificationsManagementPairedDevicesModal.vue | 2 +- .../stores/notifications/useNotificationsManagementStore.ts | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/frontend/components/notifications/management/NotificationsManagementPairedDeviceModalContent.vue b/frontend/components/notifications/management/NotificationsManagementPairedDeviceModalContent.vue index 4aa91fc2d..3118dc856 100644 --- a/frontend/components/notifications/management/NotificationsManagementPairedDeviceModalContent.vue +++ b/frontend/components/notifications/management/NotificationsManagementPairedDeviceModalContent.vue @@ -10,12 +10,12 @@ const props = defineProps<{ }>() const emit = defineEmits<{ - (e: 'remove-device', id: string): void, + (e: 'remove-device', id: number): void, (e: 'toggle-notifications', { id, value, }: { - id: string, + id: number, value: boolean, }): void, }>() diff --git a/frontend/components/notifications/management/NotificationsManagementPairedDevicesModal.vue b/frontend/components/notifications/management/NotificationsManagementPairedDevicesModal.vue index d4ed85e8a..c4fea645a 100644 --- a/frontend/components/notifications/management/NotificationsManagementPairedDevicesModal.vue +++ b/frontend/components/notifications/management/NotificationsManagementPairedDevicesModal.vue @@ -11,7 +11,7 @@ const handleToggleNotifications = ({ id, value, }: { - id: string, + id: number, value: boolean, }) => { notificationsManagementStore.setNotificationForPairedDevice({ diff --git a/frontend/stores/notifications/useNotificationsManagementStore.ts b/frontend/stores/notifications/useNotificationsManagementStore.ts index f27f09553..776caa87e 100644 --- a/frontend/stores/notifications/useNotificationsManagementStore.ts +++ b/frontend/stores/notifications/useNotificationsManagementStore.ts @@ -44,7 +44,7 @@ export const useNotificationsManagementStore = defineStore('notifications-manage ) } - const removeDevice = async (id: string) => { + const removeDevice = async (id: number) => { await fetch( API_PATH.NOTIFICATIONS_MANAGEMENT_PAIRED_DEVICES_DELETE, {}, @@ -61,7 +61,7 @@ export const useNotificationsManagementStore = defineStore('notifications-manage id, value, }: { - id: string, + id: number, value: boolean, }) => { await fetch(