<commit_before>/**
 * Created by aleckim on 2016. 5. 2..
 */

"use strict";

var apn = require('apn');
var gcm = require('node-gcm');
var config = require('../config/config');
var PushInfo = require('../models/modelPush');
var async = require('async');
var req = require('request');
var ControllerTown24h = require('./controllerTown24h');
var cTown = new ControllerTown24h();

var kmaTimeLib = require('../lib/kmaTimeLib');

var dnscache = require('dnscache')({
    "enable" : true,
    "ttl" : 300,
    "cachesize" : 1000
});

var apnGateway;

var production = false;
if (process.env.NODE_ENV === 'production') {
    apnGateway = "gateway.push.apple.com";
    production = true;
}
else {
    apnGateway = "gateway.sandbox.push.apple.com";
}

var apnOptions = {
    gateway : apnGateway,
    cert: './config/aps_cert.pem',
    key: './config/aps_key.pem',
    production: production,
    batchFeedback: true,
    interval: 300 //seconds
};

var server_access_key = config.push.gcmAccessKey;

var apnConnection = new apn.Connection(apnOptions);
var sender = new gcm.Sender(server_access_key);

var i18n = require('i18n');

function ControllerPush() {
    this.timeInterval = 60*1000; //1min

    this.domain = config.push.serviceServer.replace('http://', '').replace('https://', '');
    this.url = config.push.serviceServer;

    [apnGateway, this.domain].forEach(function (value) {
        var domain = value;
        dnscache.lookup(domain, function(err, result) {
            if (err) {
                console.error(err);
            }
            else {
                console.info('pushctrl cached domain:', domain, ', result:', result);
            }
        });
    });
}

ControllerPush.prototype.updateRegistrationId = function (newId, oldId, callback) {
    PushInfo.find({registrationId: oldId}, function (err, pushList) {
        if (err) {
            return callback(err);
        }
        if (pushList.length == 0) {
            log.info("no registrationId="+oldId);
            return callback(undefined, 'not found oldRegId='+oldId);
        }

        pushList.forEach(function (push) {
            push.registrationId = newId;
            push.save(function (err) {
                if (err) {
                    log.error(err);
                }
            });
        });
        return callback(undefined, 'We will change itmes='+pushList.length+' regid to '+newId);
    });

    return this;
};

ControllerPush.prototype._getCurrentTime = function () {
   return new Date();
};

/**
 * save push info
 */
ControllerPush.prototype.updatePushInfo = function (pushInfo, callback) {
    pushInfo.updatedAt = this._getCurrentTime();

    if (pushInfo.hasOwnProperty('geo')) {
       if (typeof pushInfo.geo[0] !== 'number' || typeof pushInfo.geo[1] !== 'number')  {
           return callback(new Error('invalid geo info pushInfo:'+JSON.stringify(pushInfo)));
       }
    }
    else if (pushInfo.hasOwnProperty('town')) {
        if (!pushInfo.town.hasOwnProperty('first')) {
            return callback(new Error('invalid town info pushInfo:'+JSON.stringify(pushInfo)));
        }
    }
    else {
        return callback(new Error('invalid info pushInfo:'+JSON.stringify(pushInfo)));
    }

    if (pushInfo.id == undefined) {
        pushInfo.id = pushInfo.cityIndex;
    }

    PushInfo.update(
        {registrationId: pushInfo.registrationId, cityIndex: pushInfo.cityIndex},
        pushInfo,
        {upsert : true},
        function (err, result) {
            if (err) {
                return callback(err);
            }
            return callback(undefined, result);
        });

    return this;
};

ControllerPush.prototype.removePushInfo = function (pushInfo, callback) {
    PushInfo.remove({
            registrationId: pushInfo.registrationId,
            type:pushInfo.type,
            cityIndex: pushInfo.cityIndex},
        function (err, result) {
            if (err) {
                return callback(err);
            }
            if (!result) {
                return callback(new Error('Fail to get result'));
            }
            log.info(result.toString());
            callback(undefined, result.toString());
        });

    return this;
};<commit_msg>fix wrong key #2134 pushes colleciton에 강제로 id를 추가함. id 기준으로 업데이트, 삭제함<commit_after>/**
 * Created by aleckim on 2016. 5. 2..
 */

"use strict";

var apn = require('apn');
var gcm = require('node-gcm');
var config = require('../config/config');
var PushInfo = require('../models/modelPush');
var async = require('async');
var req = require('request');
var ControllerTown24h = require('./controllerTown24h');
var cTown = new ControllerTown24h();

var kmaTimeLib = require('../lib/kmaTimeLib');

var dnscache = require('dnscache')({
    "enable" : true,
    "ttl" : 300,
    "cachesize" : 1000
});

var apnGateway;

var production = false;
if (process.env.NODE_ENV === 'production') {
    apnGateway = "gateway.push.apple.com";
    production = true;
}
else {
    apnGateway = "gateway.sandbox.push.apple.com";
}

var apnOptions = {
    gateway : apnGateway,
    cert: './config/aps_cert.pem',
    key: './config/aps_key.pem',
    production: production,
    batchFeedback: true,
    interval: 300 //seconds
};

var server_access_key = config.push.gcmAccessKey;

var apnConnection = new apn.Connection(apnOptions);
var sender = new gcm.Sender(server_access_key);

var i18n = require('i18n');

function ControllerPush() {
    this.timeInterval = 60*1000; //1min

    this.domain = config.push.serviceServer.replace('http://', '').replace('https://', '');
    this.url = config.push.serviceServer;

    [apnGateway, this.domain].forEach(function (value) {
        var domain = value;
        dnscache.lookup(domain, function(err, result) {
            if (err) {
                console.error(err);
            }
            else {
                console.info('pushctrl cached domain:', domain, ', result:', result);
            }
        });
    });
}

ControllerPush.prototype.updateRegistrationId = function (newId, oldId, callback) {
    PushInfo.find({registrationId: oldId}, function (err, pushList) {
        if (err) {
            return callback(err);
        }
        if (pushList.length == 0) {
            log.info("no registrationId="+oldId);
            return callback(undefined, 'not found oldRegId='+oldId);
        }

        pushList.forEach(function (push) {
            push.registrationId = newId;
            push.save(function (err) {
                if (err) {
                    log.error(err);
                }
            });
        });
        return callback(undefined, 'We will change itmes='+pushList.length+' regid to '+newId);
    });

    return this;
};

ControllerPush.prototype._getCurrentTime = function () {
   return new Date();
};

/**
 * save push info
 */
ControllerPush.prototype.updatePushInfo = function (pushInfo, callback) {
    pushInfo.updatedAt = this._getCurrentTime();

    if (pushInfo.hasOwnProperty('geo')) {
       if (typeof pushInfo.geo[0] !== 'number' || typeof pushInfo.geo[1] !== 'number')  {
           return callback(new Error('invalid geo info pushInfo:'+JSON.stringify(pushInfo)));
       }
    }
    else if (pushInfo.hasOwnProperty('town')) {
        if (!pushInfo.town.hasOwnProperty('first')) {
            return callback(new Error('invalid town info pushInfo:'+JSON.stringify(pushInfo)));
        }
    }
    else {
        return callback(new Error('invalid info pushInfo:'+JSON.stringify(pushInfo)));
    }

    if (pushInfo.id == undefined) {
        pushInfo.id = pushInfo.cityIndex;
    }

    PushInfo.update(
        {registrationId: pushInfo.registrationId, type:pushInfo.type, id: pushInfo.id},
        pushInfo,
        {upsert : true},
        function (err, result) {
            if (err) {
                return callback(err);
            }
            return callback(undefined, result);
        });

    return this;
};

ControllerPush.prototype.removePushInfo = function (pushInfo, callback) {
    PushInfo.remove({
            registrationId: pushInfo.registrationId,
            type:pushInfo.type,
            id: pushInfo.id},
        function (err, result) {
            if (err) {
                return callback(err);
            }
            if (!result) {
                return callback(new Error('Fail to get result'));
            }
            log.info(result.toString());
            callback(undefined, result.toString());
        });

    return this;
};
{'fraction_non_alphanumeric': 0.1420481359131666, 'fraction_numerical': 0.008966493629070316, 'mean_word_length': 2.3880895283772983, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '2871254', 'n_tokens_mistral': 2592, 'n_tokens_neox': 2509, 'n_words': 543}