Repository navigation
feat(rest): add GET /notifications/{id} - #640
Open
faisalahammad wants to merge 2 commits into
Open
faisalahammad wants to merge 2 commits into
faisalahammad wants to merge 2 commits into
Conversation
Remove the current user's copy of a notification. The message itself is kept for other users. Returns 401 when logged out and 404 when the user has no such notification. Fixes WordPress#363
- Add a readable endpoint on /notifications/{id} that returns the
current user's notification for a message ID. Logged out users get
401, missing or other users' notifications get 404.
- Implement Wpdb_Notification_Repository::find_by_id() with a JOIN of
the queue and messages tables, scoped to the user.
- Deserialize MySQL date strings in the Message factory.
- Fix user_id and icon schema types and allow null on optional
message fields so responses match the schema.
- Add tests for the new endpoint.
Fixes WordPress#362
2 of 22 tasks
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What?
Adds
GET /wp-notifications/v1/notifications/{id}. It returns the current user's notification for a message ID.Fixes #362
Depends on #639. This branch is built on top of it (repository injection and the
/notifications/{id}route), so the DELETE commit shows here too. Please review only the last commit, or merge #639 first.Why?
#362 asks for an endpoint to get a single notification by ID. Same as in #639 and #612, the
{id}is a message ID and a notification is that message for one user. So the response is the message joined with the user's row innotifications_queue.How?
Notification_Repository::find_by_id()now has the signaturefind_by_id( int $message_id, int $user_id ): ?array, the same as in fix: make notification repository user-scoped #626. It returnsarray( 'notification' => Model\Notification, 'message' => Model\Message )ornull.Wpdb_Notification_Repository::find_by_id()runs one prepared query that joinsnotifications_queueandnotifications_messagesonmessage_idanduser_id. It decodes themetaJSON and builds both models with the existing factories. Table columns win over the same keys inmeta.Factory\Messagenow turns MySQL date strings intoDateTime, likeFactory\Notificationalready does. Without this a DB row throws aTypeErroroncreated_at.Notification_Controllergets aREADABLEendpoint onnotifications/(?P<id>[\d]+):401 rest_notifications_login_required404 rest_notification_invalid_id. Same error for both, so it does not tell if another user has that message.statusisdismissedifdismissed_atis set, elsedisplayedifdisplayed_atis set, elseundisplayed.New
prepare_item_for_response()supportscontextand_fields. Fields not inmetacome back asnull.Schema fixes so the response matches its own schema:
user_idis nowinteger(wasstring).iconis nowstring(wasinteger).accept_label,accept_link,dismiss_label,expires_at,icon,messageandtitlenow allownull.This overlaps a bit with REST schema and response cleanup for the notifications controller #618. Happy to drop it here if REST schema and response cleanup for the notifications controller #618 lands first.
Testing Instructions
There is no UI yet to send a notification, so add rows by hand. Change the
wp_prefix if needed.await wp.apiFetch( { path: '/wp-notifications/v1/notifications/201' } );You get
id: 201,user_id: 1,title: "Hello",accept_label: "Open",icon: "admin-site",created_at: "2026-01-02T03:04:05+00:00"andstatus: "undisplayed"./notifications/202. You getstatus: "dismissed"andaccept_label: null(no meta)./notifications/201?_fields=id,title,status. You get only those 3 fields./notifications/999999. You getrest_notification_invalid_idwith status 404./notifications/201. You get 404. With/notifications/202you get user 2's copy withstatus: "undisplayed".curl -i http://localhost:8888/wp-json/wp-notifications/v1/notifications/201. You get 401.Automated tests: 6 new tests in
test-notification-controller.phpcover these cases, and two of them check the response against the schema. PHPUnit passes on single site and multisite, andcomposer lintis clean.Screenshots or screencast
REST only, no UI changes.