n8n-nodes-magic-inbox
Version:
Magic Inbox P2P - Revolutionary workflow teleportation system for n8n
205 lines (194 loc) ⢠8.71 kB
JavaScript
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 };