UNPKG

n8n-nodes-magic-inbox

Version:

Magic Inbox P2P - Revolutionary workflow teleportation system for n8n

205 lines (194 loc) • 8.71 kB
class MagicInbox { constructor() { this.description = { displayName: 'šŸ“¬ Magic Inbox', name: 'magicInbox', icon: 'fa:envelope', group: ['trigger'], version: 11, subtitle: '={{$parameter["magicInboxMode"] === "simple" ? "Simple Mode" : "Advanced Mode"}}', description: '🌟 Receive workflows and messages P2P from other n8n instances', defaults: { name: 'šŸ“¬ Magic Inbox P2P', }, inputs: [], outputs: ['main'], webhooks: [ { name: 'default', httpMethod: 'POST', responseMode: 'onReceived', path: 'magic-inbox', } ], properties: [ { displayName: 'šŸ“¬ Magic Inbox P2P - Decentralized Reception', name: 'inboxGuide', type: 'notice', default: '🌟 MAGIC INBOX P2P RECEIVER:\n\nšŸ“„ Receive workflows and messages from other n8n instances\nšŸ”„ Auto-import or save according to your configuration\nšŸ›”ļø Required in production to receive P2P transmissions\n⚔ Simple Mode: All-in-one | Advanced Mode: Modular workflow\n\nšŸš€ Decentralized P2P communication activated!', }, { displayName: 'šŸŽÆ Magic Inbox Mode', name: 'magicInboxMode', type: 'options', options: [ { name: '⚔ Simple Mode - All-in-One', value: 'simple', description: 'Configuration and processing in single node' }, { name: '🧠 Advanced Mode - Modular Workflow', value: 'advanced', description: 'Raw data for custom multi-node workflow' } ], default: 'simple', description: 'šŸŽ›ļø Choose your Magic Inbox experience level' }, { displayName: '🌐 N8N Instance URL', name: 'n8nInstanceUrl', type: 'string', displayOptions: { show: { magicInboxMode: ['simple'] } }, required: true, default: 'http://localhost:5678', placeholder: 'https://your-instance.com', description: '🌐 Your n8n instance URL for auto-import' }, { displayName: 'šŸ”‘ N8N API Key', name: 'n8nApiKey', type: 'string', typeOptions: { password: true }, displayOptions: { show: { magicInboxMode: ['simple'] } }, required: true, default: '', placeholder: 'n8n_api_xxx', description: 'šŸ”‘ Your n8n instance API key' }, { displayName: '⚔ Auto Import', name: 'autoImport', type: 'boolean', displayOptions: { show: { magicInboxMode: ['simple'] } }, default: true, description: '⚔ Automatically import received workflows' }, { displayName: 'šŸ“’ P2P Address Book', name: 'addressBook', type: 'string', typeOptions: { rows: 6 }, default: '', placeholder: 'Your Magic Inbox contacts:\n- Alice: https://alice-n8n.com/webhook/magic-inbox\n- Bob: https://bob-automation.net/webhook/magic-inbox\n- Team: https://team-workflows.org/webhook/magic-inbox', description: 'šŸ“ Free space to store your Magic Inbox P2P contacts' }, { displayName: 'šŸ” Enable Whitelist', name: 'enableWhitelist', type: 'boolean', default: false, description: 'šŸ›”ļø Only accept messages from authorized senders' }, { displayName: 'šŸ‘„ Allowed Senders', name: 'allowedSenders', type: 'string', typeOptions: { rows: 3 }, displayOptions: { show: { enableWhitelist: [true] } }, default: '', placeholder: 'alice@magic.dev, bob@team.com', description: 'šŸ“ List of authorized senders (comma-separated)' } ], }; } async webhook() { const mode = this.getNodeParameter('magicInboxMode', 0); const data = this.getBodyData(); // Security validation const enableWhitelist = this.getNodeParameter('enableWhitelist', 0); if (enableWhitelist) { const allowedSenders = this.getNodeParameter('allowedSenders', 0); const sendersList = allowedSenders.split(',').map(s => s.trim()); if (data.from && typeof data.from === 'string' && !sendersList.includes(data.from)) { return { workflowData: [[{ json: { error: 'Sender not in whitelist', from: data.from, status: 'rejected' } }]] }; } } if (mode === 'simple') { const instanceUrl = this.getNodeParameter('n8nInstanceUrl', 0); const apiKey = this.getNodeParameter('n8nApiKey', 0); const autoImport = this.getNodeParameter('autoImport', 0); const result = { action: 'magic_inbox_simple_received', success: true, mode: 'simple', from: data.from || 'anonymous@magic.inbox', message: data.message || '', hasWorkflow: !!data.workflowJson, receivedAt: new Date().toISOString() }; if (data.workflowJson && autoImport && apiKey) { try { const workflowJson = data.workflowJson; const cleanWorkflow = { name: `[Magic Inbox P2P] ${data.workflowName || 'Received Workflow'}`, nodes: workflowJson.nodes, connections: workflowJson.connections, settings: { timezone: 'Europe/Paris', ...workflowJson.settings } }; const response = await this.helpers.request({ method: 'POST', url: `${instanceUrl.replace(/\/$/, '')}/api/v1/workflows`, headers: { 'X-N8N-API-KEY': apiKey, 'Content-Type': 'application/json', 'User-Agent': 'n8n-magic-inbox-p2p/11' }, body: cleanWorkflow, json: true }); result.workflowImport = { success: true, workflowId: response.id, workflowUrl: `${instanceUrl}/workflow/${response.id}`, message: `āœ… Workflow "${data.workflowName}" imported successfully!` }; } catch (error) { result.workflowImport = { success: false, error: error instanceof Error ? error.message : 'Unknown error' }; } } return { workflowData: [[{ json: result }]] }; } else { return { workflowData: [[{ json: { ...data, magicInboxMode: 'advanced', receivedAt: new Date().toISOString(), processingNote: 'Use multi-node workflow for advanced processing' } }]] }; } } } module.exports = { MagicInbox };