How to get confluence page comments with limit (incl. child-comments) by REST?

Bastian Kippelt
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 30, 2015

Which is the best way to load confluence page comments with limit (incl. child-comments) by REST?

function load_comments(id, location, start, limit) {
	var rest_url = '/rest/api/content/' + id + '/child/comment?expand=body.view&location=' + location + '&start=' + start + '&limit=' + limit;
$.ajax({
    url: config.contextPath + rest_url,
    success: function(results) {
        if (results) {
            // if has childs .... recursive load_comments(...)
        }
    }
});
}

Is it a good way to use nested ajax functions?

2 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Answer accepted
Stephen Deutsch
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 1, 2015

I'm not sure this is the cleanest way to do it, but it works.  It has two variables: limit and commentLimit.  The "limit" variable sets the limit for the number of comments returned in a request.  The "commentLimit" variable limits the total number of comments that are operated upon.  Hopefully it will be helpful.

var commentCount = 0;
function load_comments(id, location, start, limit, commentLimit) {
    var start = start || 0;
    var rest_url = '/rest/api/content/' + id + '/child/comment?expand=body.view,children.comment&start=' + start;
    if (location) {
        rest_url += '&location=' + location;
    }
    if (limit) {
        rest_url += '&limit=' + limit;
    }
    $.ajax({
        url: contextPath + rest_url,
        success: function(response) {
            if (response.size) {
                $(response.results).each(function() {
                    if (!commentLimit || commentLimit >= (commentCount += 1)) {
                        console.log(this.body.view.value);
                        if (this.children.comment.size) {
                            load_comments(this.id, location, 0, limit, commentLimit);
                        }
                    }
                });
            }
            if (response.size === response.limit) {
                load_comments(id, location, start += response.limit, limit, commentLimit);
            }
        }
    });
}
load_comments(AJS.params.pageId, "", 0, 0, 5);
1 vote
Bastian Kippelt
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 1, 2015

Thanks for your Anwser. Im so stupid laugh ... depth=all Parameter is the Anwser for my Problem.

/rest/api/content/[PAGEID]/child/comment?expand=body.view&depth=all&limit=10

Stephen Deutsch
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 1, 2015

haha, nice! I learned something new today :)

Bastian Kippelt
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 1, 2015

But i dont find a parameter for the username... such as &expand=content. Any idea?

Bastian Kippelt
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 1, 2015

Okay i get it: ?expand=version

Stephen Deutsch
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 1, 2015

right, that should work. You can also expand both by putting a comma betwen them (i.e. ?expand=content,version Then you don't have to make duplicate requests :)

TAGS
AUG Leaders

Atlassian Community Events