<commit_before>/**
 * Copyright (c) 2016,
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

"use strict"

var google       = require('googleapis');
var doGoogleAuth = require('do-google-auth');

var method = GmailModel.prototype;

var name
   ,userId
   ,gmail
   ,googleAuth
   ,log
   ,log4js;



/**
 * Gmail Model
 *
 * @classdesc Interface with Gmail REST API that takes care of authorization.
 * @namespace gmailModel
 * @version  v1
 * @variation v1
 * @this GmailModel
 * @param {object=} options Options for Gmail
 */

function GmailModel(params) {

  this.name      = params.name
  this.userId    = params.userId

  googleAuth = new doGoogleAuth(
    params.googleScopes,
    params.tokenFile,
    params.tokenDir,
    params.clientSecretFile
  );

  this.googleAuth = params.googleAuth;

  this.gmail = google.gmail('v1');


  this.log4js = params.log4js
  this.log = this.log4js.getLogger('Mailbox-' + this.name);
  this.log.setLevel(params.logLevel);
}

/**
 * gmailModel.createLabel
 *
 * @desc Gets the specified attachment.
 *
 * @alias gmailModel.createLabel
 * @memberOf! gmailModel(v1)
 *
 * @param  {object} params - Parameters for request
 * @param  {string} params.labelName - The name of the label to create
 * @param  {callback} callback - The callback that handles the response.
 */
method.createLabel = function (params,callback) {

  var self = this;

  self.log.info('Creating label: ' + params.labelName)

  // Authorize a client with the loaded credentials, then call the
  // Gmail API.
  googleAuth.authorize(function (err, auth) {

    if (err) { callback(err); return null}

    self.gmail.users.labels.create({
      auth: auth,
      userId: self.userId,
      resource: {
        name: params.labelName
      }
    }, function(err, response) {

      if (err) { callback(err); return null}

      self.log.trace('Returned response:')
      self.log.trace(response)

      self.log.info('Label created: %s', params.labelName)
      callback(null,response.id)
    });
  });
}

/**
 * gmailModel.getAttachment
 *
 * @desc Gets the specified attachment.
 *
 * @alias gmailModel.getAttachment
 * @memberOf! gmailModel(v1)
 *
 * @param  {object} params - Parameters for request
 * @param  {string} params.attachmentId - The ID of the attachment being retrieved.
 * @param  {string} params.messageId - The message the attachment belongs to being retrieved.
 * @param  {callback} callback - The callback that handles the response.
 */
method.getAttachment = function (params,callback) {

  var self = this;

  self.log.debug('Getting attachment')

  // Authorize a client with the loaded credentials, then call the
  // Gmail API.
  googleAuth.authorize(function (err, auth) {

    if (err) { callback(err); return null}

    self.gmail.users.messages.attachments.get(
      {<commit_msg>Added functionality to delete label<commit_after>/**
 * Copyright (c) 2016,
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

"use strict"

var google       = require('googleapis');
var doGoogleAuth = require('do-google-auth');

var method = GmailModel.prototype;

var name
   ,userId
   ,gmail
   ,googleAuth
   ,log
   ,log4js;



/**
 * Gmail Model
 *
 * @classdesc Interface with Gmail REST API that takes care of authorization.
 * @namespace gmailModel
 * @version  v1
 * @variation v1
 * @this GmailModel
 * @param {object=} options Options for Gmail
 */

function GmailModel(params) {

  this.name      = params.name
  this.userId    = params.userId

  googleAuth = new doGoogleAuth(
    params.googleScopes,
    params.tokenFile,
    params.tokenDir,
    params.clientSecretFile
  );

  this.googleAuth = params.googleAuth;

  this.gmail = google.gmail('v1');


  this.log4js = params.log4js
  this.log = this.log4js.getLogger('Mailbox-' + this.name);
  this.log.setLevel(params.logLevel);
}

/**
 * gmailModel.createLabel
 *
 * @desc Gets the specified attachment.
 *
 * @alias gmailModel.createLabel
 * @memberOf! gmailModel(v1)
 *
 * @param  {object} params - Parameters for request
 * @param  {string} params.labelName - The name of the label to create
 * @param  {callback} callback - The callback that handles the response.
 */
method.createLabel = function (params,callback) {

  var self = this;

  self.log.info('Creating label: ' + params.labelName)

  // Authorize a client with the loaded credentials, then call the
  // Gmail API.
  googleAuth.authorize(function (err, auth) {

    if (err) { callback(err); return null}

    self.gmail.users.labels.create({
      auth: auth,
      userId: self.userId,
      resource: {
        name: params.labelName
      }
    }, function(err, response) {

      if (err) { callback(err); return null}

      self.log.trace('Returned response:')
      self.log.trace(response)

      self.log.info('Label created: %s', params.labelName)
      callback(null,response.id)
    });
  });
}


/**
 * gmailModel.deleteLabel
 *
 * @desc Gets the specified attachment.
 *
 * @alias gmailModel.deleteLabel
 * @memberOf! gmailModel(v1)
 *
 * @param  {object} params - Parameters for request
 * @param  {string} params.labelId - The id of the label to delete
 * @param  {callback} callback - The callback that handles the response.
 */
method.deleteLabel = function (params,callback) {

  var self = this;

  self.log.info('Deleting label: ' + params.labelId)

  // Authorize a client with the loaded credentials, then call the
  // Gmail API.
  googleAuth.authorize(function (err, auth) {

    if (err) { callback(err); return null}

    self.gmail.users.labels.delete({
      auth: auth,
      userId: self.userId,
      id: params.labelId
    }, function(err, response) {

      if (err) { callback(err); return null}
      self.log.info('Deleted label: %s', params.labelId)
      callback(null)
    });
  });
}


/**
 * gmailModel.getAttachment
 *
 * @desc Gets the specified attachment.
 *
 * @alias gmailModel.getAttachment
 * @memberOf! gmailModel(v1)
 *
 * @param  {object} params - Parameters for request
 * @param  {string} params.attachmentId - The ID of the attachment being retrieved.
 * @param  {string} params.messageId - The message the attachment belongs to being retrieved.
 * @param  {callback} callback - The callback that handles the response.
 */
method.getAttachment = function (params,callback) {

  var self = this;

  self.log.debug('Getting attachment')

  // Authorize a client with the loaded credentials, then call the
  // Gmail API.
  googleAuth.authorize(function (err, auth) {

    if (err) { callback(err); return null}

    self.gmail.users.messages.attachments.get(
      {
{'fraction_non_alphanumeric': 0.10655428355768021, 'fraction_numerical': 0.0033966536671279405, 'mean_word_length': 3.3585526315789473, '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': '172806', 'n_tokens_mistral': 2430, 'n_tokens_neox': 2330, 'n_words': 777}