<commit_before>/* jslint node: true */
'use strict';

//  ENiGMA½
const logger = require('./logger.js');
const Events = require('./events.js');
const UserProps = require('./user_property.js');

//  deps
const _ = require('lodash');
const moment = require('moment');
const hashids = require('hashids/cjs');

exports.getActiveConnections = getActiveConnections;
exports.getActiveConnectionList = getActiveConnectionList;
exports.addNewClient = addNewClient;
exports.removeClient = removeClient;
exports.getConnectionByUserId = getConnectionByUserId;
exports.getConnectionByNodeId = getConnectionByNodeId;

const clientConnections = [];
exports.clientConnections = clientConnections;

const AllConnections = { authUsersOnly: false, visibleOnly: false, availOnly: false };
exports.AllConnections = AllConnections;

const UserVisibleConnections = {
    authUsersOnly: false,
    visibleOnly: true,
    availOnly: false,
};
exports.UserVisibleConnections = UserVisibleConnections;

const UserMessageableConnections = {
    authUsersOnly: true,
    visibleOnly: true,
    availOnly: true,
};
exports.UserMessageableConnections = UserMessageableConnections;

function getActiveConnections(
    options = { authUsersOnly: true, visibleOnly: true, availOnly: false }
) {
    return clientConnections.filter(conn => {
        if (options.authUsersOnly && !conn.user.isAuthenticated()) {
            return false;
        }
        if (options.visibleOnly && !conn.user.isVisible()) {
            return false;
        }
        if (options.availOnly && !conn.user.isAvailable()) {
            return false;
        }

        return true;
    });
}

function getActiveConnectionList(
    options = { authUsersOnly: true, visibleOnly: true, availOnly: false }
) {
    const now = moment();

    return _.map(getActiveConnections(options), ac => {
        let action;
        try {
            //  attempting to fetch a bad menu stack item can blow up/assert
            action = _.get(ac, 'currentMenuModule.menuConfig.desc', 'Unknown');
        } catch (e) {
            action = 'Unknown';
        }

        const entry = {
            node: ac.node,
            authenticated: ac.user.isAuthenticated(),
            userId: ac.user.userId,
            action: action,
            serverName: ac.session.serverName,
            isSecure: ac.session.isSecure,
            isVisible: ac.user.isVisible(),
            isAvailable: ac.user.isAvailable(),
            remoteAddress: ac.friendlyRemoteAddress(),
        };

        //
        //  There may be a connection, but not a logged in user as of yet
        //
        if (ac.user.isAuthenticated()) {
            entry.text = ac.user.username;
            entry.userName = ac.user.username;
            entry.realName = ac.user.properties[UserProps.RealName];
            entry.location = ac.user.properties[UserProps.Location];
            entry.affils = entry.affiliation = ac.user.properties[UserProps.Affiliations];

            const diff = now.diff(
                moment(ac.user.properties[UserProps.LastLoginTs]),
                'minutes'
            );
            entry.timeOn = moment.duration(diff, 'minutes');
        }

        return entry;
    });
}

function addNewClient(client, clientSock) {
    //
    //  Find a node ID "slot"
    //
    let nodeId;
    for (nodeId = 1; nodeId < Number.MAX_SAFE_INTEGER; ++nodeId) {
        const existing = clientConnections.find(client => nodeId === client.node);
        if (!existing) {
            break; //  available slot
        }
    }

    client.session.id = nodeId;
    const remoteAddress = (client.remoteAddress = clientSock.remoteAddress);
    //  create a unique identifier one-time ID for this session
    client.session.uniqueId = new hashids('ENiGMA½ClientSession').encode([<commit_msg>Fix pre-auth properties in user list<commit_after>/* jslint node: true */
'use strict';

//  ENiGMA½
const logger = require('./logger.js');
const Events = require('./events.js');
const UserProps = require('./user_property.js');

//  deps
const _ = require('lodash');
const moment = require('moment');
const hashids = require('hashids/cjs');

exports.getActiveConnections = getActiveConnections;
exports.getActiveConnectionList = getActiveConnectionList;
exports.addNewClient = addNewClient;
exports.removeClient = removeClient;
exports.getConnectionByUserId = getConnectionByUserId;
exports.getConnectionByNodeId = getConnectionByNodeId;

const clientConnections = [];
exports.clientConnections = clientConnections;

const AllConnections = { authUsersOnly: false, visibleOnly: false, availOnly: false };
exports.AllConnections = AllConnections;

const UserVisibleConnections = {
    authUsersOnly: false,
    visibleOnly: true,
    availOnly: false,
};
exports.UserVisibleConnections = UserVisibleConnections;

const UserMessageableConnections = {
    authUsersOnly: true,
    visibleOnly: true,
    availOnly: true,
};
exports.UserMessageableConnections = UserMessageableConnections;

function getActiveConnections(
    options = { authUsersOnly: true, visibleOnly: true, availOnly: false }
) {
    return clientConnections.filter(conn => {
        if (options.authUsersOnly && !conn.user.isAuthenticated()) {
            return false;
        }
        if (options.visibleOnly && !conn.user.isVisible()) {
            return false;
        }
        if (options.availOnly && !conn.user.isAvailable()) {
            return false;
        }

        return true;
    });
}

function getActiveConnectionList(
    options = { authUsersOnly: true, visibleOnly: true, availOnly: false }
) {
    const now = moment();

    return _.map(getActiveConnections(options), ac => {
        let action;
        try {
            //  attempting to fetch a bad menu stack item can blow up/assert
            action = _.get(ac, 'currentMenuModule.menuConfig.desc', 'Unknown');
        } catch (e) {
            action = 'Unknown';
        }

        const entry = {
            node: ac.node,
            authenticated: ac.user.isAuthenticated(),
            userId: ac.user.userId,
            action: action,
            serverName: ac.session.serverName,
            isSecure: ac.session.isSecure,
            isVisible: ac.user.isVisible(),
            isAvailable: ac.user.isAvailable(),
            remoteAddress: ac.friendlyRemoteAddress(),
        };

        //
        //  There may be a connection, but not a logged in user as of yet
        //
        entry.text = ac.user?.username || 'N/A';
        entry.userName = ac.user?.username || 'N/A';
        entry.realName = ac.user?.getProperty(UserProps.RealName) || 'N/A';
        entry.location = ac.user?.getProperty(UserProps.Location) || 'N/A';
        entry.affils = entry.affiliation =
            ac.user?.getProperty(UserProps.Affiliations) || 'N/A';

        if (ac.user.isAuthenticated()) {
            // :TODO: track pre-auth time so we can properly track this
            const diff = now.diff(
                moment(ac.user.properties[UserProps.LastLoginTs]),
                'minutes'
            );
            entry.timeOn = moment.duration(diff, 'minutes');
        }

        return entry;
    });
}

function addNewClient(client, clientSock) {
    //
    //  Find a node ID "slot"
    //
    let nodeId;
    for (nodeId = 1; nodeId < Number.MAX_SAFE_INTEGER; ++nodeId) {
        const existing = clientConnections.find(client => nodeId === client.node);
        if (!existing) {
            break; //  available slot
        }
    }

    client.session.id = nodeId;
    const remoteAddress = (client.remoteAddress = clientSock.remoteAddress);
    //  create a unique identifier one-time ID for this session
    client.session.uniqueId = new hashids('ENiGMA½ClientSession').encode([
{'fraction_non_alphanumeric': 0.1142636058808357, 'fraction_numerical': 0.00025793139025019347, 'mean_word_length': 2.5869565217391304, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '1651754', 'n_tokens_mistral': 2194, 'n_tokens_neox': 2168, 'n_words': 493}