Skip to content

grpc: Move /posts/<id> to backend#127

Merged
mrjvs merged 28 commits intodevfrom
work/grpc
Jun 29, 2025
Merged

grpc: Move /posts/<id> to backend#127
mrjvs merged 28 commits intodevfrom
work/grpc

Conversation

@ashquarky
Copy link
Member

Part of #112
Need to submit this before it gets unreviewable

Changes:

This PR adds the extra infrastructure needed for miiverse-api to act as datasource (authentication, error handling, type conversions, validation etc.) and then moves the "post by id" endpoint (and only this endpoint) to the new backend. A few calls to the old database exist in the frontend since they rely on the Post being a Mongoose object for further DB calls - these will be ported once those further calls are in the API.

  • The Endpoint: Just sends the document right out of MongoDB for now.
  • Validation: On the frontend side, that document gets passed through a Zod schema. The one I've written (Post) is descriptive, not prescriptive, made in consultation with the prod database so all the jank of the current DB schema is preserved. This will be useful to make sure changes we make to miiverse-api don't break the frontend.
  • Authentication: Either the OAuth token (web), service token (console) or nothing (guest web) are included with requests to the backend as headers. It has middlewares to process these tokens, and also has per-endpoint middlewares to select between guest, user, moderator access. On the frontend side, a "UserTokens" object keeps the current state of things cached and can be blindly passed back into the API, so this doesn't make much mess in the business logic.
  • Error handling (API): Errors can be thrown deliberately to get specific HTTP codes (Unauthorized, Forbidden etc.), or can just be general JavaScript errors. I know throwing is a different design to what I had refactored into the Nintendo API, but I think this is nicer again. Maybe I'll re-refactor the Nintendo API.
  • Error handling (frontend): HTTP codes from the backend are thrown and allowed to bubble. The error handlers and rendering are tweaked to work better (instead of sometimes spewing a stacktrace), include the request ID, etc. A special case is included for when the user needs to re-log-in; the frontend is extremely permissive about caching and not re-checking tokens, see also juxtaposition-ui: Check for expired tokens in frontend #114 - I'm expecting this might be a problem in prod

I know this is an absolute boatload of infrastructure for a very minimalist endpoint (and even it's missing some things, like the moderator/removed posts override) but hopefully it will serve as a foundation for us to work on top of.

ashquarky added 16 commits June 7, 2025 19:04
If we're putting the method and such in the JSON object, it makes sense to keep the headers at that layer rather than pushing them up to gRPC. This lets the Express router access those headers in the usual way.
This worked well for miiverse-api. It still just spits the stacktrace into the browser, but hey, at least it doesn't crash the server process
i'm not stupid i'm smart
some callsites to the old database method still exist, since the helpers on Post are used for things the backend doesn't have yet

it's annoying to pass the whole Express request so just have a blob object for the things that the user is authenticated with
@ashquarky ashquarky requested a review from mrjvs June 8, 2025 08:45
@ashquarky
Copy link
Member Author

Note to self: Need to check Juxt bans on the API too (not just PNID bans)

I went through a copy of the prod db and found lots of optional fields that aren't marked as such, old documents missing fields, etc.

Introduces an IPostInput type that accounts for the mongoose schema doing defaults and fixes, while also allowing IPost to reflect the real db type.

Groundwork for a proper DTO structure.
@ashquarky
Copy link
Member Author

I hate CodeQL actually. I do exactly what it suggests and it goes "nuh"

This provides a layer of abstraction between the actual database implementation and the on-the-wire types which will be useful when the database is changed soon
@ashquarky ashquarky requested a review from mrjvs June 13, 2025 07:17
Some paths use the login template to render errors (?) and this now requires a redirect URL. Provide one.
@ashquarky ashquarky requested a review from mrjvs June 13, 2025 13:00
@mrjvs
Copy link
Contributor

mrjvs commented Jun 15, 2025

lgtm, is this ready for merge?

@ashquarky
Copy link
Member Author

I think so. I am a bit worried about token expiry (web frontend requests that go thru this api check the token on every request now) but we can always branch this back off if it becomes a problem

@ashquarky
Copy link
Member Author

Token expiry issue PretendoNetwork/account#184

@mrjvs
Copy link
Contributor

mrjvs commented Jun 21, 2025

This is waiting on PretendoNetwork/account#184 to be resolved, can be merged when that's done

@ashquarky
Copy link
Member Author

ashquarky commented Jun 27, 2025

Account server side is good to go, ~~now just need our side (which is present in #138) and this can land

@ashquarky
Copy link
Member Author

This should be ok to go barring any conflict with #138

@mrjvs mrjvs merged commit 3e20596 into dev Jun 29, 2025
13 checks passed
@mrjvs mrjvs deleted the work/grpc branch June 29, 2025 15:33
@mrjvs mrjvs mentioned this pull request Jul 2, 2025
@ashquarky ashquarky mentioned this pull request Feb 5, 2026
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants