<commit_before>'kiwi public';

import * as Misc from '@/helpers/Misc';
import Vue from 'vue';
import _ from 'lodash';
import { configTemplates } from '@/res/configTemplates';
import NetworkState from './state/NetworkState';
import BufferState from './state/BufferState';
import UserState from './state/UserState';
import Message from './Message';

function createNewState() {
    const stateObj = {
        // May be set by a StatePersistence instance
        persistence: null,

        // Settings may be overridden via config.json
        settings: configTemplates.default,
        user_settings: {
        },
        connection: {
            // disconnected / connecting / connected
            status: 'connected',
            sessionId: '',
        },
        ui: {
            active_network: 0,
            active_buffer: '',
            last_active_buffers: [],
            app_has_focus: true,
            app_width: 0,
            app_height: 0,
            is_touch: false,
            is_narrow: false,
            favicon_counter: 0,
            current_input: '',
            input_history: [],
            input_history_pos: 0,
            show_advanced_tab: false,
        },
        networks: [],
    };

    const userDict = new Vue({
        data() {
            return {
                networks: {},
            };
        },
        /*
        (network id): {
            (lowercase nick): UserState,
            (lowercase nick): UserState,
        },
        */
    });

    const bufferDict = new Vue({
        data() {
            return {
                networks: {},
            };
        },
        /*
        (network id): [
            BufferState,
            BufferState,
        ]
        */
    });

    // Messages are seperate from the above state object to keep them from being reactive. Saves CPU
    const messages = [
        /* {
            networkid: 1,
            buffer: '#kiwiirc',
            messages: [
                Message,
                Message
            ],
        }, */
    ];

    const availableStartups = Object.create(null);

    const state = new Vue({
        data: stateObj,
        methods: {
            // Export enough state so that it can be imported in future to resume
            exportState(includeBuffers) {
                let toExport = {};

                if (includeBuffers) {
                    toExport.networks = state.networks.map((network) => {
                        let networkObj = {
                            id: network.id,
                            name: network.name,
                            connection: {
                                nick: network.connection.nick,
                                server: network.connection.server,
                                port: network.connection.port,
                                tls: network.connection.tls,
                                path: network.connection.path,
                                password: <PASSWORD>,
                                direct: network.connection.direct,
                                encoding: network.connection.encoding,
                                bncnetid: network.connection.bncnetid,
                            },
                            auto_commands: network.auto_commands,
                            settings: _.cloneDeep(network.settings),
                            username: network.username,
                            gecos: network.gecos,
                            password: <PASSWORD>,
                            hidden: network.hidden,
                            is_bnc: network.is_bnc,
                            buffers: [],
                        };

                        networkObj.buffers = network.buffers.map((buffer) => {
                            let bufferObj = {
                                name: buffer.name,
                                key: buffer.key,
                                enabled: !!buffer.enabled,
                                settings: _.cloneDeep(buffer.settings),
                                latest_messages: [],
                            };

                            buffer.latest_messages.forEach((msg) => {
                                bufferObj.latest_messages.push(msg.serialise());
                            });

                            return bufferObj;
                        });

                        return networkObj;
                    });
                }

                toExport.user_settings = state.user_settings;

                return JSON.stringify(toExport);
            },

            // Import a previously exported state to continue that state
            importState(stateStr) {
                let importObj = JSON.parse(stateStr);
                if (importObj && importObj.networks) {
                    this.resetState();
                    importObj.networks.forEach((importNetwork) => {
                        let network = new NetworkState(
                            importNetwork.id,
                            state,
                            userDict,
                            bufferDict
                        );
                        network.name = importNetwork.name;
                        network.connection = { ...network.connection, ...importNetwork.connection };
                        network.auto_commands = importNetwork.auto_commands || '';
                        network.settings = importNetwork.settings;
                        // First check importNetwork.nick as this was used in older versions
                        // TODO: Eventually remove this importNetwork.nick check
                        network.nick = importNetwork.nick || importNetwork.connection.nick;
                        if (!network.connection.nick && importNetwork.nick) {
                            network.connection.nick = importNetwork.nick;
                        }
                        network.username = importNetwork.username;
                        network.gecos = importNetwork.gecos;
                        network.password = importNetwork.password;
                        network.hidden = importNetwork.hidden;
                        network.is_bnc = importNetwork.is_bnc;

                        this.networks.push(network);

                        importNetwork.buffers.forEach((impBuffer) => {
                            let buffer = new BufferState(
                                impBuffer.name,
                                network.id,
                                state,
                                messages
                            );
                            buffer.key = impBuffer.key;
                            buffer.enabled = !!impBuffer.enabled;
                            buffer.settings = impBuffer.settings;

                            impBuffer.latest_messages.forEach((msg) => {
                                buffer.latest_messages.push(new Message(msg));
                            });<commit_msg>Fixes buffer importState when there is no latest_messages;<commit_after>'kiwi public';

import * as Misc from '@/helpers/Misc';
import Vue from 'vue';
import _ from 'lodash';
import { configTemplates } from '@/res/configTemplates';
import NetworkState from './state/NetworkState';
import BufferState from './state/BufferState';
import UserState from './state/UserState';
import Message from './Message';

function createNewState() {
    const stateObj = {
        // May be set by a StatePersistence instance
        persistence: null,

        // Settings may be overridden via config.json
        settings: configTemplates.default,
        user_settings: {
        },
        connection: {
            // disconnected / connecting / connected
            status: 'connected',
            sessionId: '',
        },
        ui: {
            active_network: 0,
            active_buffer: '',
            last_active_buffers: [],
            app_has_focus: true,
            app_width: 0,
            app_height: 0,
            is_touch: false,
            is_narrow: false,
            favicon_counter: 0,
            current_input: '',
            input_history: [],
            input_history_pos: 0,
            show_advanced_tab: false,
        },
        networks: [],
    };

    const userDict = new Vue({
        data() {
            return {
                networks: {},
            };
        },
        /*
        (network id): {
            (lowercase nick): UserState,
            (lowercase nick): UserState,
        },
        */
    });

    const bufferDict = new Vue({
        data() {
            return {
                networks: {},
            };
        },
        /*
        (network id): [
            BufferState,
            BufferState,
        ]
        */
    });

    // Messages are seperate from the above state object to keep them from being reactive. Saves CPU
    const messages = [
        /* {
            networkid: 1,
            buffer: '#kiwiirc',
            messages: [
                Message,
                Message
            ],
        }, */
    ];

    const availableStartups = Object.create(null);

    const state = new Vue({
        data: stateObj,
        methods: {
            // Export enough state so that it can be imported in future to resume
            exportState(includeBuffers) {
                let toExport = {};

                if (includeBuffers) {
                    toExport.networks = state.networks.map((network) => {
                        let networkObj = {
                            id: network.id,
                            name: network.name,
                            connection: {
                                nick: network.connection.nick,
                                server: network.connection.server,
                                port: network.connection.port,
                                tls: network.connection.tls,
                                path: network.connection.path,
                                password: <PASSWORD>,
                                direct: network.connection.direct,
                                encoding: network.connection.encoding,
                                bncnetid: network.connection.bncnetid,
                            },
                            auto_commands: network.auto_commands,
                            settings: _.cloneDeep(network.settings),
                            username: network.username,
                            gecos: network.gecos,
                            password: <PASSWORD>,
                            hidden: network.hidden,
                            is_bnc: network.is_bnc,
                            buffers: [],
                        };

                        networkObj.buffers = network.buffers.map((buffer) => {
                            let bufferObj = {
                                name: buffer.name,
                                key: buffer.key,
                                enabled: !!buffer.enabled,
                                settings: _.cloneDeep(buffer.settings),
                                latest_messages: [],
                            };

                            buffer.latest_messages.forEach((msg) => {
                                bufferObj.latest_messages.push(msg.serialise());
                            });

                            return bufferObj;
                        });

                        return networkObj;
                    });
                }

                toExport.user_settings = state.user_settings;

                return JSON.stringify(toExport);
            },

            // Import a previously exported state to continue that state
            importState(stateStr) {
                let importObj = JSON.parse(stateStr);
                if (importObj && importObj.networks) {
                    this.resetState();
                    importObj.networks.forEach((importNetwork) => {
                        let network = new NetworkState(
                            importNetwork.id,
                            state,
                            userDict,
                            bufferDict
                        );
                        network.name = importNetwork.name;
                        network.connection = { ...network.connection, ...importNetwork.connection };
                        network.auto_commands = importNetwork.auto_commands || '';
                        network.settings = importNetwork.settings;
                        // First check importNetwork.nick as this was used in older versions
                        // TODO: Eventually remove this importNetwork.nick check
                        network.nick = importNetwork.nick || importNetwork.connection.nick;
                        if (!network.connection.nick && importNetwork.nick) {
                            network.connection.nick = importNetwork.nick;
                        }
                        network.username = importNetwork.username;
                        network.gecos = importNetwork.gecos;
                        network.password = <PASSWORD>;
                        network.hidden = importNetwork.hidden;
                        network.is_bnc = importNetwork.is_bnc;

                        this.networks.push(network);

                        importNetwork.buffers.forEach((impBuffer) => {
                            let buffer = new BufferState(
                                impBuffer.name,
                                network.id,
                                state,
                                messages
                            );
                            buffer.key = impBuffer.key;
                            buffer.enabled = !!impBuffer.enabled;
                            buffer.settings = impBuffer.settings;

                            let latestMessages = impBuffer.latest_messages || [];
                            latestMessages.forEach((msg) => {
                                buffer.latest_messages.push(new Message(msg));
                            });
{'fraction_non_alphanumeric': 0.08204838025180365, 'fraction_numerical': 0.0008487763474324516, 'mean_word_length': 0.9922502465830633, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 20, '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': '6768928', 'n_tokens_mistral': 3105, 'n_tokens_neox': 2929, 'n_words': 648}