<commit_before>/**
 * Created on 2016-06-04.
 * Collaborators : <NAME>, <NAME>, <NAME>.
 */
var Bot = require('slackbots');
var getphoto = require('./getphoto.js');
var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database('vacation.db');
var check;
var channels;
var users;
var users_in_vacation;

var settings = {
    token: '<KEY>',
    name: 'syov'
};

//Exemple console.log( channels.getChannelById({id:'C1E6AUVK3'}));
Array.prototype.getByProps = function (obj) {
    return this.filter(function (item) {
        for (var prop in obj)
            if (!(prop in item) || obj[prop] !== item[prop])
                return false;
        return prop;
    });
};

function dbRead() {
    db.all("SELECT * from user_vacation", function (err, rows) {
        users_in_vacation = rows;
        //console.log(users_in_vacation);
    });
}

var bot = new Bot(settings);
bot.on('start', function () {
    db.run("CREATE TABLE if not exists user_vacation (user TEXT PRIMARY KEY ASC, user_to TEXT, dest TEXT, date TEXT)");
    dbRead();
    //addVacation();
    bot.getChannels().then(function (data) {
        channels = data.channels;
        //console.log(channels);
    });
    bot.getUsers().then(function (data) {
        users = data.members;
        //console.log(users);
    });
});

function addVacation(user, userTo, dest, date) {
    db.run("INSERT OR REPLACE INTO user_vacation VALUES ('" + user + "','" + userTo + "','" + dest + "','" + date + "')");
}

function removeVacation(user) {
    db.run("DELETE FROM user_vacation WHERE user='" + user + "'")
}

function verifyMention(data) {
    text = data.text;
    if (text.indexOf("@") > -1) {
        //content = text.substring(text.indexOf(' ') + 1, text.length);
        user_to = text.substring(text.indexOf("@") + 1, text.indexOf(">"));
        channel = channels.getByProps({id: data.channel});
        post_user = data.user;
        //if user is in db user_vacation user column then ...
        //console.log('user_to' + user_to);
        vacation_user = users_in_vacation.getByProps({user: user_to});
        //console.log("users_in_vacation" + vacation_user[0].name);
        if (vacation_user.length > 0) {
            //post @userTo User.name is in vacation maybe @UserTo.name can respond to you
            var messageParams = {};
            message = 'Sorry, ' + users.getByProps({id: vacation_user[0].user})[0].name + " is on vacation";
            if(vacation_user[0].date !== 'Invalid Date') {
                var endDate = new Date(vacation_user[0].date);
                message += ' until ' + endDate.getFullYear() + "-" + (endDate.getMonth() + 1) + "-" + endDate.getDate();
            }
            if(vacation_user[0].dest !== 'null' || vacation_user[0].dest !== 'undefined') {
                var destination = vacation_user[0].dest.toLowerCase().replace(/\b[a-z]/g, function(letter) {
                    return letter.toUpperCase();
                });
                message += ' in ' + destination;

                getphoto.search(destination + ' landscape', function(result) {
                    if(result) {
                        messageParams.attachments = [
                            {
                                "color": "#1CC6FF",
                                "title": result.pageUrl,
                                "title_link": result.pageUrl,
                                "image_url": result.imageUrl
                            }
                        ];
                    }
                    message += '.';

                    if (vacation_user[0].user_to != 'null') {
                        message += " Maybe <@"
                            + users.getByProps({id: vacation_user[0].user_to})[0].id + "> can respond to you."
                    }
                    bot.postMessageToChannel(channel[0].name, message, messageParams);
                });
            }
            else {
                message += '.';

                if (vacation_user[0].user_to != 'null') {
                    message += " Maybe <@"
                        + users.getByProps({id: vacation_user[0].user_to})[0].id + "> can respond to you."
                }
                bot.postMessageToChannel(channel[0].name, message, messageParams);
            }
        }
    }
}

function isPrivate(channel) {
    return channels.getByProps({id: channel}).length == 0;
}

function apiVacation(data) {
    // First check if its a private channel
    text = data.text;
    var user_to;
    var confirmationMessage;
    var textArr = text.split(" ");
    //console.log(textArr);
    try {
        var dest = findValue(textArr, "-w");
        var date = findValue(textArr, "-d");
        if (textArr[1]) {
            if (textArr[1] == 'over') {
                removeVacation(data.user);
                confirmationMessage = "You are no longer on vacation. Welcome back!";
                bot.postMessageToChannel('general', users.getByProps({id: data.user})[0].name+" is no longer on vacation. Welcome back!");
            }
            else if (textArr[1].indexOf('@') > -1) {
                textArr[1] = textArr[1].substring(textArr[1].indexOf('@') + 1, textArr[1].length - 1);
                user_to = users.getByProps({id: textArr[1]})[0].id;
                confirmationMessage = "You are on vacation and <@" + user_to +
                    "> is responding for you. Have fun!";<commit_msg>Destination toLowerCase<commit_after>/**
 * Created on 2016-06-04.
 * Collaborators : <NAME>, <NAME>, <NAME>.
 */
var Bot = require('slackbots');
var getphoto = require('./getphoto.js');
var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database('vacation.db');
var check;
var channels;
var users;
var users_in_vacation;

var settings = {
    token: '<KEY>',
    name: 'syov'
};

//Exemple console.log( channels.getChannelById({id:'C1E6AUVK3'}));
Array.prototype.getByProps = function (obj) {
    return this.filter(function (item) {
        for (var prop in obj)
            if (!(prop in item) || obj[prop] !== item[prop])
                return false;
        return prop;
    });
};

function dbRead() {
    db.all("SELECT * from user_vacation", function (err, rows) {
        users_in_vacation = rows;
        //console.log(users_in_vacation);
    });
}

var bot = new Bot(settings);
bot.on('start', function () {
    db.run("CREATE TABLE if not exists user_vacation (user TEXT PRIMARY KEY ASC, user_to TEXT, dest TEXT, date TEXT)");
    dbRead();
    //addVacation();
    bot.getChannels().then(function (data) {
        channels = data.channels;
        //console.log(channels);
    });
    bot.getUsers().then(function (data) {
        users = data.members;
        //console.log(users);
    });
});

function addVacation(user, userTo, dest, date) {
    db.run("INSERT OR REPLACE INTO user_vacation VALUES ('" + user + "','" + userTo + "','" + dest + "','" + date + "')");
}

function removeVacation(user) {
    db.run("DELETE FROM user_vacation WHERE user='" + user + "'")
}

function verifyMention(data) {
    text = data.text;
    if (text.indexOf("@") > -1) {
        //content = text.substring(text.indexOf(' ') + 1, text.length);
        user_to = text.substring(text.indexOf("@") + 1, text.indexOf(">"));
        channel = channels.getByProps({id: data.channel});
        post_user = data.user;
        //if user is in db user_vacation user column then ...
        //console.log('user_to' + user_to);
        vacation_user = users_in_vacation.getByProps({user: user_to});
        //console.log("users_in_vacation" + vacation_user[0].name);
        if (vacation_user.length > 0) {
            //post @userTo User.name is in vacation maybe @UserTo.name can respond to you
            var messageParams = {};
            message = 'Sorry, ' + users.getByProps({id: vacation_user[0].user})[0].name + " is on vacation";
            if(vacation_user[0].date !== 'Invalid Date') {
                var endDate = new Date(vacation_user[0].date);
                message += ' until ' + endDate.getFullYear() + "-" + (endDate.getMonth() + 1) + "-" + endDate.getDate();
            }
            if(vacation_user[0].dest.toLowerCase() !== 'null' || vacation_user[0].dest.toLowerCase() !== 'undefined') {
                var destination = vacation_user[0].dest.toLowerCase().replace(/\b[a-z]/g, function(letter) {
                    return letter.toUpperCase();
                });
                message += ' in ' + destination;

                getphoto.search(destination + ' landscape', function(result) {
                    if(result) {
                        messageParams.attachments = [
                            {
                                "color": "#1CC6FF",
                                "title": result.pageUrl,
                                "title_link": result.pageUrl,
                                "image_url": result.imageUrl
                            }
                        ];
                    }
                    message += '.';

                    if (vacation_user[0].user_to != 'null') {
                        message += " Maybe <@"
                            + users.getByProps({id: vacation_user[0].user_to})[0].id + "> can respond to you."
                    }
                    bot.postMessageToChannel(channel[0].name, message, messageParams);
                });
            }
            else {
                message += '.';

                if (vacation_user[0].user_to != 'null') {
                    message += " Maybe <@"
                        + users.getByProps({id: vacation_user[0].user_to})[0].id + "> can respond to you."
                }
                bot.postMessageToChannel(channel[0].name, message, messageParams);
            }
        }
    }
}

function isPrivate(channel) {
    return channels.getByProps({id: channel}).length == 0;
}

function apiVacation(data) {
    // First check if its a private channel
    text = data.text;
    var user_to;
    var confirmationMessage;
    var textArr = text.split(" ");
    //console.log(textArr);
    try {
        var dest = findValue(textArr, "-w");
        var date = findValue(textArr, "-d");
        var endDate;
        if(date) {

        }
        console.log(date);
        if (textArr[1]) {
            if (textArr[1] == 'over') {
                removeVacation(data.user);
                confirmationMessage = "You are no longer on vacation. Welcome back!";
                bot.postMessageToChannel('general', users.getByProps({id: data.user})[0].name+" is no longer on vacation. Welcome back!");
            }
            else if (textArr[1].indexOf('@') > -1) {
                textArr[1] = textArr[1].substring(textArr[1].indexOf('@') + 1, textArr[1].length - 1);
                user_to = users.getByProps({id: textArr[1]})[0].id;
                confirmationMessage = "You are on vacation and <@" + user_to +
                    "> is responding for you. Have fun!";
{'fraction_non_alphanumeric': 0.15168949771689497, 'fraction_numerical': 0.009315068493150684, 'mean_word_length': 1.8924986793449552, 'pattern_counts': {'":': 8, '<': 17, '<?xml version=': 0, '>': 25, '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': '5057906', 'n_tokens_mistral': 3316, 'n_tokens_neox': 3177, 'n_words': 688}