<commit_before>/// <reference path='../typings/tsd.d.ts' />
var logger = require('./logger'),
    moment = require('moment'),
    chrono = require('chrono-node'),
    RSVP = require('rsvp');

/**
 * @module OOO_User
 */
var OOO_User = (function () {
    /**
     * Constructor
     *
     * @constructor
     * @param {string} username the name of the user
     */
    function OOO_User(username, email) {
        this.email = email;
        this.username = username;
        this.STATUS_UNCONFIRMED = 'unconfirmed';
        this.STATUS_AWAITING_CONFIRMATION = 'awaiting_confirmation';
        this.STATUS_AWAITING_MESSAGE = 'awaiting_message';
        this.STATUS_REGISTERED = 'registered';
        this.STATUS_AWAITING_DEREGISTER = 'awaiting_deregister';
        this.COMMAND_MESSAGE = 'message';
        this.COMMAND_START = 'start';
        this.COMMAND_END = 'end';
        this.status = this.STATUS_UNCONFIRMED;
        this.MESSAGE_TIMEOUT = 60000; // Five minutes
        this.DEREGISTER_TIMEOUT = 60000; // Five minutes
        this.POSITIVE_REGEXP = /(yes|ok|sure|yeah)/i;
        this.NEGATIVE_REGEXP = /(no|negative)/i;
        this.last_communication = moment();
    }
    /**
     * Check if the user is out of the office
     *
     * @return {promise}
     */
    OOO_User.prototype.isOOO = function () {

        var now = moment(),
            self = this;
        return new RSVP.Promise(function (resolve) {
            var retVal = false;
            retVal = (self.ooo_start && self.ooo_start.isBefore(now));
            if (self.ooo_end) {
                retVal = retVal && (self.ooo_end.isAfter(now));
            }
            console.log('RESOLVING WITH ' + retVal);
            resolve(retVal);
        });

    };
    /**
     * Gets the ms since last communication
     *
     * @return {integer}
     */
    OOO_User.prototype.lastCommunication = function () {
        return this.last_communication ? moment().diff(this.last_communication) : 0;
    };
    /**
     * Set the user's OOO message and return a response
     *
     * @param {string} message The message to set
     * @return {string} A response for the user<commit_msg>Add outlook aware isOOO function<commit_after>/// <reference path='../typings/tsd.d.ts' />
var logger = require('./logger'),
    moment = require('moment'),
    chrono = require('chrono-node'),
    RSVP = require('rsvp');

var https = require("https");
var MongoClient = require('mongodb').MongoClient;
var mongo_url = "mongodb://MongoLab-rj:4rEBWb.CqOGPSoLjJ5.5xoeSr2U59nnWHhJdt7lkY4U-@ds034348.mongolab.com:34348/MongoLab-rj";

function __isOOO(email, cb) {

  var startTime = new Date();
  startTime.setHours(0,0,0,0);
  var endTime = new Date();
  endTime.setHours(23,59,59,0);

  MongoClient.connect(mongo_url, function(err, db) {
    if (err) {
      console.log(err);
      cb(null, err);
    } else {
      console.log("Connected to db..");
      var collection = db.collection('users');
      collection.find({}).toArray(function(err, users) {
        var found = false;
        console.log("Processing users.. (" + users.length + ")");
        for (var i in users) {
          var user = users[i];
          console.log(user);
          if (user.email == email) {

            found = true;
            console.log("Found user match...");

            var options = {
              hostname: 'outlook.office.com',
              path: '/api/v1.0/Me/CalendarView?startDateTime=' + startTime.toISOString() + '&endDateTime=' + endTime.toISOString(),
              headers: { "Authorization": "Bearer " + user.accessToken }
            };

            var body = '';
            https.get(options, function(response) {

              response.on("data", function(data) {
                body += data;
              });

              response.on("end", function() {
                var now = Date.now();  
                body = JSON.parse(body);
                var events = body.value; 
                for (var i in events) {
                  var e = events[i];
                  var start = new Date(e.Start);
                  var end = new Date(e.End);
                  if (start.getTime() <= now && end.getTime() > now && 
                      (e.Subject.indexOf("ooo") != -1 || e.Subject.indexOf("oof") != -1)) {
                    cb(null, true);
                    return;
                  }
                }
                cb(null, false);
              });
            });
          }
        }
        if (!found) {
          cb("user not found");
        }
      });
    } 
  });
}

/**
 * @module OOO_User
 */
var OOO_User = (function () {
    /**
     * Constructor
     *
     * @constructor
     * @param {string} username the name of the user
     */
    function OOO_User(username, email) {
        this.email = email;
        this.username = username;
        this.STATUS_UNCONFIRMED = 'unconfirmed';
        this.STATUS_AWAITING_CONFIRMATION = 'awaiting_confirmation';
        this.STATUS_AWAITING_MESSAGE = 'awaiting_message';
        this.STATUS_REGISTERED = 'registered';
        this.STATUS_AWAITING_DEREGISTER = 'awaiting_deregister';
        this.COMMAND_MESSAGE = 'message';
        this.COMMAND_START = 'start';
        this.COMMAND_END = 'end';
        this.status = this.STATUS_UNCONFIRMED;
        this.MESSAGE_TIMEOUT = 60000; // Five minutes
        this.DEREGISTER_TIMEOUT = 60000; // Five minutes
        this.POSITIVE_REGEXP = /(yes|ok|sure|yeah)/i;
        this.NEGATIVE_REGEXP = /(no|negative)/i;
        this.last_communication = moment();
    }
    /**
     * Check if the user is out of the office
     *
     * @return {promise}
     */
    OOO_User.prototype.isOOO = function () {

        var now = moment(),
            self = this;
        return new RSVP.Promise(function (resolve) {
            __isOOO(self.email, function(err, result) {
              if (err) {
                console.log("ERROR fetch OOO status: " + err);
              } else {
                resolve(result);
              }
            });
        });

    };
    /**
     * Gets the ms since last communication
     *
     * @return {integer}
     */
    OOO_User.prototype.lastCommunication = function () {
        return this.last_communication ? moment().diff(this.last_communication) : 0;
    };
    /**
     * Set the user's OOO message and return a response
     *
     * @param {string} message The message to set
     * @return {string} A response for the user
{'fraction_non_alphanumeric': 0.13320970483696493, 'fraction_numerical': 0.008653994745788905, 'mean_word_length': 1.9009412819363514, 'pattern_counts': {'":': 1, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, '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': '7107355', 'n_tokens_mistral': 2037, 'n_tokens_neox': 1947, 'n_words': 408}