Skip to content

[devices] public key operations - #258

Open
capcom6 wants to merge 2 commits into
masterfrom
devices/public-key-support
Open

[devices] public key operations#258
capcom6 wants to merge 2 commits into
masterfrom
devices/public-key-support

Conversation

@capcom6

@capcom6 capcom6 commented Jul 28, 2026

Copy link
Copy Markdown
Member

Greptile Summary

This PR adds E2E public-key support to device registration and update flows: new publicKey / keyVersion fields propagate through the domain model, GORM model, repository update, handler, DTO converter, and OpenAPI docs. It also resizes the phone_number column from 128 to 512 characters, moves device error sentinel variables from repository.go to errors.go, cleans up the ErrInvalidUser reference that was dropped, and adds a lint-suppression comment in the FCM client.

  • E2E fields added end-to-end: migration, GORM model, domain struct, repository Update map, handler PATCH/POST, DTO converter, and OpenAPI Device schema are all updated consistently.
  • Phone-number column widened: the SQL migration extends message_recipients.phone_number to varchar(512), matching the updated OpenAPI maxLength: 512 constraint.
  • Error cleanup: ErrInvalidUser is removed from both errors.go and the 3rdparty.go error handler; the remaining three device errors are reformatted from fallthrough chains to a single comma-separated errors.Is expression.

Confidence Score: 4/5

  • Safe to merge with caveats — the E2E validation gaps flagged in earlier rounds remain unresolved, but they do not introduce new failure modes beyond what already existed before this PR.
  • The core plumbing — migration, GORM model, repository, handler, converter, and OpenAPI docs — is internally consistent and the column-widening change is straightforward. The outstanding concerns from prior review rounds (no E2E pair validation on Insert, no sentinel error for inconsistent pairs, empty-string public key stored rather than coerced to NULL) are still present in the codebase and not addressed by this iteration, meaning a device can reach the DB with a partially-configured E2E state through the registration path.
  • internal/sms-gateway/modules/devices/service.go (Insert still lacks E2E consistency validation) and internal/sms-gateway/handlers/mobile.go (postDevice creates a user before validating device fields).

Important Files Changed

Filename Overview
internal/sms-gateway/modules/devices/repository.go Error sentinels moved to errors.go; public_key / key_version update logic added with independent nil-checks (already flagged in prior rounds). Pointer passed directly for public_key vs dereferenced-and-re-pointed for push_token.
internal/sms-gateway/handlers/mobile.go Correctly wires PublicKey/KeyVersion from request to domain struct for both POST and PATCH device. ErrInconsistentE2E (if it were ever returned) would still surface as a 500 — flagged in prior rounds and still unresolved.
internal/sms-gateway/models/migrations/mysql/20260728000000_add_device_e2e_keys.sql Adds public_key (TEXT NULL) and key_version (INT NULL DEFAULT NULL) correctly. Widens phone_number to varchar(512). Missing trailing newline (noted previously). The cosmetic --- separator between Up and Down sections is harmless.
internal/sms-gateway/modules/devices/domain.go Adds PublicKey/*string and KeyVersion/*int to DeviceUpdate with clear doc-comments. References to the unimplemented ErrInconsistentE2E removed from doc-comments in this revision.
internal/sms-gateway/modules/devices/errors.go ErrInvalidUser removed; ErrNotFound / ErrInvalidFilter / ErrMoreThanOne kept and moved here from repository.go. Clean refactor.
internal/sms-gateway/modules/devices/models.go PublicKey *string and KeyVersion *int added to DeviceModel with gorm:"default:null", which is consistent with the SQL migration using DEFAULT NULL. toDomain and newDeviceModel correctly propagate both fields.
internal/sms-gateway/handlers/converters/devices.go VersionedPublicKey embedded struct correctly populated from domain Device. Test coverage added in devices_test.go.
internal/sms-gateway/handlers/messages/3rdparty.go ErrInvalidUser case removed (error no longer exists anywhere); fallthrough chains replaced with comma-separated errors.Is — purely cosmetic and correct.
internal/sms-gateway/modules/push/fcm/client.go Only change is adding //nolint:staticcheck comment acknowledging a planned migration away from FCM token to FID. No logic changes.

Sequence Diagram

sequenceDiagram
    participant Client as Mobile Client
    participant MH as mobileHandler
    participant AuthSvc as auth.Service
    participant DevSvc as devices.Service
    participant Repo as devices.Repository
    participant DB as MySQL

    Note over Client,DB: Device Registration (POST /mobile/v1/device)
    Client->>MH: "POST {name, pushToken, publicKey, keyVersion}"
    MH->>MH: "BodyParserValidator (validates keyVersion min=1)"
    MH->>AuthSvc: "RegisterDevice(userID, DeviceInfo{PublicKey, KeyVersion})"
    AuthSvc->>DevSvc: Insert(ctx, userID, DeviceInfo)
    DevSvc->>Repo: Insert(ctx, DeviceInput)
    Repo->>DB: INSERT devices (public_key, key_version, ...)
    DB-->>Repo: device row
    Repo-->>DevSvc: "*Device"
    DevSvc-->>AuthSvc: "*Device"
    AuthSvc-->>MH: "*Device"
    MH-->>Client: "201 {id, token, login, password}"

    Note over Client,DB: Device Update (PATCH /mobile/v1/device)
    Client->>MH: "PATCH {id, pushToken, publicKey, keyVersion}"
    MH->>MH: BodyParserValidator
    MH->>DevSvc: "Update(ctx, id, DeviceUpdate{PublicKey, KeyVersion})"
    DevSvc->>Repo: Update(ctx, id, DeviceUpdate)
    Repo->>Repo: build map[string]any (nil-guarded per field)
    Repo->>DB: "UPDATE devices SET public_key=?, key_version=? WHERE id=?"
    DB-->>Repo: ok
    Repo-->>DevSvc: nil
    DevSvc->>DevSvc: cache.DeleteByID(id)
    DevSvc-->>MH: nil
    MH-->>Client: 204

    Note over Client,DB: List Devices (GET /3rdparty/v1/devices)
    Client->>MH: GET /3rdparty/v1/devices
    MH->>DevSvc: Select(ctx, userID)
    DevSvc->>Repo: Select(ctx, filters)
    DB-->>Repo: []DeviceModel (with public_key, key_version)
    Repo-->>DevSvc: []Device
    DevSvc-->>MH: []Device
    MH->>MH: "DeviceToDTO → smsgateway.Device{VersionedPublicKey}"
    MH-->>Client: "200 [{..., publicKey, keyVersion}]"
Loading

Reviews (28): Last reviewed commit: "[lint] ignore FCM Fid migration" | Re-trigger Greptile

@github-actions

github-actions Bot commented Jul 28, 2026

Copy link
Copy Markdown

🤖 Pull request artifacts

Platform File
🐳 Docker GitHub Container Registry
🍎 Darwin arm64 server_Darwin_arm64.tar.gz
🍎 Darwin x86_64 server_Darwin_x86_64.tar.gz
🐧 Linux arm64 server_Linux_arm64.tar.gz
🐧 Linux i386 server_Linux_i386.tar.gz
🐧 Linux x86_64 server_Linux_x86_64.tar.gz
🪟 Windows arm64 server_Windows_arm64.zip
🪟 Windows i386 server_Windows_i386.zip
🪟 Windows x86_64 server_Windows_x86_64.zip

@capcom6
capcom6 force-pushed the devices/public-key-support branch from 1c556ac to f8364a1 Compare July 29, 2026 07:50
@capcom6
capcom6 marked this pull request as ready for review July 29, 2026 07:50
Comment thread internal/sms-gateway/modules/devices/repository.go
Comment thread internal/sms-gateway/openapi/docs.go Outdated
Comment thread internal/sms-gateway/modules/devices/models.go Outdated
@capcom6
capcom6 force-pushed the devices/public-key-support branch from 7b74cd5 to 0bd4583 Compare July 30, 2026 00:29
@greptile-apps

greptile-apps Bot commented Jul 30, 2026

Copy link
Copy Markdown

Want your agent to iterate on Greptile's feedback? Try greploops.

@capcom6
capcom6 force-pushed the devices/public-key-support branch from 0bd4583 to 3d10ade Compare July 31, 2026 02:16
@capcom6
capcom6 force-pushed the devices/public-key-support branch from 4303779 to b167711 Compare August 1, 2026 01:21
Comment thread internal/sms-gateway/modules/devices/service.go Outdated
@capcom6

capcom6 commented Aug 2, 2026

Copy link
Copy Markdown
Member Author

@greptile review

@capcom6
capcom6 force-pushed the devices/public-key-support branch 3 times, most recently from 0883119 to 90b1780 Compare August 4, 2026 07:39
@capcom6 capcom6 added the ready label Aug 4, 2026
@capcom6
capcom6 force-pushed the devices/public-key-support branch from 90b1780 to 7b24780 Compare August 5, 2026 01:01
@github-actions github-actions Bot removed the ready label Aug 5, 2026
@capcom6 capcom6 added the ready label Aug 5, 2026
@github-actions github-actions Bot removed the ready label Aug 7, 2026
@capcom6
capcom6 force-pushed the devices/public-key-support branch 2 times, most recently from 5e03d03 to 04f32ec Compare August 10, 2026 02:55
Comment thread internal/sms-gateway/handlers/mobile.go
@capcom6
capcom6 force-pushed the devices/public-key-support branch from 5649879 to 8b37237 Compare August 11, 2026 02:13
Comment thread internal/sms-gateway/modules/devices/domain.go
@capcom6
capcom6 force-pushed the devices/public-key-support branch 3 times, most recently from 066ae5e to 7b16d41 Compare August 13, 2026 08:25
@capcom6 capcom6 added the ready label Aug 14, 2026
@capcom6
capcom6 force-pushed the devices/public-key-support branch from 7b16d41 to f24a89c Compare August 18, 2026 00:43
@github-actions github-actions Bot removed the ready label Aug 18, 2026
Comment thread internal/sms-gateway/modules/devices/repository.go
@capcom6 capcom6 added the ready label Aug 19, 2026
@capcom6
capcom6 force-pushed the devices/public-key-support branch from f24a89c to 7957312 Compare August 20, 2026 00:43
@github-actions github-actions Bot removed the ready label Aug 20, 2026
@capcom6 capcom6 added the ready label Aug 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant