<commit_before>/*jshint camelcase:false */
define(function (require) {
    'use strict';

    var client = require('api').getDefaultInstance();
    var Q = require('q');
    var _ = require('lodash');

    function DataBridge(mediator) {
        this.mediator = mediator;
    }

    DataBridge.prototype.listen = function () {
        this.mediator.subscribe('uiLogin', this.login.bind(this));
        this.mediator.subscribe('uiLogout', this.logout.bind(this));
        this.mediator.subscribe('uiRefreshCTLs', this.refreshCTLs.bind(this));
        this.mediator.subscribe('uiResolveCTL', this.resolveCTL.bind(this));
        this.mediator.subscribe('uiCreateCTL', this.createCTL.bind(this));
        this.mediator.subscribe('uiOverwriteCTL', this.overwriteCTL.bind(this));
        this.mediator.subscribe('uiUserInfo', this.getUserInfo.bind(this));
    };

    DataBridge.prototype.login = function () {
        client.login();
    };

    DataBridge.prototype.logout = function (data) {
        client.logout().then(function () {
            this.mediator.publish('dataLogout', {
                key: data.key
            });
        }.bind(this), function (e) {
            this.mediator.publish('dataError', {
                method: 'uiLogout',
                status: e.status,
                message: 'Logging out failed',
                data: {}
            });
        }.bind(this)).done();
    };

    DataBridge.prototype.getUserInfo = function (data) {
        client.getUser(data.id).then(
            function (response) {
            this.mediator.publish('dataUserInfo', {
                key: data.key,
                response: response
            });
        }.bind(this), function (e) {
            this.mediator.publish('dataError', {
                method: 'uiUserInfo',
                status: e.status,
                message: 'Fetching user information failed.',
                data: data
            });
        }.bind(this)).done();
    };

    DataBridge.prototype.resolveCTL = function (data) {
        client.getCTL(data.id, {
            include_cards: 1,
            send_error_codes: 1,
            include_entites: 1
        }).then(function (response) {
            this.mediator.publish('dataResolveCTL', {
                ctl: response
            });
        }.bind(this), function (e) {
            this.mediator.publish('dataError', {<commit_msg>Fix #21: Fetch more than 20 Tweets per CTL<commit_after>/*jshint camelcase:false */
define(function (require) {
    'use strict';

    var client = require('api').getDefaultInstance();
    var Q = require('q');
    var _ = require('lodash');

    function DataBridge(mediator) {
        this.mediator = mediator;
    }

    DataBridge.prototype.listen = function () {
        this.mediator.subscribe('uiLogin', this.login.bind(this));
        this.mediator.subscribe('uiLogout', this.logout.bind(this));
        this.mediator.subscribe('uiRefreshCTLs', this.refreshCTLs.bind(this));
        this.mediator.subscribe('uiResolveCTL', this.resolveCTL.bind(this));
        this.mediator.subscribe('uiCreateCTL', this.createCTL.bind(this));
        this.mediator.subscribe('uiOverwriteCTL', this.overwriteCTL.bind(this));
        this.mediator.subscribe('uiUserInfo', this.getUserInfo.bind(this));

        this.mediator.subscribe('dataResolveCTLPartial', this.resolveCTL.bind(this));
    };

    DataBridge.prototype.login = function () {
        client.login();
    };

    DataBridge.prototype.logout = function (data) {
        client.logout().then(function () {
            this.mediator.publish('dataLogout', {
                key: data.key
            });
        }.bind(this), function (e) {
            this.mediator.publish('dataError', {
                method: 'uiLogout',
                status: e.status,
                message: 'Logging out failed',
                data: {}
            });
        }.bind(this)).done();
    };

    DataBridge.prototype.getUserInfo = function (data) {
        client.getUser(data.id).then(
            function (response) {
            this.mediator.publish('dataUserInfo', {
                key: data.key,
                response: response
            });
        }.bind(this), function (e) {
            this.mediator.publish('dataError', {
                method: 'uiUserInfo',
                status: e.status,
                message: 'Fetching user information failed.',
                data: data
            });
        }.bind(this)).done();
    };

    function mergeCTLResponses(oldCTL, newCTL) {
        return newCTL ? _.merge(newCTL, {
            objects: oldCTL.objects
        }) : oldCTL;
    }

    DataBridge.prototype.resolveCTL = function (data) {
        var options = {
            include_cards: 1,
            send_error_codes: 1,
            include_entites: 1
        };

        if (data.maxPosition) {
            options.max_position = data.maxPosition;
        }

        client.getCTL(data.id, options).then(function (response) {
            if (response.response.position.was_truncated) {
                // Publish partial results so the UI can update, but handle them
                // within this component.
                this.mediator.publish('dataResolveCTLPartial', {
                    ctl: response,
                    id: data.id,
                    maxPosition: response.response.position.min_position
                });
            } else {
                this.mediator.publish('dataResolveCTL', {
                    ctl: mergeCTLResponses(response, data.ctl)
                });
            }
        }.bind(this), function (e) {
            this.mediator.publish('dataError', {
{'fraction_non_alphanumeric': 0.13245149911816578, 'fraction_numerical': 0.001763668430335097, 'mean_word_length': 1.8904179408766564, 'pattern_counts': {'":': 0, '<': 3, '<?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': '2729364', 'n_tokens_mistral': 1611, 'n_tokens_neox': 1528, 'n_words': 258}