Repository navigation
feat(rest): add DELETE /notifications/{id} - #639
Open
faisalahammad wants to merge 1 commit into
Open
faisalahammad wants to merge 1 commit into
faisalahammad wants to merge 1 commit 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
5 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
DELETE /wp-notifications/v1/notifications/{id}. It removes the current user's copy of a notification.Fixes #363
Why?
#363 asks for an endpoint to delete a single notification by ID. As discussed in #612, the
{id}is a message ID and a notification is a message for one user. So delete only removes that user's row innotifications_queue. Other users who got the same message keep theirs.How?
Notification_Repositorygets a newremove( int $message_id, int $user_id ): boolmethod. This is the same signature as in fix: make notification repository user-scoped #626, so the two should merge easily.Wpdb_Notification_Repository::remove()runs$wpdb->delete()onnotifications_queuewith bothmessage_idanduser_idin the where clause.Notification_Controllernow takes an optional repository in the constructor. It falls back toWpdb_Notification_Repository, soload.phpdoes not change. This also makes it easy to swap in Instantiate the notification repository and inject it into REST controllers #617 later.notifications/(?P<id>[\d]+)withWP_REST_Server::DELETABLE:401 rest_notifications_login_required404 rest_notification_invalid_id. Same error for both, so it does not tell if another user has that message.200 { "deleted": true, "id": 42 }Orphaned messages (no queue rows left) are not removed here. That is for
purge_orphans()later.Testing Instructions
There is no UI yet to send a notification, so add queue rows by hand. Change the
wp_prefix if needed.npx wp-env run cli wp db query "INSERT INTO wp_notifications_queue (message_id, user_id) VALUES (101, 1), (101, 2), (102, 2);"await wp.apiFetch( { path: '/wp-notifications/v1/notifications/101', method: 'DELETE' } );You get
{ deleted: true, id: 101 }. Row(101, 1)is gone,(101, 2)is still there.rest_notification_invalid_idwith status 404./notifications/102(owned by user 2). You get 404 and row(102, 2)is still there.curl -i -X DELETE http://localhost:8888/wp-json/wp-notifications/v1/notifications/101. You get 401 and no rows are removed.Automated tests: 4 new tests in
test-notification-controller.phpcover these cases. PHPUnit passes on single site and multisite, andcomposer lintis clean.Screenshots or screencast
REST only, no UI changes.