<commit_before>/*
 * Copyright 2013 Jive Software
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

/**
 * Library for manipulating external stream instances.
 */

var q = require('q');
var util = require('util');
var instances = require('./instances');
var pusher = require('./dataPusher');
var jive = require('../api');

var extstreams = Object.create(instances);
module.exports = extstreams;

extstreams.getCollection = function() {
    return "extstreamInstance";
};

extstreams.pushActivity = function ( tileInstance, activity) {
    return pusher.pushActivity(tileInstance, activity);
};

var pushComment = function ( tileInstance, comment, commentURL) {
    return pusher.pushComment(tileInstance, commentURL, comment);
};

extstreams.commentOnActivity = function(activity, comment ) {
    if (!(activity && activity.resources && activity.resources.comments && activity.resources.comments.ref)
        || !activity.parent) {

        throw new Error('Error in jive.extstreams.commentOnActivity: input activity is not a valid Jive object.' +
            'It is missing the resources.comments.ref field or parent field.');
    }
    var commentsURL = activity.resources.comments.ref;
    var parentInstanceURL = activity.parent + '/activities';

    return extstreams.findByURL(parentInstanceURL).then(function(extstream) {
        if (!comment.externalID){
            comment.externalID = extstream.name + '_' + 'comment' + '_' + String(new Date().getTime());
            jive.logger.warn(util.format('No externalID field given when creating new comment. Assigning ID %s', comment.externalID ));
        }
        return pushComment(extstream, comment, commentsURL);
    });

};

extstreams.fetchCommentsOnActivity = function(activity, optionalFieldList, optionalItemsPerPage) {
    if (!(activity && activity.resources && activity.resources.comments && activity.resources.comments.ref)
        || !activity.parent) {
        throw new Error('Error in jive.extstreams.fetchCommentsOnActivity: ' +
            'input activity is not a valid Jive object. It is missing the resources.comments.ref field or parent field.');
    }

    var commentsURL = activity.resources.comments.ref;
    if (optionalFieldList || optionalItemsPerPage) {
        commentsURL += '?';

        var q = false;
        if (optionalFieldList) {
            commentsURL +=  'fields=' + optionalFieldList.join(',');
            q = true;
        }
        if (optionalItemsPerPage) {
            commentsURL += (q ? '&' : '') + 'count=' + optionalItemsPerPage;
            q = true;
        }
    }

    var parentInstanceURL = activity.parent + '/activities';
    return extstreams.findByURL(parentInstanceURL).then( function(extstream) {
        return pusher.getPaginated(extstream, commentsURL );
    });

};
<commit_msg>Added method extstreams.fetchAllCommentsForExtstream() to get all comments on all activities for an external stream.<commit_after>/*
 * Copyright 2013 Jive Software
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

/**
 * Library for manipulating external stream instances.
 */

var q = require('q');
var util = require('util');
var instances = require('./instances');
var pusher = require('./dataPusher');
var jive = require('../api');

var extstreams = Object.create(instances);
module.exports = extstreams;

extstreams.getCollection = function() {
    return "extstreamInstance";
};

extstreams.pushActivity = function ( tileInstance, activity) {
    return pusher.pushActivity(tileInstance, activity);
};

var pushComment = function ( tileInstance, comment, commentURL) {
    return pusher.pushComment(tileInstance, commentURL, comment);
};

extstreams.commentOnActivity = function(activity, comment ) {
    if (!(activity && activity.resources && activity.resources.comments && activity.resources.comments.ref)
        || !activity.parent) {

        throw new Error('Error in jive.extstreams.commentOnActivity: input activity is not a valid Jive object.' +
            'It is missing the resources.comments.ref field or parent field.');
    }
    var commentsURL = activity.resources.comments.ref;
    var parentInstanceURL = activity.parent + '/activities';

    return extstreams.findByURL(parentInstanceURL).then(function(extstream) {
        if (!comment.externalID){
            comment.externalID = extstream.name + '_' + 'comment' + '_' + String(new Date().getTime());
            jive.logger.warn(util.format('No externalID field given when creating new comment. Assigning ID %s', comment.externalID ));
        }
        return pushComment(extstream, comment, commentsURL);
    });

};

extstreams.fetchCommentsOnActivity = function(activity, optionalFieldList, optionalItemsPerPage) {
    if (!(activity && activity.resources && activity.resources.comments && activity.resources.comments.ref)
        || !activity.parent) {
        throw new Error('Error in jive.extstreams.fetchCommentsOnActivity: ' +
            'input activity is not a valid Jive object. It is missing the resources.comments.ref field or parent field.');
    }

    var commentsURL = activity.resources.comments.ref;

    commentsURL += buildQueryString(optionalFieldList, optionalItemsPerPage);

    var parentInstanceURL = activity.parent + '/activities';
    return extstreams.findByURL(parentInstanceURL).then( function(extstream) {
        return pusher.getPaginated(extstream, commentsURL );
    });

};

extstreams.fetchAllCommentsForExtstream = function(extstream, optionalFieldList, optionalItemsPerPage) {


    var dataURL = extstream['url'];
    var commentsURL = commentsUrlFromDataUrl(dataURL);

    commentsURL += buildQueryString(optionalFieldList, optionalItemsPerPage);

    return pusher.getPaginated(extstream, commentsURL );

};

function buildQueryString(optionalFieldList, optionalItemsPerPage) {
    var queryStr = '';
    if (optionalFieldList || optionalItemsPerPage) {
        queryStr += '?';

        var q = false;
        if (optionalFieldList) {
            queryStr +=  'fields=' + optionalFieldList.join(',');
            q = true;
        }
        if (optionalItemsPerPage) {
            queryStr += (q ? '&' : '') + 'count=' + optionalItemsPerPage;
            q = true;
        }
    }

    return queryStr;

}

//Sort of a hack to build the comments URL, because we are not storing the "resources" JSON from a Jive external stream object currently
function commentsUrlFromDataUrl(dataURL) {
     return dataURL.slice(0, dataURL.indexOf('activities')) + 'comments';
}

{'fraction_non_alphanumeric': 0.10955649419218585, 'fraction_numerical': 0.0022439281942977824, 'mean_word_length': 3.4702064896755163, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5854894', 'n_tokens_mistral': 2022, 'n_tokens_neox': 2001, 'n_words': 589}