<commit_before>/**
 * New and Noteworthy Projects
 */

var $ = require('jquery');
var m = require('mithril');
var $osf = require('js/osfHelpers');
var Raven = require('raven-js');
var lodashGet = require('lodash.get');


// CSS
require('css/new-and-noteworthy-plugin.css');
require('loaders.css/loaders.min.css');


// XHR config for apiserver connection
var xhrconfig = function(xhr) {
    xhr.withCredentials = true;
};


var NewAndNoteworthy = {
    controller: function() {
        var self = this;
        self.newAndNoteworthyNodes = m.prop([]); // New and noteworthy nodes list
        self.popularNodes = m.prop([]); // Most popular nodes list
        self.contributorsMapping = {}; // Dictionary mapping node id to some of the contrib names and the contrib total.
        self.SHOW_TOTAL = 5; // Number of new and noteworthy projects displayed in each column
        self.errorLoading = m.prop(false);  // True if error retrieving projects or contributors.
        self.someDataLoaded = m.prop(false);
        self.someContributorsLoaded = m.prop(false);

        // Switches errorLoading to true
        self.requestError = function(result){
            self.errorLoading = m.prop(true);
            Raven.captureMessage('Error loading new and noteworthy projects on home page.', {
                extra: { requestReturn: result }
            });
        };

        // Load new and noteworthy nodes
        var newAndNoteworthyUrl = $osf.apiV2Url('nodes/' + window.contextVars.newAndNoteworthy + '/node_links/', {});
        var newAndNoteworthyPromise = m.request({method: 'GET', url: newAndNoteworthyUrl, config: xhrconfig, background: true});
        newAndNoteworthyPromise.then(function(result){
            var numNew = Math.min(result.data.length, self.SHOW_TOTAL);
            for (var l = 0; l < numNew; l++) {
                var data = result.data[l];
                if (lodashGet(data, 'embeds.target_node.data', null)) {
                    self.newAndNoteworthyNodes().push(result.data[l]);
                    self.fetchContributors(result.data[l]);
                }
            }
            self.someDataLoaded(true);
        }, function _error(result){
            self.requestError(result);
            m.redraw();
        });

        // Load popular nodes
        var popularUrl = $osf.apiV2Url('nodes/' + window.contextVars.popular + '/node_links/', {});
        var popularPromise = m.request({method: 'GET', url: popularUrl, config: xhrconfig, background: true});
        popularPromise.then(function(result){
            var numPopular = Math.min(result.data.length, self.SHOW_TOTAL);
            for (var l = 0; l < numPopular; l++) {
                var data = result.data[l];
                if (lodashGet(data, 'embeds.target_node.data', null)) {
                    self.popularNodes().push(result.data[l]);
                    self.fetchContributors(result.data[l]);
                }
            }
            self.someDataLoaded(true);
        }, function _error(result){
            self.requestError(result);
            m.redraw();
        });

        // Additional API call to fetch node link contributors
        self.fetchContributors = function(nodeLink) {
            var url = lodashGet(nodeLink, 'embeds.target_node.data.relationships.contributors.links.related.href', null);
            var promise = m.request({method: 'GET', url : url, config: xhrconfig});
            promise.then(function(result){
                var contribNames = [];
                result.data.forEach(function (contrib){
                    if (contrib.embeds.users.data){
                        contribNames.push($osf.findContribName(contrib.embeds.users.data.attributes));
                    }
                    else if (contrib.embeds.users.errors) {
                        contribNames.push($osf.findContribName(contrib.embeds.users.errors[0].meta));
                    }

                });
                self.someContributorsLoaded(true);
                var numContrib = result.links.meta.total;
                var nodeId = nodeLink.id;
                self.contributorsMapping[nodeId] = {'names': contribNames, 'total': numContrib};
            }, function _error(result){
                self.requestError(result);
            });
        };

        // Gets contrib family name for display
        self.getFamilyName = function(i, node) {<commit_msg>Only list visible contributors under "Popular Public Projects"<commit_after>/**
 * New and Noteworthy Projects
 */

var $ = require('jquery');
var m = require('mithril');
var $osf = require('js/osfHelpers');
var Raven = require('raven-js');
var lodashGet = require('lodash.get');


// CSS
require('css/new-and-noteworthy-plugin.css');
require('loaders.css/loaders.min.css');


// XHR config for apiserver connection
var xhrconfig = function(xhr) {
    xhr.withCredentials = true;
};


var NewAndNoteworthy = {
    controller: function() {
        var self = this;
        self.newAndNoteworthyNodes = m.prop([]); // New and noteworthy nodes list
        self.popularNodes = m.prop([]); // Most popular nodes list
        self.contributorsMapping = {}; // Dictionary mapping node id to some of the contrib names and the contrib total.
        self.SHOW_TOTAL = 5; // Number of new and noteworthy projects displayed in each column
        self.errorLoading = m.prop(false);  // True if error retrieving projects or contributors.
        self.someDataLoaded = m.prop(false);
        self.someContributorsLoaded = m.prop(false);

        // Switches errorLoading to true
        self.requestError = function(result){
            self.errorLoading = m.prop(true);
            Raven.captureMessage('Error loading new and noteworthy projects on home page.', {
                extra: { requestReturn: result }
            });
        };

        // Load new and noteworthy nodes
        var newAndNoteworthyUrl = $osf.apiV2Url('nodes/' + window.contextVars.newAndNoteworthy + '/node_links/', {});
        var newAndNoteworthyPromise = m.request({method: 'GET', url: newAndNoteworthyUrl, config: xhrconfig, background: true});
        newAndNoteworthyPromise.then(function(result){
            var numNew = Math.min(result.data.length, self.SHOW_TOTAL);
            for (var l = 0; l < numNew; l++) {
                var data = result.data[l];
                if (lodashGet(data, 'embeds.target_node.data', null)) {
                    self.newAndNoteworthyNodes().push(result.data[l]);
                    self.fetchContributors(result.data[l]);
                }
            }
            self.someDataLoaded(true);
        }, function _error(result){
            self.requestError(result);
            m.redraw();
        });

        // Load popular nodes
        var popularUrl = $osf.apiV2Url('nodes/' + window.contextVars.popular + '/node_links/', {});
        var popularPromise = m.request({method: 'GET', url: popularUrl, config: xhrconfig, background: true});
        popularPromise.then(function(result){
            var numPopular = Math.min(result.data.length, self.SHOW_TOTAL);
            for (var l = 0; l < numPopular; l++) {
                var data = result.data[l];
                if (lodashGet(data, 'embeds.target_node.data', null)) {
                    self.popularNodes().push(result.data[l]);
                    self.fetchContributors(result.data[l]);
                }
            }
            self.someDataLoaded(true);
        }, function _error(result){
            self.requestError(result);
            m.redraw();
        });

        // Additional API call to fetch node link contributors
        self.fetchContributors = function(nodeLink) {
            var url = lodashGet(nodeLink, 'embeds.target_node.data.relationships.contributors.links.related.href', null);
            var promise = m.request({method: 'GET', url : url, config: xhrconfig});
            promise.then(function(result){
                var contribNames = [];
                result.data.forEach(function (contrib){
                    if (contrib.embeds.users.data && contrib.attributes.bibliographic){
                        contribNames.push($osf.findContribName(contrib.embeds.users.data.attributes));
                    }
                    else if (contrib.embeds.users.errors) {
                        contribNames.push($osf.findContribName(contrib.embeds.users.errors[0].meta));
                    }

                });
                self.someContributorsLoaded(true);
                var numContrib = result.links.meta.total_bibliographic;
                var nodeId = nodeLink.id;
                self.contributorsMapping[nodeId] = {'names': contribNames, 'total': numContrib};
            }, function _error(result){
                self.requestError(result);
            });
        };

        // Gets contrib family name for display
        self.getFamilyName = function(i, node) {
{'fraction_non_alphanumeric': 0.12215111709891097, 'fraction_numerical': 0.0013472549680026945, 'mean_word_length': 2.1016713091922004, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4436891', 'n_tokens_mistral': 2553, 'n_tokens_neox': 2364, 'n_words': 530}