<commit_before>//realtime
//external modules
var cookie = require('cookie');
var cookieParser = require('cookie-parser');
var url = require('url');
var async = require('async');
var LZString = require('lz-string');
var shortId = require('shortid');
var randomcolor = require("randomcolor");
var Chance = require('chance'),
    chance = new Chance();
var md5 = require("blueimp-md5").md5;
var moment = require('moment');

//core
var config = require("../config.js");
var logger = require("./logger.js");

//ot
var ot = require("./ot/index.js");

//others
var db = require("./db.js");
var Note = require("./note.js");
var User = require("./user.js");

//public
var realtime = {
    io: null,
    onAuthorizeSuccess: onAuthorizeSuccess,
    onAuthorizeFail: onAuthorizeFail,
    secure: secure,
    connection: connection,
    getStatus: getStatus
};

function onAuthorizeSuccess(data, accept) {
    accept(null, true);
}

function onAuthorizeFail(data, message, error, accept) {
    if (error) throw new Error(message);
    accept(null, true);
}

function secure(socket, next) {
    try {
        var handshakeData = socket.request;
        if (handshakeData.headers.cookie) {
            handshakeData.cookie = cookie.parse(handshakeData.headers.cookie);
            handshakeData.sessionID = cookieParser.signedCookie(handshakeData.cookie[config.sessionname], config.sessionsecret);
            if (handshakeData.cookie[config.sessionname] == handshakeData.sessionID) {
                next(new Error('AUTH failed: Cookie is invalid.'));
            }
        } else {
            next(new Error('AUTH failed: No cookie transmitted.'));
        }
        if (config.debug)
            logger.info("AUTH success cookie: " + handshakeData.sessionID);

        next();
    } catch (ex) {
        next(new Error("AUTH failed:" + JSON.stringify(ex)));
    }
}

function emitCheck(note) {
    var out = {
        updatetime: note.updatetime
    };
    realtime.io.to(note.id).emit('check', out);
    /*
    for (var i = 0, l = note.socks.length; i < l; i++) {
        var sock = note.socks[i];
        sock.emit('check', out);
    };
    */
}

//actions
var users = {};
var notes = {};
var updater = setInterval(function () {
    async.each(Object.keys(notes), function (key, callback) {
        var note = notes[key];
        if (note.server.isDirty) {
            if (config.debug)
                logger.info("updater found dirty note: " + key);
            var body = note.server.document;
            var title = Note.getNoteTitle(body);
            title = LZString.compressToBase64(title);
            body = LZString.compressToBase64(body);
            db.saveToDB(key, title, body, function (err, result) {
                if (err) return;
                note.server.isDirty = false;
                note.updatetime = Date.now();
                emitCheck(note);
            });
        }
        callback();
    }, function (err) {
        if (err) return logger.error('updater error', err);
    });
}, 1000);

function getStatus(callback) {
    db.countFromDB(function (err, data) {
        if (err) return logger.info(err);
        var distinctaddresses = [];
        var regaddresses = [];
        var distinctregaddresses = [];<commit_msg>Added cleaner in realtime.js, to clean up users which aren't in any rooms in a interval<commit_after>//realtime
//external modules
var cookie = require('cookie');
var cookieParser = require('cookie-parser');
var url = require('url');
var async = require('async');
var LZString = require('lz-string');
var shortId = require('shortid');
var randomcolor = require("randomcolor");
var Chance = require('chance'),
    chance = new Chance();
var md5 = require("blueimp-md5").md5;
var moment = require('moment');

//core
var config = require("../config.js");
var logger = require("./logger.js");

//ot
var ot = require("./ot/index.js");

//others
var db = require("./db.js");
var Note = require("./note.js");
var User = require("./user.js");

//public
var realtime = {
    io: null,
    onAuthorizeSuccess: onAuthorizeSuccess,
    onAuthorizeFail: onAuthorizeFail,
    secure: secure,
    connection: connection,
    getStatus: getStatus
};

function onAuthorizeSuccess(data, accept) {
    accept(null, true);
}

function onAuthorizeFail(data, message, error, accept) {
    if (error) throw new Error(message);
    accept(null, true);
}

function secure(socket, next) {
    try {
        var handshakeData = socket.request;
        if (handshakeData.headers.cookie) {
            handshakeData.cookie = cookie.parse(handshakeData.headers.cookie);
            handshakeData.sessionID = cookieParser.signedCookie(handshakeData.cookie[config.sessionname], config.sessionsecret);
            if (handshakeData.cookie[config.sessionname] == handshakeData.sessionID) {
                next(new Error('AUTH failed: Cookie is invalid.'));
            }
        } else {
            next(new Error('AUTH failed: No cookie transmitted.'));
        }
        if (config.debug)
            logger.info("AUTH success cookie: " + handshakeData.sessionID);

        next();
    } catch (ex) {
        next(new Error("AUTH failed:" + JSON.stringify(ex)));
    }
}

function emitCheck(note) {
    var out = {
        updatetime: note.updatetime
    };
    realtime.io.to(note.id).emit('check', out);
    /*
    for (var i = 0, l = note.socks.length; i < l; i++) {
        var sock = note.socks[i];
        sock.emit('check', out);
    };
    */
}

//actions
var users = {};
var notes = {};
//update when the note is dirty
var updater = setInterval(function () {
    async.each(Object.keys(notes), function (key, callback) {
        var note = notes[key];
        if (note.server.isDirty) {
            if (config.debug)
                logger.info("updater found dirty note: " + key);
            var body = note.server.document;
            var title = Note.getNoteTitle(body);
            title = LZString.compressToBase64(title);
            body = LZString.compressToBase64(body);
            db.saveToDB(key, title, body, function (err, result) {
                if (err) return;
                note.server.isDirty = false;
                note.updatetime = Date.now();
                emitCheck(note);
            });
        }
        callback();
    }, function (err) {
        if (err) return logger.error('updater error', err);
    });
}, 1000);
//clean when user not in any rooms
var cleaner = setInterval(function () {
    async.each(Object.keys(users), function (key, callback) {
        var socket = realtime.io.sockets.connected[key];
        if (!socket || !socket.rooms || socket.rooms.length <= 0) {
            if (config.debug)
                logger.info("cleaner found redundant user: " + key);
            disconnectSocketQueue.push(socket);
            disconnect(socket);
        }
        callback();
    }, function (err) {
        if (err) return logger.error('cleaner error', err);
    });
}, 60000);

function getStatus(callback) {
    db.countFromDB(function (err, data) {
        if (err) return logger.info(err);
        var distinctaddresses = [];
        var regaddresses = [];
        var distinctregaddresses = [];
{'fraction_non_alphanumeric': 0.14409141583054627, 'fraction_numerical': 0.004180602006688963, 'mean_word_length': 2.5354679802955666, 'pattern_counts': {'":': 0, '<': 6, '<?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': '2589697', 'n_tokens_mistral': 2137, 'n_tokens_neox': 2095, 'n_words': 516}