muxinc / strapi-plugin-mux-video-uploader

A Strapi plugin for managing uploads to Mux.
https://mux.com
24 stars 19 forks source link

When used in a relational feild, Strapi v4 does not seem to populate it in the find rest api #30

Closed karlmoubarak closed 2 years ago

karlmoubarak commented 2 years ago

heyo, thanks for this great work. It's helped a lot.

I'm buillding an api for some livestream events and using assets uploaded with the mux video uploader in a relational field for each event. Event is a collection type. Screenshot 2022-03-15 at 09 28 31

When i use the strapi rest API to findone event and populate it's 'recording' field, everything works as expected: /api/events/dfdfg?populate[0]=messages&populate[1]=recording ( here i am replacing the findOne controller with a custom one to use slugs instead of ids. But this shouldn't affect population since params are passed on normally ).

{
    "data": {
        "id": 2,
        "title": "dfdfg",
        "slug": "dfdfg",
        "starts": "2022-01-31T23:00:00.000Z",
        "ends": "2022-01-30T23:00:00.000Z",
        "marquee": "The Hmm || ",
        "info": null,
        "backgroundColor": "rgb(255,255,255)",
        "showViewerCount": null,
        "allowFlyingEmoji": true,
        "highlightDonateButton": false,
        "chatPlaybackDelay": null,
        "createdAt": "2022-02-21T22:34:25.313Z",
        "updatedAt": "2022-03-15T08:23:21.968Z",
        "publishedAt": "2022-02-28T15:23:46.867Z",
        "messages": {
            "data": []
        },
        "recording": {
            "data": {
                "id": 3,
                "title": "another test now",
                "upload_id": "F01FV5mDewtEWcukyzhjHmgnoHZpfTRC1W5VSUfF70228",
                "asset_id": "wlip7KeikzLQnjCMur6yXk6Je7rC3ha2pr5GQiwdV7M",
                "playback_id": "PB26fp01hHuI6jwmLEFfY3RYmWmEfS011bcPKs2pNeEDA",
                "error_message": null,
                "isReady": true,
                "createdAt": "2022-03-09T10:38:57.635Z",
                "updatedAt": "2022-03-09T10:39:23.683Z"
            }
        }
    }
}

However, when i use the rest API to findmany events and populate the 'recording' field, it doesn't work: api/events?populate[0]=messages&populate[1]=recording

{
    "data": [
                 {
                    ...
                },
        {
            "id": 2,
            "title": "dfdfg",
            "slug": "dfdfg",
            "starts": "2022-01-31T23:00:00.000Z",
            "ends": "2022-01-30T23:00:00.000Z",
            "marquee": "The Hmm || ",
            "info": null,
            "backgroundColor": "rgb(255,255,255)",
            "showViewerCount": null,
            "allowFlyingEmoji": true,
            "highlightDonateButton": false,
            "chatPlaybackDelay": null,
            "createdAt": "2022-02-21T22:34:25.313Z",
            "updatedAt": "2022-03-15T08:23:21.968Z",
            "publishedAt": "2022-02-28T15:23:46.867Z",
            "messages": {
                "data": []
            }
        },
        {
           .....    
        }
    ]
}

I was thinking of posting this issue to strapi but it looks like populating works for all other fields (as you can see with 'messages' field).

Is it because of the type of relation? Am I missing something? Maybe I did something wrong?

andreigabreanu commented 2 years ago

Hi @karlmoubarak . I've had the same issue as you. TL;DR; insert in your database, up_permissions table the permission "plugin::mux-video-uploader.mux-asset.find", then insert into the table "up_permissions_role_links" the id's for the role/permission you just added, then ensure you have it checked in the strapi admin under "USERS & PERMISSIONS PLUGIN > Roles > Mux-video-uploader" (this is shown only after you insert the permission in the table above.

Long answer: The plugin should probably insert these permissions in the DB itself when first installed. The populate doesn't work otherwise, as told by the strapi docs here: https://docs.strapi.io/developer-docs/latest/developer-resources/database-apis-reference/rest/populating-fields.html#population

"If the users-permissions plugin is installed, the find permission must be enabled for the content-types that are being populated. If a role doesn't have access to a content-type it will not be populated."

erikpena commented 2 years ago

Hey @karlmoubarak and @andreigabreanu, Thanks for this conversation. I agree with @andreigabreanu. We should actually set this permission as settable within the plugin. We'll work through exposing this in the user's permissions page. In the meantime, Andrei's workaround sounds like it'll work.

javiercornejo commented 2 years ago

Exactly the same here. Just add that this happend also with graphql endpoint with admin credentials (not tested with user-permission users yet)

karlmoubarak commented 2 years ago

Thank you all for your help <3

PaulBratslavsky commented 2 years ago

Had same issue, glad to see that this is being addressed but for now will use @andreigabreanu work around.

PaulBratslavsky commented 2 years ago

Hey @karlmoubarak and @andreigabreanu, Thanks for this conversation. I agree with @andreigabreanu. We should actually set this permission as settable within the plugin. We'll work through exposing this in the user's permissions page. In the meantime, Andrei's workaround sounds like it'll work.

I agree as well. Wanted to make a tutorial on how to set up MUX plugin with Strapi but I feel that for users that may not be familiar with how to insert data into DB manually will add unnecessary friction witch may prevent them taking the extra step and using MUX as a service. Especially for someone like me, who is more of a frontend developer.

Looking forwards to the update.🙂

deepakgupta-unthinkable commented 2 years ago

@andreigabreanu I am facing the same issue the mux asset values are not getting returned in the find and findone api. Does the plugin still have the issue and do we need to make DB changes manually?

I have added mux_video_uploader_mux_asset: { populate: “2A” } in findone but did not get any data related to mux data.

Kindly suggest

erikpena commented 2 years ago

This is now resolved in the latest release of 2.4.0 which included some updates to the permission system between the plugin and Strapi.

Creating a basic collection type that has a relationship to the mux-asset, I was able to perform a query for a list of entries—

curl --location -g --request GET 'http://localhost:1337/api/videos?populate[0]=mux_asset' 
--header 'Authorization: Bearer REDACTED'

and receive the following response with the populated mux_asset relation field—

{
    "data": [
        {
            "id": 1,
            "attributes": {
                "title": "Test",
                "description": null,
                "createdAt": "2022-11-18T06:03:49.314Z",
                "updatedAt": "2022-11-19T05:24:11.975Z",
                "publishedAt": "2022-11-19T05:24:11.972Z",
                "mux_asset": {
                    "data": {
                        "id": 2,
                        "attributes": {
                            "title": "Test",
                            "upload_id": "63ZdvqJqVfM2YRH00x67ASpoK01MIvbMKwHkwy8Vl01ni8",
                            "asset_id": "kLv4w4XyRipV1yvAhbXeuvfYxs01V9o8UITLNRSucw00s",
                            "playback_id": "00mqfFvCGH1oKqdhYIM2vaSVZItLeHxh1R02cMNwse00SE",
                            "error_message": null,
                            "isReady": true,
                            "duration": 300.75,
                            "aspect_ratio": "16:9",
                            "createdAt": "2022-11-18T05:08:08.524Z",
                            "updatedAt": "2022-11-18T05:09:52.441Z"
                        }
                    }
                }
            }
        }
    ],
    "meta": {
        "pagination": {
            "page": 1,
            "pageSize": 25,
            "pageCount": 1,
            "total": 1
        }
    }
}