RSS Git Download  Clone
Raw Blame History 2kB 65 lines
const sharedIoRedis = require('../shared')

module.exports = async(options) => {
    const { socket } = options;

    const connectionSave = options.payload.model;

    let disableReadonlyConnections = true

    try {
        sharedIoRedis.ensureReadonlyConnections()
        disableReadonlyConnections = false

        let connectionIndexExisting;
        for(let connectionIndex in p3xrs.connections.list) {
            const connection = p3xrs.connections.list[connectionIndex]
            if (connection.id === connectionSave.id) {
                connectionIndexExisting = connectionIndex
                break;
            }
        }
        p3xrs.connections.update = new Date()
        if (connectionIndexExisting !== undefined) {

            if (p3xrs.connections.list[connectionIndexExisting].id === connectionSave.password) {
                connectionSave.password = p3xrs.connections.list[connectionIndexExisting].password;
            }

            p3xrs.connections.list[connectionIndexExisting] = connectionSave
        } else {
            p3xrs.connections.list.push(connectionSave)
        }


        const fs = require('fs')
        fs.writeFileSync(p3xrs.cfg.connections.home, JSON.stringify(p3xrs.connections, null, 4))

        socket.emit(options.responseEvent, {
            status: 'ok',
        })

    } catch (e) {
        console.error(e)
        socket.emit(options.responseEvent, {
            status: 'error',
            error: e
        })

    } finally {
        if (!disableReadonlyConnections) {
            sharedIoRedis.sendConnections({
                socket: socket,
            })


            sharedIoRedis.triggerDisconnect({
                connectionId: connectionSave.id,
                code: 'save-connection',
                socket: socket,
            })
        }

    }

}