<commit_before>/*
  A general purpose user CordovaPush
  ios, android, mail, twitter?, facebook?, sms?, snailMail? :)

  Phonegap generic :
  https://github.com/phonegap-build/PushPlugin
 */

// getText / getBinary

Push.setBadge = function(/* id, count */) {
    // throw new Error('Push.setBadge not implemented on the server');
};

Push.Connections = {};

var isConfigured = false;

const defaults = {
  sendInterval: 5000
};

Push.Configure = function(options) {
    var self = this;

    let cfgOptions = _.extend(defaults, options);
    // https://npmjs.org/package/apn

    // After requesting the certificate from Apple, export your private key as
    // a .p12 file anddownload the .cer file from the iOS Provisioning Portal.

    // gateway.push.apple.com, port 2195
    // gateway.sandbox.push.apple.com, port 2195

    // Now, in the directory containing cert.cer and key.p12 execute the
    // following commands to generate your .pem files:
    // $ openssl x509 -in cert.cer -inform DER -outform PEM -out cert.pem
    // $ openssl pkcs12 -in key.p12 -out key.pem -nodes

    // Block multiple calls
    if (isConfigured)
      console.warn('Push.Configure should only be called once.');

    isConfigured = true;

    // Add debug info
    if (Push.debug) {
      console.log('Push.Configure', options);
    }

    // This function is called when a token is replaced on a device - normally
    // this should not happen, but if it does we should take action on it
    _replaceToken = function(currentToken, newToken) {
        // console.log('Replace token: ' + currentToken + ' -- ' + newToken);
        // If the server gets a token event its passing in the current token and
        // the new value - if new value is undefined this empty the token
        self.emitState('token', currentToken, newToken);
    };

    // Rig the removeToken callback
    _removeToken = function(token) {
       console.log('Remove token: ' + token);
        // Invalidate the token
        self.emitState('token', token, null);
    };

    if (Push.debug) {
      console.log('Push: APN configured');
    }

    var apn = Npm.require('apn');

    var useProductionCert = process.env.NODE_ENV === 'production';

    if (!useProductionCert) {
      console.log("This is being configured in development mode");
    }

    const vets = Vets.find().fetch();
    vets.forEach((vet) => {
      Meteor.call('raix:configure-push-connections', vet._id);
    });

    // apnConnection.connect().then(function() {
    //     console.info('CHECK: Push APN connection OK');
    // }, function(err) {
    //     console.warn('CHECK: Push APN connection FAILURE');
    // });
    // Note: the above code spoils the connection - investigate how to
    // shutdown/close it.

    self.sendAPN = function(userToken, notification) {
        if (Match.test(notification.apn, Object)) {
          notification = _.extend({}, notification, notification.apn);
        }

        if (!Match.test(notification.appIdentifier, String)) {
          console.error('ERROR: PUSH.sendAPN - No appIdentifier found for notification: ', notification);
          return;<commit_msg>Don’t configure push for disabled apps.<commit_after>/*
  A general purpose user CordovaPush
  ios, android, mail, twitter?, facebook?, sms?, snailMail? :)

  Phonegap generic :
  https://github.com/phonegap-build/PushPlugin
 */

// getText / getBinary

Push.setBadge = function(/* id, count */) {
    // throw new Error('Push.setBadge not implemented on the server');
};

Push.Connections = {};

var isConfigured = false;

const defaults = {
  sendInterval: 5000
};

Push.Configure = function(options) {
    var self = this;

    let cfgOptions = _.extend(defaults, options);
    // https://npmjs.org/package/apn

    // After requesting the certificate from Apple, export your private key as
    // a .p12 file anddownload the .cer file from the iOS Provisioning Portal.

    // gateway.push.apple.com, port 2195
    // gateway.sandbox.push.apple.com, port 2195

    // Now, in the directory containing cert.cer and key.p12 execute the
    // following commands to generate your .pem files:
    // $ openssl x509 -in cert.cer -inform DER -outform PEM -out cert.pem
    // $ openssl pkcs12 -in key.p12 -out key.pem -nodes

    // Block multiple calls
    if (isConfigured)
      console.warn('Push.Configure should only be called once.');

    isConfigured = true;

    // Add debug info
    if (Push.debug) {
      console.log('Push.Configure', options);
    }

    // This function is called when a token is replaced on a device - normally
    // this should not happen, but if it does we should take action on it
    _replaceToken = function(currentToken, newToken) {
        // console.log('Replace token: ' + currentToken + ' -- ' + newToken);
        // If the server gets a token event its passing in the current token and
        // the new value - if new value is undefined this empty the token
        self.emitState('token', currentToken, newToken);
    };

    // Rig the removeToken callback
    _removeToken = function(token) {
       console.log('Remove token: ' + token);
        // Invalidate the token
        self.emitState('token', token, null);
    };

    if (Push.debug) {
      console.log('Push: APN configured');
    }

    var apn = Npm.require('apn');

    var useProductionCert = process.env.NODE_ENV === 'production';

    if (!useProductionCert) {
      console.log("This is being configured in development mode");
    }

    const vets = Vets.find({$or: [{disabled: {$exists: false}}, {disabled: false}]}).fetch();
    vets.forEach((vet) => {
      Meteor.call('raix:configure-push-connections', vet._id);
    });

    // apnConnection.connect().then(function() {
    //     console.info('CHECK: Push APN connection OK');
    // }, function(err) {
    //     console.warn('CHECK: Push APN connection FAILURE');
    // });
    // Note: the above code spoils the connection - investigate how to
    // shutdown/close it.

    self.sendAPN = function(userToken, notification) {
        if (Match.test(notification.apn, Object)) {
          notification = _.extend({}, notification, notification.apn);
        }

        if (!Match.test(notification.appIdentifier, String)) {
          console.error('ERROR: PUSH.sendAPN - No appIdentifier found for notification: ', notification);
          return;
{'fraction_non_alphanumeric': 0.1250392218387198, 'fraction_numerical': 0.0072168183244430495, 'mean_word_length': 2.6595866819747416, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4588628', 'n_tokens_mistral': 1913, 'n_tokens_neox': 1803, 'n_words': 573}