<commit_before>define(function(require) {
    'use strict';

    var angular = require('angular'),
        appController = require('js/app-controller'),
        _ = require('underscore'),
        config = require('js/app-config').config,
        emailDao, senderIntervalId;

    //
    // Controller
    //

    var NavigationCtrl = function($scope) {
        $scope.navOpen = false;
        $scope.writerOpen = false;
        $scope.accountOpen = false;

        emailDao = appController._emailDao;

        //
        // scope functions
        //

        $scope.openNav = function() {
            $scope.navOpen = true;
        };
        $scope.closeNav = function() {
            $scope.navOpen = false;
        };

        $scope.openWriter = function(replyTo) {
            $scope.writerReply = !! (replyTo);
            $scope.writerOpen = true;
        };
        $scope.closeWriter = function() {
            $scope.writerOpen = false;
        };

        $scope.openFolder = function(folder) {
            $scope.currentFolder = folder;
            $scope.closeNav();
        };

        $scope.openAccount = function() {
            $scope.accountOpen = true;
        };
        $scope.closeAccount = function() {
            $scope.accountOpen = false;
        };

        $scope.remove = function(email) {
            var trashFolder = _.findWhere($scope.folders, {
                type: 'Trash'
            });

            if ($scope.currentFolder === trashFolder) {
                emailDao.imapDeleteMessage({
                    folder: $scope.currentFolder.path,
                    uid: email.uid
                }, moved);
                return;
            }

            emailDao.imapMoveMessage({
                folder: $scope.currentFolder.path,
                uid: email.uid,
                destination: trashFolder.path
            }, moved);

            function moved(err) {
                var index;

                if (err) {
                    console.error(err);
                    return;
                }

                index = $scope.emails.indexOf(email);
                // show the next mail
                if ($scope.emails.length > 1) {
                    // if we're about to delete the last entry of the array, show the previous (i.e. the one below in the list), 
                    // otherwise show the next one (i.e. the one above in the list)
                    $scope.select(_.last($scope.emails) === email ? $scope.emails[index - 1] : $scope.emails[index + 1]);
                } else {
                    // if we have only one email in the array, show nothing
                    $scope.select();
                    $scope.selected = undefined;
                }
                $scope.emails.splice(index, 1);
                $scope.$apply();
            }
        };

        $scope.sendFirstFromOutbox = function() {
            var dbType = 'email_OUTBOX',
                outbox = _.findWhere($scope.folders, {
                    type: 'Outbox'
                });

            checkStorage();

            function checkStorage() {
                // get last item from outbox
                emailDao._devicestorage.listItems(dbType, 0, null, function(err, pending) {
                    if (err) {
                        console.error(err);
                        return;
                    }

                    // update outbox folder count
                    outbox.count = pending.length;
                    $scope.$apply();
                    if (pending.length < 1) {
                        return;
                    }

                    // sending the first one pending
                    send(pending[0]);
                });
            }

            function send(email) {
                emailDao.smtpSend(email, function(err) {
                    if (err) {
                        console.error(err);
                        return;
                    }

                    removeFromStorage(email.id);
                });
            }

            function removeFromStorage(id) {
                if (!id) {
                    console.error('Cannot remove email from storage without a valid id!');
                    return;
                }

                // delete email from local storage
                var key = dbType + '_' + id;
                emailDao._devicestorage.removeList(key, function(err) {
                    if (err) {
                        console.error(err);
                        return;
                    }

                    outbox.count = (outbox.count > 0) ? outbox.count - 1 : outbox.count;
                    $scope.$apply();<commit_msg>send emails in outbox subsequently<commit_after>define(function(require) {
    'use strict';

    var angular = require('angular'),
        appController = require('js/app-controller'),
        _ = require('underscore'),
        config = require('js/app-config').config,
        emailDao, senderIntervalId;

    //
    // Controller
    //

    var NavigationCtrl = function($scope) {
        $scope.navOpen = false;
        $scope.writerOpen = false;
        $scope.accountOpen = false;

        emailDao = appController._emailDao;

        //
        // scope functions
        //

        $scope.openNav = function() {
            $scope.navOpen = true;
        };
        $scope.closeNav = function() {
            $scope.navOpen = false;
        };

        $scope.openWriter = function(replyTo) {
            $scope.writerReply = !! (replyTo);
            $scope.writerOpen = true;
        };
        $scope.closeWriter = function() {
            $scope.writerOpen = false;
        };

        $scope.openFolder = function(folder) {
            $scope.currentFolder = folder;
            $scope.closeNav();
        };

        $scope.openAccount = function() {
            $scope.accountOpen = true;
        };
        $scope.closeAccount = function() {
            $scope.accountOpen = false;
        };

        $scope.remove = function(email) {
            var trashFolder = _.findWhere($scope.folders, {
                type: 'Trash'
            });

            if ($scope.currentFolder === trashFolder) {
                emailDao.imapDeleteMessage({
                    folder: $scope.currentFolder.path,
                    uid: email.uid
                }, moved);
                return;
            }

            emailDao.imapMoveMessage({
                folder: $scope.currentFolder.path,
                uid: email.uid,
                destination: trashFolder.path
            }, moved);

            function moved(err) {
                var index;

                if (err) {
                    console.error(err);
                    return;
                }

                index = $scope.emails.indexOf(email);
                // show the next mail
                if ($scope.emails.length > 1) {
                    // if we're about to delete the last entry of the array, show the previous (i.e. the one below in the list), 
                    // otherwise show the next one (i.e. the one above in the list)
                    $scope.select(_.last($scope.emails) === email ? $scope.emails[index - 1] : $scope.emails[index + 1]);
                } else {
                    // if we have only one email in the array, show nothing
                    $scope.select();
                    $scope.selected = undefined;
                }
                $scope.emails.splice(index, 1);
                $scope.$apply();
            }
        };

        $scope.sendFirstFromOutbox = function() {
            var dbType = 'email_OUTBOX',
                outbox = _.findWhere($scope.folders, {
                    type: 'Outbox'
                });

            checkStorage();

            function checkStorage() {
                // get last item from outbox
                emailDao._devicestorage.listItems(dbType, 0, null, function(err, pending) {
                    if (err) {
                        console.error(err);
                        return;
                    }

                    // update outbox folder count
                    outbox.count = pending.length;
                    $scope.$apply();

                    // sending pending mails
                    send(pending);
                });
            }

            function send(emails) {
                if (emails.length === 0) {
                    return;
                }
                
                var email = emails.shift();
                emailDao.smtpSend(email, function(err) {
                    if (err) {
                        console.error(err);
                        return;
                    }

                    removeFromStorage(email.id);
                    send(emails);
                });
            }

            function removeFromStorage(id) {
                if (!id) {
                    console.error('Cannot remove email from storage without a valid id!');
                    return;
                }

                // delete email from local storage
                var key = dbType + '_' + id;
                emailDao._devicestorage.removeList(key, function(err) {
                    if (err) {
                        console.error(err);
                        return;
                    }

                    outbox.count = (outbox.count > 0) ? outbox.count - 1 : outbox.count;
                    $scope.$apply();
{'fraction_non_alphanumeric': 0.10505220968252293, 'fraction_numerical': 0.0017930598038181627, 'mean_word_length': 1.0867077464788732, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 7, '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': '6063414', 'n_tokens_mistral': 2419, 'n_tokens_neox': 2226, 'n_words': 495}