<commit_before>angular.module('codebrag.commits')

    .factory('commitsListService', function($resource, $q, $http, Commits, $rootScope, commitLoadFilter, events) {

        var commits = new codebrag.AsyncCollection();

        function loadCommitsPendingReview() {
            commitLoadFilter.setPendingMode();
            return _loadCommits();
        }

        function loadAllCommits() {
            commitLoadFilter.setAllMode();
            return _loadCommits();
        }

        function loadOneMore() {
            return loadMore(1)
        }

        function loadMore(limit) {
            var request = {
                filter: commitLoadFilter.current,
                skip: commits.elements.length,
                limit: limit
            };
            var responsePromise = Commits.get(request).$then(function(response) {
                _broadcastNewCommitCountEvent(response.data.totalCount);
                return response.data.commits;
            });
            return commits.addElements(responsePromise);
        }

        function loadMoreCommits() {
            return loadMore(commitLoadFilter.maxCommitsOnList());
        }

        function _loadCommits() {
            var responsePromise = Commits.get({filter: commitLoadFilter.current}).$then(function (response) {
                _broadcastNewCommitCountEvent(response.data.totalCount);
                return response.data.commits;
            });
            return commits.loadElements(responsePromise);
        }

        function syncCommits() {
            $http({method: 'POST', url: 'rest/commits/sync'}).success(function(response) {
                commits.elements.length = 0;
                _broadcastNewCommitCountEvent(response.totalCount);
                _.forEach(response.commits, function(commit) {
                    commits.elements.push(commit);
                });
            });
        }

        function allCommits() {
            return commits.elements;
        }

        function markAsNotReviewable(commitId) {
            commits.elements.some(function (commit) {
                if (commit.id == commitId) {
                    commit.pendingReview = false;
                    return true;
                }
                return false;
            });
        }

        /**
         * Removes commit with given identifier. Broadcasts a global event with new commit count.
         * @param commitId identifier of commit to remove.
         * @returns a promise of successful commit removal with no parameters in callback.
         */
        function removeCommit(commitId) {
            var responsePromise = Commits.remove({id: commitId}).$then();
            if (commitLoadFilter.isAll()) {
                markAsNotReviewable(commitId);
            } else {
                commits.removeElement(_matchingId(commitId), responsePromise).then(loadOneMore);
            }
            return responsePromise;
        }

        function _matchingId(id) {
            return function(element) {
                return element.id == id;
            }
        }

        /**
         * Removes commit with given identifier and returns promise of next element.
         * Broadcasts a global event with new commit count.
         * @param commitId identifier of commit to remove.
         * @returns a promise of successful commit removal. Callback function passes next available commit for review or
         * null if removed commit was last.
         */
        function removeCommitAndGetNext(commitId) {
            var responsePromise = Commits.remove({id: commitId}).$then();
            var getNextPromise;
            if (commitLoadFilter.isAll()) {
                markAsNotReviewable(commitId);
                getNextPromise = commits.getNextAfter(_matchingId(commitId), responsePromise);
            } else {
                getNextPromise = commits.removeElementAndGetNext(_matchingId(commitId), responsePromise.then(loadOneMore));
            }
            return getNextPromise
        }

        function loadCommitById(commitId) {
            return Commits.get({id: commitId}).$then(function(response) {<commit_msg>Fix order of server calls in removeCommitAndGetNext - This should now behave well when marking commit as reviewed<commit_after>angular.module('codebrag.commits')

    .factory('commitsListService', function($resource, $q, $http, Commits, $rootScope, commitLoadFilter, events) {

        var commits = new codebrag.AsyncCollection();

        function loadCommitsPendingReview() {
            commitLoadFilter.setPendingMode();
            return _loadCommits();
        }

        function loadAllCommits() {
            commitLoadFilter.setAllMode();
            return _loadCommits();
        }

        function loadOneMore() {
            return loadMore(1)
        }

        function loadMore(limit) {
            var request = {
                filter: commitLoadFilter.current,
                skip: commits.elements.length,
                limit: limit
            };
            var responsePromise = Commits.get(request).$then(function(response) {
                _broadcastNewCommitCountEvent(response.data.totalCount);
                return response.data.commits;
            });
            return commits.addElements(responsePromise);
        }

        function loadMoreCommits() {
            return loadMore(commitLoadFilter.maxCommitsOnList());
        }

        function _loadCommits() {
            var responsePromise = Commits.get({filter: commitLoadFilter.current}).$then(function (response) {
                _broadcastNewCommitCountEvent(response.data.totalCount);
                return response.data.commits;
            });
            return commits.loadElements(responsePromise);
        }

        function syncCommits() {
            $http({method: 'POST', url: 'rest/commits/sync'}).success(function(response) {
                commits.elements.length = 0;
                _broadcastNewCommitCountEvent(response.totalCount);
                _.forEach(response.commits, function(commit) {
                    commits.elements.push(commit);
                });
            });
        }

        function allCommits() {
            return commits.elements;
        }

        function markAsNotReviewable(commitId) {
            commits.elements.some(function (commit) {
                if (commit.id == commitId) {
                    commit.pendingReview = false;
                    return true;
                }
                return false;
            });
        }

        /**
         * Removes commit with given identifier. Broadcasts a global event with new commit count.
         * @param commitId identifier of commit to remove.
         * @returns a promise of successful commit removal with no parameters in callback.
         */
        function removeCommit(commitId) {
            var responsePromise = Commits.remove({id: commitId}).$then();
            if (commitLoadFilter.isAll()) {
                markAsNotReviewable(commitId);
            } else {
                commits.removeElement(_matchingId(commitId), responsePromise).then(loadOneMore);
            }
            return responsePromise;
        }

        function _matchingId(id) {
            return function(element) {
                return element.id == id;
            }
        }

        /**
         * Removes commit with given identifier and returns promise of next element.
         * Broadcasts a global event with new commit count.
         * @param commitId identifier of commit to remove.
         * @returns a promise of successful commit removal. Callback function passes next available commit for review or
         * null if removed commit was last.
         */
        function removeCommitAndGetNext(commitId) {
            var responsePromise = Commits.remove({id: commitId}).$then();
            var getNextPromise;
            if (commitLoadFilter.isAll()) {
                markAsNotReviewable(commitId);
                getNextPromise = commits.getNextAfter(_matchingId(commitId), responsePromise);
            } else {
                getNextPromise = loadOneMore().then(function () {
                    return commits.removeElementAndGetNext(_matchingId(commitId), responsePromise);
                });
            }
            return getNextPromise
        }

        function loadCommitById(commitId) {
            return Commits.get({id: commitId}).$then(function(response) {
{'fraction_non_alphanumeric': 0.0878680800942285, 'fraction_numerical': 0.0004711425206124853, 'mean_word_length': 1.8379010695187166, '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': '4939751', 'n_tokens_mistral': 2070, 'n_tokens_neox': 1977, 'n_words': 450}