UNPKG

n8n-nodes-magic-dev

Version:

🪄 Magic Dev - Revolutionary community n8n node: AI Generation, Creator Hub, Quest Unlock, Market, P2P Magic Inbox (send & receive)!

193 lines (188 loc) • 6.74 kB
import { INodeType, INodeTypeDescription, IWebhookFunctions, INodeExecutionData, IWebhookResponseData, NodeConnectionType, } from 'n8n-workflow'; export class MagicInboxReceiver implements INodeType { description: INodeTypeDescription = { displayName: '📬 Magic Inbox Receiver', name: 'magicInboxReceiver', icon: 'fa:gift', // Blue gift icon (like MagicDev) group: ['trigger'], version: 1, subtitle: '={{$parameter["magicInboxMode"] === "simple" ? "Simple Mode" : "Advanced Mode"}}', description: '🌟 Trigger: Receive messages/workflows from Magic Inbox (P2P/Forum) at /webhook/magic-inbox', defaults: { name: '📬 Magic Inbox Receiver', color: '#2362ba' // Blue color like MagicDev }, inputs: [], outputs: [NodeConnectionType.Main], webhooks: [ { name: 'default', httpMethod: 'POST', responseMode: 'onReceived', path: 'magic-inbox', displayName: 'Reception P2P/Forum Magic Inbox (POST /webhook/magic-inbox)', }, ], properties: [ { displayName: 'Guide', name: 'inboxGuide', type: 'notice', default: `🌟 MAGIC INBOX P2P RECEIVER Receive workflows/messages from other n8n instances or Magic Forum. - Simple mode: auto-import (works out-of-the-box) - Advanced: use your logic - Whitelist: restrict senders `, }, { displayName: 'Inbox Mode', name: 'magicInboxMode', type: 'options', options: [ { name: '⚡ Simple (auto-import)', value: 'simple', description: 'Easy! Auto-import into your n8n.' }, { name: '🧠 Advanced (raw)', value: 'advanced', description: 'Raw message, use with your workflow logic.' } ], default: 'simple', description: 'Choose "Simple" for automatic import, or "Advanced" for manual/advanced processing.' }, { displayName: 'Your n8n Instance URL', name: 'n8nInstanceUrl', type: 'string', required: true, displayOptions: { show: { magicInboxMode: ['simple'] } }, default: 'http://localhost:5678', placeholder: 'https://your-n8n-instance.com', description: 'Destination n8n URL where received workflows should be imported (Simple mode only).' }, { displayName: 'n8n API Key', name: 'n8nApiKey', type: 'string', typeOptions: { password: true }, required: true, displayOptions: { show: { magicInboxMode: ['simple'] } }, default: '', placeholder: 'n8n_api_xxx', description: 'API Key for workflow import (Simple mode only).' }, { displayName: 'Auto Import', name: 'autoImport', type: 'boolean', displayOptions: { show: { magicInboxMode: ['simple'] } }, default: true, description: 'Automatically import received workflows (Simple mode only).' }, { displayName: 'Enable Whitelist', name: 'enableWhitelist', type: 'boolean', default: false, description: 'Restrict accepted messages to trusted senders only.' }, { displayName: 'Allowed Senders', name: 'allowedSenders', type: 'string', typeOptions: { rows: 3 }, displayOptions: { show: { enableWhitelist: [true] } }, default: '', placeholder: 'alice@magic.dev, bob@team.com', description: 'Comma-separated list of allowed sender identifiers (e.g. their URL or email).' } ], }; async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> { const mode = this.getNodeParameter('magicInboxMode', 0) as string; const data = this.getBodyData() as any; // Whitelist check const enableWhitelist = this.getNodeParameter('enableWhitelist', 0) as boolean; if (enableWhitelist) { const allowedSenders = this.getNodeParameter('allowedSenders', 0) as string; 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' } }]] }; } } // Simple mode: auto-imports workflow into instance if (mode === 'simple') { const instanceUrl = this.getNodeParameter('n8nInstanceUrl', 0) as string; const apiKey = this.getNodeParameter('n8nApiKey', 0) as string; const autoImport = this.getNodeParameter('autoImport', 0) as boolean; const result: any = { 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/1.0' }, body: cleanWorkflow, json: true }); result.workflowImport = { success: true, workflowId: response.id, workflowUrl: `${instanceUrl}/workflow/${response.id}`, message: `✅ Workflow "${data.workflowName}" imported successfully!` }; } catch (error: any) { result.workflowImport = { success: false, error: error instanceof Error ? error.message : 'Unknown error' }; } } return { workflowData: [[{ json: result }]] }; } else { // Advanced mode: returns all data for use in multi-node workflows return { workflowData: [[{ json: { ...data, magicInboxMode: 'advanced', receivedAt: new Date().toISOString(), processingNote: 'Use multi-node workflow for advanced processing' } }]] }; } } }