Add support for flag/unflag actions via JSON:API Resources

Created on 15 December 2021, almost 3 years ago
Updated 23 July 2024, 4 months ago

Problem/Motivation

It would be nice if this module provided support for flag/unflag actions via an intuitive API.

Proposed resolution

I've been looking at using JSON:API Resources β†’ , and providing routes like /jsonapi/flagging/{flag}/flag/{entity_id} and /jsonapi/flagging/{flag}/unflag/{entity_id}. That would perform the respective actions for the user identified by the request.

This could then potentially be combined with JSON:API Hypermedia to add links in the JSON:API response for any entity that the current user could flag/unflag. And then the client could visit those links to perform the action. Making the actions easier to discover.

Outstanding issues/questions

I've been able to get that part working, but I have a question about the best implementation. Specifically, should the request to a route like /jsonapi/flagging/{flag}/flag/{entity_id} require a POST or a GET request? It'll work with a GET request, all the required data (the flag ID, the entity ID, and the user ID) are present in the GET request. And I'm kind of inclined to take that approach.

However, the action does result in the creation of an entity. And shouldn't that then be a POST request? And maybe a DELETE request for unflag? If so, what would be in the body of the request if all the information is already available in the link? Or ... am I overthinking things here?

There's also this issue https://www.drupal.org/project/flag/issues/3091824 πŸ› Use jsonapi to post a flagging needs permission 'administer flaggings' and always failed to "A flagging can't be moved to another entity." Needs review which when resoled would allow for making a POST request to something like /jsonapi/flagging/{flag} with the entity ID, user ID, etc. in the body of the request in a more traditional REST approach. But I still think there's potentially value in the easier to discover and implement approach discussed above.

✨ Feature request
Status

Needs review

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States eojthebrave Minneapolis, MN

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024