Search code examples
quickblox

quickblox - how can I get content by owner?


I'm working on a CMS and I'd like to know if I can get the content by owner, in other to show the pictures from the app's users.

I know that i can get the content list of the current user with:

QB.content.list(function(error, response){
    if(error) {
        console.log(error);
    } else {
        // Success
    }
});

but can I access to other user's content?

thank you very much


Solution

  • Unfortunately, this functionality isn't in the API right now. If it's profile pictures you want, you can just use the blob parameter on a user.

    If you can provide us some good use cases for - send me an email at alex.bass@quickblox.com and we'll consider adding it.

    Hopefully this hasn't inconvenienced you too much.

    EDIT:

    I've just been informed by my colleague that there is a way around this - it's not perfect, but it does get the job done.

    You can create a custom objects class with just 2 fields: blob_id and user_id. Then when content is uploaded, just add a record in the callback.

    Here's the complete code - I haven't tested it though. Let me know if you have any troubles.

    When uploading:

    QB.init(app_id, auth_key, auth_secret);
    
    var user_id;
    
    QB.createSession({login: <username>, password: <password>}, function(error, response){
        if(error) {
            console.log(error);
        } else {
            // Just making a record of the user_id for use later
            user_id = response.id;
        }
    });
    
    //
    // later...
    //
    
    var files = $("input[type=file]")[0].files;
    
    // This function will create "content" record in QB, then when QB returns AWS URL to
    // post to, it will automatically upload it, then on completion, mark uploaded.
    QB.content.createAndUpload({'file': files, 'public': true}, function(err, response){
        if (err) {
            console.log(err);
        } else {
            // Response will contain blob ID.
    
            var data = {
                blob_id: response.id,
                blob_owner: user_id
            }
    
            QB.data.create("BlobsToUsers", data, function(err, response){
                if (err) {
                    console.log(err);
                } else {
                    // Done
                }
            });
    
        }
    });
    

    Then, later on when you're listing content:

    QB.data.list("BlobsToUsers", { blob_owner: user_id } function(err, response){
        if (err) {
            console.log(err);
        } else {
    
            // response.items will be array of all records with the specified blob_owner.
            // You could also filter by date, timestamp or whatever you want. 
    
        }
    });
    

    To break it down into steps:

    1. Use "QB.content.createAndUpload()" to upload data
    2. Create custom objects record matching blob ID to owner
    3. Later, when listing, get records from Custom Objects.
    4. Do whatever you want from there. There is a function named "QB.content.getFileUrl()" that will return an AWS URL that you use for an <img> tag.

    Hope this helps