<commit_before>/**
 * Created by john on 6/4/16.
 */
var Bot = require('slackbots');
var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database('redirect.db');
var check;
var channels;
var users;
var user_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_redirect", function (err, rows) {
        user_in_vacation = rows;
        //console.log(user_in_vacation);
    });
}

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

function addRedirect(user, userTo) {
    db.run("INSERT INTO user_redirect VALUES ('" + user + "','" + userTo + "')");
}

function removeRedirect(user) {
    db.run("DELETE FROM user_redirect 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_redirect user column then ...
        //console.log('user_to' + user_to);
        vacation_user = user_in_vacation.getByProps({user: user_to});
        //console.log("user_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
            message = 'Sorry ' + users.getByProps({id: vacation_user[0].user})[0].name + " is on vacation.";

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

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

function apiTravel(data) {
    // First check if its a private channel
    text = data.text;
    var user_to;
    var confirmationMessage;
    if (isPrivate(data.channel)) {
        var textArr = text.split(" ");
        try {
            if (textArr[1] == 'undefined') {
                user_to = 'nobody';
            }
            else if (textArr[1] == 'over') {
                removeRedirect(data.user);
                confirmationMessage = "You are 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];
                } else {
                    user_to = users.getByProps({name: textArr[1]})[0];
                }
                confirmationMessage = "You are on vacation and <@" + user_to.id +
                    "> is answering for you. Have fun!";
                addRedirect(data.user, user_to.id);
            }
            bot.postMessageToUser(users.getByProps({id: data.user})[0].name, confirmationMessage);
            dbRead();
        }
        catch<commit_msg>Set user of user_redirect as PRIMARY KEY and add "insert or replace" in insert query.<commit_after>/**
 * Created by john on 6/4/16.
 */
var Bot = require('slackbots');
var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database('redirect.db');
var check;
var channels;
var users;
var user_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_redirect", function (err, rows) {
        user_in_vacation = rows;
        //console.log(user_in_vacation);
    });
}

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

function addRedirect(user, userTo) {
    db.run("INSERT OR REPLACE INTO user_redirect(user, user_to) " +
        "VALUES ('" + user + "', '" + userTo + "')");
}

function removeRedirect(user) {
    db.run("DELETE FROM user_redirect 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_redirect user column then ...
        //console.log('user_to' + user_to);
        vacation_user = user_in_vacation.getByProps({user: user_to});
        //console.log("user_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
            message = 'Sorry ' + users.getByProps({id: vacation_user[0].user})[0].name + " is on vacation.";

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

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

function apiTravel(data) {
    // First check if its a private channel
    text = data.text;
    var user_to;
    var confirmationMessage;
    if (isPrivate(data.channel)) {
        var textArr = text.split(" ");
        try {
            if(textArr[1])
            {
                if (textArr[1] == 'over') {
                    removeRedirect(data.user);
                    confirmationMessage = "You are 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.id +
                        "> is responding for you. Have fun!";
                    addRedirect(data.user, user_to);
                }
            }
            else {
                user_to = null;
                confirmationMessage = "You are on vacation. Have fun!";
                addRedirect(data.user, user_to);
            }
            bot.postMessageToUser(users.getByProps({id: data.user})[0].name, confirmationMessage);
            dbRead();
        }
        catch
{'fraction_non_alphanumeric': 0.15054860933911712, 'fraction_numerical': 0.009186016841030875, 'mean_word_length': 2.1865853658536585, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 17, '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': '1587482', 'n_tokens_mistral': 2453, 'n_tokens_neox': 2379, 'n_words': 548}