From cbf2fdf0f257dd4b9c709ba3760a5ae81646b2a2 Mon Sep 17 00:00:00 2001 From: limes Date: Thu, 20 Aug 2026 03:29:58 +0200 Subject: [PATCH] feat: simplify region update logic, per review --- package-lock.json | 8 +-- package.json | 2 +- src/services/grpc/api/v2/update-user-data.ts | 54 ++++---------------- 3 files changed, 16 insertions(+), 48 deletions(-) diff --git a/package-lock.json b/package-lock.json index bc74dfa..a2e593b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,7 +12,7 @@ "@aws-sdk/client-s3": "^3.657.0", "@aws-sdk/client-ses": "^3.515.0", "@inquirer/prompts": "^7.2.0", - "@pretendonetwork/grpc": "^2.5.6", + "@pretendonetwork/grpc": "^2.5.7", "bcrypt": "^5.0.0", "buffer-crc32": "^0.2.13", "colors": "^1.4.0", @@ -1669,9 +1669,9 @@ } }, "node_modules/@pretendonetwork/grpc": { - "version": "2.5.6", - "resolved": "https://registry.npmjs.org/@pretendonetwork/grpc/-/grpc-2.5.6.tgz", - "integrity": "sha512-AHsWYTIJMipokrBEi6azf71i8NLsb1p3LkNJMdJ3Guc/+imlsdFSRnRoAj6K85YxE6prOgm0wsQQ5JEOFDd8Sg==", + "version": "2.5.7", + "resolved": "https://registry.npmjs.org/@pretendonetwork/grpc/-/grpc-2.5.7.tgz", + "integrity": "sha512-HmWyBxm/Om6S5k+PYLxVU/MaRcSUEV0NAfKccy4i02ZJTZccCJRRKiSBl3WfXSYE+TJNJhSoURGK56E1i8CXqA==", "license": "AGPL-3.0-only", "dependencies": { "@bufbuild/protobuf": "^2.2.2", diff --git a/package.json b/package.json index 76b202b..f18b0ad 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ "@aws-sdk/client-s3": "^3.657.0", "@aws-sdk/client-ses": "^3.515.0", "@inquirer/prompts": "^7.2.0", - "@pretendonetwork/grpc": "^2.5.6", + "@pretendonetwork/grpc": "^2.5.7", "bcrypt": "^5.0.0", "buffer-crc32": "^0.2.13", "colors": "^1.4.0", diff --git a/src/services/grpc/api/v2/update-user-data.ts b/src/services/grpc/api/v2/update-user-data.ts index 95fac2e..f21451f 100644 --- a/src/services/grpc/api/v2/update-user-data.ts +++ b/src/services/grpc/api/v2/update-user-data.ts @@ -3,7 +3,7 @@ import Mii from 'mii-js'; import { isValidBirthday } from '@/util'; import { config } from '@/config-manager'; import timezones from '@/services/nnas/timezones.json'; -import regionsList from '@/services/nnas/regions.json'; +import regions from '@/services/nnas/regions.json'; import type { CallContext } from 'nice-grpc'; import type { UpdateUserDataRequest, @@ -23,7 +23,6 @@ export async function updateUserData( const mii = request?.mii?.trim(); const birthday = request.birthday?.trim(); const gender = request.gender?.trim(); - const country = request.country?.trim(); const region = request.region; const timezone = request.timezone?.trim(); @@ -92,50 +91,19 @@ export async function updateUserData( pnid.gender = gender; } - if (country || region) { - let countryId = 0; + if (region) { + const countryObj = regions.find(c => c.id === ((region >>> 24) & 0xFF)); + const regionObj = countryObj?.regions.find(r => r.id === region); - // if we have a region but no country, we extract the country id from it - if (region && !country) { - const regionHex = region.toString(16).padStart(8, '0'); - countryId = parseInt(regionHex.slice(0, 2), 16); - } - - const countryObj = regionsList.find((c) => { - return c.iso_code === country || c.id === countryId; - }); - - if (!countryObj?.iso_code) { - throw new ServerError(Status.INVALID_ARGUMENT, `Invalid country`); - } - - if (region) { - const regionObj = countryObj.regions.find((r) => { - return r.id === region; - }); - - if (!regionObj) { - throw new ServerError( - Status.INVALID_ARGUMENT, - `Invalid region` - ); - } - - pnid.country = countryObj.iso_code; - pnid.region = region; - } else if (pnid.country !== countryObj?.iso_code) { - const unspecifiedRegion = countryObj.regions.find( - r => r.name === 'Unspecified' + if (!countryObj || !regionObj) { + throw new ServerError( + Status.INVALID_ARGUMENT, + `Invalid region` ); - - if (!unspecifiedRegion) { - throw new ServerError(Status.INVALID_ARGUMENT, `A default region does not exist for the selected country: please set one explicitly`); - } - - // if editing the country with no explicit region, set it to Unspecified - pnid.country = countryObj.iso_code; - pnid.region = unspecifiedRegion.id; } + + pnid.country = countryObj.iso_code; + pnid.region = regionObj.id; } if (timezone) {