google realtime api / drive api -> get user role for realtime doc

229 views Asked by At

I have a realtime app and get the collaborators with doc.getCollaborators() and this gives me an array [] doc is from the type gapi.drive.realtime.Document:

enter image description here

According to the API Reference there is no field like isOwner that shows me if the current collaborator is the owner of the realtime document "doc"

My Question how can I find out which role the collaborators of a doc has. In the API documentation I find: "The Realtime API supports owner, reader and writer roles"

If I try to use gapi.client.drive.permissions.list suggested from the google drive api reference:

    function retrievePermissions(fileId, callback) {
        var request = gapi.client.drive.permissions.list({
            'fileId': fileId
        });
        request.execute(function (resp) {
            callback(resp.items);
        });
    }


    retrievePermissions(self.realtimeDocId, function (resp) {
        resp;


        });

Then I get the following error message:

Error in Realtime load callback: TypeError: Cannot read property 'permissions' of undefined TypeError: Cannot read property 'permissions' of undefined

2

There are 2 answers

0
Mr.Rebot On

You may want to check how you've place your codes, based on the documentation, you can integrate Realtime API to the Drive platform.

Realtime documents are attached to files stored in Google Drive. Accordingly, your application should use the Drive REST API to interact with Drive files. For example, to create a new file, use the files.insert method from the Drive REST API. To retrieve a file that already exists, use the files.get method.

For more information on interacting with files in Google Drive, see Google Drive Integration.

As for code implementation you can check CodeMirror Collaboration with Google Drive Realtime Api.

Drive API:

/**
 * Creates a new Realtime file.
 * @param title {string} title of the newly created file.
 * @param callback {Function} the callback to call after creation.
 */
rtclient.createRealtimeFile = function(title, callback) {
  gapi.client.load('drive', 'v2', function() {
    gapi.client.drive.files.insert({
      'resource': {
        mimeType: rtclient.REALTIME_MIMETYPE,
        title: title
      }
    }).execute(callback);
  });
}

For Realtime API:

// We have a file ID in the query parameters, so we will use it to load a file.
  if (fileId) {
    gapi.drive.realtime.load(fileId, this.onFileLoaded, this.initializeModel, handleErrors);
    return;
  }

Hope this info helps.

0
Grant Watters On

To use the drive API you must load it separately from the Realtime API.

window.gapi.client.load('drive', 'v3', function ()
{
    // Run your code here.
});

After getting the permissions list, you can use the permissions ID for each user returned from your RealtimeDoc::getCollaborators call.